|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Strong identity for one sandbox runtime launch. |
| 5 | +
|
| 6 | +use std::{fmt, str::FromStr}; |
| 7 | + |
| 8 | +use serde::{Deserialize, Deserializer, Serialize, Serializer, de}; |
| 9 | +use thiserror::Error; |
| 10 | +use uuid::Uuid; |
| 11 | + |
| 12 | +/// Identifies one create or start-from-stopped sandbox launch. |
| 13 | +/// |
| 14 | +/// Retries of the same durable launch reuse this value. A later launch gets a |
| 15 | +/// new value even when the compute platform reuses the same sandbox resource. |
| 16 | +#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 17 | +pub struct SandboxSessionId(Uuid); |
| 18 | + |
| 19 | +impl SandboxSessionId { |
| 20 | + /// Generate a fresh launch identity. |
| 21 | + #[must_use] |
| 22 | + pub fn new() -> Self { |
| 23 | + Self(Uuid::new_v4()) |
| 24 | + } |
| 25 | + |
| 26 | + /// Return the underlying UUID. |
| 27 | + #[must_use] |
| 28 | + pub const fn as_uuid(&self) -> &Uuid { |
| 29 | + &self.0 |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl Default for SandboxSessionId { |
| 34 | + fn default() -> Self { |
| 35 | + Self::new() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl fmt::Display for SandboxSessionId { |
| 40 | + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 41 | + self.0.hyphenated().fmt(formatter) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl fmt::Debug for SandboxSessionId { |
| 46 | + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 47 | + formatter |
| 48 | + .debug_tuple("SandboxSessionId") |
| 49 | + .field(&self.to_string()) |
| 50 | + .finish() |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl FromStr for SandboxSessionId { |
| 55 | + type Err = SandboxSessionIdError; |
| 56 | + |
| 57 | + fn from_str(value: &str) -> Result<Self, Self::Err> { |
| 58 | + let parsed = Uuid::parse_str(value).map_err(|_| SandboxSessionIdError)?; |
| 59 | + if parsed.is_nil() || parsed.hyphenated().to_string() != value { |
| 60 | + return Err(SandboxSessionIdError); |
| 61 | + } |
| 62 | + Ok(Self(parsed)) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl Serialize for SandboxSessionId { |
| 67 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 68 | + where |
| 69 | + S: Serializer, |
| 70 | + { |
| 71 | + serializer.collect_str(self) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl<'de> Deserialize<'de> for SandboxSessionId { |
| 76 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 77 | + where |
| 78 | + D: Deserializer<'de>, |
| 79 | + { |
| 80 | + let value = String::deserialize(deserializer)?; |
| 81 | + value.parse().map_err(|_| { |
| 82 | + de::Error::invalid_value( |
| 83 | + de::Unexpected::Str(&value), |
| 84 | + &"a non-nil canonical lowercase UUID", |
| 85 | + ) |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// A sandbox session ID was not a non-nil canonical lowercase UUID. |
| 91 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)] |
| 92 | +#[error("sandbox session ID must be a non-nil canonical lowercase UUID")] |
| 93 | +pub struct SandboxSessionIdError; |
| 94 | + |
| 95 | +#[cfg(test)] |
| 96 | +mod tests { |
| 97 | + use crate::sandbox_session::SandboxSessionId; |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn round_trips_canonical_id() { |
| 101 | + let session = SandboxSessionId::new(); |
| 102 | + let encoded = serde_json::to_string(&session).expect("serialize session ID"); |
| 103 | + let decoded: SandboxSessionId = |
| 104 | + serde_json::from_str(&encoded).expect("deserialize session ID"); |
| 105 | + assert_eq!(decoded, session); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn rejects_nil_and_noncanonical_ids() { |
| 110 | + for invalid in [ |
| 111 | + "00000000-0000-0000-0000-000000000000", |
| 112 | + "550E8400-E29B-41D4-A716-446655440000", |
| 113 | + "550e8400e29b41d4a716446655440000", |
| 114 | + "not-a-uuid", |
| 115 | + ] { |
| 116 | + assert!(invalid.parse::<SandboxSessionId>().is_err(), "{invalid}"); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments