surjection_core/model/
secrets.rs1use 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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
19#[schemars(title = "Surjection secret catalog (store/secrets.toml)")]
20pub struct SecretCatalog {
21 #[schemars(extend("const" = 1))]
23 pub schema: i64,
24 #[serde(default, skip_serializing_if = "Option::is_none")]
25 pub onepassword: Option<OnePassword>,
26 #[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#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
40pub struct OnePassword {
41 #[serde(default, skip_serializing_if = "Option::is_none")]
44 pub environment: Option<String>,
45 #[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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
58pub struct SecretEntry {
59 #[serde(rename = "ref")]
62 pub reference: String,
63 #[serde(default, skip_serializing_if = "Option::is_none")]
64 pub description: Option<String>,
65 #[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}