@@ -758,6 +758,9 @@ pub fn parse_duration_to_ms(s: &str) -> Result<i64> {
758758pub struct ProfileSuggestion {
759759 pub provider_type : String ,
760760 pub credential : String ,
761+ credential_key : String ,
762+ display_name : String ,
763+ refresh : Option < openshell_providers:: CredentialRefreshProfile > ,
761764}
762765
763766fn credential_env_matches (
@@ -788,10 +791,23 @@ fn credential_env_matches(
788791 let mut suggestions = Vec :: new ( ) ;
789792 for profile in profiles {
790793 for cred in & profile. credentials {
791- if cred. env_vars . iter ( ) . any ( |v| v. eq_ignore_ascii_case ( key) ) {
794+ if let Some ( credential_key) =
795+ cred. env_vars . iter ( ) . find ( |v| v. eq_ignore_ascii_case ( key) )
796+ {
797+ let refresh = cred. refresh . as_ref ( ) . filter ( |r| r. is_gateway_mintable ( ) ) ;
798+ // Do not offer a static shortcut for runtime credentials.
799+ // Mixed profiles and token grants need their full setup guide.
800+ if cred. is_runtime_resolvable ( )
801+ && ( refresh. is_none ( ) || !profile. allows_runtime_provider_credentials ( ) )
802+ {
803+ continue ;
804+ }
792805 suggestions. push ( ProfileSuggestion {
793806 provider_type : profile. id . clone ( ) ,
794807 credential : cred. name . clone ( ) ,
808+ credential_key : credential_key. clone ( ) ,
809+ display_name : profile. display_name . clone ( ) ,
810+ refresh : refresh. cloned ( ) ,
795811 } ) ;
796812 }
797813 }
@@ -803,7 +819,13 @@ fn credential_env_matches(
803819
804820 for key in env. keys ( ) {
805821 let sug = profile_suggestions ( key) ;
806- if !sug. is_empty ( ) || looks_like_credential ( key) {
822+ let known_credential = profiles. iter ( ) . any ( |profile| {
823+ profile
824+ . credentials
825+ . iter ( )
826+ . any ( |cred| cred. env_vars . iter ( ) . any ( |v| v. eq_ignore_ascii_case ( key) ) )
827+ } ) ;
828+ if known_credential || looks_like_credential ( key) {
807829 matches. push ( ( key. clone ( ) , sug) ) ;
808830 }
809831 }
@@ -827,34 +849,78 @@ pub fn warn_credential_env_vars(
827849 return ;
828850 }
829851
830- let matches = credential_env_matches ( env, profiles) ;
831- if matches. is_empty ( ) {
832- return ;
852+ for warning in credential_env_warnings ( env, profiles) {
853+ eprintln ! ( "{warning}" ) ;
833854 }
855+ }
834856
835- for ( key, suggestions) in & matches {
836- eprintln ! (
837- "{} {key} looks like a credential passed as a plain environment variable." ,
838- "⚠" . yellow( )
839- ) ;
840- eprintln ! ( " The agent inside the sandbox can read this value directly." ) ;
841- eprintln ! ( ) ;
857+ fn credential_env_warnings (
858+ env : & HashMap < String , String > ,
859+ profiles : & [ openshell_providers:: ProviderTypeProfile ] ,
860+ ) -> Vec < String > {
861+ let mut warnings = Vec :: new ( ) ;
862+ for ( key, suggestions) in credential_env_matches ( env, profiles) {
863+ let mut lines = vec ! [
864+ format!(
865+ "{} {key} looks like a credential passed as a plain environment variable." ,
866+ "⚠" . yellow( )
867+ ) ,
868+ " The agent inside the sandbox can read this value directly." . to_owned( ) ,
869+ String :: new( ) ,
870+ ] ;
842871
843872 if suggestions. is_empty ( ) {
844- eprintln ! ( " To hide it from the agent, use a provider instead of --env." ) ;
873+ lines . push ( " To hide it from the agent, use a provider instead of --env." . to_owned ( ) ) ;
845874 } else {
846- eprintln ! ( " To hide it from the agent, use a provider instead:" ) ;
847- for s in suggestions {
848- eprintln ! (
849- " openshell provider create --name my-{ty} --type {ty} --credential {key}" ,
850- ty = s. provider_type
851- ) ;
875+ lines. push ( " To hide it from the agent, choose a provider setup that matches your authentication:" . to_owned ( ) ) ;
876+ for s in & suggestions {
877+ if let Some ( refresh) = & s. refresh {
878+ let strategy =
879+ super :: provider:: provider_refresh_strategy_name ( refresh. strategy )
880+ . replace ( '_' , "-" ) ;
881+ lines. push ( format ! (
882+ " {} ({strategy}, gateway-managed refresh):" ,
883+ s. display_name
884+ ) ) ;
885+ lines. push ( format ! (
886+ " openshell provider create --name my-{ty} --type {ty} --runtime-credentials" ,
887+ ty = s. provider_type
888+ ) ) ;
889+ lines. push ( format ! (
890+ " Then run openshell provider refresh configure my-{} --credential-key {} --strategy {strategy}" ,
891+ s. provider_type, s. credential_key
892+ ) ) ;
893+ let material = refresh
894+ . material
895+ . iter ( )
896+ . filter ( |m| m. required )
897+ . map ( |m| m. name . as_str ( ) )
898+ . collect :: < Vec < _ > > ( ) ;
899+ if !material. is_empty ( ) {
900+ lines. push ( format ! (
901+ " Supply required refresh material: {}. Use --secret-material-env for secrets." ,
902+ material. join( ", " )
903+ ) ) ;
904+ }
905+ lines. push ( " Follow the refresh setup guide before attaching this provider to a sandbox." . to_owned ( ) ) ;
906+ } else {
907+ lines. push ( format ! (
908+ " {} (existing token or credential):" ,
909+ s. display_name
910+ ) ) ;
911+ lines. push ( format ! (
912+ " openshell provider create --name my-{ty} --type {ty} --credential {key}" ,
913+ ty = s. provider_type
914+ ) ) ;
915+ }
852916 }
853- eprintln ! ( " openshell sandbox create --provider my-<name> ..." ) ;
917+ lines . push ( " openshell sandbox create --provider my-<name> ..." . to_owned ( ) ) ;
854918 }
855- eprintln ! ( " See: {DOCS_PROVIDERS_URL}" ) ;
856- eprintln ! ( ) ;
919+ lines. push ( format ! ( " See: {DOCS_PROVIDERS_URL}" ) ) ;
920+ lines. push ( String :: new ( ) ) ;
921+ warnings. push ( lines. join ( "\n " ) ) ;
857922 }
923+ warnings
858924}
859925
860926pub fn parse_key_value_pairs ( items : & [ String ] , flag : & str ) -> Result < HashMap < String , String > > {
@@ -1254,13 +1320,15 @@ mod tests {
12541320 assert_eq ! ( prof[ 0 ] . 0 , "GITHUB_TOKEN" ) ;
12551321
12561322 let sug = & prof[ 0 ] . 1 ;
1257- assert_eq ! ( sug. len( ) , 2_usize ) ;
1323+ assert_eq ! ( sug. len( ) , 3_usize ) ;
12581324
12591325 assert_eq ! ( sug[ 0 ] . provider_type, "copilot" ) ;
12601326 assert_eq ! ( sug[ 0 ] . credential, "api_token" ) ;
12611327
12621328 assert_eq ! ( sug[ 1 ] . provider_type, "github" ) ;
12631329 assert_eq ! ( sug[ 1 ] . credential, "api_token" ) ;
1330+ assert_eq ! ( sug[ 2 ] . provider_type, "github-app" ) ;
1331+ assert_eq ! ( sug[ 2 ] . credential, "api_token" ) ;
12641332 }
12651333
12661334 #[ test]
@@ -1272,13 +1340,103 @@ mod tests {
12721340 assert_eq ! ( prof[ 0 ] . 0 , "gh_token" ) ;
12731341
12741342 let sug = & prof[ 0 ] . 1 ;
1275- assert_eq ! ( sug. len( ) , 2_usize ) ;
1343+ assert_eq ! ( sug. len( ) , 3_usize ) ;
12761344
12771345 assert_eq ! ( sug[ 0 ] . provider_type, "copilot" ) ;
12781346 assert_eq ! ( sug[ 0 ] . credential, "api_token" ) ;
12791347
12801348 assert_eq ! ( sug[ 1 ] . provider_type, "github" ) ;
12811349 assert_eq ! ( sug[ 1 ] . credential, "api_token" ) ;
1350+ assert_eq ! ( sug[ 2 ] . provider_type, "github-app" ) ;
1351+ assert_eq ! ( sug[ 2 ] . credential, "api_token" ) ;
1352+ }
1353+
1354+ #[ test]
1355+ fn credential_warning_distinguishes_existing_github_tokens_from_app_refresh ( ) {
1356+ for key in [ "GITHUB_TOKEN" , "GH_TOKEN" , "gh_token" ] {
1357+ let warnings = credential_env_warnings ( & env ( & [ ( key, "secretVALUE42" ) ] ) , catalog ( ) ) ;
1358+ let warning = & warnings[ 0 ] ;
1359+ assert ! ( warning. contains( & format!(
1360+ "openshell provider create --name my-github --type github --credential {key}"
1361+ ) ) ) ;
1362+ assert ! ( warning. contains( "GitHub (existing token or credential)" ) ) ;
1363+ assert ! (
1364+ warning. contains( "GitHub App (github-app-installation, gateway-managed refresh)" )
1365+ ) ;
1366+ assert ! ( warning. contains(
1367+ "openshell provider create --name my-github-app --type github-app --runtime-credentials"
1368+ ) ) ;
1369+ assert ! ( warning. contains( & format!(
1370+ "openshell provider refresh configure my-github-app --credential-key {} --strategy github-app-installation" ,
1371+ key. to_ascii_uppercase( )
1372+ ) ) ) ;
1373+ assert ! (
1374+ warning. contains(
1375+ "client_id, installation_id, private_key, repository_ids, permissions"
1376+ )
1377+ ) ;
1378+ assert ! ( warning. contains( "--secret-material-env" ) ) ;
1379+ assert ! ( warning. contains( DOCS_PROVIDERS_URL ) ) ;
1380+ assert ! ( !warning. contains( "--type github-app --credential" ) ) ;
1381+ assert ! ( !warning. contains( "secretVALUE42" ) ) ;
1382+ }
1383+ }
1384+
1385+ #[ test]
1386+ fn credential_warning_uses_profile_capabilities_not_its_name ( ) {
1387+ let mut profile = catalog ( )
1388+ . iter ( )
1389+ . find ( |p| p. id == "github-app" )
1390+ . unwrap ( )
1391+ . clone ( ) ;
1392+ profile. id = "enterprise-repos" . to_owned ( ) ;
1393+ let env = env ( & [ ( "GITHUB_TOKEN" , "secretVALUE42" ) ] ) ;
1394+ let warning = credential_env_warnings ( & env, & [ profile. clone ( ) ] ) . join ( "\n " ) ;
1395+ assert ! ( warning. contains( "--type enterprise-repos --runtime-credentials" ) ) ;
1396+ assert ! ( warning. contains( "--strategy github-app-installation" ) ) ;
1397+
1398+ // A refresh declaration alone does not imply gateway minting.
1399+ for strategy in [
1400+ openshell_core:: proto:: ProviderCredentialRefreshStrategy :: Static ,
1401+ openshell_core:: proto:: ProviderCredentialRefreshStrategy :: External ,
1402+ ] {
1403+ profile. credentials [ 0 ] . refresh . as_mut ( ) . unwrap ( ) . strategy = strategy;
1404+ let warning = credential_env_warnings ( & env, & [ profile. clone ( ) ] ) . join ( "\n " ) ;
1405+ assert ! ( warning. contains( "--type enterprise-repos --credential GITHUB_TOKEN" ) ) ;
1406+ assert ! ( !warning. contains( "--runtime-credentials" ) ) ;
1407+ assert ! ( !warning. contains( "refresh configure" ) ) ;
1408+ }
1409+ }
1410+
1411+ #[ test]
1412+ fn credential_warning_does_not_offer_invalid_runtime_creation_for_mixed_profile ( ) {
1413+ let mut profile = catalog ( )
1414+ . iter ( )
1415+ . find ( |p| p. id == "github-app" )
1416+ . unwrap ( )
1417+ . clone ( ) ;
1418+ let mut static_credential = profile. credentials [ 0 ] . clone ( ) ;
1419+ static_credential. name = "other_secret" . to_owned ( ) ;
1420+ static_credential. env_vars = vec ! [ "OTHER_SECRET" . to_owned( ) ] ;
1421+ static_credential. refresh = None ;
1422+ profile. credentials . push ( static_credential) ;
1423+ // A known alias must still warn even without a credential keyword.
1424+ profile. credentials [ 0 ] . env_vars = vec ! [ "CUSTOM_AUTH" . to_owned( ) ] ;
1425+ let warning = credential_env_warnings ( & env ( & [ ( "CUSTOM_AUTH" , "x" ) ] ) , & [ profile] ) . join ( "\n " ) ;
1426+ assert ! ( warning. contains( "CUSTOM_AUTH looks like a credential" ) ) ;
1427+ assert ! ( warning. contains( "use a provider instead of --env" ) ) ;
1428+ assert ! ( !warning. contains( "provider create" ) ) ;
1429+ }
1430+
1431+ #[ test]
1432+ fn credential_warning_without_catalog_is_generic_and_does_not_leak_values ( ) {
1433+ let warnings = credential_env_warnings ( & env ( & [ ( "APP_SECRET" , "secretVALUE42" ) ] ) , & [ ] ) ;
1434+ assert_eq ! ( warnings. len( ) , 1 ) ;
1435+ assert ! ( warnings[ 0 ] . contains( "APP_SECRET looks like a credential" ) ) ;
1436+ assert ! ( warnings[ 0 ] . contains( "use a provider instead of --env" ) ) ;
1437+ assert ! ( !warnings[ 0 ] . contains( "provider create" ) ) ;
1438+ assert ! ( !warnings[ 0 ] . contains( "secretVALUE42" ) ) ;
1439+ assert ! ( credential_env_warnings( & env( & [ ( "PATH" , "/usr/bin" ) ] ) , & [ ] ) . is_empty( ) ) ;
12821440 }
12831441
12841442 #[ test]
0 commit comments