1use std::collections::BTreeMap;
5
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8
9use crate::env::Os;
10use crate::error::ErrorCode;
11use crate::model::ids::IdKind;
12use crate::model::secrets::SecretEntry;
13use crate::model::server::{OS_NAMES, ServerOverride};
14use crate::model::validate::{Diagnostics, join};
15use crate::model::{SCHEMA_VERSION, StoreModel};
16
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
18#[schemars(title = "Surjection machine overlay (store/machines/<machine-id>.toml)")]
19pub struct MachineOverlay {
20 #[schemars(extend("const" = 1))]
22 pub schema: i64,
23 #[schemars(extend("pattern" = crate::model::ids::ULID_PATTERN))]
25 pub machine_id: String,
26 pub label: String,
28 pub os: String,
30 #[serde(default, skip_serializing_if = "Option::is_none")]
31 pub enable: Option<MachineEnable>,
32 #[serde(default, skip_serializing_if = "Option::is_none")]
34 pub secrets: Option<BTreeMap<String, SecretEntry>>,
35 #[serde(default, skip_serializing_if = "Option::is_none")]
37 pub servers: Option<BTreeMap<String, ServerOverride>>,
38 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub projects: Option<BTreeMap<String, MachineProject>>,
41 #[serde(default, skip_serializing_if = "Option::is_none")]
43 pub delivery: Option<MachineDelivery>,
44 #[serde(flatten)]
45 #[schemars(skip)]
46 pub extra: toml::Table,
47}
48
49impl StoreModel for MachineOverlay {
50 const SCHEMA_NAME: &'static str = "machine";
51}
52
53#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
54pub struct MachineEnable {
55 #[serde(default, skip_serializing_if = "Option::is_none")]
57 pub exclude_clients: Option<Vec<String>>,
58 #[serde(default, skip_serializing_if = "Option::is_none")]
63 pub released: Option<BTreeMap<String, Vec<String>>>,
64 #[serde(flatten)]
65 #[schemars(skip)]
66 pub extra: toml::Table,
67}
68
69#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
71pub struct MachineDelivery {
72 #[serde(default, skip_serializing_if = "Option::is_none")]
77 pub export_provider_secrets: Option<bool>,
78 #[serde(flatten)]
79 #[schemars(skip)]
80 pub extra: toml::Table,
81}
82
83#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
84pub struct MachineProject {
85 pub path: String,
87 #[serde(flatten)]
88 #[schemars(skip)]
89 pub extra: toml::Table,
90}
91
92impl MachineOverlay {
93 pub fn new(machine_id: impl Into<String>, label: impl Into<String>, os: Os) -> Self {
94 Self {
95 schema: SCHEMA_VERSION,
96 machine_id: machine_id.into(),
97 label: label.into(),
98 os: os.as_str().to_owned(),
99 enable: None,
100 secrets: None,
101 servers: None,
102 projects: None,
103 delivery: None,
104 extra: toml::Table::new(),
105 }
106 }
107
108 pub fn validate(&self, file_id: Option<&str>) -> Diagnostics {
110 let mut d = Diagnostics::new();
111 d.check_named_id(IdKind::Machine, &self.machine_id, "machine_id", file_id);
112 if self.label.trim().is_empty() {
113 d.error(
114 ErrorCode::InvalidValue,
115 "label",
116 "`label` must not be empty",
117 );
118 }
119 let os = os_from_name(&self.os);
120 if os.is_none() {
121 d.warning(
122 ErrorCode::UnknownOs,
123 "os",
124 format!(
125 "unknown OS {:?}; expected one of {OS_NAMES:?}; kept as written",
126 self.os
127 ),
128 );
129 }
130 if let Some(clients) = self
131 .enable
132 .as_ref()
133 .and_then(|e| e.exclude_clients.as_ref())
134 {
135 d.check_client_ids(clients, "enable.exclude_clients");
136 }
137 for (id, clients) in self.enable.iter().flat_map(|e| e.released.iter().flatten()) {
138 let path = join("enable.released", id);
139 d.check_id(IdKind::Server, id, &path);
140 d.check_client_ids(clients, &path);
141 }
142 for (alias, entry) in self.secrets.iter().flatten() {
143 let path = join("secrets", alias);
144 d.check_id(IdKind::SecretAlias, alias, &path);
145 entry.validate_into(&path, &mut d);
146 }
147 for (id, over) in self.servers.iter().flatten() {
148 let path = join("servers", id);
149 d.check_id(IdKind::Server, id, &path);
150 over.fields.check_templates(&path, &mut d);
151 }
152 for (id, project) in self.projects.iter().flatten() {
153 let path = join("projects", id);
154 d.check_id(IdKind::Project, id, &path);
155 if let Some(os) = os
156 && !is_absolute_on(os, &project.path)
157 {
158 d.error(
159 ErrorCode::InvalidPath,
160 join(&path, "path"),
161 format!("{:?} is not an absolute {} path", project.path, os.as_str()),
162 );
163 }
164 }
165 d
166 }
167}
168
169fn os_from_name(name: &str) -> Option<Os> {
170 [Os::Windows, Os::Linux, Os::MacOs]
171 .into_iter()
172 .find(|os| os.as_str() == name)
173}
174
175fn is_absolute_on(os: Os, path: &str) -> bool {
178 match os {
179 Os::Windows => {
180 let bytes = path.as_bytes();
181 let drive = bytes.len() >= 3
182 && bytes[0].is_ascii_alphabetic()
183 && bytes[1] == b':'
184 && matches!(bytes[2], b'\\' | b'/');
185 drive || path.starts_with(r"\\") || path.starts_with("//")
186 }
187 Os::Linux | Os::MacOs => path.starts_with('/'),
188 }
189}