use crate::{
is_prepared_cluster, ownership_policy_for_resource_type, Build, ComputeCluster, Container,
CustomResource, Daemon, ErrorData, Key, KubernetesCluster, KubernetesClusterOwnership,
ManagementPermissions, NetworkSettings, Platform, ResourceEntry, ResourceLifecycle,
ResourceRef, ResourceType, Result, Sandbox, SetupManagement, Stack, StackSettings, Storage,
Worker,
};
use alien_error::AlienError;
fn is_granted_on(stack: &Stack, id: &str, resource_type: &ResourceType) -> bool {
let links_sets = stack
.descriptor(resource_type)
.is_some_and(|descriptor| !descriptor.link.permission_sets.is_empty());
let linked = |links: &[ResourceRef]| links_sets && links.iter().any(|link| link.id == id);
stack
.permissions
.profiles
.values()
.any(|profile| profile.0.get(id).is_some_and(|sets| !sets.is_empty()))
|| stack.resources().any(|(_, entry)| {
let config = &entry.config;
config
.downcast_ref::<Worker>()
.is_some_and(|w| linked(&w.links))
|| config
.downcast_ref::<Container>()
.is_some_and(|c| linked(&c.links))
|| config
.downcast_ref::<Daemon>()
.is_some_and(|d| d.permissions.is_some() && linked(&d.links))
|| config
.downcast_ref::<Build>()
.is_some_and(|b| linked(&b.links))
})
}
pub fn validate_automatic_setup_resources(
stack: &Stack,
platform: Platform,
management: SetupManagement,
) -> Result<()> {
if management == SetupManagement::Automatic
&& matches!(platform, Platform::Aws | Platform::Gcp | Platform::Azure)
{
let unsupported = |resource_type: &str, resource_id: &str| {
AlienError::new(ErrorData::AutomaticSetupResourceUnsupported {
resource_type: resource_type.to_string(),
resource_id: resource_id.to_string(),
platform,
})
};
match stack.management() {
ManagementPermissions::Override(_) => {
return Err(unsupported(
"management permission override",
"permissions.management",
));
}
ManagementPermissions::Extend(profile) if platform == Platform::Aws => {
if let Some(scope) = profile.0.keys().find(|scope| scope.as_str() != "*") {
return Err(unsupported("resource-scoped management permission", scope));
}
}
_ => {}
}
for (id, entry) in stack.resources() {
if let Some(resource_type) = automatic_setup_unsupported_resource(entry, platform) {
return Err(unsupported(&resource_type, id));
}
if platform == Platform::Aws
&& entry.config.downcast_ref::<CustomResource>().is_some()
&& is_granted_on(stack, id, &entry.config.resource_type())
{
return Err(unsupported("custom resource grant", id));
}
}
}
if management == SetupManagement::Automatic && platform == Platform::Azure {
for (id, entry) in stack.resources() {
if entry.config.downcast_ref::<Sandbox>().is_some() {
return Err(AlienError::new(ErrorData::SandboxCapabilityUnsupported {
capability: format!(
"automatic infrastructure management for sandbox '{id}'; use manual setup to create and import its sandbox group"
),
platform: platform.to_string(),
}));
}
}
}
Ok(())
}
fn automatic_setup_unsupported_resource(
entry: &ResourceEntry,
platform: Platform,
) -> Option<String> {
if entry.config.downcast_ref::<Key>().is_some() {
return Some("key".to_string());
}
let resource_type = entry.config.resource_type();
if matches!(
resource_type.as_ref(),
"email" | "experimental/aws-opensearch" | "kubernetes-cluster"
) {
return Some(resource_type.as_ref().to_string());
}
if platform == Platform::Azure
&& entry
.config
.downcast_ref::<Storage>()
.is_some_and(|storage| storage.has_cors())
{
return Some("storage with CORS origins".to_string());
}
None
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SetupConfigurationProfile {
Standard,
Advanced,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetupTarget {
pub deployment_platform: Platform,
pub cloud_platform: Platform,
}
impl SetupTarget {
pub fn new(deployment_platform: Platform, base_platform: Option<Platform>) -> Self {
Self {
deployment_platform,
cloud_platform: base_platform.unwrap_or(deployment_platform),
}
}
pub fn from_cloud(cloud_platform: Platform, targets_kubernetes: bool) -> Self {
Self {
deployment_platform: if targets_kubernetes {
Platform::Kubernetes
} else {
cloud_platform
},
cloud_platform,
}
}
pub fn is_kubernetes(&self) -> bool {
self.deployment_platform == Platform::Kubernetes
}
}
pub fn is_kubernetes_native_resource_type(resource_type: &crate::ResourceType) -> bool {
*resource_type == Sandbox::RESOURCE_TYPE || *resource_type == ComputeCluster::RESOURCE_TYPE
}
pub fn is_network_database(entry: &ResourceEntry) -> bool {
let resource_type = entry.config.resource_type();
resource_type == crate::Postgres::RESOURCE_TYPE || resource_type == crate::Redis::RESOURCE_TYPE
}
pub fn keeps_cloud_network(stack: &Stack, entry: &ResourceEntry) -> bool {
is_network_database(entry)
|| stack
.descriptor(&entry.config.resource_type())
.is_some_and(|descriptor| descriptor.requires_network)
}
pub fn existing_cluster_network_hosts(entry: &ResourceEntry, cloud_platform: Platform) -> bool {
if !is_network_database(entry) {
return true;
}
entry.config.resource_type() == crate::Postgres::RESOURCE_TYPE
&& cloud_platform == Platform::Gcp
}
pub fn existing_cluster_network_refusal(
resource_type: &str,
resource_id: &str,
network_hint: Option<&str>,
) -> String {
let hint = network_hint
.map(|hint| format!(" {hint},"))
.unwrap_or_default();
format!(
"Setup cannot yet place the {resource_type} '{resource_id}' on an existing cluster's network, so the cluster could not reach it. Use a cluster that setup creates,{hint} or remove the {resource_type}."
)
}
pub fn setup_targets_existing_cluster(stack: &Stack, settings: &StackSettings) -> bool {
let ownership = stack
.resources()
.filter(|(id, entry)| !is_prepared_cluster(id, entry))
.find_map(|(_, entry)| entry.config.downcast_ref::<KubernetesCluster>())
.map(|cluster| cluster.ownership)
.or_else(|| {
settings
.kubernetes
.as_ref()
.and_then(|kubernetes| kubernetes.cluster.as_ref())
.map(|cluster| cluster.ownership)
});
ownership.is_some_and(|ownership| ownership != KubernetesClusterOwnership::Managed)
}
pub fn refuse_unreachable_databases(
stack: &Stack,
settings: &StackSettings,
cloud_platform: Platform,
network_hint: Option<&str>,
creates: impl Fn(&ResourceEntry) -> bool,
) -> Result<()> {
if !setup_targets_existing_cluster(stack, settings)
|| matches!(
settings.network,
Some(
NetworkSettings::ByoVpcAws { .. }
| NetworkSettings::ByoVpcGcp { .. }
| NetworkSettings::ByoVnetAzure { .. }
)
)
{
return Ok(());
}
for (id, entry) in stack.resources() {
if entry.enabled_when.is_none()
&& !existing_cluster_network_hosts(entry, cloud_platform)
&& creates(entry)
{
return Err(AlienError::new(ErrorData::SetupPlacementUnsupported {
message: existing_cluster_network_refusal(
entry.config.resource_type().as_ref(),
id,
network_hint,
),
resource_type: entry.config.resource_type().to_string(),
resource_id: id.to_string(),
}));
}
}
Ok(())
}
pub fn refuse_declinable_network_extensions(stack: &Stack, settings: &StackSettings) -> Result<()> {
if !setup_targets_existing_cluster(stack, settings)
|| matches!(
settings.network,
Some(
NetworkSettings::ByoVpcAws { .. }
| NetworkSettings::ByoVpcGcp { .. }
| NetworkSettings::ByoVnetAzure { .. }
)
)
{
return Ok(());
}
let frozen_gating: std::collections::HashSet<&str> = crate::frozen_gated(stack)
.map(|(_, input_id)| input_id)
.collect();
let declinable = |entry: &ResourceEntry| {
entry
.enabled_when
.as_deref()
.is_some_and(|input_id| frozen_gating.contains(input_id))
};
if stack
.resources()
.any(|(_, entry)| keeps_cloud_network(stack, entry) && !declinable(entry))
{
return Ok(());
}
for (id, entry) in stack.resources() {
if is_network_database(entry) || !keeps_cloud_network(stack, entry) || !declinable(entry) {
continue;
}
let resource_type = entry.config.resource_type();
let input_id = entry.enabled_when.as_deref().unwrap_or_default();
return Err(AlienError::new(ErrorData::SetupPlacementUnsupported {
message: format!(
"Setup cannot yet place the {resource_type} '{id}' on an existing cluster while its input '{input_id}' also turns off a resource setup creates, because the network it requires is fixed when the template is rendered. Use a cluster that setup creates, bring the cluster's own network in the stack's network settings, give the {resource_type} an input that turns off only resources the runtime creates, or remove its input."
),
resource_type: resource_type.to_string(),
resource_id: id.to_string(),
}));
}
Ok(())
}
pub fn emits_package_scaffolding(
entry: &ResourceEntry,
management: SetupManagement,
target: SetupTarget,
) -> bool {
let resource_type = entry.config.resource_type();
if target.is_kubernetes() && is_kubernetes_native_resource_type(&resource_type) {
return false;
}
if management == SetupManagement::Automatic {
is_automatic_setup_package_resource_type(resource_type.as_ref(), target.cloud_platform)
} else {
ownership_policy_for_resource_type(resource_type.as_ref())
.emits_setup_scaffolding(entry.lifecycle)
}
}
pub fn is_automatic_setup_package_resource_type(resource_type: &str, platform: Platform) -> bool {
matches!(
resource_type,
"remote-stack-management" | "vault" | "azure_resource_group" | "service_activation"
) || (platform == Platform::Azure
&& matches!(resource_type, "service-account" | "resource-access"))
}
pub fn removed_by_runtime_cleanup(
resource_type: &str,
lifecycle: ResourceLifecycle,
management: SetupManagement,
platform: Platform,
) -> bool {
if management == SetupManagement::Automatic {
return !is_automatic_setup_package_resource_type(resource_type, platform);
}
lifecycle == ResourceLifecycle::Live
|| ownership_policy_for_resource_type(resource_type).has_runtime_cleanup_before_teardown()
}
#[cfg(test)]
mod cors_tests {
use super::*;
#[test]
fn automatic_cors_requires_an_implementing_controller() {
let stack = Stack::new("browser-storage".into())
.add(
Storage::new("assets".into())
.cors_allowed_origins(vec!["https://app.example.com".into()])
.build(),
ResourceLifecycle::Live,
)
.build();
for platform in [Platform::Aws, Platform::Gcp] {
validate_automatic_setup_resources(&stack, platform, SetupManagement::Automatic)
.unwrap();
}
let error =
validate_automatic_setup_resources(&stack, Platform::Azure, SetupManagement::Automatic)
.unwrap_err();
assert!(matches!(
error.error,
Some(ErrorData::AutomaticSetupResourceUnsupported { .. })
));
}
}
#[cfg(test)]
mod custom_resource_grant_tests {
use super::*;
use crate::{ResourceType, WorkerCode};
fn linked_zone() -> Stack {
let zone =
CustomResource::from_untagged("acme/dns-zone", serde_json::json!({ "id": "zone" }))
.unwrap();
let mut api = Worker::new("api".to_string())
.code(WorkerCode::Image {
image: "registry.example.com/api:1".to_string(),
})
.permissions("api".to_string())
.build();
api.links.push(ResourceRef::new(
ResourceType::from("acme/dns-zone"),
"zone",
));
let mut stack = Stack::new("app".to_string())
.add(zone, ResourceLifecycle::Live)
.add(api, ResourceLifecycle::Live)
.build();
stack
.extensions
.push(acme(&["acme/dns-zone/records-write"]));
stack
}
fn acme(link_sets: &[&str]) -> crate::ExtensionManifest {
serde_json::from_value(serde_json::json!({
"name": "acme", "version": "1.0.0", "digest": "sha256:0",
"resourceTypes": { "acme/dns-zone": {
"lifecycles": ["live"], "holdsData": false,
"setup": { "cloudformation": [], "terraform": [], "helm": [], "importers": [] },
"controllerPlatforms": ["aws"], "permissionSets": [],
"link": { "permissionSets": link_sets },
"schemas": { "config": {}, "outputs": {} }
}}
}))
.unwrap()
}
#[test]
fn automatic_setup_on_aws_refuses_a_grant_on_a_custom_resource() {
let error = validate_automatic_setup_resources(
&linked_zone(),
Platform::Aws,
SetupManagement::Automatic,
)
.expect_err("the link is refused");
assert_eq!(
error.code, "AUTOMATIC_SETUP_RESOURCE_UNSUPPORTED",
"{error}"
);
assert!(error.to_string().contains("'zone'"), "{error}");
assert!(!error.retryable);
let mut scoped = linked_zone();
let api = scoped.resources.get_mut("api").unwrap();
api.config.downcast_mut::<Worker>().unwrap().links.clear();
scoped.permissions.profiles.insert(
"api".to_string(),
serde_json::from_value(serde_json::json!({ "zone": ["acme/dns-zone/records-write"] }))
.unwrap(),
);
validate_automatic_setup_resources(&scoped, Platform::Aws, SetupManagement::Automatic)
.expect_err("the profile's grant is refused");
let mut unlinked = linked_zone();
unlinked.resources.shift_remove("api");
let mut no_link_sets = linked_zone();
no_link_sets.extensions = vec![acme(&[])];
for (stack, platform, management) in [
(linked_zone(), Platform::Aws, SetupManagement::Manual),
(linked_zone(), Platform::Gcp, SetupManagement::Automatic),
(unlinked, Platform::Aws, SetupManagement::Automatic),
(no_link_sets, Platform::Aws, SetupManagement::Automatic),
] {
validate_automatic_setup_resources(&stack, platform, management)
.unwrap_or_else(|error| panic!("{platform:?} {management:?}: {error}"));
}
}
}