Skip to content

Commit 034b660

Browse files
authored
Merge pull request #290 from cipherstash/handle-invalid-workspace-crn
fix: error on invalid workspace_crn
2 parents f0e1111 + 0596fc1 commit 034b660

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

packages/cipherstash-proxy/src/config/tandem.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ pub struct DevelopmentConfig {
138138
///
139139
/// ENV vars must be prefixed with `CS_`.
140140
///
141+
/// IMPORTANT!
142+
/// Config needs to be loaded *before* logging can be initalized
143+
/// Tracing macros will not output and you may be confused.
144+
/// Use `println!`` and `eprintln!`` instead.
145+
///
141146
impl TandemConfig {
142147
pub fn default_path() -> String {
143148
DEFAULT_CONFIG_FILE_PATH.to_string()
@@ -194,9 +199,13 @@ impl TandemConfig {
194199
env.insert("CS_ENCRYPT__DEFAULT_KEYSET_ID".into(), value);
195200
}
196201

197-
if let Ok(Ok(value)) = std::env::var(CS_WORKSPACE_CRN).map(|crn| crn.parse::<Crn>())
198-
{
199-
env.insert("CS_AUTH__WORKSPACE_CRN".into(), value.to_string());
202+
if let Ok(value) = std::env::var(CS_WORKSPACE_CRN) {
203+
value
204+
.parse::<Crn>()
205+
.map(|crn| {
206+
env.insert("CS_AUTH__WORKSPACE_CRN".into(), crn.to_string());
207+
})
208+
.map_err(|_| ConfigError::InvalidWorkspaceCrn { crn: value })?;
200209
}
201210

202211
if let Ok(value) = std::env::var(CS_WORKSPACE_ID) {
@@ -652,6 +661,25 @@ mod tests {
652661
});
653662
}
654663

664+
#[test]
665+
fn invalid_crn_provided() {
666+
let env = merge_env_vars(vec![(
667+
"CS_WORKSPACE_CRN",
668+
Some("crn:ap-southeast-N.aws:ABCDE12345"),
669+
)]);
670+
671+
with_no_cs_vars(|| {
672+
temp_env::with_vars(env, || {
673+
let config = TandemConfig::build("tests/config/unknown.toml");
674+
assert!(config.is_err());
675+
676+
if let Err(e) = config {
677+
assert!(e.to_string().contains("Invalid Workspace CRN"));
678+
}
679+
})
680+
});
681+
}
682+
655683
#[test]
656684
fn missing_auth_config() {
657685
let env = merge_env_vars(vec![("CS_CLIENT_ACCESS_KEY", None)]);

packages/cipherstash-proxy/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ pub enum ConfigError {
125125
#[error("Invalid {name}: {value}")]
126126
InvalidParameter { name: String, value: String },
127127

128+
#[error(
129+
"Invalid Workspace CRN: {crn}. CRN format is `crn:{{region}}.aws:{{workspace_id}}` For help visit {}",
130+
ERROR_DOC_CONFIG_URL
131+
)]
132+
InvalidWorkspaceCrn { crn: String },
133+
128134
#[error("Missing an active Encrypt configuration")]
129135
MissingActiveEncryptConfig,
130136

0 commit comments

Comments
 (0)