Skip to content

Commit 2191f10

Browse files
committed
feat(gateway): verify supervisor workload assertions for provider grants
Authorize a dedicated supervisor's provider grant from its signed session and two JWT-SVIDs, then perform the OAuth exchange in the gateway. - The gateway derives the expected stable and launch SPIFFE IDs from the session and the persisted launch. It verifies both signatures against the Workload API trust bundle and requires the exact subject, a single audience, and an unexpired token for each. - The session, the launch, and the provider attachment are rechecked before each OAuth exchange and before a cached or new token is returned. A stopped or superseded launch is denied on cache hits too. - The token cache key includes the launch, the attachment epoch, the provider revision, and the resolved exchange subject. Token lifetime is capped by the session, both assertions, and the subject expiry. - Requests, cache misses, and OAuth exchanges have separate per-sandbox limits, and the gateway-wide cap scales with them, so warm grants are served while other grants wait on OAuth. - A token service timeout is shared with queued requests whatever status its response headers carried. - The stored token-exchange subject stays in the gateway, and the direct subject-exchange RPC is closed while this mode is configured. Signed-off-by: Shiju <shiju@nvidia.com>
1 parent 6c824e7 commit 2191f10

5 files changed

Lines changed: 1600 additions & 230 deletions

File tree

‎crates/openshell-credential-issuer/src/embedded/test_support.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ impl CredentialDriver for TestStaticCredentialDriver {
146146
responses.push(ResolvedCredential {
147147
request_id: request.request_id,
148148
value,
149-
expiration_time: None,
149+
expiration_time: handle.metadata.get("test_expiration_ms").map(|expiry| {
150+
let expiry = expiry.parse().expect("test expiry in milliseconds");
151+
openshell_core::time::timestamp_from_millis(expiry)
152+
.expect("test expiry timestamp")
153+
}),
150154
});
151155
}
152156

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

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,7 +2800,7 @@ use crate::auth::workspace_authz::{
28002800
};
28012801
use openshell_core::oauth::{
28022802
self, TokenExchangeParams, TokenGrantClient, effective_client_assertion_type,
2803-
effective_token_type,
2803+
effective_jwt_svid_audience, effective_token_type,
28042804
};
28052805
use openshell_core::token_grant_failure::{
28062806
TokenGrantFailure, TokenGrantFailureKind, TokenGrantStage, WORKLOAD_IDENTITY_CALL_TIMEOUT,
@@ -4828,6 +4828,11 @@ async fn exchange_provider_subject_token_inner(
48284828
)
48294829
.to_status_with_code(code));
48304830
}
4831+
if state.compute.provider_workload_identity().is_some() {
4832+
return Err(token_grant_configuration_status(
4833+
tonic::Code::FailedPrecondition,
4834+
));
4835+
}
48314836
drop(request);
48324837

48334838
if req.provider.trim().is_empty()
@@ -4919,7 +4924,7 @@ async fn exchange_provider_subject_token_inner(
49194924
&provider,
49204925
&subject_token.credential,
49214926
)?;
4922-
let stored_subject_token =
4927+
let (stored_subject_token, _) =
49234928
resolve_subject_token_credential(&state.credentials, &provider, &subject_token.credential)
49244929
.await?;
49254930

