greentic-aw-runtime 1.2.2

Enterprise Agentic Worker runtime — Plan-Act-Observe loop, Redis state, tool dispatch via greentic-ext-runtime
Documentation
//! Category-parameterised `secrets://` URIs for the credentials an agent tool
//! resolves at run time.
//!
//! The shape is greentic-designer-admin's `SecretScope::uri`:
//! `secrets://default/<tenant>/<team|_>/<category>/<name>`. The env is pinned
//! to `default`, the team and name are emitted VERBATIM (never canonicalised),
//! and an absent team is written `_`.
//!
//! This began as `mcp_secrets`' private builder with the category hard-coded.
//! A2A (category `a2a`, cross-repo contract 2026-09-21 §5) needs the identical
//! candidate order, so the category is a parameter here. `mcp_secrets`
//! delegates, so there is no second copy that could start resolving a
//! different URI with nothing failing.

use greentic_secrets_lib::SecretsManager;

/// Env segment every category is sealed under. Admin pins these secrets to
/// `default` regardless of the tenant's flow env. Reading at the flow env
/// (`prod`/`local`) would silently miss admin's write.
pub const ENV_SEGMENT: &str = "default";

/// Team segment for a secret that is not scoped to a team.
pub const TENANT_DEFAULT_TEAM: &str = "_";

/// Build `secrets://default/<tenant>/<team|_>/<category>/<key>`.
///
/// An empty category or key is rejected as a caller error rather than
/// producing a URI with an empty segment.
pub fn secret_uri(
    category: &str,
    tenant: &str,
    team: Option<&str>,
    key: &str,
) -> Result<String, String> {
    if category.trim().is_empty() {
        return Err("secret category must not be empty".to_string());
    }
    if key.trim().is_empty() {
        return Err("secret key must not be empty".to_string());
    }
    // Every segment is looked up verbatim by the secrets backend; a `..`,
    // `/`, `?`, `#` or `%2e%2e` could otherwise step into another tenant's
    // path. Pack-controlled values (MCP server id / auth team, SoR name)
    // reach this function.
    for (label, value) in [
        ("category", category),
        ("tenant", tenant),
        ("team", team.unwrap_or(TENANT_DEFAULT_TEAM)),
        ("key", key),
    ] {
        if !is_safe_segment(value) {
            return Err(format!(
                "secret {label} is not a safe secret-path segment; refusing to build a secrets uri"
            ));
        }
    }
    Ok(format!(
        "secrets://{ENV_SEGMENT}/{tenant}/{}/{category}/{key}",
        team.unwrap_or(TENANT_DEFAULT_TEAM)
    ))
}

/// Whether `value` is safe to use as ONE segment of a `secrets://` URI:
/// only `[A-Za-z0-9._-]`, non-empty, no leading dot and no `..`. This excludes
/// `/`, backslash, `?`, `#`, `%` (so `%2e%2e`) and control characters.
pub fn is_safe_segment(value: &str) -> bool {
    let charset_ok = value
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'));
    !value.is_empty() && charset_ok && !value.starts_with('.') && !value.contains("..")
}

/// Every URI [`read_secret_for_unit`] tries, in order:
///
/// 1. the UNIT scope, `…/<team|_>/<category>/<key>.unit-<segment>`, when a unit
///    is known. The name comes from [`crate::mcp_secrets::mcp_unit_secret_key`],
///    whose shape is category-neutral and which the designer calls by that name
///    when it stages a value;
/// 2. the caller's team scope;
/// 3. the tenant-default `_` scope.
///
/// Steps 2 and 3 mirror greentic-designer-admin's `AdminSecretResolver`
/// precedence. A caller with no team, or one naming `_`, yields a single
/// tenant-default URI for them.
///
/// REVIEW BY 2026-12-18: when a unit is known, candidates 2 and 3 are a
/// COMPATIBILITY fallback for deployments staged before the unit scope
/// existed; see `mcp_secrets` for the full note.
pub fn secret_uri_candidates(
    category: &str,
    tenant: &str,
    team: Option<&str>,
    unit: Option<&str>,
    key: &str,
) -> Result<Vec<String>, String> {
    let team = team.filter(|t| !t.is_empty() && *t != TENANT_DEFAULT_TEAM);
    let mut uris = Vec::with_capacity(3);
    let unit_key = unit.and_then(|unit| crate::mcp_secrets::mcp_unit_secret_key(key, unit));
    match unit_key {
        Some(unit_key) => {
            // A key that cannot form a safe unit name (e.g. one ending in `.`)
            // only loses the unit scope; team / tenant candidates still apply.
            match secret_uri(category, tenant, team, &unit_key) {
                Ok(uri) => uris.push(uri),
                Err(reason) => note_unit_scope_skipped(category, unit, &reason),
            }
        }
        None if unit.is_some() => {
            note_unit_scope_skipped(category, unit, "no safe unit-scoped secret name");
        }
        None => {}
    }
    if let Some(team) = team {
        uris.push(secret_uri(category, tenant, Some(team), key)?);
    }
    uris.push(secret_uri(category, tenant, None, key)?);
    Ok(uris)
}

