1use 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#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
14#[serde(rename_all = "camelCase", deny_unknown_fields)]
15pub struct ConfigBody {
16 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
18 pub env: BTreeMap<String, String>,
19 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
21 pub inputs: BTreeMap<String, serde_json::Value>,
22 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
26 pub bindings: BTreeMap<String, serde_json::Value>,
27 #[serde(default, skip_serializing_if = "Option::is_none")]
30 pub telemetry_endpoint: Option<String>,
31}
32
33impl ConfigBody {
34 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}