Skip to content

Commit c1c07fa

Browse files
committed
fix(auth): complete review findings for OIDC
1. High: UpdateConfig dual-auth for sandbox policy sync (NVIDIA#1): - Add DUAL_AUTH_METHODS list for RPCs that accept either Bearer token (CLI users) or sandbox secret (supervisor) - UpdateConfig now works for both CLI policy mutations and sandbox startup policy sync in OIDC deployments 2. Medium: end-to-end client_id vs audience separation (NVIDIA#2): - Thread optional audience parameter through oidc_browser_auth_flow and oidc_client_credentials_flow - gateway login reads oidc_audience from metadata and passes it to the auth/token requests - Adds audience parameter to authorization URL for providers that require it (Entra ID) 3. Medium: gateway start exposes full OIDC RBAC config (NVIDIA#3): - Add --oidc-client-id, --oidc-roles-claim, --oidc-admin-role, --oidc-user-role flags to gateway start - Thread all values through gateway_admin_deploy into DeployOptions - Stop hardcoding "openshell-cli" as client_id in bootstrap metadata
1 parent 859b713 commit c1c07fa

6 files changed

Lines changed: 88 additions & 11 deletions

File tree

‎crates/openshell-bootstrap/src/lib.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ pub struct DeployOptions {
126126
pub recreate: bool,
127127
/// OIDC issuer URL. When set, the server validates Bearer JWTs.
128128
pub oidc_issuer: Option<String>,
129-
/// OIDC audience (client ID). Defaults to "openshell-cli".
129+
/// OIDC audience for the API resource server. Defaults to "openshell-cli".
130130
pub oidc_audience: String,
131+
/// OIDC client ID for CLI login. Defaults to "openshell-cli".
132+
pub oidc_client_id: String,
131133
/// OIDC roles claim path (e.g. "realm_access.roles").
132134
pub oidc_roles_claim: Option<String>,
133135
/// OIDC admin role name.
@@ -152,6 +154,7 @@ impl DeployOptions {
152154
recreate: false,
153155
oidc_issuer: None,
154156
oidc_audience: "openshell-cli".to_string(),
157+
oidc_client_id: "openshell-cli".to_string(),
155158
oidc_roles_claim: None,
156159
oidc_admin_role: None,
157160
oidc_user_role: None,
@@ -304,6 +307,7 @@ where
304307
let recreate = options.recreate;
305308
let oidc_issuer = options.oidc_issuer;
306309
let oidc_audience = options.oidc_audience;
310+
let oidc_client_id = options.oidc_client_id;
307311
let oidc_roles_claim = options.oidc_roles_claim;
308312
let oidc_admin_role = options.oidc_admin_role;
309313
let oidc_user_role = options.oidc_user_role;
@@ -606,7 +610,7 @@ where
606610
if oidc_issuer.is_some() {
607611
metadata.auth_mode = Some("oidc".to_string());
608612
metadata.oidc_issuer = oidc_issuer.clone();
609-
metadata.oidc_client_id = Some("openshell-cli".to_string());
613+
metadata.oidc_client_id = Some(oidc_client_id.clone());
610614
metadata.oidc_audience = Some(oidc_audience.clone());
611615
}
612616
store_gateway_metadata(&name, &metadata)?;

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,9 +858,25 @@ enum GatewayCommands {
858858
#[arg(long)]
859859
oidc_issuer: Option<String>,
860860

861-
/// OIDC audience (client ID) for JWT validation.
861+
/// OIDC audience for the API resource server.
862862
#[arg(long, default_value = "openshell-cli", requires = "oidc_issuer")]
863863
oidc_audience: String,
864+
865+
/// OIDC client ID stored in gateway metadata for CLI login.
866+
#[arg(long, default_value = "openshell-cli", requires = "oidc_issuer")]
867+
oidc_client_id: String,
868+
869+
/// Dot-separated path to the roles array in the JWT claims.
870+
#[arg(long, requires = "oidc_issuer")]
871+
oidc_roles_claim: Option<String>,
872+
873+
/// Role name that grants admin access.
874+
#[arg(long, requires = "oidc_issuer")]
875+
oidc_admin_role: Option<String>,
876+
877+
/// Role name that grants standard user access.
878+
#[arg(long, requires = "oidc_issuer")]
879+
oidc_user_role: Option<String>,
864880
},
865881

866882
/// Stop the gateway (preserves state).
@@ -1724,6 +1740,10 @@ async fn main() -> Result<()> {
17241740
gpu,
17251741
oidc_issuer,
17261742
oidc_audience,
1743+
oidc_client_id,
1744+
oidc_roles_claim,
1745+
oidc_admin_role,
1746+
oidc_user_role,
17271747
} => {
17281748
let gpu = if gpu {
17291749
vec!["auto".to_string()]
@@ -1744,6 +1764,10 @@ async fn main() -> Result<()> {
17441764
gpu,
17451765
oidc_issuer.as_deref(),
17461766
&oidc_audience,
1767+
&oidc_client_id,
1768+
oidc_roles_claim.as_deref(),
1769+
oidc_admin_role.as_deref(),
1770+
oidc_user_role.as_deref(),
17471771
)
17481772
.await?;
17491773
}

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ fn csprng_fill(buf: &mut [u8]) {
100100
///
101101
/// Opens the user's browser to the Keycloak login page and waits for
102102
/// the authorization code redirect on a localhost callback server.
103-
pub async fn oidc_browser_auth_flow(issuer: &str, client_id: &str) -> Result<OidcTokenBundle> {
103+
pub async fn oidc_browser_auth_flow(
104+
issuer: &str,
105+
client_id: &str,
106+
audience: Option<&str>,
107+
) -> Result<OidcTokenBundle> {
104108
let discovery = discover(issuer).await?;
105109

106110
let code_verifier = generate_code_verifier();
@@ -111,14 +115,19 @@ pub async fn oidc_browser_auth_flow(issuer: &str, client_id: &str) -> Result<Oid
111115
let port = listener.local_addr().into_diagnostic()?.port();
112116
let redirect_uri = format!("http://127.0.0.1:{port}/callback");
113117

114-
let auth_url = format!(
118+
let mut auth_url = format!(
115119
"{}?response_type=code&client_id={}&redirect_uri={}&code_challenge={}&code_challenge_method=S256&state={}&scope=openid",
116120
discovery.authorization_endpoint,
117121
urlencoded(client_id),
118122
urlencoded(&redirect_uri),
119123
urlencoded(&code_challenge),
120124
urlencoded(&state),
121125
);
126+
// Request a specific API audience when configured (needed for providers
127+
// like Entra ID where the API audience differs from the client ID).
128+
if let Some(aud) = audience {
129+
auth_url.push_str(&format!("&audience={}", urlencoded(aud)));
130+
}
122131

123132
let (tx, rx) = oneshot::channel::<String>();
124133
let expected_state = state.clone();
@@ -170,6 +179,7 @@ pub async fn oidc_browser_auth_flow(issuer: &str, client_id: &str) -> Result<Oid
170179
pub async fn oidc_client_credentials_flow(
171180
issuer: &str,
172181
client_id: &str,
182+
audience: Option<&str>,
173183
) -> Result<OidcTokenBundle> {
174184
let client_secret = std::env::var("OPENSHELL_OIDC_CLIENT_SECRET").map_err(|_| {
175185
miette::miette!(
@@ -179,11 +189,14 @@ pub async fn oidc_client_credentials_flow(
179189

180190
let discovery = discover(issuer).await?;
181191

182-
let params = [
192+
let mut params = vec![
183193
("grant_type", "client_credentials"),
184194
("client_id", client_id),
185-
("client_secret", &client_secret),
195+
("client_secret", client_secret.as_str()),
186196
];
197+
if let Some(aud) = audience {
198+
params.push(("audience", aud));
199+
}
187200

188201
let client = reqwest::Client::new();
189202
let resp: TokenResponse = client

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ pub async fn gateway_add(
10731073

10741074
// Check for client_credentials env var (CI mode).
10751075
if std::env::var("OPENSHELL_OIDC_CLIENT_SECRET").is_ok() {
1076-
match crate::oidc_auth::oidc_client_credentials_flow(issuer, oidc_client_id).await {
1076+
match crate::oidc_auth::oidc_client_credentials_flow(issuer, oidc_client_id, oidc_audience).await {
10771077
Ok(bundle) => {
10781078
openshell_bootstrap::oidc_token::store_oidc_token(name, &bundle)?;
10791079
eprintln!(
@@ -1086,7 +1086,7 @@ pub async fn gateway_add(
10861086
}
10871087
}
10881088
} else {
1089-
match crate::oidc_auth::oidc_browser_auth_flow(issuer, oidc_client_id).await {
1089+
match crate::oidc_auth::oidc_browser_auth_flow(issuer, oidc_client_id, oidc_audience).await {
10901090
Ok(bundle) => {
10911091
openshell_bootstrap::oidc_token::store_oidc_token(name, &bundle)?;
10921092
eprintln!("{} Authenticated successfully", "✓".green().bold());
@@ -1239,11 +1239,12 @@ pub async fn gateway_login(name: &str) -> Result<()> {
12391239
miette::miette!("Gateway '{name}' has OIDC auth but no issuer URL in metadata")
12401240
})?;
12411241
let client_id = metadata.oidc_client_id.as_deref().unwrap_or("openshell-cli");
1242+
let audience = metadata.oidc_audience.as_deref();
12421243

12431244
let bundle = if std::env::var("OPENSHELL_OIDC_CLIENT_SECRET").is_ok() {
1244-
crate::oidc_auth::oidc_client_credentials_flow(issuer, client_id).await?
1245+
crate::oidc_auth::oidc_client_credentials_flow(issuer, client_id, audience).await?
12451246
} else {
1246-
crate::oidc_auth::oidc_browser_auth_flow(issuer, client_id).await?
1247+
crate::oidc_auth::oidc_browser_auth_flow(issuer, client_id, audience).await?
12471248
};
12481249

12491250
let username = jwt_preferred_username(&bundle.access_token);
@@ -1564,6 +1565,10 @@ pub async fn gateway_admin_deploy(
15641565
gpu: Vec<String>,
15651566
oidc_issuer: Option<&str>,
15661567
oidc_audience: &str,
1568+
oidc_client_id: &str,
1569+
oidc_roles_claim: Option<&str>,
1570+
oidc_admin_role: Option<&str>,
1571+
oidc_user_role: Option<&str>,
15671572
) -> Result<()> {
15681573
let location = if remote.is_some() { "remote" } else { "local" };
15691574

@@ -1633,6 +1638,16 @@ pub async fn gateway_admin_deploy(
16331638
if let Some(issuer) = oidc_issuer {
16341639
options = options.with_oidc_issuer(issuer);
16351640
options = options.with_oidc_audience(oidc_audience);
1641+
options.oidc_client_id = oidc_client_id.to_string();
1642+
if let Some(claim) = oidc_roles_claim {
1643+
options.oidc_roles_claim = Some(claim.to_string());
1644+
}
1645+
if let Some(role) = oidc_admin_role {
1646+
options.oidc_admin_role = Some(role.to_string());
1647+
}
1648+
if let Some(role) = oidc_user_role {
1649+
options.oidc_user_role = Some(role.to_string());
1650+
}
16361651
}
16371652

16381653
let handle = deploy_gateway_with_panel(options, name, location).await?;

‎crates/openshell-server/src/multiplex.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ where
215215
return inner.ready().await?.call(req).await;
216216
}
217217

218+
// Dual-auth methods (e.g. UpdateConfig) — accept either a
219+
// Bearer token (CLI users) or sandbox secret (supervisor).
220+
if oidc::is_dual_auth_method(&path) {
221+
if oidc::validate_sandbox_secret(req.headers(), &sandbox_secret).is_ok() {
222+
return inner.ready().await?.call(req).await;
223+
}
224+
// Fall through to Bearer token validation below.
225+
}
226+
218227
// Extract Bearer token from the authorization header.
219228
let token = req
220229
.headers()

‎crates/openshell-server/src/oidc.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ const SANDBOX_SECRET_METHODS: &[&str] = &[
4646
"/openshell.sandbox.v1.SandboxService/GetSandboxConfig",
4747
];
4848

49+
/// Methods that accept either OIDC Bearer token (CLI users) or sandbox
50+
/// secret (supervisor). UpdateConfig is called by both CLI (policy/settings
51+
/// mutations) and the sandbox supervisor (policy sync on startup).
52+
const DUAL_AUTH_METHODS: &[&str] = &[
53+
"/openshell.v1.OpenShell/UpdateConfig",
54+
];
55+
56+
/// Returns `true` if the method accepts either Bearer or sandbox-secret auth.
57+
pub fn is_dual_auth_method(path: &str) -> bool {
58+
DUAL_AUTH_METHODS.contains(&path)
59+
}
60+
4961
/// Returns `true` if the method needs no authentication at all.
5062
pub fn is_unauthenticated_method(path: &str) -> bool {
5163
UNAUTHENTICATED_METHODS.contains(&path)

0 commit comments

Comments
 (0)