use std::collections::BTreeMap;
use alien_error::AlienError;
use serde::{Deserialize, Serialize};
use crate::envelope::Kind;
use crate::error::{ErrorData, Result};
use crate::release::{is_loader_env, ReleaseBody};
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ConfigBody {
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub env: BTreeMap<String, String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub inputs: BTreeMap<String, serde_json::Value>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub bindings: BTreeMap<String, serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub telemetry_endpoint: Option<String>,
}
impl ConfigBody {
pub fn check(&self, release: &ReleaseBody) -> Result<()> {
for name in self.env.keys() {
if is_loader_env(name) || !release.configurable.env.contains(name) {
return Err(refused(
Kind::Config,
format!("env {name} is not configurable in the signed release"),
));
}
}
for id in self.inputs.keys() {
if !release.configurable.inputs.contains(id) {
return Err(refused(
Kind::Config,
format!("input '{id}' is not configurable in the signed release"),
));
}
}
for id in self.bindings.keys() {
if !release
.workloads
.iter()
.any(|workload| workload.bindings.contains_key(id))
{
return Err(refused(
Kind::Config,
format!("binding '{id}' is not a binding of the signed release"),
));
}
}
if let Some(endpoint) = &self.telemetry_endpoint {
if !endpoint.starts_with("https://") {
return Err(refused(
Kind::Config,
format!("telemetry endpoint '{endpoint}' is not an https URL"),
));
}
}
Ok(())
}
}
fn refused(kind: Kind, reason: impl Into<String>) -> AlienError<ErrorData> {
AlienError::new(ErrorData::BodyRefused {
kind: kind.as_str().to_string(),
reason: reason.into(),
})
}