Skip to content

Commit a1e940f

Browse files
committed
refactor(config): move client_key validation to build() and remove dev endpoint config
Move ClientKey::from_hex_v1 validation from load() to build() so all config validation happens in one place. This also ensures build_path() callers get the same validation. Add EncryptConfig::client_key() accessor that returns Result instead of requiring callers to validate manually. Replace expect() with proper error propagation in init_zerokms_client. Remove zerokms_host/cts_host dev config fields now handled directly by cipherstash-client via CS_ZEROKMS_HOST/CS_CTS_HOST env vars.
1 parent a02bb7a commit a1e940f

File tree

7 files changed

+28
-62
lines changed

7 files changed

+28
-62
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DEVELOPMENT.md

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -501,29 +501,14 @@ Certificates are generated by `mkcert`, and live in `tests/tls/`.
501501

502502
#### Configuration: development endpoints
503503

504+
ZeroKMS and CTS host endpoints can be configured for local development using environment variables.
504505

505-
ZeroKMS and CTS host endpoints can be configured for local development.
506+
These are read directly by `cipherstash-client` and do not require proxy configuration:
506507

507-
Env variables are `CS_DEVELOPMENT__ZEROKMS_HOST` and `CS_DEVELOPMENT__CTS_HOST`.
508-
509-
510-
```toml
511-
512-
[development]
513-
# ZeroKMS host
514-
# Optional
515-
# Defaults to CipherStash Production ZeroKMS host
516-
# Env: CS_DEVELOPMENT__ZEROKMS_HOST
517-
zerokms_host = "1.1.1.1"
518-
519-
520-
# CTS host
521-
# Optional
522-
# Defaults to CipherStash Production CTS host
523-
# Env: CS_DEVELOPMENT__CTS_HOST
524-
cts_host = "1.1.1.1"
525-
526-
```
508+
| Variable | Description |
509+
|---|---|
510+
| `CS_ZEROKMS_HOST` | Override ZeroKMS endpoint (default: resolved from JWT `services` claim) |
511+
| `CS_CTS_HOST` | Override CTS auth endpoint (default: resolved from workspace CRN region) |
527512

528513

529514

packages/cipherstash-proxy/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ tokio-rustls = "0.26.0"
5454
tokio-util = { version = "0.7.13", features = ["rt"] }
5555
tracing = { workspace = true }
5656
tracing-subscriber = { workspace = true }
57-
url = "2"
5857
# Pin vitaminc to 0.1.0-pre4.1 — pre4.2 removed root-level re-exports breaking cts-common
5958
vitaminc = { version = "= 0.1.0-pre4.1", features = ["random", "protected", "encrypt"] }
6059
vitaminc-random = { version = "= 0.1.0-pre4.1" }

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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ pub struct DevelopmentConfig {
6767

6868
#[serde(default)]
6969
pub enable_mapping_errors: bool,
70-
71-
#[serde(default)]
72-
pub zerokms_host: Option<url::Url>,
73-
74-
#[serde(default)]
75-
pub cts_host: Option<String>,
7670
}
7771

7872
/// Config defaults to a file called `tandem` in the current directory.
@@ -114,9 +108,6 @@ impl TandemConfig {
114108
config.log.format = args.log_format;
115109
}
116110

117-
ClientKey::from_hex_v1(config.encrypt.client_id, &config.encrypt.client_key)
118-
.map_err(|e| ConfigError::InvalidClientKey(e.into()))?;
119-
120111
Ok(config)
121112
}
122113

@@ -226,6 +217,9 @@ impl TandemConfig {
226217
}
227218
})?;
228219

220+
ClientKey::from_hex_v1(config.encrypt.client_id, &config.encrypt.client_key)
221+
.map_err(|e| ConfigError::InvalidClientKey(e.into()))?;
222+
229223
Ok(config)
230224
}
231225

@@ -250,18 +244,6 @@ impl TandemConfig {
250244
}
251245
}
252246

253-
pub fn zerokms_host(&self) -> Option<url::Url> {
254-
self.development
255-
.as_ref()
256-
.and_then(|dev| dev.zerokms_host.clone())
257-
}
258-
259-
pub fn cts_host(&self) -> Option<String> {
260-
self.development
261-
.as_ref()
262-
.and_then(|dev| dev.cts_host.clone())
263-
}
264-
265247
pub fn use_structured_logging(&self) -> bool {
266248
matches!(self.log.format, LogFormat::Structured)
267249
}
@@ -344,6 +326,13 @@ impl TandemConfig {
344326
}
345327
}
346328

