diff --git a/.github/workflows/terraform-apply.yaml b/.github/workflows/terraform-apply.yaml index 9b529c4..497deb3 100644 --- a/.github/workflows/terraform-apply.yaml +++ b/.github/workflows/terraform-apply.yaml @@ -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 diff --git a/.github/workflows/terraform-plan.yaml b/.github/workflows/terraform-plan.yaml index 9caeca2..a5aa294 100644 --- a/.github/workflows/terraform-plan.yaml +++ b/.github/workflows/terraform-plan.yaml @@ -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 diff --git a/README.md b/README.md index 82645e9..012c768 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/terraform/.terraform.lock.hcl b/terraform/.terraform.lock.hcl index f9d0f79..1a50ad7 100644 --- a/terraform/.terraform.lock.hcl +++ b/terraform/.terraform.lock.hcl @@ -1,6 +1,27 @@ # This file is maintained automatically by "terraform init". # Manual edits may be lost in future updates. +provider "registry.terraform.io/azure/azapi" { + version = "2.14.0" + constraints = "~> 2.14.0" + hashes = [ + "h1:swCDLD5HqZVL56GuDHM26qiAPKARaxgXzdFbwDVjRVs=", + "zh:037eecc3d75bd594c16b911ce5e7590fa8c81aa3e5a0c35c74da6d2533e401f6", + "zh:1934365aac45ce429b9fd388e420a668516a199218c55266385c50b0bccd346d", + "zh:506c6ad3c6a822c33ea57adf3f2216d7246131ac41b6a1dad51ae1a749d8d8fd", + "zh:5748071f01672e68a77898c07eb9f0b1bc5a83563d7f3d7464bc113bebc61dd1", + "zh:5b29d4767193fd159f537aa210d4d0440eea5b56509ea419ae23d2016e45529d", + "zh:737ce76417c32b92a75d037b1e0ee0ac79bc430987788fbb5d29747e9dd78f57", + "zh:7e2d85cac00a2b044e7cf543ea7157a26a826b721f74e6b3858cc301db605d92", + "zh:8394cdf5e9a0c2e3a52677329b0d2da3956fadafc0a197b1ad7f67c0c08b72ca", + "zh:96d02bc9def3b76d36e65dad2609cb15272f83bf7aa4b140b738e7ce2a95aba2", + "zh:ade67a7debca02e8188aea671b0a613e796921c2d4f8ee8fefe3e73808a36735", + "zh:b8d4521a888033e56d468073618daddf04c5cbe3b4b2b3d7454bfe7b939a273e", + "zh:bd80d0fec4ed4fa678f57eac02c95e77ba6d4a21b0348afff31d84942eea0795", + "zh:e08c953a5f4e289da0e6d0d7df4b73d8dccfee08ee6c7ade759db9ea8436fded", + ] +} + provider "registry.terraform.io/hashicorp/archive" { version = "2.8.1" constraints = "~> 2.8.0" @@ -51,6 +72,46 @@ provider "registry.terraform.io/hashicorp/aws" { ] } +provider "registry.terraform.io/hashicorp/azuread" { + version = "3.10.0" + constraints = "~> 3.10.0" + hashes = [ + "h1:EpQO5p0pkCpJF4mjzYOCKdReCa91Wysre9hv06oyPlM=", + "zh:0305d94e8cdba6c9fa868a0e6b63b59f5e85cd9745915542ac90d0411a1fcb41", + "zh:0e434655f525cc85a3f7aca969e394553305fee6d9d0ac5b57fba087d86c221f", + "zh:1c3e89cf19118fc07d7b04257251fc9897e722c16e0a0df7b07fcd261f8c12e7", + "zh:2b6f0592a5b31aaa4d12328fd902aaee6a03852643002756e7a58f00b0999688", + "zh:31269f45aa2c66454d29be24db2e2e809567bfab1fb2b233e4f473f55afe12e3", + "zh:588764a41cc2ebfd06798c72bf8ded88c618556e34953e6981d042671373e824", + "zh:5c9fdff0c142686f455dc4e93e1161eee0cbe898bcdf77440415d29c29eedd65", + "zh:9087fe2842d4c9cbb10e58a679cf2ed1fe04528411cb460e6c44112205719a70", + "zh:9cb71e517138d226b2e0b5d866ce7fc975a83d9df0908be0aa12b75dcfee9d09", + "zh:c6c9ef3caf8981bf69c92ef0403b9a085a6ead84681c024d4b9c967103bcd2e1", + "zh:e861fd87703d417f6c2c27cc2ef62867b0da13b301df3762f9f298ab8f6bf1aa", + "zh:f3a02ba7b5c6a300a0bffd19cb73932c6ef33bdcc9fb78d8ff289353c9646941", + ] +} + +provider "registry.terraform.io/hashicorp/azurerm" { + version = "5.15.0" + constraints = "~> 5.15.0" + hashes = [ + "h1:RDyfMQwcKUUKKGuu0dJTt9JkKz8OMMRLPi1doBclJXc=", + "zh:1dc25f28a470492c42586427dc601d12833cba72136b61066a198cd7a13ac10b", + "zh:3393b79a26615a29486fc9fb4d05625c6f60a575f766994703abd916214ffc03", + "zh:3b4f159002965ef07645f18beea345241dea7be7359db4758d7d0550ea482cb5", + "zh:565585061e53d32ab3a37cdb95fdbe3f7fd49e559b166bc16a76f555eb325914", + "zh:5f017fee7db02d5e5c7a0c62b4b8649d3ecadfc765fe2bf6e465ffbea958ae2c", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:c090f197ebd1e0f38fe43a8dadeaa8101c7117881dd159b26cb80c691a91a886", + "zh:d1e75a916d4dca406843ec240b22849da0e8819ce6e0e4a3cf2ce15ca80cc06a", + "zh:de4d7afa1f7827b9dda97c2c4f03486d08ec4c574eddb75f623bc10d71fd2ab2", + "zh:ee9b22544b1d972908f34102a6a4e05af0768fefa69097c7449ec3435452adfb", + "zh:f51f5d024b01bad21b0e20e679de8d52e5b5b7fcb088319e453354fae2e589a2", + "zh:ff6b214af8401f6f604c0c59cba17fa2c0e055694aaaf01b4ed60331665fba24", + ] +} + provider "registry.terraform.io/hashicorp/tls" { version = "4.4.0" hashes = [ diff --git a/terraform/README.md b/terraform/README.md index 2f9c122..99b9773 100644 --- a/terraform/README.md +++ b/terraform/README.md @@ -34,6 +34,9 @@ Terraform directory structure | [terraform](#requirement\_terraform) | ~> 1.16.0 | | [archive](#requirement\_archive) | ~> 2.8.0 | | [aws](#requirement\_aws) | ~> 6.64.0 | +| [azapi](#requirement\_azapi) | ~> 2.14.0 | +| [azuread](#requirement\_azuread) | ~> 3.10.0 | +| [azurerm](#requirement\_azurerm) | ~> 5.15.0 | ## Providers @@ -41,6 +44,9 @@ Terraform directory structure |------|---------| | [archive](#provider\_archive) | 2.8.1 | | [aws](#provider\_aws) | 6.64.0 | +| [azapi](#provider\_azapi) | 2.14.0 | +| [azuread](#provider\_azuread) | 3.10.0 | +| [azurerm](#provider\_azurerm) | 5.15.0 | ## Modules @@ -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 @@ -113,5 +126,7 @@ Terraform directory structure ## Outputs -No outputs. +| Name | Description | +|------|-------------| +| [incubator\_azure\_ci\_client\_ids](#output\_incubator\_azure\_ci\_client\_ids) | Client IDs of incubator's Azure CI identities, keyed plan/apply. | diff --git a/terraform/azure-ci-identities.tf b/terraform/azure-ci-identities.tf new file mode 100644 index 0000000..880a178 --- /dev/null +++ b/terraform/azure-ci-identities.tf @@ -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 } +} diff --git a/terraform/backend.tf b/terraform/backend.tf index cae7150..bb8cbe6 100644 --- a/terraform/backend.tf +++ b/terraform/backend.tf @@ -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" + } } } @@ -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" {}