Template modifiers
A template modifier is the part of an extension that changes what initial setup creates. It gets the CloudFormation template, Terraform module, or Helm chart Alien generated, and returns a changed one.
This one turns on IAM authentication for every Postgres cluster in the CloudFormation template:
use alien_extension_sdk::prelude::*;
/// Turns on IAM auth for every RDS cluster in the CloudFormation template.
pub struct CloudFormation;
impl CloudFormationModifier for CloudFormation {
fn modify(&self, mut template: CfTemplate) -> Result<CfTemplate> {
for cluster in template.resources_of_type_mut("AWS::RDS::DBCluster") {
cluster.set_property("EnableIAMDatabaseAuthentication", true);
}
Ok(template)
}
}The setup template your customers get now carries the property:
alien render --format cloudformation --stack alien.ts --json \
| jq '.Resources[] | select(.Type == "AWS::RDS::DBCluster") | .Properties.EnableIAMDatabaseAuthentication'trueRegistration
The register function in your hooks crate lists every modifier the extension has:
use alien_extension_sdk::prelude::*;
mod postgres_iam_auth;
fn register(ext: &mut Extension) {
ext.cloudformation_modifier(postgres_iam_auth::CloudFormation);
ext.terraform_modifier(postgres_iam_auth::Terraform);
}
alien_extension_sdk::export!("acme", register);Modifiers of one extension run in the order you register them. Your stack turns the extension on with .extensions([acme]), as the quickstart shows.
A modifier runs wherever Alien generates a setup template:
- when Alien builds your setup packages
- in
alien renderon your machine - when the Alien manager serves a Helm chart, and when the setup CLI renders one for a Kubernetes install
Each applies the same modifiers, so each shows the same resources. Packages list what each extension changed: the CloudFormation template's header comments and the end of the Terraform module's README.md name every part an extension added or changed. alien render prints the same list, and writes it at the end of the Terraform README.md it renders. A modifier never runs in your customer's cloud.
CloudFormation
A CloudFormation modifier gets the whole template as a CfTemplate and returns it. It runs on the template for both CloudFormation targets, aws and eks.
It can:
- add resources, parameters, conditions, mappings, outputs, rules and metadata
- add, change, or remove properties on any resource, Alien's included
It can't change the Condition, DeletionPolicy or UpdateReplacePolicy of one of Alien's resources, or change or remove one of Alien's parameters, conditions, mappings, outputs, rules or metadata entries, or the template's Description: those decide whether a resource exists, what happens to its data, and what the installer reads and enters. It can't add a Transform, an Fn::Transform or a nested stack (AWS::CloudFormation::Stack or StackSet), which would run content the reviewed template doesn't show. It can't refer, with Ref, Fn::GetAtt, Fn::Sub or Fn::If, to a resource, parameter or condition the template doesn't define, which CloudFormation would refuse only when the customer applies it.
resources_of_type_mut finds resources by their CloudFormation type. set_property adds a property, or replaces its value when the resource already has it.
Terraform
A Terraform modifier gets Alien's module as a TfModule and edits Alien's own files in place:
/// The same flag in Terraform.
pub struct Terraform;
impl TerraformModifier for Terraform {
fn modify(&self, mut module: TfModule) -> Result<TfModule> {
for cluster in module.resources_of_type_mut("aws_rds_cluster") {
cluster.set_attribute("iam_database_authentication_enabled", true);
}
Ok(module)
}
}Alien writes back each file your modifier changed. Every byte you didn't change stays as Alien wrote it, and the result still passes terraform fmt -check.
The module your customer's admin applies is Alien's module with your edits in it.
alien render --format terraform --target aws --stack alien.ts --output review/awsThe new attribute is in review/aws/db.tf, inside the aws_rds_cluster block it belongs to.
Because the modifier edits Alien's resource itself, it can change a nested block too. On Google Cloud, for example, it can add a database_flags block inside a Cloud SQL instance's settings.
A Terraform modifier:
- adds attributes and changes their values; the SDK has no call that removes an attribute Alien emitted, such as Cloud SQL's
tier - puts the resources it adds in a new file named after your extension, and can't add any other file
- can't move one of Alien's blocks, or change its
count,for_each,providerorlifecycle - can't add a
terraform,provider,moved,removedorimportblock, a module with a source that isn't a local path, a variable to one of Alien's files, or change an output Alien registers - can't refer to a variable, local, module, resource or data source the module doesn't declare, which Terraform would refuse only when the customer applies it
- runs on the module for every target (
aws,gcp,azure,eks,gke, andaks)
Look resources up by type, so one modifier covers every cloud. In the gcp module, aws_rds_cluster finds nothing and the loop does nothing.
Helm
A Helm modifier adds files to the chart. It can't change Alien's own templates. Each of them contains Go template actions, so none can be read and edited as YAML.
use alien_extension_sdk::prelude::*;
use std::collections::BTreeMap;
/// Lets the chart's pods send traffic only inside the cluster.
pub struct NetworkPolicy;
impl HelmModifier for NetworkPolicy {
fn templates(&self, _chart: &HelmChart) -> Result<BTreeMap<String, String>> {
let policy = r#"apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: acme-deny-egress
namespace: {{ .Release.Namespace }}
spec:
podSelector: {}
policyTypes: [Egress]
egress:
- to:
- namespaceSelector: {}
"#;
Ok(BTreeMap::from([("acme-deny-egress.yaml".into(), policy.into())]))
}
}Register it with ext.helm_modifier(NetworkPolicy). Its one method, templates, gets Alien's chart to read and returns new files, keyed by file name.
- Alien puts your files in a subchart of their own,
charts/<extension>/templates/, whose values are its own: under<extension>in the chart's values. - A file name is lowercase letters, digits,
.,_and-, and ends in.yaml,.ymlor.tpl. A name with a/, or one the chart already has, fails the build. - Your files are Helm templates too, so they can use template actions such as
{{ .Release.Namespace }}. A named template must start with<extension>., asacme.labelsdoes. - A file can't call
set,unset,merge,mergeOverwrite,mustMerge,mustMergeOverwriteortpl. Helm hands every subchart the same.Releaseand part of the chart's values, so changing them in place, or running text as a template, would change what Alien's templates render. - An extension can't be named like one of Alien's values keys or named templates.
What it can't change
A modifier can't remove a resource Alien emits, hide it, or change its type. The template your customer's security team reviews shows every resource Alien emits.
After each modifier, Alien compares the template's resources with the ones before it:
| Format | What Alien compares |
|---|---|
| CloudFormation | Each key under Resources, and its Type |
| Terraform | Each resource address, such as aws_rds_cluster.db |
| Helm | Each object's kind and name, with every optional component switched on as well as off, when the package is built |
A missing resource or a changed type fails the package build, and alien render for CloudFormation and Terraform, with an error that names the extension. Added resources and changed properties pass. A refusal isn't retried: the same template is refused the same way.
Several extensions
Modifiers run in the order your stack lists extensions in .extensions([...]). Each extension gets the template the previous one returned, so a later extension can change a value an earlier one set.
alien render lists which extension changed which resource, so a review shows each extension's changes apart.
Existing deployments
A modifier changes the setup templates built from your release. A deployment set up before that release gets the change when its admin reruns initial setup with the new template.
Removing a modifier works the same way. The next template the admin applies no longer carries the setting, so it goes back to the value Alien sets, or to the cloud's default.
Frozen and live
A modifier reaches only frozen resources, the ones a setup template creates. That includes the cloud template that sets up a Kubernetes deployment on EKS, GKE, or AKS next to the Operator.
Alien's controllers create live resources. To reach those, add an interceptor to the same extension. An extension that sets one value everywhere ships both.
Limits and failures
Modifiers run sandboxed, with no network, no files, and no credentials. Each call has these limits:
| Limit | Value |
|---|---|
| Time | 2 seconds |
| Memory | 64 MiB |
| Output | 16 MiB |
A modifier that returns an error, panics, or goes over a limit fails alien render or the package build. The error names the extension and the modifier, and no package is built, so no customer gets a partly changed template.
Alien doesn't retry a modifier. It gives the same output for the same template on every run.
Next
- Interceptors: change the calls Alien's controllers make, for live resources
- Testing: test modifiers natively and in the module Alien loads
- Longer backups: a CloudFormation and Terraform modifier on AWS, Google Cloud, and Azure