@@ -1066,13 +1066,18 @@ async fn active_provider_environment_keys(
10661066) -> Result < Vec < String > , Status > {
10671067 let mut keys = active_provider_credential_keys ( provider, now_ms) ;
10681068 if !provider. object_id ( ) . is_empty ( ) {
1069- keys . extend (
1069+ for state in
10701070 crate :: provider_refresh:: list_refresh_states_for_provider ( store, provider. object_id ( ) )
10711071 . await ?
1072- . into_iter ( )
1073- . map ( |state| state. credential_key )
1074- . filter ( |key| is_valid_env_key ( key) ) ,
1075- ) ;
1072+ {
1073+ // The primary key plus every co-minted output key this refresh owns,
1074+ // so a configured-but-not-yet-minted refresh reserves all of them.
1075+ keys. extend (
1076+ std:: iter:: once ( state. credential_key )
1077+ . chain ( state. additional_output_keys . into_values ( ) )
1078+ . filter ( |key| is_valid_env_key ( key) ) ,
1079+ ) ;
1080+ }
10761081 }
10771082 keys. sort ( ) ;
10781083 keys. dedup ( ) ;
@@ -1512,6 +1517,21 @@ async fn provider_refresh_defaults(
15121517 . and_then ( |credential| credential. refresh . clone ( ) ) )
15131518}
15141519
1520+ /// Resolve the env keys a refresh co-mints (beyond its primary credential) from
1521+ /// the provider's profile `additional_outputs`. Returns semantic output id ->
1522+ /// env key. Empty when the provider type has no profile or the credential
1523+ /// declares no additional outputs.
1524+ async fn resolved_additional_output_keys (
1525+ store : & Store ,
1526+ provider : & Provider ,
1527+ credential_key : & str ,
1528+ ) -> Result < std:: collections:: HashMap < String , String > , Status > {
1529+ let Some ( profile) = get_provider_type_profile ( store, & provider. r#type ) . await ? else {
1530+ return Ok ( std:: collections:: HashMap :: new ( ) ) ;
1531+ } ;
1532+ Ok ( profile. resolved_additional_output_keys ( credential_key) )
1533+ }
1534+
15151535fn validate_refresh_material (
15161536 material : & std:: collections:: HashMap < String , String > ,
15171537 refresh_defaults : Option < & CredentialRefreshProfile > ,
@@ -2076,15 +2096,19 @@ pub(super) async fn handle_configure_provider_refresh(
20762096 credential_key,
20772097 )
20782098 . await ?;
2079- if strategy == ProviderCredentialRefreshStrategy :: AwsStsAssumeRole {
2080- for additional_key in [ "AWS_SECRET_ACCESS_KEY" , "AWS_SESSION_TOKEN" ] {
2081- validate_provider_credential_key_available_for_attached_sandboxes (
2082- state. store . as_ref ( ) ,
2083- & provider,
2084- additional_key,
2085- )
2086- . await ?;
2087- }
2099+ // Reserve every env key this refresh will co-mint (resolved from the
2100+ // profile's additional_outputs), not just the primary, so a
2101+ // configured-but-not-yet-minted refresh can't collide with another provider
2102+ // that later claims one of the derived keys.
2103+ let additional_output_keys =
2104+ resolved_additional_output_keys ( state. store . as_ref ( ) , & provider, credential_key) . await ?;
2105+ for additional_key in additional_output_keys. values ( ) {
2106+ validate_provider_credential_key_available_for_attached_sandboxes (
2107+ state. store . as_ref ( ) ,
2108+ & provider,
2109+ additional_key,
2110+ )
2111+ . await ?;
20882112 }
20892113 let refresh_defaults =
20902114 provider_refresh_defaults ( state. store . as_ref ( ) , & provider, credential_key) . await ?;
@@ -2152,6 +2176,7 @@ pub(super) async fn handle_configure_provider_refresh(
21522176 scopes,
21532177 refresh_before_seconds,
21542178 max_lifetime_seconds,
2179+ additional_output_keys,
21552180 } ,
21562181 ) ?;
21572182 if let Some ( existing) = existing_refresh_state {
@@ -3061,6 +3086,7 @@ mod tests {
30613086 scopes : Vec :: new ( ) ,
30623087 refresh_before_seconds : 300 ,
30633088 max_lifetime_seconds : 3600 ,
3089+ additional_outputs : Vec :: new ( ) ,
30643090 material : vec ! [
30653091 ProviderCredentialRefreshMaterial {
30663092 name: "client_id" . to_string( ) ,
@@ -4390,6 +4416,7 @@ mod tests {
43904416 & provider,
43914417 "API_TOKEN" ,
43924418 crate :: provider_refresh:: NewRefreshStateConfig {
4419+ additional_output_keys : HashMap :: new ( ) ,
43934420 strategy : ProviderCredentialRefreshStrategy :: External ,
43944421 material : HashMap :: from ( [ (
43954422 "endpoint" . to_string ( ) ,
@@ -4599,6 +4626,7 @@ mod tests {
45994626 scopes: vec![ "https://example.test/.default" . to_string( ) ] ,
46004627 refresh_before_seconds: 300 ,
46014628 max_lifetime_seconds: 3600 ,
4629+ additional_outputs: Vec :: new( ) ,
46024630 material: vec![
46034631 ProviderCredentialRefreshMaterial {
46044632 name: "client_id" . to_string( ) ,
@@ -6171,6 +6199,151 @@ mod tests {
61716199 ) ;
61726200 }
61736201
6202+ #[ tokio:: test]
6203+ async fn configure_aws_sts_persists_resolved_additional_output_keys ( ) {
6204+ use crate :: grpc:: StoredSettingValue ;
6205+ use crate :: grpc:: StoredSettings ;
6206+ use crate :: grpc:: policy:: save_global_settings;
6207+
6208+ let state = test_server_state ( ) . await ;
6209+ let global_settings = StoredSettings {
6210+ revision : 1 ,
6211+ settings : std:: iter:: once ( (
6212+ openshell_core:: settings:: PROVIDERS_V2_ENABLED_KEY . to_string ( ) ,
6213+ StoredSettingValue :: Bool ( true ) ,
6214+ ) )
6215+ . collect ( ) ,
6216+ ..Default :: default ( )
6217+ } ;
6218+ save_global_settings ( state. store . as_ref ( ) , & global_settings)
6219+ . await
6220+ . unwrap ( ) ;
6221+
6222+ create_provider_record (
6223+ state. store . as_ref ( ) ,
6224+ Provider {
6225+ metadata : Some ( openshell_core:: proto:: datamodel:: v1:: ObjectMeta {
6226+ id : String :: new ( ) ,
6227+ name : "aws-outputs" . to_string ( ) ,
6228+ created_at_ms : 0 ,
6229+ labels : HashMap :: new ( ) ,
6230+ resource_version : 0 ,
6231+ } ) ,
6232+ r#type : "aws" . to_string ( ) ,
6233+ credentials : HashMap :: new ( ) ,
6234+ config : HashMap :: new ( ) ,
6235+ credential_expires_at_ms : HashMap :: new ( ) ,
6236+ } ,
6237+ )
6238+ . await
6239+ . unwrap ( ) ;
6240+
6241+ handle_configure_provider_refresh (
6242+ & state,
6243+ Request :: new ( ConfigureProviderRefreshRequest {
6244+ provider : "aws-outputs" . to_string ( ) ,
6245+ credential_key : "AWS_ACCESS_KEY_ID" . to_string ( ) ,
6246+ strategy : ProviderCredentialRefreshStrategy :: AwsStsAssumeRole as i32 ,
6247+ material : HashMap :: from ( [ (
6248+ "role_arn" . to_string ( ) ,
6249+ "arn:aws:iam::123456789012:role/Test" . to_string ( ) ,
6250+ ) ] ) ,
6251+ secret_material_keys : Vec :: new ( ) ,
6252+ expires_at_ms : None ,
6253+ } ) ,
6254+ )
6255+ . await
6256+ . unwrap ( ) ;
6257+
6258+ let provider = state
6259+ . store
6260+ . get_message_by_name :: < Provider > ( "aws-outputs" )
6261+ . await
6262+ . unwrap ( )
6263+ . unwrap ( ) ;
6264+ let stored = crate :: provider_refresh:: get_refresh_state (
6265+ state. store . as_ref ( ) ,
6266+ provider. object_id ( ) ,
6267+ "AWS_ACCESS_KEY_ID" ,
6268+ )
6269+ . await
6270+ . unwrap ( )
6271+ . expect ( "refresh state should exist" ) ;
6272+
6273+ assert_eq ! (
6274+ stored. additional_output_keys. get( "secret_access_key" ) ,
6275+ Some ( & "AWS_SECRET_ACCESS_KEY" . to_string( ) )
6276+ ) ;
6277+ assert_eq ! (
6278+ stored. additional_output_keys. get( "session_token" ) ,
6279+ Some ( & "AWS_SESSION_TOKEN" . to_string( ) )
6280+ ) ;
6281+ }
6282+
6283+ #[ tokio:: test]
6284+ async fn active_provider_environment_keys_include_additional_output_keys ( ) {
6285+ let state = test_server_state ( ) . await ;
6286+ create_provider_record (
6287+ state. store . as_ref ( ) ,
6288+ Provider {
6289+ metadata : Some ( openshell_core:: proto:: datamodel:: v1:: ObjectMeta {
6290+ id : String :: new ( ) ,
6291+ name : "aws-env" . to_string ( ) ,
6292+ created_at_ms : 0 ,
6293+ labels : HashMap :: new ( ) ,
6294+ resource_version : 0 ,
6295+ } ) ,
6296+ r#type : "aws" . to_string ( ) ,
6297+ credentials : HashMap :: new ( ) ,
6298+ config : HashMap :: new ( ) ,
6299+ credential_expires_at_ms : HashMap :: new ( ) ,
6300+ } ,
6301+ )
6302+ . await
6303+ . unwrap ( ) ;
6304+ let provider = state
6305+ . store
6306+ . get_message_by_name :: < Provider > ( "aws-env" )
6307+ . await
6308+ . unwrap ( )
6309+ . unwrap ( ) ;
6310+
6311+ // A configured-but-not-yet-minted STS refresh: no minted values are in
6312+ // provider.credentials yet, but the reserved keys must still surface.
6313+ let refresh_state = crate :: provider_refresh:: new_refresh_state (
6314+ & provider,
6315+ "AWS_ACCESS_KEY_ID" ,
6316+ crate :: provider_refresh:: NewRefreshStateConfig {
6317+ additional_output_keys : HashMap :: from ( [
6318+ (
6319+ "secret_access_key" . to_string ( ) ,
6320+ "AWS_SECRET_ACCESS_KEY" . to_string ( ) ,
6321+ ) ,
6322+ ( "session_token" . to_string ( ) , "AWS_SESSION_TOKEN" . to_string ( ) ) ,
6323+ ] ) ,
6324+ strategy : ProviderCredentialRefreshStrategy :: AwsStsAssumeRole ,
6325+ material : HashMap :: new ( ) ,
6326+ secret_material_keys : Vec :: new ( ) ,
6327+ expires_at_ms : 0 ,
6328+ token_url : String :: new ( ) ,
6329+ scopes : Vec :: new ( ) ,
6330+ refresh_before_seconds : 0 ,
6331+ max_lifetime_seconds : 0 ,
6332+ } ,
6333+ )
6334+ . unwrap ( ) ;
6335+ crate :: provider_refresh:: put_refresh_state ( state. store . as_ref ( ) , & refresh_state)
6336+ . await
6337+ . unwrap ( ) ;
6338+
6339+ let keys = active_provider_environment_keys ( state. store . as_ref ( ) , & provider, 0 )
6340+ . await
6341+ . unwrap ( ) ;
6342+ assert ! ( keys. contains( & "AWS_ACCESS_KEY_ID" . to_string( ) ) ) ;
6343+ assert ! ( keys. contains( & "AWS_SECRET_ACCESS_KEY" . to_string( ) ) ) ;
6344+ assert ! ( keys. contains( & "AWS_SESSION_TOKEN" . to_string( ) ) ) ;
6345+ }
6346+
61746347 #[ tokio:: test]
61756348 async fn configure_aws_sts_validates_additional_credential_key_collision ( ) {
61766349 use crate :: grpc:: StoredSettingValue ;
0 commit comments