Docs

Turn on IAM database authentication

This extension turns on IAM database authentication for the Postgres database Alien creates, on AWS, Google Cloud, and Azure. It sets the flag on every path a database is created by: template modifiers for the setup template, and interceptors for the create calls Alien's controllers make.

alien init extensions/postgres-iam-auth postgres-iam-auth

What you build:

  • CloudFormation and Terraform modifiers that turn the flag on in every setup template.
  • AWS, Google Cloud, and Azure interceptors that turn it on in the create call of a database Alien's controllers create.
  • A stack that lists the extension, and a template you can read before any customer runs it.

Source: examples/extensions/postgres-iam-auth.

In your stack

alien.ts
import * as alien from "@alienplatform/core"
import postgresIamAuth from "./postgres-iam-auth/generated"

const db = new alien.Postgres("db").build()

export default new alien.Stack("my-app")
  .extensions([postgresIamAuth])
  .add(db, "frozen")
  .build()

Register the extension

postgres-iam-auth/hooks/src/lib.rs
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);
    ext.aws_interceptor(postgres_iam_auth::Rds);
    ext.gcp_interceptor(postgres_iam_auth::CloudSql);
    ext.azure_interceptor(postgres_iam_auth::FlexibleServer);
}

alien_extension_sdk::export!("postgres-iam-auth", register);

The flag

The flag is IAM database authentication. It is off by default on every cloud and leaves password login working, so Alien's own connection to the database keeps working:

CloudTemplate settingCreate call setting
AWSCloudFormation EnableIAMDatabaseAuthentication, Terraform iam_database_authentication_enabledEnableIAMDatabaseAuthentication on CreateDBCluster and ModifyDBCluster
Google CloudTerraform database_flags with cloudsql.iam_authentication = onsettings.databaseFlags on POST projects/*/instances
AzureTerraform authentication blockproperties.authConfig on the server PUT

Who creates the database decides which part sets the flag:

  • The setup template creates a frozen database, so the template modifiers set it. That covers pull deployments on EKS, GKE, and AKS, whose cloud template creates the database.
  • Alien's controllers create a live database. The interceptors set it on their create call. The setup CLI doesn't run interceptors, so it refuses this extension's release.

Modifiers

postgres-iam-auth/hooks/src/postgres_iam_auth.rs
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 same flag in the Terraform module, on AWS, Google Cloud and Azure.
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);
        }
        for instance in module.resources_of_type_mut("google_sql_database_instance") {
            let flag = instance.block_mut("settings")?.add_block("database_flags");
            flag.set_attribute("name", "cloudsql.iam_authentication");
            flag.set_attribute("value", "on");
        }
        // Microsoft Entra authentication needs the tenant, which Alien's server block doesn't
        // carry. The client config is the tenant Terraform applies the module in.
        let tenant_id = expr::traversal([
            "data",
            "azurerm_client_config",
            "postgres_iam_auth",
            "tenant_id",
        ]);
        let mut azure = false;
        for server in module.resources_of_type_mut("azurerm_postgresql_flexible_server") {
            server
                .add_block("authentication")
                .set_attribute("active_directory_auth_enabled", true)
                .set_attribute("password_auth_enabled", true)
                .set_attribute("tenant_id", tenant_id.clone());
            azure = true;
        }
        if azure {
            module.add_data_source("azurerm_client_config", "postgres_iam_auth")?;
        }
        Ok(module)
    }
}

The Terraform modifier edits Alien's own files in place, so the database_flags block lands inside Alien's settings block and keeps every value Alien wrote there. On Azure it adds an authentication block to azurerm_postgresql_flexible_server, and, in a new file postgres-iam-auth.tf, the azurerm_client_config data source the tenant id comes from. A module with no Azure server gets no data source.

Interceptors

Each interceptor runs inside the create call, before Alien signs it. The AWS one also matches ModifyDBCluster, which Alien sends when it adopts a cluster or changes its capacity:

postgres-iam-auth/hooks/src/postgres_iam_auth.rs
pub struct Rds;

impl AwsInterceptor for Rds {
    fn matches(&self, call: &AwsCall) -> bool {
        call.service() == "rds"
            && matches!(call.operation(), "CreateDBCluster" | "ModifyDBCluster")
    }

    fn modify_request(&self, request: &mut AwsRequest) -> Result<()> {
        request.set("EnableIAMDatabaseAuthentication", true);
        Ok(())
    }
}

An interceptor reaches a database only through a call it matches. A database that already exists gets the flag the next time Alien sends it one, and on Google Cloud and Azure these match create calls only.

Try it

Test and build

cd postgres-iam-auth
alien extensions test
alien extensions build

alien extensions test runs the hooks crate's tests natively and again in the WebAssembly module, and fails if a result differs. alien extensions build writes the bundle and generated/index.ts, the module your stack imports.

Read the template

cd ..
alien render --format cloudformation --stack alien.ts | grep EnableIAMDatabaseAuthentication
alien render --format terraform --target azure --stack alien.ts | grep -A 4 'authentication {'

The CloudFormation template has EnableIAMDatabaseAuthentication: true on the database cluster. On Azure, the module gains postgres-iam-auth.tf with the azurerm_client_config data source the tenant id comes from.

Next

On this page