Skip to content

Commit f14e960

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

69 files changed

Lines changed: 16356 additions & 4708 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openshell-cli/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,4 @@ serde_json = { workspace = true }
102102
temp-env = "0.3"
103103
tokio-stream = { workspace = true }
104104
url = { workspace = true }
105+
wiremock = "0.6"

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

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,13 @@ enum Commands {
594594
command: Option<ProfileCommands>,
595595
},
596596

597+
/// Manage delegated identity credential records.
598+
#[command(help_template = SUBCOMMAND_HELP_TEMPLATE)]
599+
DelegatedCredential {
600+
#[command(subcommand)]
601+
command: Option<DelegatedCredentialCommands>,
602+
},
603+
597604
/// Manage workspaces.
598605
#[command(alias = "ws", after_help = WORKSPACE_EXAMPLES, help_template = SUBCOMMAND_HELP_TEMPLATE)]
599606
Workspace {
@@ -1280,6 +1287,62 @@ impl ProfileCommands {
12801287
}
12811288
}
12821289

1290+
#[derive(Subcommand, Debug)]
1291+
enum DelegatedCredentialCommands {
1292+
/// List delegated identity credentials.
1293+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1294+
List {
1295+
/// Maximum number of credentials to return in this page.
1296+
#[arg(long, default_value_t = 100)]
1297+
page_size: i32,
1298+
1299+
/// Opaque continuation token from a previous page.
1300+
#[arg(long, default_value = "")]
1301+
page_token: String,
1302+
1303+
/// Output only credential IDs.
1304+
#[arg(long, conflicts_with = "output")]
1305+
ids: bool,
1306+
1307+
/// Output format.
1308+
#[arg(short = 'o', long = "output", value_enum, default_value_t = OutputFormat::Table, conflicts_with = "ids")]
1309+
output: OutputFormat,
1310+
},
1311+
1312+
/// Show delegated identity credential status.
1313+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1314+
Status {
1315+
/// Delegated identity credential ID.
1316+
id: String,
1317+
1318+
/// Output format.
1319+
#[arg(short = 'o', long = "output", value_enum, default_value_t = OutputFormat::Table)]
1320+
output: OutputFormat,
1321+
},
1322+
1323+
/// Revoke a delegated identity credential.
1324+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1325+
Revoke {
1326+
/// Delegated identity credential ID.
1327+
id: String,
1328+
1329+
/// Expected resource version for compare-and-swap updates.
1330+
#[arg(long = "resource-version", default_value_t = 0)]
1331+
resource_version: u64,
1332+
},
1333+
1334+
/// Delete a delegated identity credential record.
1335+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1336+
Delete {
1337+
/// Delegated identity credential ID.
1338+
id: String,
1339+
1340+
/// Expected resource version for compare-and-swap deletes.
1341+
#[arg(long = "resource-version", default_value_t = 0)]
1342+
resource_version: u64,
1343+
},
1344+
}
1345+
12831346
// -----------------------------------------------------------------------
12841347
// Gateway commands (replaces the old `cluster` / `cluster admin` groups)
12851348
// -----------------------------------------------------------------------
@@ -1520,6 +1583,12 @@ enum SandboxCommands {
15201583
#[arg(long = "provider")]
15211584
providers: Vec<String>,
15221585

1586+
/// Allow the sandbox to use a separately authorized copy of your identity for a bounded duration.
1587+
/// Opens a browser authorization flow when no usable gateway-owned grant exists. Accepts
1588+
/// positive durations with m, h, or d suffixes, for example 30m, 8h, or 7d.
1589+
#[arg(long = "use-my-identity-for", value_name = "DURATION")]
1590+
delegate_identity_for: Option<String>,
1591+
15231592
/// Path to a custom sandbox policy YAML file.
15241593
/// Overrides the built-in default and the `OPENSHELL_SANDBOX_POLICY` env var.
15251594
#[arg(long, value_hint = ValueHint::FilePath)]
@@ -1828,6 +1897,41 @@ enum SandboxCommands {
18281897
/// Manage reusable sandbox workload templates.
18291898
#[command(subcommand)]
18301899
Template(SandboxTemplateCommands),
1900+
1901+
/// Manage sandbox delegated identity.
1902+
#[command(subcommand, name = "delegated-identity")]
1903+
DelegatedIdentity(SandboxDelegatedIdentityCommands),
1904+
}
1905+
1906+
#[derive(Subcommand, Debug)]
1907+
enum SandboxDelegatedIdentityCommands {
1908+
/// Show delegated identity status for a sandbox.
1909+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1910+
Status {
1911+
/// Sandbox name.
1912+
#[arg(add = ArgValueCompleter::new(completers::complete_sandbox_names))]
1913+
name: String,
1914+
},
1915+
1916+
/// Withdraw delegated identity from a sandbox.
1917+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1918+
Withdraw {
1919+
/// Sandbox name.
1920+
#[arg(add = ArgValueCompleter::new(completers::complete_sandbox_names))]
1921+
name: String,
1922+
},
1923+
1924+
/// Set delegated identity expiry to now plus the requested duration.
1925+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1926+
Extend {
1927+
/// Sandbox name.
1928+
#[arg(add = ArgValueCompleter::new(completers::complete_sandbox_names))]
1929+
name: String,
1930+
1931+
/// New authorization window, for example 24h.
1932+
#[arg(long = "for", value_name = "DURATION")]
1933+
duration: String,
1934+
},
18311935
}
18321936

18331937
/// Common observation flags; the deadline starts after the mutation is saved.
@@ -3350,6 +3454,7 @@ async fn run_async() -> Result<()> {
33503454
memory,
33513455
driver_config_json,
33523456
providers,
3457+
delegate_identity_for,
33533458
policy,
33543459
forward,
33553460
expose,
@@ -3440,6 +3545,7 @@ async fn run_async() -> Result<()> {
34403545
driver_config_json: driver_config_json.as_deref(),
34413546
editor,
34423547
providers: &providers,
3548+
delegate_identity_for: delegate_identity_for.as_deref(),
34433549
policy: policy.as_deref(),
34443550
forward,
34453551
expose,
@@ -3790,6 +3896,37 @@ async fn run_async() -> Result<()> {
37903896
.await?;
37913897
}
37923898
},
3899+
SandboxCommands::DelegatedIdentity(command) => match command {
3900+
SandboxDelegatedIdentityCommands::Status { name } => {
3901+
run::sandbox_delegated_identity_status(
3902+
endpoint,
3903+
&name,
3904+
&cli.workspace,
3905+
&tls,
3906+
)
3907+
.await?;
3908+
}
3909+
SandboxDelegatedIdentityCommands::Withdraw { name } => {
3910+
run::sandbox_delegated_identity_withdraw(
3911+
endpoint,
3912+
&name,
3913+
&cli.workspace,
3914+
&tls,
3915+
)
3916+
.await?;
3917+
}
3918+
SandboxDelegatedIdentityCommands::Extend { name, duration } => {
3919+
run::sandbox_delegated_identity_extend(
3920+
endpoint,
3921+
&ctx.name,
3922+
&name,
3923+
&duration,
3924+
&cli.workspace,
3925+
&tls,
3926+
)
3927+
.await?;
3928+
}
3929+
},
37933930
}
37943931
}
37953932
}
@@ -4048,6 +4185,61 @@ async fn run_async() -> Result<()> {
40484185
apply_auth(&mut tls, &ctx.name)?;
40494186
command.run(endpoint, &cli.workspace, &tls).await?;
40504187
}
4188+
Some(Commands::DelegatedCredential {
4189+
command: Some(command),
4190+
}) => {
4191+
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
4192+
let endpoint = &ctx.endpoint;
4193+
let mut tls = tls.with_gateway_name(&ctx.name);
4194+
apply_auth(&mut tls, &ctx.name)?;
4195+
4196+
match command {
4197+
DelegatedCredentialCommands::List {
4198+
page_size,
4199+
page_token,
4200+
ids,
4201+
output,
4202+
} => {
4203+
run::delegated_identity_credential_list(
4204+
endpoint,
4205+
page_size,
4206+
&page_token,
4207+
ids,
4208+
output.as_str(),
4209+
&tls,
4210+
)
4211+
.await?;
4212+
}
4213+
DelegatedCredentialCommands::Status { id, output } => {
4214+
run::delegated_identity_credential_status(endpoint, &id, output.as_str(), &tls)
4215+
.await?;
4216+
}
4217+
DelegatedCredentialCommands::Revoke {
4218+
id,
4219+
resource_version,
4220+
} => {
4221+
run::delegated_identity_credential_revoke(
4222+
endpoint,
4223+
&id,
4224+
resource_version,
4225+
&tls,
4226+
)
4227+
.await?;
4228+
}
4229+
DelegatedCredentialCommands::Delete {
4230+
id,
4231+
resource_version,
4232+
} => {
4233+
run::delegated_identity_credential_delete(
4234+
endpoint,
4235+
&id,
4236+
resource_version,
4237+
&tls,
4238+
)
4239+
.await?;
4240+
}
4241+
}
4242+
}
40514243
Some(Commands::Term { theme }) => {
40524244
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
40534245
let mut tls = tls.with_gateway_name(&ctx.name);
@@ -4187,6 +4379,13 @@ async fn run_async() -> Result<()> {
41874379
.print_help()
41884380
.expect("Failed to print help");
41894381
}
4382+
Some(Commands::DelegatedCredential { command: None }) => {
4383+
Cli::command()
4384+
.find_subcommand_mut("delegated-credential")
4385+
.expect("delegated-credential subcommand exists")
4386+
.print_help()
4387+
.expect("Failed to print help");
4388+
}
41904389
Some(Commands::Gateway { command: None }) => {
41914390
Cli::command()
41924391
.find_subcommand_mut("gateway")
@@ -5143,6 +5342,76 @@ mod tests {
51435342
}) if ids == ["custom-api", "custom-alt"]));
51445343
}
51455344

