Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/terraform-apply.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ jobs:

- name: Terraform Apply
uses: dflook/terraform-apply@v1
# As in terraform-plan.yaml, but as devops-security-tf-apply, which only trusts
# refs/heads/main -- so a workflow_dispatch from any other branch can't log in.
env:
ARM_USE_OIDC: "true"
ARM_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID_APPLY }}
ARM_TENANT_ID: ${{ vars.AZURE_TENANT_ID }}
ARM_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }}
with:
path: terraform
backend_config_file: terraform/prod.backend.tfvars
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/terraform-plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ jobs:

- name: Terraform Plan
uses: dflook/terraform-plan@v1
# The Azure providers log in themselves with GitHub's OIDC token, as the hand-made
# devops-security-tf-plan identity (terraform/azure-ci-bootstrap.md). azure/login
# would not help: it signs in the runner's az CLI, which this container can't see.
env:
ARM_USE_OIDC: "true"
ARM_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID_PLAN }}
ARM_TENANT_ID: ${{ vars.AZURE_TENANT_ID }}
ARM_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }}
with:
path: terraform
backend_config_file: terraform/prod.backend.tfvars
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# DevOps Security | AWS IAM Resources
# DevOps Security | AWS and Azure Identity Resources

DevOps Security is a code repository used to maintain Hack for L.A.'s AWS Identity Access Management (IAM) resources as code. This includes users, groups, policies, and roles.
DevOps Security is a code repository used to maintain Hack for L.A.'s identity and access resources as code. On AWS that is Identity and Access Management (IAM): users, groups, policies, and roles. On Azure it is the managed identities other repos' CI logs in with, with their role assignments and Microsoft Graph permissions.

### Technology used

