Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions crates/rocm-core/src/codex_bridge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright © Advanced Micro Devices, Inc., or its affiliates.
//
// SPDX-License-Identifier: MIT

//! The `rocmd-codex-bridge-v0` host snapshot: the serde DTOs describing a
//! host — examine summary, GPU snapshot, config, automation state, engines,
//! and managed services — at one point in time.
//!
//! Plain data only. `rocm` and `rocmd` each build and emit their own snapshot
//! of this shape (`rocmd`'s automation watchers also use the GPU part on its
//! own); sharing the types here keeps the two from drifting apart.

use serde::{Deserialize, Serialize};

use crate::{
AutomationEventRecord, AutomationRuntimeState, ExamineSummary, ManagedServiceRecord,
RocmCliConfig,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodexBridgeSnapshot {
pub protocol: String,
pub generated_at_unix_ms: u128,
pub examine: ExamineSummary,
pub gpu: CodexBridgeGpuSnapshot,
pub config: RocmCliConfig,
#[serde(default)]
pub automation_runtime: Option<AutomationRuntimeState>,
#[serde(default)]
pub recent_automation_events: Vec<AutomationEventRecord>,
#[serde(default)]
pub engines: Vec<CodexBridgeEngine>,
#[serde(default)]
pub services: Vec<ManagedServiceRecord>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodexBridgeGpuSnapshot {
pub amd_smi_available: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub static_snapshot: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub monitor_snapshot: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodexBridgeEngine {
pub id: String,
pub summary: String,
pub default_for_platform: bool,
pub installed_binary: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub binary_path: Option<String>,
}
92 changes: 92 additions & 0 deletions crates/rocm-core/src/endpoint_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright © Advanced Micro Devices, Inc., or its affiliates.
//
// SPDX-License-Identifier: MIT

//! The bearer key that guards a managed service's endpoint.
//!
//! The endpoint-API-key generator and its header-safety check. Drawn from a
//! CSPRNG — deliberately never derived from [`crate::service_identity`]'s
//! timestamp-based, guessable [`crate::generate_service_id`] — the two meet
//! only at the filesystem layer, where a service's endpoint key is stored in a
//! sidecar named after its [`crate::ServiceId`].

/// Number of random alphanumeric characters in a generated endpoint API key.
/// 48 chars from a 62-symbol alphabet is ~285 bits of entropy — far beyond what
/// a bearer token guarding a network endpoint needs, with no padding characters
/// that would complicate copy/paste into client configs or shell env vars.
const ENDPOINT_API_KEY_LEN: usize = 48;

/// Generate a fresh, cryptographically-random API key for a public endpoint.
///
/// The value is URL-safe alphanumeric (`[A-Za-z0-9]`) so it can be dropped
/// verbatim into an `Authorization: Bearer` header, a client config file, or an
/// environment variable without escaping.
///
/// Drawn from `rand::rng()`, a CSPRNG seeded from the operating system;
/// deliberately *not* derived from `generate_service_id` (a timestamp-based,
/// guessable identifier — unsuitable as a secret).
pub fn generate_endpoint_api_key() -> String {
use rand::Rng;
use rand::distr::Alphanumeric;

rand::rng()
.sample_iter(&Alphanumeric)
.take(ENDPOINT_API_KEY_LEN)
.map(char::from)
.collect()
}

/// Return `true` if `key` contains a character that must never appear in an API
/// key used verbatim in an `Authorization: Bearer` header.
///
/// The CLI and engine adapters build those header lines by raw string
/// interpolation (`Authorization: Bearer {key}\r\n`), so an embedded CR or LF in
/// the key would inject additional header lines (HTTP header injection). A
/// control character has no legitimate place in a bearer token, so we reject the
/// whole class rather than only CR/LF. Callers apply this at input validation
/// (rejecting a supplied key) and defensively when reading the key file.
pub fn endpoint_api_key_has_forbidden_chars(key: &str) -> bool {
key.chars().any(char::is_control)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn generate_endpoint_api_key_is_random_and_alphanumeric() {
let key = generate_endpoint_api_key();
assert_eq!(key.len(), ENDPOINT_API_KEY_LEN);
// The message deliberately omits the key: it is a credential, and a
// failing assertion's message ends up in CI logs.
assert!(
key.chars().all(|c| c.is_ascii_alphanumeric()),
"key must be URL-safe alphanumeric"
);
// Two draws must differ — a constant key would be catastrophic for auth.
assert_ne!(generate_endpoint_api_key(), generate_endpoint_api_key());
// A freshly generated key must itself pass the header-safety predicate.
assert!(!endpoint_api_key_has_forbidden_chars(
&generate_endpoint_api_key()
));
}

#[test]
fn endpoint_api_key_has_forbidden_chars_flags_control_chars() {
// Control characters (notably CR/LF) enable header injection when the key
// is interpolated into a raw `Authorization: Bearer` line.
for bad in ["key\r\ninject", "key\nother", "tab\there", "nul\0byte"] {
assert!(
endpoint_api_key_has_forbidden_chars(bad),
"should reject {bad:?}"
);
}
// Ordinary printable keys are accepted.
for good in ["my-key", "AbC123._~-", "sk-proj-abcDEF0123456789"] {
assert!(
!endpoint_api_key_has_forbidden_chars(good),
"should accept {good:?}"
);
}
}
}
Loading
Loading