use std::collections::{BTreeMap, BTreeSet};
use alien_error::AlienError;
use serde_json::json;
use crate::error::{ErrorData, Result};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BindingContext {
pub platform: String,
pub prefix: String,
pub account: String,
pub region: Option<String>,
}
pub fn binding_env_name(id: &str) -> String {
format!("ALIEN_{}_BINDING", id.replace('-', "_").to_uppercase())
}
pub const DEPLOYER_SECRETS_VAULT: &str = "deployer-secrets";
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct OwnBindingFacts {
pub context: Option<BindingContext>,
pub resource_name: String,
pub resource_group: Option<String>,
pub location: Option<String>,
pub urls: Vec<String>,
}
pub fn check_own_binding(value: &str, facts: &OwnBindingFacts) -> std::result::Result<(), String> {
let context = facts
.context
.as_ref()
.ok_or("the enforcer knows no install to check it against")?;
let binding: serde_json::Value = serde_json::from_str(value)
.map_err(|_| "the own binding isn't a JSON object".to_string())?;
let object = binding
.as_object()
.ok_or("the own binding isn't a JSON object")?;
let text = |key: &str| object.get(key).and_then(|value| value.as_str());
let expect = |key: &str, expected: &str| match text(key) {
Some(found) if found == expected => Ok(()),
_ => Err(format!(
"its {key} is {}, not {expected}",
object
.get(key)
.map_or("missing".to_string(), |v| v.to_string())
)),
};
let url = |key: &str, required: bool| match object.get(key) {
None if !required => Ok(()),
None => Err(format!("it has no {key}")),
Some(found) => {
let found = found
.as_str()
.ok_or_else(|| format!("its {key} isn't a URL"))?;
let same = |url: &String| url.trim_end_matches('/') == found.trim_end_matches('/');
if facts.urls.iter().any(same) {
Ok(())
} else {
Err(format!(
"its {key} is {found}, which is no URL the cloud reports for {}",
facts.resource_name
))
}
}
};
let only = |keys: &[&str]| {
for key in object.keys() {
if !keys.contains(&key.as_str()) {
return Err(format!(
"it has a field {key} the platform's binding doesn't"
));
}
}
Ok(())
};
let name = facts.resource_name.as_str();
match context.platform.as_str() {
"aws" => {
only(&["service", "workerName", "region", "url"])?;
expect("service", "lambda")?;
expect("workerName", name)?;
expect(
"region",
context
.region
.as_deref()
.ok_or("the enforcer knows no region")?,
)?;
url("url", false)?;
}
"gcp" => {
only(&[
"service",
"projectId",
"serviceName",
"location",
"privateUrl",
"publicUrl",
])?;
expect("service", "cloudrun")?;
expect("projectId", &context.account)?;
expect("serviceName", name)?;
expect(
"location",
facts
.location
.as_deref()
.ok_or("the enforcer found no such service to read its location from")?,
)?;
url("privateUrl", true)?;
url("publicUrl", false)?;
}
"azure" => {
only(&[
"service",
"subscriptionId",
"resourceGroupName",
"containerAppName",
"privateUrl",
"publicUrl",
])?;
expect("service", "containerapp")?;
expect("subscriptionId", &context.account)?;
expect(
"resourceGroupName",
facts
.resource_group
.as_deref()
.ok_or("the enforcer knows no resource group")?,
)?;
expect("containerAppName", name)?;
url("privateUrl", true)?;
url("publicUrl", false)?;
}
"machines" => {
only(&[
"service",
"containerName",
"internalUrl",
"internalUrls",
"publicUrl",
])?;
if let Some(service) = object.get("service") {
let plain = service.as_str().is_some_and(|found| {
!found.is_empty()
&& found.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
});
if !plain {
return Err(format!("field service is {service}, not a plain name"));
}
}
expect("containerName", name)?;
let internal = |found: &serde_json::Value, key: &str| {
let found = found.as_str().unwrap_or_default();
let parsed = url::Url::parse(found).ok().filter(|parsed| {
parsed.scheme() == "http"
&& parsed.host_str() == Some(&format!("{name}.svc"))
&& parsed.port().is_some()
&& parsed.username().is_empty()
&& parsed.password().is_none()
&& matches!(parsed.path(), "" | "/")
&& parsed.query().is_none()
&& parsed.fragment().is_none()
});
if parsed.is_some() {
Ok(())
} else {
Err(format!(
"its {key} is {found}, not an address of {name}.svc"
))
}
};
if let Some(found) = object.get("internalUrl") {
internal(found, "internalUrl")?;
}
for found in object
.get("internalUrls")
.and_then(|urls| urls.as_object())
.into_iter()
.flat_map(|urls| urls.values())
{
internal(found, "internalUrls")?;
}
if object
.get("internalUrls")
.is_some_and(|urls| !urls.is_object())
{
return Err("its internalUrls isn't a map".to_string());
}
url("publicUrl", false)?;
}
other => return Err(format!("{other} has no own binding the enforcer can check")),
}
Ok(())
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Bindings {
pub values: BTreeMap<String, String>,
pub unresolved: BTreeMap<String, String>,
}
pub fn binding_env(
bindings: &BTreeMap<String, String>,
context: &BindingContext,
) -> Result<Bindings> {
binding_env_with(bindings, &BTreeSet::new(), context)
}
pub fn binding_env_with(
bindings: &BTreeMap<String, String>,
live: &BTreeSet<String>,
context: &BindingContext,
) -> Result<Bindings> {
let mut derived = Bindings::default();
for (id, resource_type) in bindings {
let name = format!("{}-{id}", context.prefix);
let region = || {
context
.region
.clone()
.ok_or_else(|| refused(id, resource_type, context, "the enforcer knows no region"))
};
let value = match (context.platform.as_str(), resource_type.as_str()) {
("aws", "storage") => json!({ "service": "s3", "bucketName": name }),
("aws", "kv") => {
json!({ "service": "dynamodb", "tableName": name, "region": region()? })
}
("aws", "queue") => json!({
"service": "sqs",
"queueUrl": format!("https://sqs.{}.amazonaws.com/{}/{name}", region()?, context.account),
}),
("aws", "vault") => json!({ "service": "parameter-store", "vaultPrefix": name }),
("gcp", "storage") => json!({ "service": "gcs", "bucketName": name }),
("gcp", "vault") => json!({ "service": "secret-manager", "vaultPrefix": name }),
("gcp", "queue") => json!({
"service": "pubsub",
"topic": name,
"subscription": format!("{name}{}", if live.contains(id) { "-sub" } else { "-default" }),
}),
("gcp", "kv") if live.contains(id) => json!({
"service": "firestore",
"projectId": context.account,
"databaseId": "(default)",
"collectionName": format!("kv-{name}").to_lowercase().replace('_', "-"),
}),
("gcp", "kv") => json!({
"service": "firestore",
"projectId": context.account,
"databaseId": name,
"collectionName": id,
}),
("azure", "vault") if id != DEPLOYER_SECRETS_VAULT => {
json!({ "service": "key-vault", "vaultName": name })
}
_ => {
derived.unresolved.insert(id.clone(), resource_type.clone());
continue;
}
};
derived
.values
.insert(binding_env_name(id), canonical(&value));
}
Ok(derived)
}
pub fn resolve_bindings(
derived: Bindings,
read: &BTreeMap<String, String>,
signed_config: &BTreeMap<String, serde_json::Value>,
) -> Result<BTreeMap<String, String>> {
let mut values = derived.values;
for (id, resource_type) in &derived.unresolved {
let value = match (read.get(id), signed_config.get(id)) {
(Some(value), _) => value.clone(),
(None, Some(value)) => canonical(value),
(None, None) => {
return Err(AlienError::new(ErrorData::WorkloadRefused {
workload: id.clone(),
reason: format!(
"binding '{id}' ({resource_type}) has no value: it doesn't follow from the install's names, the enforcer found no such resource in its install, and no signed config sets it"
),
}))
}
};
values.insert(binding_env_name(id), value);
}
Ok(values)
}
fn canonical(value: &serde_json::Value) -> String {
fn sorted(value: &serde_json::Value) -> serde_json::Value {
match value {
serde_json::Value::Object(map) => {
let keys: BTreeMap<&String, &serde_json::Value> = map.iter().collect();
serde_json::Value::Object(
keys.into_iter()
.map(|(key, value)| (key.clone(), sorted(value)))
.collect(),
)
}
serde_json::Value::Array(items) => {
serde_json::Value::Array(items.iter().map(sorted).collect())
}
other => other.clone(),
}
}
sorted(value).to_string()
}
fn refused(
id: &str,
resource_type: &str,
context: &BindingContext,
reason: &str,
) -> AlienError<ErrorData> {
AlienError::new(ErrorData::WorkloadRefused {
workload: id.to_string(),
reason: format!(
"binding '{id}' ({resource_type} on {}): {reason}",
context.platform
),
})
}