/// A known unit's own secret scope was skipped, so the lookup silently falls
/// back to the broader team / tenant scope. That can serve another unit's
/// credential, which an operator must be able to see. Warns once per process
/// (the lookup runs per call), then stays at debug.
fn note_unit_scope_skipped(category: &str, unit: Option<&str>, reason: &str) {
    use std::sync::atomic::{AtomicBool, Ordering};
    static WARNED: AtomicBool = AtomicBool::new(false);
    if WARNED.swap(true, Ordering::Relaxed) {
        tracing::debug!(category, unit = ?unit, reason, "unit-scoped secret candidate skipped; using the team / tenant scope");
    } else {
        tracing::warn!(
            category,
            unit = ?unit,
            reason,
            "unit-scoped secret candidate skipped; falling back to the team / tenant scope (logged once; later skips at debug)"
        );
    }
}

/// No credential was readable at any scope [`read_secret_for_unit`] tried.
///
/// Carries the URIs because an operator cannot tell a missing secret from a
/// backend that cannot resolve `secrets://` URIs at all without them.
#[derive(Debug, Clone)]
pub struct SecretMiss {
    pub uris: Vec<String>,
    pub error: String,
}

impl std::fmt::Display for SecretMiss {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "no credential at {} ({})",
            self.uris.join(" or "),
            self.error
        )
    }
}

