@@ -23,11 +23,12 @@ use openshell_core::proto::{
2323 HealthRequest , HealthResponse , ListProvidersRequest , ListProvidersResponse ,
2424 ListSandboxProvidersRequest , ListSandboxProvidersResponse , ListSandboxesRequest ,
2525 ListSandboxesResponse , Provider , ProviderCredentialRefresh , ProviderCredentialRefreshStatus ,
26- ProviderCredentialRefreshStrategy , ProviderProfile , ProviderProfileCredential ,
27- ProviderProfileDiscovery , ProviderResponse , RevokeSshSessionRequest , RevokeSshSessionResponse ,
28- RotateProviderCredentialRequest , RotateProviderCredentialResponse , Sandbox , SandboxResponse ,
29- SandboxStreamEvent , ServiceStatus , SettingValue , SupervisorMessage , UpdateProviderRequest ,
30- WatchSandboxRequest ,
26+ ProviderCredentialRefreshStrategy , ProviderCredentialTokenGrant ,
27+ ProviderCredentialTokenGrantSubjectToken , ProviderCredentialTokenGrantType , ProviderProfile ,
28+ ProviderProfileCredential , ProviderProfileDiscovery , ProviderResponse , RevokeSshSessionRequest ,
29+ RevokeSshSessionResponse , RotateProviderCredentialRequest , RotateProviderCredentialResponse ,
30+ Sandbox , SandboxResponse , SandboxStreamEvent , ServiceStatus , SettingValue , SupervisorMessage ,
31+ UpdateProviderRequest , WatchSandboxRequest ,
3132} ;
3233use openshell_core:: { ObjectId , ObjectName } ;
3334use std:: collections:: HashMap ;
@@ -44,6 +45,7 @@ use tonic::{Response, Status};
4445struct ProviderState {
4546 providers : Arc < Mutex < HashMap < String , Provider > > > ,
4647 profiles : Arc < Mutex < HashMap < String , ProviderProfile > > > ,
48+ scoped_profiles : Arc < Mutex < HashMap < ( String , String ) , ProviderProfile > > > ,
4749 refresh_statuses : Arc < Mutex < HashMap < ( String , String ) , ProviderCredentialRefreshStatus > > > ,
4850 refresh_requests : Arc < Mutex < Vec < ProviderRefreshRequestLog > > > ,
4951 provider_update_requests : Arc < Mutex < Vec < Provider > > > ,
@@ -489,8 +491,18 @@ impl OpenShell for TestOpenShell {
489491 & self ,
490492 request : tonic:: Request < openshell_core:: proto:: GetProviderProfileRequest > ,
491493 ) -> Result < Response < openshell_core:: proto:: ProviderProfileResponse > , Status > {
492- let id = request. into_inner ( ) . id ;
493- let profile = if let Some ( profile) = openshell_providers:: builtin_profiles ( )
494+ let request = request. into_inner ( ) ;
495+ let id = request. id ;
496+ let scoped_profile = self
497+ . state
498+ . scoped_profiles
499+ . lock ( )
500+ . await
501+ . get ( & ( request. workspace , id. clone ( ) ) )
502+ . cloned ( ) ;
503+ let profile = if let Some ( profile) = scoped_profile {
504+ profile
505+ } else if let Some ( profile) = openshell_providers:: builtin_profiles ( )
494506 . iter ( )
495507 . find ( |profile| profile. id == id)
496508 {
@@ -2201,6 +2213,147 @@ async fn provider_update_from_existing_uses_profile_discovery() {
22012213 ) ;
22022214}
22032215
2216+ #[ tokio:: test]
2217+ async fn provider_update_from_existing_preserves_global_profile_scope ( ) {
2218+ let ts = run_server ( ) . await ;
2219+ let profile_id = "shadowed-update-discovery" ;
2220+ for ( profile_workspace, env_var) in [
2221+ ( "" , "GLOBAL_UPDATE_DISCOVERY_API_KEY" ) ,
2222+ ( "default" , "WORKSPACE_UPDATE_DISCOVERY_API_KEY" ) ,
2223+ ] {
2224+ ts. state . scoped_profiles . lock ( ) . await . insert (
2225+ ( profile_workspace. to_string ( ) , profile_id. to_string ( ) ) ,
2226+ ProviderProfile {
2227+ id : profile_id. to_string ( ) ,
2228+ credentials : vec ! [ ProviderProfileCredential {
2229+ name: "api_key" . to_string( ) ,
2230+ env_vars: vec![ env_var. to_string( ) ] ,
2231+ required: true ,
2232+ ..Default :: default ( )
2233+ } ] ,
2234+ discovery : Some ( ProviderProfileDiscovery {
2235+ credentials : vec ! [ "api_key" . to_string( ) ] ,
2236+ } ) ,
2237+ ..Default :: default ( )
2238+ } ,
2239+ ) ;
2240+ }
2241+ ts. state . providers . lock ( ) . await . insert (
2242+ "global-update" . to_string ( ) ,
2243+ Provider {
2244+ metadata : Some ( openshell_core:: proto:: datamodel:: v1:: ObjectMeta {
2245+ id : "id-global-update" . to_string ( ) ,
2246+ name : "global-update" . to_string ( ) ,
2247+ workspace : "default" . to_string ( ) ,
2248+ ..Default :: default ( )
2249+ } ) ,
2250+ r#type : profile_id. to_string ( ) ,
2251+ profile_workspace : String :: new ( ) ,
2252+ ..Default :: default ( )
2253+ } ,
2254+ ) ;
2255+ let _env = EnvVarGuard :: set ( & [
2256+ ( "GLOBAL_UPDATE_DISCOVERY_API_KEY" , "global-secret" ) ,
2257+ ( "WORKSPACE_UPDATE_DISCOVERY_API_KEY" , "workspace-secret" ) ,
2258+ ] ) ;
2259+
2260+ run:: provider_update ( run:: ProviderUpdateOptions {
2261+ server : & ts. endpoint ,
2262+ name : "global-update" ,
2263+ from_existing : true ,
2264+ from_oidc_token : false ,
2265+ credentials : & [ ] ,
2266+ config : & [ ] ,
2267+ credential_expires_at : & [ ] ,
2268+ workspace : "default" ,
2269+ tls : & ts. tls ,
2270+ } )
2271+ . await
2272+ . expect ( "global profile-backed provider update --from-existing" ) ;
2273+
2274+ let provider = ts
2275+ . state
2276+ . providers
2277+ . lock ( )
2278+ . await
2279+ . get ( "global-update" )
2280+ . cloned ( )
2281+ . expect ( "global provider should still be stored" ) ;
2282+ assert_eq ! (
2283+ provider. credentials. get( "GLOBAL_UPDATE_DISCOVERY_API_KEY" ) ,
2284+ Some ( & "global-secret" . to_string( ) )
2285+ ) ;
2286+ assert ! (
2287+ !provider
2288+ . credentials
2289+ . contains_key( "WORKSPACE_UPDATE_DISCOVERY_API_KEY" )
2290+ ) ;
2291+ }
2292+
2293+ #[ tokio:: test]
2294+ async fn provider_update_from_oidc_token_preserves_global_profile_scope ( ) {
2295+ let ts = run_server ( ) . await ;
2296+ let profile_id = "shadowed-oidc-update" ;
2297+ for ( profile_workspace, subject_credential) in [
2298+ ( "" , "GLOBAL_SUBJECT_TOKEN" ) ,
2299+ ( "default" , "WORKSPACE_SUBJECT_TOKEN" ) ,
2300+ ] {
2301+ ts. state . scoped_profiles . lock ( ) . await . insert (
2302+ ( profile_workspace. to_string ( ) , profile_id. to_string ( ) ) ,
2303+ ProviderProfile {
2304+ id : profile_id. to_string ( ) ,
2305+ credentials : vec ! [ ProviderProfileCredential {
2306+ name: "dynamic_token" . to_string( ) ,
2307+ token_grant: Some ( ProviderCredentialTokenGrant {
2308+ grant_type: ProviderCredentialTokenGrantType :: TokenExchange as i32 ,
2309+ subject_token: Some ( ProviderCredentialTokenGrantSubjectToken {
2310+ source: "provider_credential" . to_string( ) ,
2311+ credential: subject_credential. to_string( ) ,
2312+ ..Default :: default ( )
2313+ } ) ,
2314+ ..Default :: default ( )
2315+ } ) ,
2316+ ..Default :: default ( )
2317+ } ] ,
2318+ ..Default :: default ( )
2319+ } ,
2320+ ) ;
2321+ }
2322+ ts. state . providers . lock ( ) . await . insert (
2323+ "global-oidc-update" . to_string ( ) ,
2324+ Provider {
2325+ metadata : Some ( openshell_core:: proto:: datamodel:: v1:: ObjectMeta {
2326+ id : "id-global-oidc-update" . to_string ( ) ,
2327+ name : "global-oidc-update" . to_string ( ) ,
2328+ workspace : "default" . to_string ( ) ,
2329+ ..Default :: default ( )
2330+ } ) ,
2331+ r#type : profile_id. to_string ( ) ,
2332+ profile_workspace : String :: new ( ) ,
2333+ ..Default :: default ( )
2334+ } ,
2335+ ) ;
2336+
2337+ let err = run:: provider_update ( run:: ProviderUpdateOptions {
2338+ server : & ts. endpoint ,
2339+ name : "global-oidc-update" ,
2340+ from_existing : false ,
2341+ from_oidc_token : true ,
2342+ credentials : & [ "GLOBAL_SUBJECT_TOKEN" . to_string ( ) ] ,
2343+ config : & [ ] ,
2344+ credential_expires_at : & [ ] ,
2345+ workspace : "default" ,
2346+ tls : & ts. tls ,
2347+ } )
2348+ . await
2349+ . expect_err ( "unnamed test gateway should stop after profile validation" ) ;
2350+
2351+ assert ! (
2352+ err. to_string( ) . contains( "active named OIDC gateway" ) ,
2353+ "global profile should accept GLOBAL_SUBJECT_TOKEN before OIDC loading: {err}"
2354+ ) ;
2355+ }
2356+
22042357#[ tokio:: test]
22052358async fn provider_profile_import_from_directory_imports_supported_profile_files ( ) {
22062359 let ts = run_server ( ) . await ;
0 commit comments