Expand Down
61 changes: 61 additions & 0 deletions terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion terraform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ Terraform directory structure
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | ~> 1.16.0 |
| <a name="requirement_archive"></a> [archive](#requirement\_archive) | ~> 2.8.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 6.64.0 |
| <a name="requirement_azapi"></a> [azapi](#requirement\_azapi) | ~> 2.14.0 |
| <a name="requirement_azuread"></a> [azuread](#requirement\_azuread) | ~> 3.10.0 |
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | ~> 5.15.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_archive"></a> [archive](#provider\_archive) | 2.8.1 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 6.64.0 |
| <a name="provider_azapi"></a> [azapi](#provider\_azapi) | 2.14.0 |
| <a name="provider_azuread"></a> [azuread](#provider\_azuread) | 3.10.0 |
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | 5.15.0 |

## Modules

Expand Down Expand Up @@ -102,8 +108,15 @@ Terraform directory structure
| [aws_s3_bucket_server_side_encryption_configuration.management_events](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
| [aws_s3_bucket_server_side_encryption_configuration.tf_backend_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
| [aws_ssm_parameter.user_bot_slack_token](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_parameter) | resource |
| [azapi_resource.incubator_ci_federated_credential](https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/resource) | resource |
| [azuread_app_role_assignment.incubator_ci](https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/app_role_assignment) | resource |
| [azurerm_resource_group.incubator_ci](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) | resource |
| [azurerm_role_assignment.incubator_ci](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) | resource |
| [azurerm_user_assigned_identity.incubator_ci](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/user_assigned_identity) | resource |
| [archive_file.user_bot_placeholder](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [azuread_service_principal.msgraph](https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/data-sources/service_principal) | data source |
| [azurerm_subscription.current](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/subscription) | data source |

## Inputs

Expand All @@ -113,5 +126,7 @@ Terraform directory structure

## Outputs

No outputs.
| Name | Description |
|------|-------------|
| <a name="output_incubator_azure_ci_client_ids"></a> [incubator\_azure\_ci\_client\_ids](#output\_incubator\_azure\_ci\_client\_ids) | Client IDs of incubator's Azure CI identities, keyed plan/apply. |
<!-- END_TF_DOCS -->
119 changes: 119 additions & 0 deletions terraform/azure-ci-identities.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// This file declares the Azure identities used by hackforla/incubator CI: the Azure
// counterpart of incubator-tf-plan and incubator-tf-apply in aws-gha-oidc-providers.tf.
//
// The two identities used by THIS repo's CI -- devops-security-tf-plan and
// devops-security-tf-apply, in resource group terraform-ci -- are deliberately NOT
// here. They were created by hand on 2026-11-30 and are tagged managed-by=exempt, for
// the same bootstrap reason as the AWS roles: the workflow that would run this
// Terraform has to log in as them first. They are documented in
// azure-ci-bootstrap.md. Do not "fix" their absence by adding them below, and do not
// put anything declared here into terraform-ci.
//
// Everything is skipped unless var.iam_only is false, like cloudtrail.tf: these exist
// only in Hack for LA's Azure tenant. See hackforla/devops-security#238.

locals {
azure_enabled = !var.iam_only

// azurerm has no default_tags, so every taggable Azure resource takes this.
azure_tags = {
managed-by = "terraform-devops-security"
}

// GitHub's numeric IDs for hackforla/incubator and the hackforla org
// (gh api repos/hackforla/incubator --jq '.id, .owner.id'). Checking them stops a
// deleted-and-recreated repo with the same name, or the repo transferred out of
// hackforla, from logging in.
incubator_repo_ids = "claims['repository_id'] eq '348201813' and claims['repository_owner_id'] eq '11635254'"

// Mirrors the AWS roles. Plan trusts any ref and pull requests; apply trusts main
// only, because incubator's apply has admin rights.
incubator_ci_identities = local.azure_enabled ? {
plan = "claims['sub'] matches 'repo:*' and ${local.incubator_repo_ids}"
apply = "claims['sub'] eq 'repo:hackforla/incubator:ref:refs/heads/main' and ${local.incubator_repo_ids}"
} : {}

incubator_ci_azure_roles = local.azure_enabled ? {
"apply-contributor" = { identity = "apply", role = "Contributor" }
"apply-rbac-admin" = { identity = "apply", role = "Role Based Access Control Administrator" }
"plan-reader" = { identity = "plan", role = "Reader" }
"plan-kv-secrets" = { identity = "plan", role = "Key Vault Secrets User" }
} : {}

// Microsoft Graph application permissions. apply can create the shared app
// registration and manage the ones it owns; plan can read them to refresh state.
incubator_ci_graph_roles = local.azure_enabled ? {
"apply-app-readwrite-ownedby" = { identity = "apply", permission = "Application.ReadWrite.OwnedBy" }
"plan-app-read-all" = { identity = "plan", permission = "Application.Read.All" }
} : {}
}

data "azurerm_subscription" "current" {
count = local.azure_enabled ? 1 : 0
}

resource "azurerm_resource_group" "incubator_ci" {
count = local.azure_enabled ? 1 : 0
name = "incubator-ci"
location = "westus2"
tags = local.azure_tags
}

resource "azurerm_user_assigned_identity" "incubator_ci" {
for_each = local.incubator_ci_identities
name = "incubator-tf-${each.key}"
resource_group_name = azurerm_resource_group.incubator_ci[0].name
location = azurerm_resource_group.incubator_ci[0].location
tags = local.azure_tags
}

// Flexible federated identity credentials match an expression instead of one exact
// subject. Neither azurerm nor the CLI can create them yet, so this goes straight to
// the ARM API. Schema validation is off because azapi's bundled schema may not know
// claimsMatchingExpression. Credentials can't be tagged.
resource "azapi_resource" "incubator_ci_federated_credential" {
for_each = local.incubator_ci_identities
type = "Microsoft.ManagedIdentity/userAssignedIdentities/federatedIdentityCredentials@2025-05-31-preview"
name = "github-incubator"
parent_id = azurerm_user_assigned_identity.incubator_ci[each.key].id

schema_validation_enabled = false

body = {
properties = {
issuer = "https://token.actions.githubusercontent.com"
audiences = ["api://AzureADTokenExchange"]
claimsMatchingExpression = {
value = each.value
languageVersion = 1
}
}
}
}

resource "azurerm_role_assignment" "incubator_ci" {
for_each = local.incubator_ci_azure_roles
scope = data.azurerm_subscription.current[0].id
role_definition_name = each.value.role
principal_id = azurerm_user_assigned_identity.incubator_ci[each.value.identity].principal_id
principal_type = "ServicePrincipal"
}

data "azuread_service_principal" "msgraph" {
count = local.azure_enabled ? 1 : 0
client_id = "00000003-0000-0000-c000-000000000000" // Microsoft Graph
}

resource "azuread_app_role_assignment" "incubator_ci" {
for_each = local.incubator_ci_graph_roles
app_role_id = data.azuread_service_principal.msgraph[0].app_role_ids[each.value.permission]
principal_object_id = azurerm_user_assigned_identity.incubator_ci[each.value.identity].principal_id
resource_object_id = data.azuread_service_principal.msgraph[0].object_id
}

// hackforla/incubator's AZURE_CLIENT_ID_PLAN and AZURE_CLIENT_ID_APPLY repository
// variables are set by hand from these; this repo has no GitHub provider.
output "incubator_azure_ci_client_ids" {
description = "Client IDs of incubator's Azure CI identities, keyed plan/apply."
value = { for k, v in azurerm_user_assigned_identity.incubator_ci : k => v.client_id }
}
34 changes: 34 additions & 0 deletions terraform/backend.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ terraform {
source = "hashicorp/archive"
version = "~> 2.8.0"
}
// Azure resources and Entra (Microsoft Graph) grants; see azure-ci-identities.tf.
azurerm = {
source = "hashicorp/azurerm"
version = "~> 5.15.0"
}
azuread = {
source = "hashicorp/azuread"
version = "~> 3.10.0"
}
// Only for flexible federated identity credentials, which azurerm cannot create yet.
azapi = {
source = "Azure/azapi"
version = "~> 2.14.0"
}
}
}

Expand All @@ -32,3 +46,23 @@ provider "aws" {
}
}
}

// Azure. Tenant, subscription and identity come from the environment: in CI the
// workflows set ARM_USE_OIDC, ARM_CLIENT_ID, ARM_TENANT_ID and ARM_SUBSCRIPTION_ID;
// locally the providers fall back to `az login`. They sign in as soon as Terraform
// configures them, even when iam_only skips every Azure resource, so a local run needs
// an Azure login. See terraform/azure-ci-bootstrap.md for the identities CI uses.
//
// azurerm has no default_tags, so Azure resources take local.azure_tags explicitly
// (azure-ci-identities.tf).
provider "azurerm" {
features {}
// The plan identity is Reader and cannot register resource providers, which azurerm
// otherwise tries to do when it starts. The subscription's providers are registered
// by hand (Microsoft.ManagedIdentity is).
resource_provider_registrations = "none"
}

provider "azuread" {}

provider "azapi" {}