Skip to content

Commit c82320f

Browse files
committed
feat(sandbox): add delegated identity for token exchange
Signed-off-by: Gordon Sim <gsim@redhat.com>
1 parent 69a05eb commit c82320f

36 files changed

Lines changed: 9261 additions & 3456 deletions

File tree

‎architecture/sandbox.md‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,23 @@ Provider profiles can also declare dynamic token grants. For matching HTTP
338338
endpoints, the supervisor obtains or exchanges OAuth2 access tokens, caches
339339
them, and injects them before forwarding the request. `client_credentials`
340340
grants use the supervisor SPIFFE JWT-SVID directly as the client assertion.
341-
`token_exchange` grants ask the gateway to broker an intermediate token using a
342-
stored provider subject credential and the gateway's own SPIFFE JWT-SVID; the
343-
supervisor then exchanges that intermediate token for the final upstream token
344-
using its own JWT-SVID. The gateway validates that its own JWT-SVID has the
341+
`token_exchange` grants ask the gateway to broker an intermediate token using
342+
either a stored provider subject credential or the sandbox creator's delegated
343+
OIDC identity, plus the gateway's own SPIFFE JWT-SVID; the supervisor then
344+
exchanges that intermediate token for the final upstream token using its own
345+
JWT-SVID. Delegated identity is opt-in at sandbox creation, stores one
346+
gateway-scoped credential per issuer/client/user subject, and stores only a
347+
per-sandbox authorization window on the sandbox. Only the delegating user can
348+
extend or withdraw that window; workspace admins and platform admins can still
349+
delete the sandbox to recover workspace resources. The gateway rejects exchange
350+
after expiry, withdrawal, missing credential state, or credential revocation.
351+
The gateway validates that its own JWT-SVID has the
345352
requested audience, a SPIFFE subject, and a non-expired `exp` claim when
346-
present. It also validates that the stored subject credential is declared by the
347-
provider profile, and that the supervisor JWT-SVID is a well-formed
353+
present. For provider-credential subject tokens, it also validates that the
354+
stored subject credential is declared by the provider profile. For delegated
355+
identity subject tokens, it validates that the sandbox was created with active
356+
delegation for the stored credential principal. The gateway also verifies that
357+
the supervisor JWT-SVID is a well-formed
348358
three-segment JWT with a SPIFFE subject in the same trust domain as the gateway
349359
SVID. The gateway verifies the supervisor JWT-SVID signature with JWT bundles
350360
fetched from its SPIFFE Workload API. Token grant endpoints are HTTPS-only

‎crates/openshell-cli/src/main.rs‎

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,13 @@ enum Commands {
564564
command: Option<ProviderCommands>,
565565
},
566566

567+
/// Manage delegated identity credential records.
568+
#[command(help_template = SUBCOMMAND_HELP_TEMPLATE)]
569+
DelegatedCredential {
570+
#[command(subcommand)]
571+
command: Option<DelegatedCredentialCommands>,
572+
},
573+
567574
/// Manage workspaces.
568575
#[command(alias = "ws", after_help = WORKSPACE_EXAMPLES, help_template = SUBCOMMAND_HELP_TEMPLATE)]
569576
Workspace {
@@ -1102,6 +1109,62 @@ enum ProviderProfileCommands {
11021109
},
11031110
}
11041111

