Skip to content

Commit d9e88fb

Browse files
committed
feat(isolation): authenticate the Sandbox Protocol
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent 397dcc3 commit d9e88fb

6 files changed

Lines changed: 351 additions & 381 deletions

File tree

‎crates/openshell-core/src/jwt.rs‎

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ mod session {
161161
pub struct SecretJwt(Zeroizing<String>);
162162

163163
impl SecretJwt {
164-
fn new(value: String) -> Result<Self, SessionJwtError> {
164+
pub fn parse(value: impl Into<String>) -> Result<Self, SessionJwtError> {
165+
let value = value.into();
165166
if value.is_empty() || value.chars().any(char::is_whitespace) {
166167
return Err(SessionJwtError::InvalidTokenEncoding);
167168
}
@@ -194,6 +195,93 @@ mod session {
194195
pub credential_epoch: CredentialEpoch,
195196
}
196197

198+
/// Refreshable Sandbox Protocol bearer credential shared by all streams on
199+
/// the supervisor's current HTTP/2 connection.
200+
#[derive(Clone)]
201+
pub struct SessionBearerTokenSlot {
202+
inner: Arc<std::sync::RwLock<Option<StoredBearer>>>,
203+
}
204+
205+
#[derive(Clone)]
206+
struct StoredBearer {
207+
token: SecretJwt,
208+
expires_at: i64,
209+
}
210+
211+
impl SessionBearerTokenSlot {
212+
#[must_use]
213+
pub fn empty() -> Self {
214+
Self {
215+
inner: Arc::new(std::sync::RwLock::new(None)),
216+
}
217+
}
218+
219+
pub fn new(token: SecretJwt, expires_at: i64) -> Result<Self, SessionJwtError> {
220+
let slot = Self::empty();
221+
slot.update(token, expires_at)?;
222+
Ok(slot)
223+
}
224+
225+
pub fn update(&self, token: SecretJwt, expires_at: i64) -> Result<(), SessionJwtError> {
226+
if expires_at <= 0 {
227+
return Err(SessionJwtError::InvalidLifetime);
228+
}
229+
*self
230+
.inner
231+
.write()
232+
.unwrap_or_else(std::sync::PoisonError::into_inner) =
233+
Some(StoredBearer { token, expires_at });
234+
Ok(())
235+
}
236+
237+
pub fn clear(&self) {
238+
*self
239+
.inner
240+
.write()
241+
.unwrap_or_else(std::sync::PoisonError::into_inner) = None;
242+
}
243+
244+
#[must_use]
245+
pub fn expires_at(&self) -> Option<i64> {
246+
self.inner
247+
.read()
248+
.unwrap_or_else(std::sync::PoisonError::into_inner)
249+
.as_ref()
250+
.map(|stored| stored.expires_at)
251+
}
252+
253+
pub fn authorization_metadata(
254+
&self,
255+
) -> Result<tonic::metadata::AsciiMetadataValue, SessionJwtError> {
256+
let stored = self
257+
.inner
258+
.read()
259+
.unwrap_or_else(std::sync::PoisonError::into_inner);
260+
let stored = stored.as_ref().ok_or(SessionJwtError::TokenUnavailable)?;
261+
if stored.expires_at <= SystemJwtClock.now_unix_seconds() {
262+
return Err(SessionJwtError::Expired);
263+
}
264+
format!("Bearer {}", stored.token.expose_secret())
265+
.parse()
266+
.map_err(|_| SessionJwtError::InvalidTokenEncoding)
267+
}
268+
}
269+
270+
impl Default for SessionBearerTokenSlot {
271+
fn default() -> Self {
272+
Self::empty()
273+
}
274+
}
275+
276+
impl fmt::Debug for SessionBearerTokenSlot {
277+
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
278+
formatter
279+
.debug_struct("SessionBearerTokenSlot")
280+
.field("expires_at", &self.expires_at())
281+
.finish_non_exhaustive()
282+
}
283+
}
284+
197285
pub trait JwtClock: Send + Sync {
198286
fn now_unix_seconds(&self) -> i64;
199287
}
@@ -305,7 +393,7 @@ mod session {
305393
let token = encode(&header, &claims, &self.encoding_key)
306394
.map_err(|_| SessionJwtError::SigningFailed)?;
307395
Ok(MintedSessionToken {
308-
token: SecretJwt::new(token)?,
396+
token: SecretJwt::parse(token)?,
309397
expires_at,
310398
token_id,
311399
})
@@ -471,6 +559,8 @@ mod session {
471559
SigningFailed,
472560
#[error("session token encoding is invalid")]
473561
InvalidTokenEncoding,
562+
#[error("session token is unavailable")]
563+
TokenUnavailable,
474564
#[error("session token is invalid")]
475565
InvalidToken,
476566
#[error("session token algorithm must be EdDSA")]

‎crates/openshell-isolation-interface/src/contract.rs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ pub struct SandboxConfirmEvidence {
545545
pub tcp_allow_round_trip: bool,
546546
pub tcp_deny_round_trip: bool,
547547
pub authenticated_supervisor: bool,
548-
pub session_epoch: String,
548+
pub session_id: SandboxSessionId,
549549
pub driver_fence: DriverFenceEvidence,
550550
/// The driver-owned containment primitive terminates the workload when its
551551
/// Sandbox Runtime exits.
@@ -581,8 +581,7 @@ impl SandboxConfirmEvidence {
581581
&& self.tcp_deny_round_trip
582582
&& self.authenticated_supervisor
583583
&& self.runtime_exit_terminates_workload
584-
&& !self.generation.is_empty()
585-
&& !self.session_epoch.is_empty();
584+
&& !self.generation.is_empty();
586585
if complete {
587586
Ok(())
588587
} else {

‎crates/openshell-isolation-interface/tests/backend_conformance.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ fn confirmation_evidence() -> SandboxConfirmEvidence {
399399
tcp_allow_round_trip: true,
400400
tcp_deny_round_trip: true,
401401
authenticated_supervisor: true,
402-
session_epoch: "epoch-1".to_string(),
402+
session_id: SandboxSessionId::new(),
403403
driver_fence: DriverFenceEvidence::Vm {
404404
generation: "generation-1".to_string(),
405405
network_device_count: 0,

0 commit comments

Comments
 (0)