Skip to main content

surjection_core/model/
secrets.rs

1//! `store/secrets.toml` (SPEC 5.3). The reference grammar it uses lives in
2//! [`crate::secrets::grammar`].
3//!
4//! The catalog holds references and metadata only, never values.
5
6use std::collections::BTreeMap;
7
8use schemars::JsonSchema;
9use serde::{Deserialize, Serialize};
10
11use crate::error::ErrorCode;
12use crate::model::ids::IdKind;
13use crate::model::validate::{Diagnostics, join};
14use crate::model::{SCHEMA_VERSION, StoreModel};
15use crate::secrets::grammar::{is_op_account, is_op_environment_id};
16
17/// `store/secrets.toml`.
18#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
19#[schemars(title = "Surjection secret catalog (store/secrets.toml)")]
20pub struct SecretCatalog {
21    /// Schema version; always 1.
22    #[schemars(extend("const" = 1))]
23    pub schema: i64,
24    #[serde(default, skip_serializing_if = "Option::is_none")]
25    pub onepassword: Option<OnePassword>,
26    /// Aliases (`${secret:ALIAS}`) and the references they resolve to.
27    #[serde(default, skip_serializing_if = "Option::is_none")]
28    pub secrets: Option<BTreeMap<String, SecretEntry>>,
29    #[serde(flatten)]
30    #[schemars(skip)]
31    pub extra: toml::Table,
32}
33
34impl StoreModel for SecretCatalog {
35    const SCHEMA_NAME: &'static str = "secrets";
36}
37
38/// 1Password settings shared by every machine.
39#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
40pub struct OnePassword {
41    /// Id of the one 1Password Environment holding MCP secrets (an
42    /// identifier, not a secret).
43    #[serde(default, skip_serializing_if = "Option::is_none")]
44    pub environment: Option<String>,
45    /// The 1Password account Surjection's `op` calls use (sign-in address,
46    /// shorthand or account id), for machines whose desktop app is signed
47    /// in to several accounts (gate V1 FB-7). A service account token
48    /// names its own account.
49    #[serde(default, skip_serializing_if = "Option::is_none")]
50    pub account: Option<String>,
51    #[serde(flatten)]
52    #[schemars(skip)]
53    pub extra: toml::Table,
54}
55
56/// One alias. Also the shape of a per-machine override (SPEC 5.6).
57#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
58pub struct SecretEntry {
59    /// Provider reference: `env:NAME`, `op://vault/item/field`,
60    /// `op-env://<environment-id>/<NAME>` or `keyring://<service>/<account>`.
61    #[serde(rename = "ref")]
62    pub reference: String,
63    #[serde(default, skip_serializing_if = "Option::is_none")]
64    pub description: Option<String>,
65    /// Environment variable name used by env delivery (default: the alias).
66    #[serde(default, skip_serializing_if = "Option::is_none")]
67    pub env_name: Option<String>,
68    #[serde(flatten)]
69    #[schemars(skip)]
70    pub extra: toml::Table,
71}
72
73impl SecretCatalog {
74    pub fn new() -> Self {
75        Self {
76            schema: SCHEMA_VERSION,
77            onepassword: None,
78            secrets: None,
79            extra: toml::Table::new(),
80        }
81    }
82
83    pub fn validate(&self) -> Diagnostics {
84        let mut d = Diagnostics::new();
85        if let Some(environment) = self
86            .onepassword
87            .as_ref()
88            .and_then(|op| op.environment.as_ref())
89            && !is_op_environment_id(environment)
90        {
91            d.error(
92                ErrorCode::InvalidValue,
93                "onepassword.environment",
94                format!("invalid 1Password Environment id {environment:?}"),
95            );
96        }
97        if let Some(account) = self.onepassword.as_ref().and_then(|op| op.account.as_ref())
98            && !is_op_account(account)
99        {
100            d.error(
101                ErrorCode::InvalidValue,
102                "onepassword.account",
103                format!("invalid 1Password account {account:?}"),
104            );
105        }
106        for (alias, entry) in self.secrets.iter().flatten() {
107            let path = join("secrets", alias);
108            d.check_id(IdKind::SecretAlias, alias, &path);
109            entry.validate_into(&path, &mut d);
110        }
111        d
112    }
113}
114
115impl Default for SecretCatalog {
116    fn default() -> Self {
117        Self::new()
118    }
119}
120
121impl SecretEntry {
122    pub(crate) fn validate_into(&self, path: &str, d: &mut Diagnostics) {
123        d.check_secret_ref(&self.reference, &join(path, "ref"));
124        if let Some(env_name) = &self.env_name {
125            d.check_id(IdKind::EnvVar, env_name, &join(path, "env_name"));
126        }
127    }
128}