1112+
#[derive(Subcommand, Debug)]
1113+
enum DelegatedCredentialCommands {
1114+
/// List delegated identity credentials.
1115+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1116+
List {
1117+
/// Maximum number of credentials to return.
1118+
#[arg(long, default_value_t = 100)]
1119+
limit: u32,
1120+
1121+
/// Number of credentials to skip.
1122+
#[arg(long, default_value_t = 0)]
1123+
offset: u32,
1124+
1125+
/// Output only credential IDs.
1126+
#[arg(long, conflicts_with = "output")]
1127+
ids: bool,
1128+
1129+
/// Output format.
1130+
#[arg(short = 'o', long = "output", value_enum, default_value_t = OutputFormat::Table, conflicts_with = "ids")]
1131+
output: OutputFormat,
1132+
},
1133+
1134+
/// Show delegated identity credential status.
1135+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1136+
Status {
1137+
/// Delegated identity credential ID.
1138+
id: String,
1139+
1140+
/// Output format.
1141+
#[arg(short = 'o', long = "output", value_enum, default_value_t = OutputFormat::Table)]
1142+
output: OutputFormat,
1143+
},
1144+
1145+
/// Revoke a delegated identity credential.
1146+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1147+
Revoke {
1148+
/// Delegated identity credential ID.
1149+
id: String,
1150+
1151+
/// Expected resource version for compare-and-swap updates.
1152+
#[arg(long = "resource-version", default_value_t = 0)]
1153+
resource_version: u64,
1154+
},
1155+
1156+
/// Delete a delegated identity credential record.
1157+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1158+
Delete {
1159+
/// Delegated identity credential ID.
1160+
id: String,
1161+
1162+
/// Expected resource version for compare-and-swap deletes.
1163+
#[arg(long = "resource-version", default_value_t = 0)]
1164+
resource_version: u64,
1165+
},
1166+
}
1167+
11051168
// -----------------------------------------------------------------------
11061169
// Gateway commands (replaces the old `cluster` / `cluster admin` groups)
11071170
// -----------------------------------------------------------------------
@@ -1413,6 +1476,11 @@ enum SandboxCommands {
14131476
#[arg(long = "provider")]
14141477
providers: Vec<String>,
14151478

1479+
/// Delegate the current OIDC identity to the sandbox for a bounded duration.
1480+
/// Accepts positive durations with m, h, or d suffixes, for example 30m, 8h, or 7d.
1481+
#[arg(long = "delegate-identity-for", value_name = "DURATION")]
1482+
delegate_identity_for: Option<String>,
1483+
14161484
/// Path to a custom sandbox policy YAML file.
14171485
/// Overrides the built-in default and the `OPENSHELL_SANDBOX_POLICY` env var.
14181486
#[arg(long, value_hint = ValueHint::FilePath)]
@@ -1672,6 +1740,41 @@ enum SandboxCommands {
16721740
/// Manage providers attached to a sandbox.
16731741
#[command(subcommand)]
16741742
Provider(SandboxProviderCommands),
1743+
1744+
/// Manage sandbox delegated identity.
1745+
#[command(subcommand, name = "delegated-identity")]
1746+
DelegatedIdentity(SandboxDelegatedIdentityCommands),
1747+
}
1748+
1749+
#[derive(Subcommand, Debug)]
1750+
enum SandboxDelegatedIdentityCommands {
1751+
/// Show delegated identity status for a sandbox.
1752+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1753+
Status {
1754+
/// Sandbox name.
1755+
#[arg(add = ArgValueCompleter::new(completers::complete_sandbox_names))]
1756+
name: String,
1757+
},
1758+
1759+
/// Withdraw delegated identity from a sandbox.
1760+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1761+
Withdraw {
1762+
/// Sandbox name.
1763+
#[arg(add = ArgValueCompleter::new(completers::complete_sandbox_names))]
1764+
name: String,
1765+
},
1766+
1767+
/// Set delegated identity expiry to now plus the requested duration.
1768+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1769+
Extend {
1770+
/// Sandbox name.
1771+
#[arg(add = ArgValueCompleter::new(completers::complete_sandbox_names))]
1772+
name: String,
1773+
1774+
/// New authorization window, for example 24h.
1775+
#[arg(long = "for", value_name = "DURATION")]
1776+
duration: String,
1777+
},
16751778
}
16761779

16771780
#[derive(Subcommand, Debug)]
@@ -2994,6 +3097,7 @@ async fn run_async() -> Result<()> {
29943097
memory,
29953098
driver_config_json,
29963099
providers,
3100+
delegate_identity_for,
29973101
policy,
29983102
forward,
29993103
tty,
@@ -3085,6 +3189,7 @@ async fn run_async() -> Result<()> {
30853189
driver_config_json: driver_config_json.as_deref(),
30863190
editor,
30873191
providers: &providers,
3192+
delegate_identity_for: delegate_identity_for.as_deref(),
30883193
policy: policy.as_deref(),
30893194
forward,
30903195
command: &command,
@@ -3304,6 +3409,37 @@ async fn run_async() -> Result<()> {
33043409
.await?;
33053410
}
33063411
},
3412+
SandboxCommands::DelegatedIdentity(command) => match command {
3413+
SandboxDelegatedIdentityCommands::Status { name } => {
3414+
run::sandbox_delegated_identity_status(
3415+
endpoint,
3416+
&name,
3417+
&cli.workspace,
3418+
&tls,
3419+
)
3420+
.await?;
3421+
}
3422+
SandboxDelegatedIdentityCommands::Withdraw { name } => {
3423+
run::sandbox_delegated_identity_withdraw(
3424+
endpoint,
3425+
&name,
3426+
&cli.workspace,
3427+
&tls,
3428+
)
3429+
.await?;
3430+
}
3431+
SandboxDelegatedIdentityCommands::Extend { name, duration } => {
3432+
run::sandbox_delegated_identity_extend(
3433+
endpoint,
3434+
&ctx.name,
3435+
&name,
3436+
&duration,
3437+
&cli.workspace,
3438+
&tls,
3439+
)
3440+
.await?;
3441+
}
3442+
},
33073443
}
33083444
}
33093445
}
@@ -3588,6 +3724,61 @@ async fn run_async() -> Result<()> {
35883724
}
35893725
}
35903726
}
3727+
Some(Commands::DelegatedCredential {
3728+
command: Some(command),
3729+
}) => {
3730+
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
3731+
let endpoint = &ctx.endpoint;
3732+
let mut tls = tls.with_gateway_name(&ctx.name);
3733+
apply_auth(&mut tls, &ctx.name);
3734+
3735+
match command {
3736+
DelegatedCredentialCommands::List {
3737+
limit,
3738+
offset,
3739+
ids,
3740+
output,
3741+
} => {
3742+
run::delegated_identity_credential_list(
3743+
endpoint,
3744+
limit,
3745+
offset,
3746+
ids,
3747+
output.as_str(),
3748+
&tls,
3749+
)
3750+
.await?;
3751+
}
3752+
DelegatedCredentialCommands::Status { id, output } => {
3753+
run::delegated_identity_credential_status(endpoint, &id, output.as_str(), &tls)
3754+
.await?;
3755+
}
3756+
DelegatedCredentialCommands::Revoke {
3757+
id,
3758+
resource_version,
3759+
} => {
3760+
run::delegated_identity_credential_revoke(
3761+
endpoint,
3762+
&id,
3763+
resource_version,
3764+
&tls,
3765+
)
3766+
.await?;
3767+
}
3768+
DelegatedCredentialCommands::Delete {
3769+
id,
3770+
resource_version,
3771+
} => {
3772+
run::delegated_identity_credential_delete(
3773+
endpoint,
3774+
&id,
3775+
resource_version,
3776+
&tls,
3777+
)
3778+
.await?;
3779+
}
3780+
}
3781+
}
35913782
Some(Commands::Term { theme }) => {
35923783
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
35933784
let mut tls = tls.with_gateway_name(&ctx.name);
@@ -3719,6 +3910,13 @@ async fn run_async() -> Result<()> {
37193910
.print_help()
37203911
.expect("Failed to print help");
37213912
}
3913+
Some(Commands::DelegatedCredential { command: None }) => {
3914+
Cli::command()
3915+
.find_subcommand_mut("delegated-credential")
3916+
.expect("delegated-credential subcommand exists")
3917+
.print_help()
3918+
.expect("Failed to print help");
3919+
}
37223920
Some(Commands::Gateway { command: None }) => {
37233921
Cli::command()
37243922
.find_subcommand_mut("gateway")
@@ -4439,6 +4637,60 @@ mod tests {
44394637
));
44404638
}
44414639

