Skip to content

Commit 859b713

Browse files
committed
fix(auth): address remaining OIDC review findings
1. Critical: sandbox RPCs now require x-sandbox-secret header (NVIDIA#1): - Split skip list into unauthenticated (health/reflection) and sandbox-secret (supervisor RPCs) categories - Sandbox RPCs require x-sandbox-secret header matching the server's SSH handshake secret - Sandbox gRPC client injects the secret via SandboxSecretInterceptor - Unauthenticated network callers can no longer reach sandbox RPCs 2. High: complete client_id vs audience separation (NVIDIA#2): - Add --oidc-audience flag to gateway add (separate from --oidc-client-id) - Store oidc_audience in gateway metadata independently - Thread audience through bootstrap metadata correctly 3. Medium: propagate RBAC settings through bootstrap (NVIDIA#3): - Add oidc_roles_claim, oidc_admin_role, oidc_user_role to DeployOptions - Pass OIDC_ROLES_CLAIM, OIDC_ADMIN_ROLE, OIDC_USER_ROLE env vars through Docker container → cluster-entrypoint.sh → HelmChart manifest - Full provider-neutral RBAC config now works via gateway start 4. Low: cross-platform CSPRNG (NVIDIA#4): - Replace /dev/urandom + RandomState fallback with getrandom crate - Works correctly on all platforms (Linux, macOS, Windows)
1 parent 1a7a458 commit 859b713

13 files changed

Lines changed: 188 additions & 67 deletions

File tree

‎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.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ base64 = "0.22"
7979
sha2 = "0.10"
8080
rand = "0.9"
8181
jsonwebtoken = "9"
82+
getrandom = "0.3"
8283

8384
# Filesystem embedding
8485
include_dir = "0.7"

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ pub async fn ensure_container(
485485
resume: bool,
486486
oidc_issuer: Option<&str>,
487487
oidc_audience: &str,
488+
oidc_roles_claim: Option<&str>,
489+
oidc_admin_role: Option<&str>,
490+
oidc_user_role: Option<&str>,
488491
) -> Result<u16> {
489492
let container_name = container_name(name);
490493

@@ -771,6 +774,15 @@ pub async fn ensure_container(
771774
if let Some(issuer) = oidc_issuer {
772775
env_vars.push(format!("OIDC_ISSUER={issuer}"));
773776
env_vars.push(format!("OIDC_AUDIENCE={oidc_audience}"));
777+
if let Some(claim) = oidc_roles_claim {
778+
env_vars.push(format!("OIDC_ROLES_CLAIM={claim}"));
779+
}
780+
if let Some(role) = oidc_admin_role {
781+
env_vars.push(format!("OIDC_ADMIN_ROLE={role}"));
782+
}
783+
if let Some(role) = oidc_user_role {
784+
env_vars.push(format!("OIDC_USER_ROLE={role}"));
785+
}
774786
}
775787

776788
let env = Some(env_vars);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ pub struct DeployOptions {
128128
pub oidc_issuer: Option<String>,
129129
/// OIDC audience (client ID). Defaults to "openshell-cli".
130130
pub oidc_audience: String,
131+
/// OIDC roles claim path (e.g. "realm_access.roles").
132+
pub oidc_roles_claim: Option<String>,
133+
/// OIDC admin role name.
134+
pub oidc_admin_role: Option<String>,
135+
/// OIDC user role name.
136+
pub oidc_user_role: Option<String>,
131137
}
132138

133139
impl DeployOptions {
@@ -146,6 +152,9 @@ impl DeployOptions {
146152
recreate: false,
147153
oidc_issuer: None,
148154
oidc_audience: "openshell-cli".to_string(),
155+
oidc_roles_claim: None,
156+
oidc_admin_role: None,
157+
oidc_user_role: None,
149158
}
150159
}
151160

@@ -295,6 +304,9 @@ where
295304
let recreate = options.recreate;
296305
let oidc_issuer = options.oidc_issuer;
297306
let oidc_audience = options.oidc_audience;
307+
let oidc_roles_claim = options.oidc_roles_claim;
308+
let oidc_admin_role = options.oidc_admin_role;
309+
let oidc_user_role = options.oidc_user_role;
298310

299311
// Wrap on_log in Arc<Mutex<>> so we can share it with pull_remote_image
300312
// which needs a 'static callback for the bollard streaming pull.
@@ -483,6 +495,9 @@ where
483495
resume,
484496
oidc_issuer.as_deref(),
485497
&oidc_audience,
498+
oidc_roles_claim.as_deref(),
499+
oidc_admin_role.as_deref(),
500+
oidc_user_role.as_deref(),
486501
)
487502
.await?;
488503
let port = actual_port;

‎crates/openshell-cli/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ tar = "0.4"
6262
sha2 = { workspace = true }
6363
base64 = { workspace = true }
6464
hex = "0.4"
65+
getrandom = { workspace = true }
6566

6667
# WebSocket (Cloudflare tunnel proxy)
6768
tokio-tungstenite = { workspace = true }

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,9 +943,15 @@ enum GatewayCommands {
943943
#[arg(long, conflicts_with_all = ["remote", "local"])]
944944
oidc_issuer: Option<String>,
945945

946-
/// OIDC client ID (defaults to "openshell-cli").
946+
/// OIDC client ID for the CLI login flow (defaults to "openshell-cli").
947947
#[arg(long, default_value = "openshell-cli", requires = "oidc_issuer")]
948948
oidc_client_id: String,
949+
950+
/// OIDC audience for the API resource server. When different from
951+
/// the client ID, the CLI requests this audience in the token exchange.
952+
/// Defaults to the client ID value.
953+
#[arg(long, requires = "oidc_issuer")]
954+
oidc_audience: Option<String>,
949955
},
950956

951957
/// Authenticate with an edge-authenticated or OIDC gateway.
@@ -1769,6 +1775,7 @@ async fn main() -> Result<()> {
17691775
local,
17701776
oidc_issuer,
17711777
oidc_client_id,
1778+
oidc_audience,
17721779
} => {
17731780
run::gateway_add(
17741781
&endpoint,
@@ -1778,6 +1785,7 @@ async fn main() -> Result<()> {
17781785
local,
17791786
oidc_issuer.as_deref(),
17801787
&oidc_client_id,
1788+
oidc_audience.as_deref(),
17811789
)
17821790
.await?;
17831791
}

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

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async fn discover(issuer: &str) -> Result<OidcDiscovery> {
7474
/// Generate a random PKCE code verifier (43-128 unreserved chars).
7575
fn generate_code_verifier() -> String {
7676
let mut buf = [0u8; 32];
77-
getrandom(&mut buf);
77+
csprng_fill(&mut buf);
7878
URL_SAFE_NO_PAD.encode(buf)
7979
}
8080

@@ -87,36 +87,13 @@ fn compute_code_challenge(verifier: &str) -> String {
8787
/// Generate a random state parameter.
8888
fn generate_state() -> String {
8989
let mut buf = [0u8; 16];
90-
getrandom(&mut buf);
90+
csprng_fill(&mut buf);
9191
hex::encode(buf)
9292
}
9393

94-
/// Fill a buffer with OS-backed random bytes.
95-
///
96-
/// Uses `/dev/urandom` on Unix and the platform RNG on other systems via
97-
/// `std::collections::hash_map::RandomState` as a fallback.
98-
fn getrandom(buf: &mut [u8]) {
99-
#[cfg(unix)]
100-
{
101-
use std::io::Read;
102-
if let Ok(mut f) = std::fs::File::open("/dev/urandom") {
103-
if f.read_exact(buf).is_ok() {
104-
return;
105-
}
106-
}
107-
}
108-
// Fallback: hash-based entropy (still seeded from OS on modern platforms).
109-
use std::collections::hash_map::RandomState;
110-
use std::hash::{BuildHasher, Hasher};
111-
for chunk in buf.chunks_mut(8) {
112-
let state = RandomState::new();
113-
let mut hasher = state.build_hasher();
114-
hasher.write_usize(chunk.len());
115-
let hash = hasher.finish().to_ne_bytes();
116-
for (dst, src) in chunk.iter_mut().zip(hash.iter()) {
117-
*dst = *src;
118-
}
119-
}
94+
/// Fill a buffer with cryptographically secure random bytes from the OS.
95+
fn csprng_fill(buf: &mut [u8]) {
96+
getrandom::fill(buf).expect("OS RNG failed");
12097
}
12198

12299
/// Run the OIDC Authorization Code + PKCE browser flow.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,7 @@ pub async fn gateway_add(
963963
local: bool,
964964
oidc_issuer: Option<&str>,
965965
oidc_client_id: &str,
966+
oidc_audience: Option<&str>,
966967
) -> Result<()> {
967968
// If the endpoint starts with ssh://, parse it into an SSH destination
968969
// and a gateway endpoint automatically. The host is resolved via
@@ -1054,7 +1055,7 @@ pub async fn gateway_add(
10541055
auth_mode: Some("oidc".to_string()),
10551056
oidc_issuer: Some(issuer.to_string()),
10561057
oidc_client_id: Some(oidc_client_id.to_string()),
1057-
oidc_audience: None,
1058+
oidc_audience: oidc_audience.map(String::from),
10581059
..Default::default()
10591060
};
10601061

@@ -5865,7 +5866,7 @@ mod tests {
58655866
with_tmp_xdg(tmpdir.path(), || {
58665867
let runtime = tokio::runtime::Runtime::new().expect("create runtime");
58675868
runtime.block_on(async {
5868-
gateway_add("http://127.0.0.1:8080", None, None, None, false, None, "openshell-cli")
5869+
gateway_add("http://127.0.0.1:8080", None, None, None, false, None, "openshell-cli", None)
58695870
.await
58705871
.expect("register plaintext gateway");
58715872
});
@@ -5892,6 +5893,7 @@ mod tests {
58925893
true,
58935894
None,
58945895
"openshell-cli",
5896+
None,
58955897
)
58965898
.await
58975899
.expect("register plaintext gateway");

‎crates/openshell-sandbox/src/grpc_client.rs‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use openshell_core::proto::{
1414
SandboxPolicy as ProtoSandboxPolicy, SubmitPolicyAnalysisRequest, UpdateConfigRequest,
1515
inference_client::InferenceClient, open_shell_client::OpenShellClient,
1616
};
17+
use tonic::service::interceptor::InterceptedService;
1718
use tonic::transport::{Certificate, Channel, ClientTlsConfig, Endpoint, Identity};
1819
use tracing::debug;
1920

@@ -74,10 +75,37 @@ async fn connect_channel(endpoint: &str) -> Result<Channel> {
7475
.wrap_err("failed to connect to OpenShell server")
7576
}
7677

77-
/// Connect to the OpenShell server (mTLS or plaintext based on endpoint scheme).
78-
async fn connect(endpoint: &str) -> Result<OpenShellClient<Channel>> {
78+
/// Interceptor that injects the sandbox shared secret into every gRPC request.
79+
///
80+
/// The server validates this header on sandbox-to-server RPCs (GetSandboxConfig,
81+
/// GetSandboxProviderEnvironment, etc.) instead of requiring an OIDC Bearer token.
82+
#[derive(Clone)]
83+
pub(crate) struct SandboxSecretInterceptor {
84+
secret: Option<tonic::metadata::MetadataValue<tonic::metadata::Ascii>>,
85+
}
86+
87+
impl tonic::service::Interceptor for SandboxSecretInterceptor {
88+
fn call(
89+
&mut self,
90+
mut req: tonic::Request<()>,
91+
) -> std::result::Result<tonic::Request<()>, tonic::Status> {
92+
if let Some(ref val) = self.secret {
93+
req.metadata_mut().insert("x-sandbox-secret", val.clone());
94+
}
95+
Ok(req)
96+
}
97+
}
98+
99+
type AuthenticatedClient = OpenShellClient<InterceptedService<Channel, SandboxSecretInterceptor>>;
100+
101+
/// Connect to the OpenShell server with sandbox secret authentication.
102+
async fn connect(endpoint: &str) -> Result<AuthenticatedClient> {
79103
let channel = connect_channel(endpoint).await?;
80-
Ok(OpenShellClient::new(channel))
104+
let secret = std::env::var("OPENSHELL_SSH_HANDSHAKE_SECRET")
105+
.ok()
106+
.and_then(|s| s.parse().ok());
107+
let interceptor = SandboxSecretInterceptor { secret };
108+
Ok(OpenShellClient::with_interceptor(channel, interceptor))
81109
}
82110

83111
/// Fetch sandbox policy from OpenShell server via gRPC.
@@ -97,7 +125,7 @@ pub async fn fetch_policy(endpoint: &str, sandbox_id: &str) -> Result<Option<Pro
97125

98126
/// Fetch sandbox policy using an existing client connection.
99127
async fn fetch_policy_with_client(
100-
client: &mut OpenShellClient<Channel>,
128+
client: &mut AuthenticatedClient,
101129
sandbox_id: &str,
102130
) -> Result<Option<ProtoSandboxPolicy>> {
103131
let response = client
@@ -121,7 +149,7 @@ async fn fetch_policy_with_client(
121149

122150
/// Sync a locally-discovered policy using an existing client connection.
123151
async fn sync_policy_with_client(
124-
client: &mut OpenShellClient<Channel>,
152+
client: &mut AuthenticatedClient,
125153
sandbox: &str,
126154
policy: &ProtoSandboxPolicy,
127155
) -> Result<()> {
@@ -210,7 +238,7 @@ pub async fn fetch_provider_environment(
210238
/// and status reporting, avoiding per-request TLS handshake overhead.
211239
#[derive(Clone)]
212240
pub struct CachedOpenShellClient {
213-
client: OpenShellClient<Channel>,
241+
client: AuthenticatedClient,
214242
}
215243

216244
/// Settings poll result returned by [`CachedOpenShellClient::poll_settings`].
@@ -229,13 +257,12 @@ pub struct SettingsPollResult {
229257
impl CachedOpenShellClient {
230258
pub async fn connect(endpoint: &str) -> Result<Self> {
231259
debug!(endpoint = %endpoint, "Connecting openshell gRPC client for policy polling");
232-
let channel = connect_channel(endpoint).await?;
233-
let client = OpenShellClient::new(channel);
260+
let client = connect(endpoint).await?;
234261
Ok(Self { client })
235262
}
236263

237264
/// Get a clone of the underlying tonic client for direct RPC calls.
238-
pub fn raw_client(&self) -> OpenShellClient<Channel> {
265+
pub fn raw_client(&self) -> AuthenticatedClient {
239266
self.client.clone()
240267
}
241268

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ impl MultiplexService {
6565
GrpcRouter::new(openshell, inference),
6666
self.state.oidc_cache.clone(),
6767
authz_policy,
68+
self.state.config.ssh_handshake_secret.clone(),
6869
);
6970
let http_service = http_router(self.state.clone());
7071

@@ -145,18 +146,22 @@ pub struct AuthGrpcRouter<S> {
145146
inner: S,
146147
oidc_cache: Option<Arc<oidc::JwksCache>>,
147148
authz_policy: Option<AuthzPolicy>,
149+
/// SSH handshake secret used to validate sandbox-to-server RPCs.
150+
sandbox_secret: String,
148151
}
149152

150153
impl<S> AuthGrpcRouter<S> {
151154
fn new(
152155
inner: S,
153156
oidc_cache: Option<Arc<oidc::JwksCache>>,
154157
authz_policy: Option<AuthzPolicy>,
158+
sandbox_secret: String,
155159
) -> Self {
156160
Self {
157161
inner,
158162
oidc_cache,
159163
authz_policy,
164+
sandbox_secret,
160165
}
161166
}
162167
}
@@ -182,6 +187,7 @@ where
182187
fn call(&mut self, req: Request<B>) -> Self::Future {
183188
let oidc_cache = self.oidc_cache.clone();
184189
let authz_policy = self.authz_policy.clone();
190+
let sandbox_secret = self.sandbox_secret.clone();
185191
let mut inner = self.inner.clone();
186192

187193
Box::pin(async move {
@@ -192,8 +198,20 @@ where
192198

193199
let path = req.uri().path().to_string();
194200

195-
// Skip OIDC validation for health and sandbox-to-server RPCs.
196-
if oidc::is_skip_method(&path) {
201+
// Health probes and reflection — truly unauthenticated.
202+
if oidc::is_unauthenticated_method(&path) {
203+
return inner.ready().await?.call(req).await;
204+
}
205+
206+
// Sandbox-to-server RPCs — authenticated via shared secret,
207+
// not OIDC Bearer tokens.
208+
if oidc::is_sandbox_secret_method(&path) {
209+
if let Err(status) = oidc::validate_sandbox_secret(req.headers(), &sandbox_secret) {
210+
let response = status.into_http();
211+
let (parts, body) = response.into_parts();
212+
let body = tonic::body::BoxBody::new(body);
213+
return Ok(Response::from_parts(parts, body));
214+
}
197215
return inner.ready().await?.call(req).await;
198216
}
199217

0 commit comments

Comments
 (0)