|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Shared supervisor middleware identifiers and policy validation contracts. |
| 5 | +
|
| 6 | +use miette::{Result, miette}; |
| 7 | + |
| 8 | +/// Binding identifier for the built-in secret redaction middleware. |
| 9 | +pub const BUILTIN_SECRETS: &str = "openshell/secrets"; |
| 10 | + |
| 11 | +/// Validate policy-owned configuration for a built-in middleware. |
| 12 | +pub fn validate_builtin_config(implementation: &str, config: &prost_types::Struct) -> Result<()> { |
| 13 | + match implementation { |
| 14 | + BUILTIN_SECRETS => validate_secrets_config(config), |
| 15 | + other => Err(miette!( |
| 16 | + "middleware implementation '{other}' is not available in phase 1" |
| 17 | + )), |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +fn validate_secrets_config(config: &prost_types::Struct) -> Result<()> { |
| 22 | + let mode = config |
| 23 | + .fields |
| 24 | + .get("secrets") |
| 25 | + .and_then(|value| match value.kind.as_ref() { |
| 26 | + Some(prost_types::value::Kind::StringValue(value)) => Some(value.as_str()), |
| 27 | + _ => None, |
| 28 | + }) |
| 29 | + .unwrap_or("redact"); |
| 30 | + if mode != "redact" { |
| 31 | + return Err(miette!( |
| 32 | + "{BUILTIN_SECRETS} only supports config.secrets: redact in phase 1" |
| 33 | + )); |
| 34 | + } |
| 35 | + Ok(()) |
| 36 | +} |
| 37 | + |
| 38 | +#[cfg(test)] |
| 39 | +mod tests { |
| 40 | + use super::*; |
| 41 | + |
| 42 | + #[test] |
| 43 | + fn secrets_config_defaults_to_redact() { |
| 44 | + validate_builtin_config(BUILTIN_SECRETS, &prost_types::Struct::default()).unwrap(); |
| 45 | + } |
| 46 | + |
| 47 | + #[test] |
| 48 | + fn secrets_config_rejects_unsupported_mode() { |
| 49 | + let config = prost_types::Struct { |
| 50 | + fields: std::iter::once(( |
| 51 | + "secrets".to_string(), |
| 52 | + prost_types::Value { |
| 53 | + kind: Some(prost_types::value::Kind::StringValue("allow".into())), |
| 54 | + }, |
| 55 | + )) |
| 56 | + .collect(), |
| 57 | + }; |
| 58 | + |
| 59 | + let error = validate_builtin_config(BUILTIN_SECRETS, &config).unwrap_err(); |
| 60 | + assert!( |
| 61 | + error |
| 62 | + .to_string() |
| 63 | + .contains("only supports config.secrets: redact") |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn rejects_unknown_builtin() { |
| 69 | + let error = validate_builtin_config("openshell/unknown", &prost_types::Struct::default()) |
| 70 | + .unwrap_err(); |
| 71 | + assert!( |
| 72 | + error |
| 73 | + .to_string() |
| 74 | + .contains("implementation 'openshell/unknown' is not available") |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
0 commit comments