@@ -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 ;
1718use tonic:: transport:: { Certificate , Channel , ClientTlsConfig , Endpoint , Identity } ;
1819use 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.
99127async 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.
123151async 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 ) ]
212240pub 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 {
229257impl 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
0 commit comments