@@ -9,6 +9,7 @@ use crate::Args;
99use cipherstash_client:: config:: vars:: {
1010 CS_CLIENT_ACCESS_KEY , CS_CLIENT_ID , CS_CLIENT_KEY , CS_DEFAULT_KEYSET_ID , CS_WORKSPACE_CRN ,
1111} ;
12+ use cipherstash_client:: zerokms:: ClientKey ;
1213use config:: { Config , Environment } ;
1314use cts_common:: Crn ;
1415use regex:: Regex ;
@@ -42,7 +43,7 @@ pub struct AuthConfig {
4243
4344#[ derive( Debug , Deserialize , Clone , PartialEq ) ]
4445pub struct EncryptConfig {
45- pub client_id : String ,
46+ pub client_id : Uuid ,
4647 pub client_key : String ,
4748 pub default_keyset_id : Option < Uuid > ,
4849}
@@ -66,12 +67,6 @@ pub struct DevelopmentConfig {
6667
6768 #[ serde( default ) ]
6869 pub enable_mapping_errors : bool ,
69-
70- #[ serde( default ) ]
71- pub zerokms_host : Option < String > ,
72-
73- #[ serde( default ) ]
74- pub cts_host : Option < String > ,
7570}
7671
7772/// Config defaults to a file called `tandem` in the current directory.
@@ -191,7 +186,7 @@ impl TandemConfig {
191186 }
192187
193188 // Source order is important!
194- let config = Config :: builder ( )
189+ let config: TandemConfig = Config :: builder ( )
195190 . add_source ( config:: File :: with_name ( & args. config_file_path ) . required ( false ) )
196191 . add_source ( cs_env_source)
197192 . add_source ( stash_setup_source)
@@ -203,7 +198,16 @@ impl TandemConfig {
203198 // - missing parameters are returned by at least two different errors, depending the source of the error
204199 // Easier to inspect the error message.
205200 match err. to_string ( ) {
206- s if s. contains ( "UUID parsing failed" ) => ConfigError :: InvalidDatasetId ,
201+ s if s. contains ( "UUID parsing failed" ) => {
202+ if s. contains ( "client_id" ) && !s. contains ( "keyset" ) {
203+ ConfigError :: InvalidParameter {
204+ name : "client_id" . to_string ( ) ,
205+ value : "invalid UUID" . to_string ( ) ,
206+ }
207+ } else {
208+ ConfigError :: InvalidDefaultKeysetId
209+ }
210+ }
207211 s if s. contains ( "missing field" ) => {
208212 let ( field, key) = extract_missing_field_and_key ( & s) ;
209213 match ( field, key) {
@@ -222,6 +226,8 @@ impl TandemConfig {
222226 }
223227 } ) ?;
224228
229+ config. encrypt . build_client_key ( ) ?;
230+
225231 Ok ( config)
226232 }
227233
@@ -246,18 +252,6 @@ impl TandemConfig {
246252 }
247253 }
248254
249- pub fn zerokms_host ( & self ) -> Option < String > {
250- self . development
251- . as_ref ( )
252- . and_then ( |dev| dev. zerokms_host . clone ( ) )
253- }
254-
255- pub fn cts_host ( & self ) -> Option < String > {
256- self . development
257- . as_ref ( )
258- . and_then ( |dev| dev. cts_host . clone ( ) )
259- }
260-
261255 pub fn use_structured_logging ( & self ) -> bool {
262256 matches ! ( self . log. format, LogFormat :: Structured )
263257 }
@@ -326,8 +320,8 @@ impl TandemConfig {
326320 client_access_key : "test" . to_string ( ) ,
327321 } ,
328322 encrypt : EncryptConfig {
329- client_id : "test" . to_string ( ) ,
330- client_key : "test " . to_string ( ) ,
323+ client_id : Uuid :: parse_str ( "00000000-0000-0000-0000-000000000001" ) . unwrap ( ) ,
324+ client_key : "a4627031a16b7065726d75746174696f6e900e05030d0608090007020c04010b0a0f6770325f66726f6da16b7065726d75746174696f6e900608000a0204030f01070d090e0b0c056570325f746fa16b7065726d75746174696f6e90000908060701030a05040e020d0b0c0f627033a16b7065726d75746174696f6e982107181d130d05181f08040a181c1002181e010311181818200b0f0e0915181b0c16171819060012181a14 " . to_string ( ) ,
331325 default_keyset_id : Some (
332326 Uuid :: parse_str ( "00000000-0000-0000-0000-000000000000" ) . unwrap ( ) ,
333327 ) ,
@@ -340,6 +334,13 @@ impl TandemConfig {
340334 }
341335}
342336
337+ impl EncryptConfig {
338+ pub fn build_client_key ( & self ) -> Result < ClientKey , Error > {
339+ ClientKey :: from_hex_v1 ( self . client_id , & self . client_key )
340+ . map_err ( |_| Error :: from ( ConfigError :: InvalidClientKey ) )
341+ }
342+ }
343+
343344impl PrometheusConfig {
344345 pub fn default_port ( ) -> u16 {
345346 9930
@@ -426,9 +427,12 @@ mod tests {
426427 temp_env:: with_vars (
427428 [
428429 // Orignal recipe ENV var
429- ( "CS_ENCRYPT__CLIENT_ID" , Some ( "CS_ENCRYPT__CLIENT_ID" ) ) ,
430- ( CS_CLIENT_ID , Some ( "CS_CLIENT_ID" ) ) ,
431- ( CS_CLIENT_KEY , Some ( "CS_CLIENT_KEY" ) ) ,
430+ (
431+ "CS_ENCRYPT__CLIENT_ID" ,
432+ Some ( "11111111-1111-1111-1111-111111111111" ) ,
433+ ) ,
434+ ( CS_CLIENT_ID , Some ( "22222222-2222-2222-2222-222222222222" ) ) ,
435+ ( CS_CLIENT_KEY , Some ( "a4627031a16b7065726d75746174696f6e900e05030d0608090007020c04010b0a0f6770325f66726f6da16b7065726d75746174696f6e900608000a0204030f01070d090e0b0c056570325f746fa16b7065726d75746174696f6e90000908060701030a05040e020d0b0c0f627033a16b7065726d75746174696f6e982107181d130d05181f08040a181c1002181e010311181818200b0f0e0915181b0c16171819060012181a14" ) ) ,
432436 (
433437 CS_DEFAULT_KEYSET_ID ,
434438 Some ( "dd0a239f-02e2-4c8e-ba20-d9f0f85af9ac" ) ,
@@ -440,7 +444,10 @@ mod tests {
440444 TandemConfig :: build_path ( "tests/config/cipherstash-proxy-test.toml" )
441445 . unwrap ( ) ;
442446
443- assert_eq ! ( config. encrypt. client_id, "CS_CLIENT_ID" . to_string( ) ) ;
447+ assert_eq ! (
448+ config. encrypt. client_id,
449+ Uuid :: parse_str( "22222222-2222-2222-2222-222222222222" ) . unwrap( )
450+ ) ;
444451
445452 assert_eq ! (
446453 config. auth. client_access_key,
@@ -474,8 +481,8 @@ mod tests {
474481 . unwrap ( ) ;
475482
476483 assert_eq ! (
477- & config. encrypt. client_id,
478- "dd0a239f-02e2-4c8e-ba20-d9f0f85af9ac"
484+ config. encrypt. client_id,
485+ Uuid :: parse_str ( "dd0a239f-02e2-4c8e-ba20-d9f0f85af9ac" ) . unwrap ( )
479486 ) ;
480487 } ,
481488 ) ;
@@ -512,6 +519,22 @@ mod tests {
512519 } ) ;
513520 }
514521
522+ #[ test]
523+ fn invalid_client_id_uuid ( ) {
524+ with_no_cs_vars ( || {
525+ let result =
526+ TandemConfig :: build_path ( "tests/config/cipherstash-proxy-bad-client-id.toml" ) ;
527+ assert ! ( result. is_err( ) ) ;
528+ let err = result. unwrap_err ( ) ;
529+ // Should produce InvalidParameter for client_id, not InvalidDatasetId
530+ assert ! (
531+ err. to_string( ) . contains( "Invalid client_id" ) ,
532+ "Expected 'Invalid client_id' but got: {}" ,
533+ err
534+ ) ;
535+ } ) ;
536+ }
537+
515538 #[ test]
516539 fn prometheus_config ( ) {
517540 with_no_cs_vars ( || {
@@ -584,7 +607,7 @@ mod tests {
584607 fn default_env_vars ( ) -> Vec < ( & ' static str , Option < & ' static str > ) > {
585608 vec ! [
586609 ( "CS_CLIENT_ID" , Some ( "00000000-0000-0000-0000-000000000000" ) ) ,
587- ( "CS_CLIENT_KEY" , Some ( "CS_CLIENT_KEY " ) ) ,
610+ ( "CS_CLIENT_KEY" , Some ( "a4627031a16b7065726d75746174696f6e900e05030d0608090007020c04010b0a0f6770325f66726f6da16b7065726d75746174696f6e900608000a0204030f01070d090e0b0c056570325f746fa16b7065726d75746174696f6e90000908060701030a05040e020d0b0c0f627033a16b7065726d75746174696f6e982107181d130d05181f08040a181c1002181e010311181818200b0f0e0915181b0c16171819060012181a14 " ) ) ,
588611 (
589612 "CS_DEFAULT_KEYSET_ID" ,
590613 Some ( "00000000-0000-0000-0000-000000000000" ) ,
0 commit comments