Skip to content

Commit 9821f7d

Browse files
committed
fix(provider): withhold token-exchange subject credentials
Signed-off-by: Gordon Sim <[email protected]>
1 parent 026e4f0 commit 9821f7d

8 files changed

Lines changed: 311 additions & 27 deletions

File tree

‎crates/openshell-providers/src/profiles.rs‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,10 @@ pub fn validate_profile_set(
16881688
}
16891689
}
16901690

1691+
diagnostics.extend(validate_broker_only_subject_credentials(
1692+
source, profile_id, profile,
1693+
));
1694+
16911695
let mut env_vars = HashSet::new();
16921696
for credential in &profile.credentials {
16931697
for env_var in &credential.env_vars {
@@ -2520,6 +2524,64 @@ fn validate_token_grant_subject_token(
25202524
diagnostics
25212525
}
25222526

2527+
fn validate_broker_only_subject_credentials(
2528+
source: &str,
2529+
profile_id: &str,
2530+
profile: &ProviderTypeProfile,
2531+
) -> Vec<ProfileValidationDiagnostic> {
2532+
let subject_credentials = token_exchange_subject_credential_names(profile);
2533+
if subject_credentials.is_empty() {
2534+
return Vec::new();
2535+
}
2536+
2537+
let mut diagnostics = Vec::new();
2538+
for credential in &profile.credentials {
2539+
let credential_name = credential.name.trim();
2540+
if !subject_credentials.contains(credential_name) {
2541+
continue;
2542+
}
2543+
if credential_has_workload_injection_metadata(credential) {
2544+
diagnostics.push(ProfileValidationDiagnostic::error(
2545+
source,
2546+
profile_id,
2547+
"credentials.token_grant.subject_token.credential",
2548+
format!(
2549+
"subject token credential '{credential_name}' is broker-only and cannot declare workload injection metadata"
2550+
),
2551+
));
2552+
}
2553+
}
2554+
diagnostics
2555+
}
2556+
2557+
fn token_exchange_subject_credential_names(profile: &ProviderTypeProfile) -> HashSet<&str> {
2558+
profile
2559+
.credentials
2560+
.iter()
2561+
.filter_map(|credential| credential.token_grant.as_ref())
2562+
.filter(|token_grant| {
2563+
effective_token_grant_type(token_grant.grant_type)
2564+
== ProviderCredentialTokenGrantType::TokenExchange
2565+
})
2566+
.filter_map(|token_grant| token_grant.subject_token.as_ref())
2567+
.filter(|subject_token| subject_token.source.trim() == "provider_credential")
2568+
.filter_map(|subject_token| {
2569+
let credential = subject_token.credential.trim();
2570+
(!credential.is_empty()).then_some(credential)
2571+
})
2572+
.collect()
2573+
}
2574+
2575+
fn credential_has_workload_injection_metadata(credential: &CredentialProfile) -> bool {
2576+
!credential.env_vars.is_empty()
2577+
|| !credential.auth_style.trim().is_empty()
2578+
|| !credential.header_name.trim().is_empty()
2579+
|| !credential.query_param.trim().is_empty()
2580+
|| !credential.path_template.trim().is_empty()
2581+
|| credential.refresh.is_some()
2582+
|| credential.token_grant.is_some()
2583+
}
2584+
25232585
fn validate_token_grant_audience_overrides(
25242586
source: &str,
25252587
profile_id: &str,
@@ -3562,6 +3624,44 @@ credentials:
35623624
);
35633625
}
35643626

3627+
#[test]
3628+
fn validate_profile_set_rejects_injectable_token_exchange_subject_credential() {
3629+
let profile = parse_profile_yaml(
3630+
r"
3631+
id: injectable-subject-token
3632+
display_name: Injectable Subject Token
3633+
credentials:
3634+
- name: USER_OIDC_TOKEN
3635+
auth_style: header
3636+
header_name: X-Subject-Token
3637+
- name: access_token
3638+
auth_style: bearer
3639+
header_name: Authorization
3640+
token_grant:
3641+
grant_type: token_exchange
3642+
token_endpoint: https://keycloak.example.com/realms/openshell/protocol/openid-connect/token
3643+
subject_token:
3644+
source: provider_credential
3645+
credential: USER_OIDC_TOKEN
3646+
",
3647+
)
3648+
.expect("profile should parse");
3649+
3650+
let diagnostics = validate_profile_set(&[("injectable.yaml".to_string(), profile)]);
3651+
let diagnostic = diagnostics
3652+
.iter()
3653+
.find(|diagnostic| {
3654+
diagnostic.field == "credentials.token_grant.subject_token.credential"
3655+
&& diagnostic.message.contains("broker-only")
3656+
})
3657+
.expect("expected broker-only subject token diagnostic");
3658+
assert!(
3659+
diagnostic
3660+
.message
3661+
.contains("cannot declare workload injection metadata")
3662+
);
3663+
}
3664+
35653665
#[test]
35663666
fn validate_profile_set_rejects_subject_token_on_client_credentials_grant() {
35673667
let profile = parse_profile_yaml(

‎crates/openshell-server/src/grpc/policy.rs‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10047,6 +10047,128 @@ mod tests {
1004710047
);
1004810048
}
1004910049

10050+
#[tokio::test]
10051+
async fn provider_environment_withholds_token_exchange_subject_credential() {
10052+
use openshell_core::proto::{
10053+
GetSandboxProviderEnvironmentRequest, ProviderCredentialTokenGrant,
10054+
ProviderCredentialTokenGrantSubjectToken, ProviderCredentialTokenGrantType,
10055+
ProviderProfile, ProviderProfileCategory, ProviderProfileCredential,
10056+
StoredProviderProfile,
10057+
};
10058+
10059+
let state = test_server_state().await;
10060+
let profile = StoredProviderProfile {
10061+
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {
10062+
id: "profile-token-exchange-subject".to_string(),
10063+
name: "token-exchange-subject".to_string(),
10064+
created_at_ms: 1_000_000,
10065+
labels: HashMap::new(),
10066+
resource_version: 0,
10067+
annotations: HashMap::new(),
10068+
workspace: "default".to_string(),
10069+
deletion_timestamp_ms: 0,
10070+
}),
10071+
profile: Some(ProviderProfile {
10072+
id: "token-exchange-subject".to_string(),
10073+
display_name: "Token Exchange Subject".to_string(),
10074+
category: ProviderProfileCategory::Other as i32,
10075+
credentials: vec![
10076+
ProviderProfileCredential {
10077+
name: "subject_token".to_string(),
10078+
..Default::default()
10079+
},
10080+
ProviderProfileCredential {
10081+
name: "access_token".to_string(),
10082+
auth_style: "bearer".to_string(),
10083+
header_name: "authorization".to_string(),
10084+
token_grant: Some(ProviderCredentialTokenGrant {
10085+
grant_type: ProviderCredentialTokenGrantType::TokenExchange as i32,
10086+
token_endpoint: "https://auth.example.test/token".to_string(),
10087+
audience: "api://exchange".to_string(),
10088+
subject_token: Some(ProviderCredentialTokenGrantSubjectToken {
10089+
source: "provider_credential".to_string(),
10090+
credential: "subject_token".to_string(),
10091+
subject_token_type: "urn:ietf:params:oauth:token-type:access_token"
10092+
.to_string(),
10093+
}),
10094+
..Default::default()
10095+
}),
10096+
..Default::default()
10097+
},
10098+
],
10099+
endpoints: vec![NetworkEndpoint {
10100+
host: "api.exchange.example.test".to_string(),
10101+
port: 443,
10102+
protocol: "rest".to_string(),
10103+
access: "full".to_string(),
10104+
..Default::default()
10105+
}],
10106+
..Default::default()
10107+
}),
10108+
};
10109+
state.store.put_message(&profile).await.unwrap();
10110+
10111+
let mut provider = test_provider("exchange-provider", "token-exchange-subject");
10112+
provider.credentials = HashMap::from([(
10113+
"subject_token".to_string(),
10114+
"raw-gateway-oidc-token".to_string(),
10115+
)]);
10116+
provider.credential_expires_at_ms =
10117+
HashMap::from([("subject_token".to_string(), current_time_ms() + 60_000)]);
10118+
state.store.put_message(&provider).await.unwrap();
10119+
state
10120+
.store
10121+
.put_message(&test_sandbox(
10122+
"sb-token-exchange-subject",
10123+
"token-exchange-subject",
10124+
test_policy_with_rule("sandbox_only", "sandbox.example.com"),
10125+
vec!["exchange-provider".to_string()],
10126+
))
10127+
.await
10128+
.unwrap();
10129+
10130+
let response = handle_get_sandbox_provider_environment(
10131+
&state,
10132+
with_user(Request::new(GetSandboxProviderEnvironmentRequest {
10133+
sandbox_id: "sb-token-exchange-subject".to_string(),
10134+
supports_static_credential_bindings: true,
10135+
})),
10136+
)
10137+
.await
10138+
.unwrap()
10139+
.into_inner();
10140+
10141+
assert!(
10142+
!response.environment.contains_key("subject_token"),
10143+
"token-exchange subject credentials must not enter sandbox environment material"
10144+
);
10145+
assert!(
10146+
!response
10147+
.static_credential_bindings
10148+
.contains_key("subject_token"),
10149+
"token-exchange subject credentials must not own workload placeholders"
10150+
);
10151+
assert!(
10152+
!response
10153+
.credential_expires_at_ms
10154+
.contains_key("subject_token"),
10155+
"withheld subject credentials must not emit sandbox expiry metadata"
10156+
);
10157+
let dynamic_access_token = response
10158+
.dynamic_credentials
10159+
.values()
10160+
.find(|credential| credential.name == "access_token")
10161+
.expect("dynamic access_token credential should remain available");
10162+
assert_eq!(
10163+
dynamic_access_token
10164+
.token_grant
10165+
.as_ref()
10166+
.and_then(|grant| grant.subject_token.as_ref())
10167+
.map(|subject| subject.credential.as_str()),
10168+
Some("subject_token")
10169+
);
10170+
}
10171+
1005010172
#[tokio::test]
1005110173
async fn provider_env_revision_changes_on_consecutive_provider_updates_without_delay() {
1005210174
use openshell_core::proto::GetSandboxProviderEnvironmentRequest;

0 commit comments

Comments
 (0)