Skip to main content

alien_signing/
config.rs

1//! `config/v1`: developer config for one install.
2
3use std::collections::BTreeMap;
4
5use alien_error::AlienError;
6use serde::{Deserialize, Serialize};
7
8use crate::envelope::Kind;
9use crate::error::{ErrorData, Result};
10use crate::release::{is_loader_env, ReleaseBody};
11
12/// `config/v1`: developer config for one install.
13#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
14#[serde(rename_all = "camelCase", deny_unknown_fields)]
15pub struct ConfigBody {
16    /// Environment values, by name.
17    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
18    pub env: BTreeMap<String, String>,
19    /// Developer input values, by id.
20    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
21    pub inputs: BTreeMap<String, serde_json::Value>,
22    /// Values of bindings to resources outside the customer's cloud, by
23    /// resource id. Used only for bindings the enforcer can neither derive
24    /// nor find in its own install.
25    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
26    pub bindings: BTreeMap<String, serde_json::Value>,
27    /// Where workloads send telemetry, if anywhere. The control plane can't
28    /// point it elsewhere: it is part of the signed config.
29    #[serde(default, skip_serializing_if = "Option::is_none")]
30    pub telemetry_endpoint: Option<String>,
31}
32
33impl ConfigBody {
34    /// Refuse any name the release doesn't make configurable.
35    pub fn check(&self, release: &ReleaseBody) -> Result<()> {
36        for name in self.env.keys() {
37            if is_loader_env(name) || !release.configurable.env.contains(name) {
38                return Err(refused(
39                    Kind::Config,
40                    format!("env {name} is not configurable in the signed release"),
41                ));
42            }
43        }
44        for id in self.inputs.keys() {
45            if !release.configurable.inputs.contains(id) {
46                return Err(refused(
47                    Kind::Config,
48                    format!("input '{id}' is not configurable in the signed release"),
49                ));
50            }
51        }
52        for id in self.bindings.keys() {
53            if !release
54                .workloads
55                .iter()
56                .any(|workload| workload.bindings.contains_key(id))
57            {
58                return Err(refused(
59                    Kind::Config,
60                    format!("binding '{id}' is not a binding of the signed release"),
61                ));
62            }
63        }
64        if let Some(endpoint) = &self.telemetry_endpoint {
65            if !endpoint.starts_with("https://") {
66                return Err(refused(
67                    Kind::Config,
68                    format!("telemetry endpoint '{endpoint}' is not an https URL"),
69                ));
70            }
71        }
72        Ok(())
73    }
74}
75
76fn refused(kind: Kind, reason: impl Into<String>) -> AlienError<ErrorData> {
77    AlienError::new(ErrorData::BodyRefused {
78        kind: kind.as_str().to_string(),
79        reason: reason.into(),
80    })
81}