5345+
#[test]
5346+
fn delegated_credential_commands_parse() {
5347+
let list = Cli::try_parse_from([
5348+
"openshell",
5349+
"delegated-credential",
5350+
"list",
5351+
"--page-size",
5352+
"25",
5353+
"--page-token",
5354+
"next-credential-page",
5355+
"--ids",
5356+
])
5357+
.expect("delegated credential list should parse");
5358+
match list.command {
5359+
Some(Commands::DelegatedCredential {
5360+
command:
5361+
Some(DelegatedCredentialCommands::List {
5362+
page_size,
5363+
page_token,
5364+
ids,
5365+
output,
5366+
}),
5367+
}) => {
5368+
assert_eq!(page_size, 25);
5369+
assert_eq!(page_token, "next-credential-page");
5370+
assert!(ids);
5371+
assert_eq!(output, OutputFormat::Table);
5372+
}
5373+
command => panic!("unexpected delegated credential list command: {command:?}"),
5374+
}
5375+
5376+
let status = Cli::try_parse_from([
5377+
"openshell",
5378+
"delegated-credential",
5379+
"status",
5380+
"delegated-identity-123",
5381+
"-o",
5382+
"json",
5383+
])
5384+
.expect("delegated credential status should parse");
5385+
assert!(matches!(
5386+
status.command,
5387+
Some(Commands::DelegatedCredential {
5388+
command: Some(DelegatedCredentialCommands::Status {
5389+
id,
5390+
output: OutputFormat::Json,
5391+
})
5392+
}) if id == "delegated-identity-123"
5393+
));
5394+
5395+
let revoke = Cli::try_parse_from([
5396+
"openshell",
5397+
"delegated-credential",
5398+
"revoke",
5399+
"delegated-identity-123",
5400+
"--resource-version",
5401+
"7",
5402+
])
5403+
.expect("delegated credential revoke should parse");
5404+
assert!(matches!(
5405+
revoke.command,
5406+
Some(Commands::DelegatedCredential {
5407+
command: Some(DelegatedCredentialCommands::Revoke {
5408+
id,
5409+
resource_version: 7,
5410+
})
5411+
}) if id == "delegated-identity-123"
5412+
));
5413+
}
5414+
51465415
#[test]
51475416
fn profile_import_and_lint_require_exactly_one_source() {
51485417
for verb in ["import", "lint"] {
@@ -6267,6 +6536,31 @@ mod tests {
62676536
}
62686537
}
62696538

6539+
#[test]
6540+
fn sandbox_create_use_my_identity_for_parses() {
6541+
let cli = Cli::try_parse_from([
6542+
"openshell",
6543+
"sandbox",
6544+
"create",
6545+
"--use-my-identity-for",
6546+
"8h",
6547+
])
6548+
.expect("sandbox create --use-my-identity-for should parse");
6549+
match cli.command {
6550+
Some(Commands::Sandbox {
6551+
command:
6552+
Some(SandboxCommands::Create {
6553+
delegate_identity_for,
6554+
..
6555+
}),
6556+
..
6557+
}) => {
6558+
assert_eq!(delegate_identity_for.as_deref(), Some("8h"));
6559+
}
6560+
other => panic!("expected SandboxCommands::Create, got: {other:?}"),
6561+
}
6562+
}
6563+
62706564
/// `--approval-mode <bogus>` is rejected by clap's value parser, so the
62716565
/// CLI can't smuggle through a future-mode value that the gateway
62726566
/// doesn't yet know about.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub async fn oidc_browser_auth_flow(
239239
() = tokio::time::sleep(AUTH_TIMEOUT) => {
240240
return Err(miette::miette!(
241241
"OIDC authentication timed out after {} seconds.\n\
242-
Try again with: openshell gateway login",
242+
Please retry the command.",
243243
AUTH_TIMEOUT.as_secs()
244244
));
245245
}

0 commit comments

Comments
 (0)