@@ -5012,29 +5017,30 @@ async fn resolve_subject_token_credential(
50125017
credentials: &crate::credentials::CredentialRuntime,
50135018
provider: &Provider,
50145019
credential_key: &str,
5015-
) -> Result<String, Status> {
5020+
) -> Result<(String, Option<i64>), Status> {
50165021
// Check the provider's expiry before asking a credential driver to resolve
50175022
// a handle; stale stored material cannot authorize another grant attempt.
5018-
ensure_subject_token_credential_not_expired(provider, credential_key)?;
5023+
let provider_expires_at_ms =
5024+
ensure_subject_token_credential_not_expired(provider, credential_key)?;
50195025
if let Some(value) = provider
50205026
.credentials
50215027
.get(credential_key)
50225028
.filter(|value| !value.is_empty())
50235029
{
5024-
return Ok(value.clone());
5030+
return Ok((value.clone(), provider_expires_at_ms));
50255031
}
50265032

50275033
if provider.credential_handles.contains_key(credential_key) {
5028-
let resolved = credentials
5034+
let mut resolved = credentials
50295035
.resolve_provider_handles(provider, crate::persistence::current_time_ms())
50305036
.await
50315037
.map_err(token_grant_dependency_status)?;
50325038
if let Some(value) = resolved
50335039
.values
5034-
.get(credential_key)
5040+
.remove(credential_key)
50355041
.filter(|value| !value.is_empty())
50365042
{
5037-
return Ok(value.clone());
5043+
return Ok((value, resolved.expires_at_ms.get(credential_key).copied()));
50385044
}
50395045
}
50405046

@@ -5044,12 +5050,12 @@ async fn resolve_subject_token_credential(
50445050
fn ensure_subject_token_credential_not_expired(
50455051
provider: &Provider,
50465052
credential_key: &str,
5047-
) -> Result<(), Status> {
5053+
) -> Result<Option<i64>, Status> {
50485054
let expires_at_ms = provider_credential_expiration_ms(provider, credential_key)?;
50495055
if expires_at_ms.is_some_and(|value| value <= crate::persistence::current_time_ms()) {
50505056
return Err(token_grant_authorization_required_status());
50515057
}
5052-
Ok(())
5058+
Ok(expires_at_ms)
50535059
}
50545060

50555061
/// The user's stored subject credential is absent or expired. Only the user
@@ -5289,24 +5295,6 @@ async fn perform_intermediate_token_exchange(
52895295
})
52905296
}
52915297

5292-
fn effective_jwt_svid_audience(token_endpoint: &str, jwt_svid_audience: &str) -> String {
5293-
if !jwt_svid_audience.trim().is_empty() {
5294-
return jwt_svid_audience.to_string();
5295-
}
5296-
derive_issuer_from_token_endpoint(token_endpoint)
5297-
}
5298-
5299-
fn derive_issuer_from_token_endpoint(token_endpoint: &str) -> String {
5300-
if let Some(realms_idx) = token_endpoint.find("/realms/") {
5301-
let after_realms = &token_endpoint[realms_idx + "/realms/".len()..];
5302-
if let Some(slash_idx) = after_realms.find('/') {
5303-
let realm_end = realms_idx + "/realms/".len() + slash_idx;
5304-
return token_endpoint[..realm_end].to_string();
5305-
}
5306-
}
5307-
token_endpoint.to_string()
5308-
}
5309-
53105298
pub(super) async fn handle_get_provider_refresh_status(
53115299
state: &Arc<ServerState>,
53125300
request: Request<GetProviderRefreshStatusRequest>,
@@ -6433,8 +6421,11 @@ pub(super) mod tests {
64336421
let error = ensure_subject_token_credential_not_expired(&provider, "SUBJECT_TOKEN")
64346422
.expect_err("the Unix epoch is a present, expired timestamp");
64356423
assert_eq!(error.code(), Code::FailedPrecondition);
6436-
ensure_subject_token_credential_not_expired(&provider, "UNSET_TOKEN")
6437-
.expect("an absent expiration remains non-expiring");
6424+
assert_eq!(
6425+
ensure_subject_token_credential_not_expired(&provider, "UNSET_TOKEN")
6426+
.expect("an absent expiration remains non-expiring"),
6427+
None
6428+
);
64386429
}
64396430

64406431
mod token_grant_failures {
@@ -12191,13 +12182,21 @@ pub(super) mod tests {
1219112182
.await
1219212183
.unwrap();
1219312184
provider.credential_handles = handles;
12185+
let expires_at_ms = crate::persistence::current_time_ms() + 60_000;
12186+
provider
12187+
.credential_handles
12188+
.get_mut("subject_token")
12189+
.unwrap()
12190+
.metadata
12191+
.insert("test_expiration_ms".into(), expires_at_ms.to_string());
1219412192

12195-
let subject_token =
12193+
let (subject_token, subject_expires_at_ms) =
1219612194
resolve_subject_token_credential(&credentials, &provider, "subject_token")
1219712195
.await
1219812196
.unwrap();
1219912197

1220012198
assert_eq!(subject_token, "user-token");
12199+
assert_eq!(subject_expires_at_ms, Some(expires_at_ms));
1220112200
}
1220212201

1220312202
#[tokio::test]

0 commit comments

Comments
 (0)