4640+
#[test]
4641+
fn delegated_credential_commands_parse() {
4642+
let list = Cli::try_parse_from(["openshell", "delegated-credential", "list", "--ids"])
4643+
.expect("delegated credential list should parse");
4644+
assert!(matches!(
4645+
list.command,
4646+
Some(Commands::DelegatedCredential {
4647+
command: Some(DelegatedCredentialCommands::List {
4648+
ids: true,
4649+
output: OutputFormat::Table,
4650+
..
4651+
})
4652+
})
4653+
));
4654+
4655+
let status = Cli::try_parse_from([
4656+
"openshell",
4657+
"delegated-credential",
4658+
"status",
4659+
"delegated-identity-123",
4660+
"-o",
4661+
"json",
4662+
])
4663+
.expect("delegated credential status should parse");
4664+
assert!(matches!(
4665+
status.command,
4666+
Some(Commands::DelegatedCredential {
4667+
command: Some(DelegatedCredentialCommands::Status {
4668+
id,
4669+
output: OutputFormat::Json,
4670+
})
4671+
}) if id == "delegated-identity-123"
4672+
));
4673+
4674+
let revoke = Cli::try_parse_from([
4675+
"openshell",
4676+
"delegated-credential",
4677+
"revoke",
4678+
"delegated-identity-123",
4679+
"--resource-version",
4680+
"7",
4681+
])
4682+
.expect("delegated credential revoke should parse");
4683+
assert!(matches!(
4684+
revoke.command,
4685+
Some(Commands::DelegatedCredential {
4686+
command: Some(DelegatedCredentialCommands::Revoke {
4687+
id,
4688+
resource_version: 7,
4689+
})
4690+
}) if id == "delegated-identity-123"
4691+
));
4692+
}
4693+
44424694
#[test]
44434695
fn provider_profile_commands_parse() {
44444696
let export = Cli::try_parse_from([

0 commit comments

Comments
 (0)