329+
impl EncryptConfig {
330+
pub fn client_key(&self) -> Result<ClientKey, Error> {
331+
ClientKey::from_hex_v1(self.client_id, &self.client_key)
332+
.map_err(|e| ConfigError::InvalidClientKey(e.into()).into())
333+
}
334+
}
335+
347336
impl PrometheusConfig {
348337
pub fn default_port() -> u16 {
349338
9930
@@ -435,7 +424,7 @@ mod tests {
435424
Some("11111111-1111-1111-1111-111111111111"),
436425
),
437426
(CS_CLIENT_ID, Some("22222222-2222-2222-2222-222222222222")),
438-
(CS_CLIENT_KEY, Some("test_key")),
427+
(CS_CLIENT_KEY, Some("a4627031a16b7065726d75746174696f6e900e05030d0608090007020c04010b0a0f6770325f66726f6da16b7065726d75746174696f6e900608000a0204030f01070d090e0b0c056570325f746fa16b7065726d75746174696f6e90000908060701030a05040e020d0b0c0f627033a16b7065726d75746174696f6e982107181d130d05181f08040a181c1002181e010311181818200b0f0e0915181b0c16171819060012181a14")),
439428
(
440429
CS_DEFAULT_KEYSET_ID,
441430
Some("dd0a239f-02e2-4c8e-ba20-d9f0f85af9ac"),
@@ -594,7 +583,7 @@ mod tests {
594583
fn default_env_vars() -> Vec<(&'static str, Option<&'static str>)> {
595584
vec![
596585
("CS_CLIENT_ID", Some("00000000-0000-0000-0000-000000000000")),
597-
("CS_CLIENT_KEY", Some("CS_CLIENT_KEY")),
586+
("CS_CLIENT_KEY", Some("a4627031a16b7065726d75746174696f6e900e05030d0608090007020c04010b0a0f6770325f66726f6da16b7065726d75746174696f6e900608000a0204030f01070d090e0b0c056570325f746fa16b7065726d75746174696f6e90000908060701030a05040e020d0b0c0f627033a16b7065726d75746174696f6e982107181d130d05181f08040a181c1002181e010311181818200b0f0e0915181b0c16171819060012181a14")),
598587
(
599588
"CS_DEFAULT_KEYSET_ID",
600589
Some("00000000-0000-0000-0000-000000000000"),

packages/cipherstash-proxy/src/proxy/zerokms/mod.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,25 @@ mod zerokms;
44
pub use zerokms::ZeroKms;
55

66
use crate::config::TandemConfig;
7+
use crate::error::{Error, ZeroKMSError};
78
use cipherstash_client::{
8-
zerokms::{ClientKey, ZeroKMSBuilder, ZeroKMSBuilderError},
9+
zerokms::{ClientKey, ZeroKMSBuilder},
910
AutoStrategy, ZeroKMS,
1011
};
1112

1213
pub type ScopedCipher = cipherstash_client::encryption::ScopedCipher<AutoStrategy>;
1314

1415
pub type ZerokmsClient = ZeroKMS<AutoStrategy, ClientKey>;
1516

16-
pub(crate) fn init_zerokms_client(
17-
config: &TandemConfig,
18-
) -> Result<ZerokmsClient, ZeroKMSBuilderError> {
17+
pub(crate) fn init_zerokms_client(config: &TandemConfig) -> Result<ZerokmsClient, Error> {
1918
let strategy = AutoStrategy::builder()
2019
.with_access_key(&config.auth.client_access_key)
2120
.with_workspace_crn(config.auth.workspace_crn.clone())
22-
.detect()?;
21+
.detect()
22+
.map_err(|_| ZeroKMSError::AuthenticationFailed)?;
2323

24-
let client_key = ClientKey::from_hex_v1(config.encrypt.client_id, &config.encrypt.client_key)
25-
.expect("validated during config loading");
24+
let client_key = config.encrypt.client_key()?;
2625

27-
let mut builder = ZeroKMSBuilder::new(strategy);
28-
29-
if let Some(url) = config.zerokms_host() {
30-
builder = builder.with_base_url(url);
31-
}
32-
33-
builder.with_client_key(client_key).build()
26+
let builder = ZeroKMSBuilder::new(strategy);
27+
Ok(builder.with_client_key(client_key).build()?)
3428
}

packages/cipherstash-proxy/tests/config/cipherstash-proxy-test.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ client_access_key = "client_access_key"
1616
[encrypt]
1717
default_keyset_id = "484cd205-99e8-41ca-acfe-55a7e25a8ec2" # generated guid for validation
1818
client_id = "5912717c-2c3b-4fb6-a051-0a8e71cd9e37" # generated guid for validation
19-
client_key = "client_key"
19+
client_key = "a4627031a16b7065726d75746174696f6e900e05030d0608090007020c04010b0a0f6770325f66726f6da16b7065726d75746174696f6e900608000a0204030f01070d090e0b0c056570325f746fa16b7065726d75746174696f6e90000908060701030a05040e020d0b0c0f627033a16b7065726d75746174696f6e982107181d130d05181f08040a181c1002181e010311181818200b0f0e0915181b0c16171819060012181a14"

packages/cipherstash-proxy/tests/config/cipherstash-proxy-with-crn.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ password = "password"
99

1010
[encrypt]
1111
client_id = "5912717c-2c3b-4fb6-a051-0a8e71cd9e37" # generated guid for validation
12-
client_key = "client_key"
12+
client_key = "a4627031a16b7065726d75746174696f6e900e05030d0608090007020c04010b0a0f6770325f66726f6da16b7065726d75746174696f6e900608000a0204030f01070d090e0b0c056570325f746fa16b7065726d75746174696f6e90000908060701030a05040e020d0b0c0f627033a16b7065726d75746174696f6e982107181d130d05181f08040a181c1002181e010311181818200b0f0e0915181b0c16171819060012181a14"

0 commit comments

Comments
 (0)