/// Read a secret for one deployed unit: the first URI of
/// [`secret_uri_candidates`] that resolves wins.
pub async fn read_secret_for_unit(
    secrets: &dyn SecretsManager,
    category: &str,
    tenant: &str,
    team: Option<&str>,
    unit: Option<&str>,
    key: &str,
) -> Result<Vec<u8>, SecretMiss> {
    let uris =
        secret_uri_candidates(category, tenant, team, unit, key).map_err(|error| SecretMiss {
            uris: Vec::new(),
            error,
        })?;
    let mut last_error = String::from("no scope tried");
    for uri in &uris {
        match secrets.read(uri).await {
            Ok(bytes) => return Ok(bytes),
            Err(e) => last_error = e.to_string(),
        }
    }
    Err(SecretMiss {
        uris,
        error: last_error,
    })
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use std::collections::HashMap;
    use std::sync::{Arc, Mutex};

    /// A secrets manager holding exactly the URIs it was given, recording every
    /// URI it was asked for.
    struct MapSecrets {
        entries: HashMap<String, Vec<u8>>,
        seen: Mutex<Vec<String>>,
    }

    impl MapSecrets {
        fn with(pairs: &[(&str, &str)]) -> Arc<Self> {
            Arc::new(Self {
                entries: pairs
                    .iter()
                    .map(|(k, v)| ((*k).to_string(), v.as_bytes().to_vec()))
                    .collect(),
                seen: Mutex::new(Vec::new()),
            })
        }
    }

    #[async_trait]
    impl SecretsManager for MapSecrets {
        async fn read(&self, path: &str) -> greentic_secrets_lib::Result<Vec<u8>> {
            self.seen.lock().unwrap().push(path.to_string());
            self.entries
                .get(path)
                .cloned()
                .ok_or_else(|| greentic_secrets_lib::SecretError::NotFound(path.to_string()))
        }
        async fn write(&self, _: &str, _: &[u8]) -> greentic_secrets_lib::Result<()> {
            Ok(())
        }
        async fn delete(&self, _: &str) -> greentic_secrets_lib::Result<()> {
            Ok(())
        }
    }

    #[test]
    fn the_category_is_the_fourth_segment_and_everything_else_is_verbatim() {
        // A hyphenated UUID is the A2A agent id; canonicalising it would read a
        // URI nothing writes (contract §5, §6).
        assert_eq!(
            secret_uri("a2a", "acme", None, "8d2f0c1e-7b1a-4c3d").unwrap(),
            "secrets://default/acme/_/a2a/8d2f0c1e-7b1a-4c3d"
        );
        assert_eq!(
            secret_uri("a2a", "acme", Some("Sales"), "K").unwrap(),
            "secrets://default/acme/Sales/a2a/K"
        );
    }

    #[test]
    fn an_empty_category_or_key_is_a_caller_error() {
        assert!(secret_uri("", "acme", None, "k").is_err());
        assert!(secret_uri("a2a", "acme", None, "  ").is_err());
    }

    #[test]
    fn unsafe_segments_are_refused_in_every_position() {
        for bad in [
            "..", "a/b", "a?b", "a#b", "%2e%2e", "a%2fb", "a\\b", ".hidden", "a\nb",
        ] {
            assert!(secret_uri("mcp", "acme", None, bad).is_err(), "key {bad}");
            assert!(
                secret_uri("mcp", "acme", Some(bad), "k").is_err(),
                "team {bad}"
            );
            assert!(secret_uri("mcp", bad, None, "k").is_err(), "tenant {bad}");
            assert!(
                secret_uri(bad, "acme", None, "k").is_err(),
                "category {bad}"
            );
            assert!(
                secret_uri_candidates("mcp", "acme", Some(bad), None, "k").is_err(),
                "candidates team {bad}"
            );
        }
    }

    #[test]
    fn a_key_ending_in_a_dot_loses_only_the_unit_candidate() {
        // `key.` + `.unit-x` contains `..`, so the unit name is unsafe.
        let uris = secret_uri_candidates("mcp", "acme", Some("sales"), Some("u1"), "key.")
            .expect("team and tenant candidates survive");
        assert_eq!(
            uris,
            vec![
                "secrets://default/acme/sales/mcp/key.".to_string(),
                "secrets://default/acme/_/mcp/key.".to_string(),
            ]
        );
    }

    #[test]
    fn hostile_segments_are_refused() {
        for bad in ["..", "../x", "a/b", "%2e%2e", "a?b", "a#b", ".x"] {
            assert!(secret_uri("llm", "acme", None, bad).is_err(), "key {bad}");
            assert!(secret_uri("llm", bad, None, "k").is_err(), "tenant {bad}");
            assert!(
                secret_uri("llm", "acme", Some(bad), "k").is_err(),
                "team {bad}"
            );
            assert!(
                secret_uri(bad, "acme", None, "k").is_err(),
                "category {bad}"
            );
        }
    }

    #[test]
    fn candidates_are_unit_then_team_then_tenant_default() {
        let unit_key = crate::mcp_secrets::mcp_unit_secret_key("agent-1", "worker-a").unwrap();
        assert_eq!(
            secret_uri_candidates("a2a", "acme", Some("sales"), Some("worker-a"), "agent-1")
                .unwrap(),
            vec![
                format!("secrets://default/acme/sales/a2a/{unit_key}"),
                "secrets://default/acme/sales/a2a/agent-1".to_string(),
                "secrets://default/acme/_/a2a/agent-1".to_string(),
            ]
        );
    }

    #[test]
    fn an_explicit_underscore_team_is_not_tried_twice() {
        assert_eq!(
            secret_uri_candidates("a2a", "acme", Some("_"), None, "agent-1").unwrap(),
            vec!["secrets://default/acme/_/a2a/agent-1".to_string()]
        );
    }

    #[tokio::test]
    async fn the_first_scope_that_resolves_wins() {
        let secrets = MapSecrets::with(&[
            ("secrets://default/acme/sales/a2a/agent-1", "team-token"),
            ("secrets://default/acme/_/a2a/agent-1", "tenant-token"),
        ]);
        let got = read_secret_for_unit(
            secrets.as_ref(),
            "a2a",
            "acme",
            Some("sales"),
            None,
            "agent-1",
        )
        .await
        .unwrap();
        assert_eq!(got, b"team-token".to_vec());
        assert_eq!(secrets.seen.lock().unwrap().len(), 1);
    }

    #[tokio::test]
    async fn a_miss_names_every_uri_tried() {
        let secrets = MapSecrets::with(&[]);
        let miss = read_secret_for_unit(
            secrets.as_ref(),
            "a2a",
            "acme",
            Some("sales"),
            None,
            "agent-1",
        )
        .await
        .unwrap_err();
        let rendered = miss.to_string();
        assert!(
            rendered.starts_with("no credential at ")
                && rendered.contains("secrets://default/acme/sales/a2a/agent-1")
                && rendered.contains("secrets://default/acme/_/a2a/agent-1"),
            "got: {rendered}"
        );
    }
}