Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

object & secret store config naming changes for consistency #206

Merged
merged 4 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@ async fn create_execution_context(opts: &Opts) -> Result<ExecuteCtx, anyhow::Err
let backends = config.backends();
let geolocation = config.geolocation();
let dictionaries = config.dictionaries();
let object_store = config.object_store();
let object_stores = config.object_stores();
let secret_stores = config.secret_stores();
let backend_names = itertools::join(backends.keys(), ", ");

ctx = ctx
.with_backends(backends.clone())
.with_geolocation(geolocation.clone())
.with_dictionaries(dictionaries.clone())
.with_object_store(object_store.clone())
.with_object_stores(object_stores.clone())
.with_secret_stores(secret_stores.clone())
.with_config_path(config_path.into());

Expand Down
12 changes: 6 additions & 6 deletions cli/tests/integration/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tracing_subscriber::filter::EnvFilter;
use viceroy_lib::{
body::Body,
config::{
Backend, Backends, Dictionaries, FastlyConfig, Geolocation, ObjectStore, SecretStores,
Backend, Backends, Dictionaries, FastlyConfig, Geolocation, ObjectStores, SecretStores,
},
ExecuteCtx, ProfilingStrategy, ViceroyService,
};
Expand Down Expand Up @@ -53,7 +53,7 @@ pub struct Test {
backends: Backends,
dictionaries: Dictionaries,
geolocation: Geolocation,
object_store: ObjectStore,
object_stores: ObjectStores,
secret_stores: SecretStores,
hosts: Vec<HostSpec>,
log_stdout: bool,
Expand All @@ -72,7 +72,7 @@ impl Test {
backends: Backends::new(),
dictionaries: Dictionaries::new(),
geolocation: Geolocation::new(),
object_store: ObjectStore::new(),
object_stores: ObjectStores::new(),
secret_stores: SecretStores::new(),
hosts: Vec::new(),
log_stdout: false,
Expand All @@ -91,7 +91,7 @@ impl Test {
backends: Backends::new(),
dictionaries: Dictionaries::new(),
geolocation: Geolocation::new(),
object_store: ObjectStore::new(),
object_stores: ObjectStores::new(),
secret_stores: SecretStores::new(),
hosts: Vec::new(),
log_stdout: false,
Expand All @@ -107,7 +107,7 @@ impl Test {
backends: config.backends().to_owned(),
dictionaries: config.dictionaries().to_owned(),
geolocation: config.geolocation().to_owned(),
object_store: config.object_store().to_owned(),
object_stores: config.object_stores().to_owned(),
secret_stores: config.secret_stores().to_owned(),
..self
})
Expand Down Expand Up @@ -211,7 +211,7 @@ impl Test {
.with_backends(self.backends.clone())
.with_dictionaries(self.dictionaries.clone())
.with_geolocation(self.geolocation.clone())
.with_object_store(self.object_store.clone())
.with_object_stores(self.object_stores.clone())
.with_secret_stores(self.secret_stores.clone())
.with_log_stderr(self.log_stderr)
.with_log_stdout(self.log_stdout);
Expand Down
78 changes: 54 additions & 24 deletions cli/tests/integration/object_store.rs

Large diffs are not rendered by default.

46 changes: 23 additions & 23 deletions cli/tests/integration/secret_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async fn secret_store_works() -> TestResult {
authors = ["Jill Bryson <jbryson@fastly.com>", "Rose McDowall <rmcdowall@fastly.com>"]
language = "rust"
[local_server]
secret_store.store_one = [{key = "first", data = "This is some data"},{key = "second", path = "../test-fixtures/data/object-store.txt"}]
secret_stores.store_one = [{key = "first", data = "This is some data"},{key = "second", file = "../test-fixtures/data/object-store.txt"}]
"#;

let resp = Test::using_fixture("secret-store.wasm")
Expand Down Expand Up @@ -48,7 +48,7 @@ fn bad_config_test(toml_fragment: &str) -> Result<FastlyConfig, FastlyConfigErro

#[tokio::test(flavor = "multi_thread")]
async fn bad_config_store_not_array() -> TestResult {
const TOML_FRAGMENT: &str = "secret_store.store_one = 1";
const TOML_FRAGMENT: &str = "secret_stores.store_one = 1";
match bad_config_test(TOML_FRAGMENT) {
Err(FastlyConfigError::InvalidSecretStoreDefinition {
err: SecretStoreConfigError::NotAnArray,
Expand All @@ -62,7 +62,7 @@ async fn bad_config_store_not_array() -> TestResult {

#[tokio::test(flavor = "multi_thread")]
async fn bad_config_store_not_table() -> TestResult {
const TOML_FRAGMENT: &str = "secret_store.store_one = [1]";
const TOML_FRAGMENT: &str = "secret_stores.store_one = [1]";
match bad_config_test(TOML_FRAGMENT) {
Err(FastlyConfigError::InvalidSecretStoreDefinition {
err: SecretStoreConfigError::NotATable,
Expand All @@ -76,7 +76,7 @@ async fn bad_config_store_not_table() -> TestResult {

#[tokio::test(flavor = "multi_thread")]
async fn bad_config_no_key() -> TestResult {
const TOML_FRAGMENT: &str = r#"secret_store.store_one = [{data = "This is some data"}]"#;
const TOML_FRAGMENT: &str = r#"secret_stores.store_one = [{data = "This is some data"}]"#;
match bad_config_test(TOML_FRAGMENT) {
Err(FastlyConfigError::InvalidSecretStoreDefinition {
err: SecretStoreConfigError::NoKey,
Expand All @@ -91,7 +91,7 @@ async fn bad_config_no_key() -> TestResult {
#[tokio::test(flavor = "multi_thread")]
async fn bad_config_key_not_string() -> TestResult {
const TOML_FRAGMENT: &str =
r#"secret_store.store_one = [{key = 1, data = "This is some data"}]"#;
r#"secret_stores.store_one = [{key = 1, data = "This is some data"}]"#;
match bad_config_test(TOML_FRAGMENT) {
Err(FastlyConfigError::InvalidSecretStoreDefinition {
err: SecretStoreConfigError::KeyNotAString,
Expand All @@ -104,36 +104,36 @@ async fn bad_config_key_not_string() -> TestResult {
}

#[tokio::test(flavor = "multi_thread")]
async fn bad_config_no_data_or_path() -> TestResult {
const TOML_FRAGMENT: &str = r#"secret_store.store_one = [{key = "first"}]"#;
async fn bad_config_no_data_or_file() -> TestResult {
const TOML_FRAGMENT: &str = r#"secret_stores.store_one = [{key = "first"}]"#;
match bad_config_test(TOML_FRAGMENT) {
Err(FastlyConfigError::InvalidSecretStoreDefinition {
err: SecretStoreConfigError::NoPathOrData(_),
err: SecretStoreConfigError::NoFileOrData(_),
..
}) => (),
Err(_) => panic!("Expected a FastlyConfigError::InvalidSecretStoreDefinition with SecretStoreConfigError::NoPathOrData"),
Err(_) => panic!("Expected a FastlyConfigError::InvalidSecretStoreDefinition with SecretStoreConfigError::NoFileOrData"),
_ => panic!("Expected an error"),
}
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn bad_config_both_data_and_path() -> TestResult {
const TOML_FRAGMENT: &str = r#"secret_store.store_one = [{key = "first", path = "file.txt", data = "This is some data"}]"#;
async fn bad_config_both_data_and_file() -> TestResult {
const TOML_FRAGMENT: &str = r#"secret_stores.store_one = [{key = "first", file = "file.txt", data = "This is some data"}]"#;
match bad_config_test(TOML_FRAGMENT) {
Err(FastlyConfigError::InvalidSecretStoreDefinition {
err: SecretStoreConfigError::PathAndData(_),
err: SecretStoreConfigError::FileAndData(_),
..
}) => (),
Err(_) => panic!("Expected a FastlyConfigError::InvalidSecretStoreDefinition with SecretStoreConfigError::PathAndData"),
Err(_) => panic!("Expected a FastlyConfigError::InvalidSecretStoreDefinition with SecretStoreConfigError::FileAndData"),
_ => panic!("Expected an error"),
}
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn bad_config_data_not_string() -> TestResult {
const TOML_FRAGMENT: &str = r#"secret_store.store_one = [{key = "first", data = 1}]"#;
const TOML_FRAGMENT: &str = r#"secret_stores.store_one = [{key = "first", data = 1}]"#;
match bad_config_test(TOML_FRAGMENT) {
Err(FastlyConfigError::InvalidSecretStoreDefinition {
err: SecretStoreConfigError::DataNotAString(_),
Expand All @@ -146,23 +146,23 @@ async fn bad_config_data_not_string() -> TestResult {
}

#[tokio::test(flavor = "multi_thread")]
async fn bad_config_path_not_string() -> TestResult {
const TOML_FRAGMENT: &str = r#"secret_store.store_one = [{key = "first", path = 1}]"#;
async fn bad_config_file_not_string() -> TestResult {
const TOML_FRAGMENT: &str = r#"secret_stores.store_one = [{key = "first", file = 1}]"#;
match bad_config_test(TOML_FRAGMENT) {
Err(FastlyConfigError::InvalidSecretStoreDefinition {
err: SecretStoreConfigError::PathNotAString(_),
err: SecretStoreConfigError::FileNotAString(_),
..
}) => (),
Err(_) => panic!("Expected a FastlyConfigError::InvalidSecretStoreDefinition with SecretStoreConfigError::PathNotAString"),
Err(_) => panic!("Expected a FastlyConfigError::InvalidSecretStoreDefinition with SecretStoreConfigError::FileNotAString"),
_ => panic!("Expected an error"),
}
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn bad_config_path_nonexistent() -> TestResult {
async fn bad_config_file_nonexistent() -> TestResult {
const TOML_FRAGMENT: &str =
r#"secret_store.store_one = [{key = "first", path = "nonexistent.txt"}]"#;
r#"secret_stores.store_one = [{key = "first", file = "nonexistent.txt"}]"#;
match bad_config_test(TOML_FRAGMENT) {
Err(FastlyConfigError::InvalidSecretStoreDefinition {
err: SecretStoreConfigError::IoError(_),
Expand All @@ -177,7 +177,7 @@ async fn bad_config_path_nonexistent() -> TestResult {
#[tokio::test(flavor = "multi_thread")]
async fn bad_config_invalid_store_name() -> TestResult {
const TOML_FRAGMENT: &str =
r#"secret_store.store*one = [{key = "first", data = "This is some data"}]"#;
r#"secret_stores.store*one = [{key = "first", data = "This is some data"}]"#;
match bad_config_test(TOML_FRAGMENT) {
Err(FastlyConfigError::InvalidFastlyToml(_)) => (),
Err(_) => panic!("Expected a FastlyConfigError::InvalidFastlyToml"),
Expand All @@ -189,7 +189,7 @@ async fn bad_config_invalid_store_name() -> TestResult {
#[tokio::test(flavor = "multi_thread")]
async fn bad_config_invalid_secret_name() -> TestResult {
const TOML_FRAGMENT: &str =
r#"secret_store.store_one = [{key = "first*", data = "This is some data"}]"#;
r#"secret_stores.store_one = [{key = "first*", data = "This is some data"}]"#;
match bad_config_test(TOML_FRAGMENT) {
Err(FastlyConfigError::InvalidSecretStoreDefinition {
err: SecretStoreConfigError::InvalidSecretName(_),
Expand All @@ -203,7 +203,7 @@ async fn bad_config_invalid_secret_name() -> TestResult {

#[tokio::test(flavor = "multi_thread")]
async fn bad_config_secret_name_too_long() -> TestResult {
const TOML_FRAGMENT: &str = r#"secret_store.store_one = [{key = "firstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirst", data = "This is some data"}]"#;
const TOML_FRAGMENT: &str = r#"secret_stores.store_one = [{key = "firstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirstfirst", data = "This is some data"}]"#;
match bad_config_test(TOML_FRAGMENT) {
Err(FastlyConfigError::InvalidSecretStoreDefinition {
err: SecretStoreConfigError::InvalidSecretName(_),
Expand Down
29 changes: 15 additions & 14 deletions lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub use self::geolocation::Geolocation;
/// Types and deserializers for object store configuration settings.
mod object_store;

pub use crate::object_store::ObjectStore;
pub use crate::object_store::ObjectStores;

/// Types and deserializers for secret store configuration settings.
mod secret_store;
Expand Down Expand Up @@ -95,13 +95,13 @@ impl FastlyConfig {
}

/// Get the object store configuration.
pub fn object_store(&self) -> &ObjectStore {
&self.local_server.object_store.0
pub fn object_stores(&self) -> &ObjectStores {
&self.local_server.object_stores.0
}

/// Get the secret store configuration.
pub fn secret_stores(&self) -> &SecretStores {
&self.local_server.secret_store.0
&self.local_server.secret_stores.0
}

/// Parse a `fastly.toml` file into a `FastlyConfig`.
Expand Down Expand Up @@ -184,8 +184,8 @@ pub struct LocalServerConfig {
backends: BackendsConfig,
geolocation: Geolocation,
dictionaries: DictionariesConfig,
object_store: ObjectStoreConfig,
secret_store: SecretStoreConfig,
object_stores: ObjectStoreConfig,
secret_stores: SecretStoreConfig,
}

/// Enum of available (experimental) wasi modules
Expand All @@ -203,8 +203,9 @@ struct RawLocalServerConfig {
backends: Option<Table>,
geolocation: Option<Table>,
dictionaries: Option<Table>,
object_store: Option<Table>,
secret_store: Option<Table>,
#[serde(alias = "object_store")]
object_stores: Option<Table>,
secret_stores: Option<Table>,
}

impl TryInto<LocalServerConfig> for RawLocalServerConfig {
Expand All @@ -214,8 +215,8 @@ impl TryInto<LocalServerConfig> for RawLocalServerConfig {
backends,
geolocation,
dictionaries,
object_store,
secret_store,
object_stores,
secret_stores,
} = self;
let backends = if let Some(backends) = backends {
backends.try_into()?
Expand All @@ -232,12 +233,12 @@ impl TryInto<LocalServerConfig> for RawLocalServerConfig {
} else {
DictionariesConfig::default()
};
let object_store = if let Some(object_store) = object_store {
let object_stores = if let Some(object_store) = object_stores {
object_store.try_into()?
} else {
ObjectStoreConfig::default()
};
let secret_store = if let Some(secret_store) = secret_store {
let secret_stores = if let Some(secret_store) = secret_stores {
secret_store.try_into()?
} else {
SecretStoreConfig::default()
Expand All @@ -247,8 +248,8 @@ impl TryInto<LocalServerConfig> for RawLocalServerConfig {
backends,
geolocation,
dictionaries,
object_store,
secret_store,
object_stores,
secret_stores,
})
}
}
25 changes: 18 additions & 7 deletions lib/src/config/object_store.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use {
crate::{
error::{FastlyConfigError, ObjectStoreConfigError},
object_store::{ObjectKey, ObjectStore, ObjectStoreKey},
object_store::{ObjectKey, ObjectStoreKey, ObjectStores},
},
std::fs,
toml::value::Table,
};

#[derive(Clone, Debug, Default)]
pub struct ObjectStoreConfig(pub(crate) ObjectStore);
pub struct ObjectStoreConfig(pub(crate) ObjectStores);

impl TryFrom<Table> for ObjectStoreConfig {
type Error = FastlyConfigError;
fn try_from(toml: Table) -> Result<Self, Self::Error> {
let obj_store = ObjectStore::new();
let obj_store = ObjectStores::new();
for (store, items) in toml.iter() {
let items = items.as_array().ok_or_else(|| {
FastlyConfigError::InvalidObjectStoreDefinition {
Expand All @@ -39,6 +39,7 @@ impl TryFrom<Table> for ObjectStoreConfig {
err: ObjectStoreConfigError::NotATable,
}
})?;

let key = item
.get("key")
.ok_or_else(|| FastlyConfigError::InvalidObjectStoreDefinition {
Expand All @@ -50,24 +51,33 @@ impl TryFrom<Table> for ObjectStoreConfig {
name: store.to_string(),
err: ObjectStoreConfigError::KeyNotAString,
})?;
let bytes = match (item.get("path"), item.get("data")) {

// Previously the "file" key was named "path". We want
// to continue supporting the old name.
let file = match (item.get("file"), item.get("path")) {
(None, None) => None,
(Some(file), _) => Some(file),
(None, Some(path)) => Some(path),
};

let bytes = match (file, item.get("data")) {
(None, None) => {
return Err(FastlyConfigError::InvalidObjectStoreDefinition {
name: store.to_string(),
err: ObjectStoreConfigError::NoPathOrData(key.to_string()),
err: ObjectStoreConfigError::NoFileOrData(key.to_string()),
})
}
(Some(_), Some(_)) => {
return Err(FastlyConfigError::InvalidObjectStoreDefinition {
name: store.to_string(),
err: ObjectStoreConfigError::PathAndData(key.to_string()),
err: ObjectStoreConfigError::FileAndData(key.to_string()),
})
}
(Some(path), None) => {
let path = path.as_str().ok_or_else(|| {
FastlyConfigError::InvalidObjectStoreDefinition {
name: store.to_string(),
err: ObjectStoreConfigError::PathNotAString(key.to_string()),
err: ObjectStoreConfigError::FileNotAString(key.to_string()),
}
})?;
fs::read(path).map_err(|e| {
Expand All @@ -86,6 +96,7 @@ impl TryFrom<Table> for ObjectStoreConfig {
.as_bytes()
.to_vec(),
};

obj_store
.insert(
ObjectStoreKey::new(store),
Expand Down
Loading