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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ DevOps Security is a code repository used to maintain Hack for L.A.'s AWS Identi
- [Terraform](https://www.terraform.io/)
- [Terraform Docs](https://terraform-docs.io/)
- [AWS CLI](https://aws.amazon.com/cli/)
- [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/)

The Azure identities this repo's Terraform workflows log in with were made by hand and aren't in Terraform. See [terraform/azure-ci-bootstrap.md](terraform/azure-ci-bootstrap.md).

# How to contribute

Expand Down
147 changes: 147 additions & 0 deletions terraform/azure-ci-bootstrap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Azure CI identities (made by hand)

This repo's Terraform workflows log in to Azure as two user-assigned managed identities. They were
created by hand on 2026-11-30 ([#237](https://github.com/hackforla/devops-security/issues/237)) and are
**not in Terraform**. This page is the only record of them: what exists, why, how to recreate it and how
to check it.

## Why they aren't in Terraform

The workflow that runs this repo's Terraform has to log in as these identities before it can do
anything, so the Terraform can't be what creates them. It's the same bootstrap problem as the AWS
roles `devops-security-tf-plan` and `devops-security-tf-apply`, which were also made by hand
([#182](https://github.com/hackforla/devops-security/issues/182)); the header of
[`aws-gha-oidc-providers.tf`](aws-gha-oidc-providers.tf) explains it for AWS.

Everything here is tagged `managed-by = exempt`, like those AWS roles. **Don't move these identities into
Terraform.** The identities hackforla/incubator's CI uses are a different matter: this repo's Terraform
creates those, in their own resource group `incubator-ci`
([#238](https://github.com/hackforla/devops-security/issues/238)).

## What exists

The tenant ID, subscription ID and both client IDs are this repo's Actions variables `AZURE_TENANT_ID`,
`AZURE_SUBSCRIPTION_ID`, `AZURE_CLIENT_ID_PLAN` and `AZURE_CLIENT_ID_APPLY`. They identify the
identities but aren't secret, so they're variables, not secrets.

| Resource | Details |
|---|---|
| Resource group `terraform-ci` | West US 2. Holds only these two identities. Don't put anything Terraform manages in it. |
| Identity `devops-security-tf-plan` | Used by `terraform-plan.yaml`. Read-only. |
| Identity `devops-security-tf-apply` | Used by `terraform-apply.yaml`. Can create identities and assign roles. |

Each identity has one federated identity credential, `github-devops-security`, trusting GitHub Actions
(issuer `https://token.actions.githubusercontent.com`, audience `api://AzureADTokenExchange`). They are
**flexible** credentials, which match an expression instead of one exact subject:

| Identity | Expression |
|---|---|
| plan | `claims['sub'] matches 'repo:*' and claims['repository_id'] eq '744797221' and claims['repository_owner_id'] eq '11635254'` |
| apply | `claims['sub'] eq 'repo:hackforla/devops-security:ref:refs/heads/main' and claims['repository_id'] eq '744797221' and claims['repository_owner_id'] eq '11635254'` |

- `repository_id` and `repository_owner_id` are GitHub's numeric IDs for this repo and for hackforla. They
stop a deleted-and-recreated repo with the same name, or this repo transferred out of hackforla, from
logging in.
- **Apply only trusts `main`, and that matters.** `terraform-apply.yaml`'s manual trigger applies without
a reviewed plan ([#187](https://github.com/hackforla/devops-security/issues/187)). Because apply only
trusts `main`, a manual run from any other branch is refused at login. Don't widen it.

Permissions:

| Identity | Azure roles (subscription scope) | Microsoft Graph application permissions |
|---|---|---|
| plan | Reader | `Application.Read.All` |
| apply | Contributor, Role Based Access Control Administrator | `AppRoleAssignment.ReadWrite.All`, `Application.Read.All` |

Apply needs `AppRoleAssignment.ReadWrite.All` to give incubator's identities their own Graph permissions.
It also lets apply grant any Graph permission, including to itself. That was accepted, on the same footing
as the AWS apply role's `AdministratorAccess`.

## How to recreate it

Needs `az login` as a **Global Administrator** of the tenant (subscription Owner isn't enough for the
Graph grants) and admin rights on this repo for `gh variable set`. Run in Git Bash. Two traps there:

- Set `MSYS_NO_PATHCONV=1`, or Git Bash rewrites `/subscriptions/...` arguments into Windows paths.
- Then write any `az rest --body @file` file to a Windows-form path (`cygpath -m`). `az` can't read Git
Bash's `/tmp`, and instead of failing it sends the file name as the request body.

The Azure CLI and the Terraform providers can't create flexible credentials yet, so they go through the
ARM API with `az rest`.

```bash
export MSYS_NO_PATHCONV=1
TENANT=<tenant id>; SUB=<subscription id>; RG=terraform-ci
REPO_ID=744797221; OWNER_ID=11635254 # gh api repos/hackforla/devops-security --jq '.id, .owner.id'
WORK=$(cygpath -m "$(mktemp -d)")
az account set --subscription "$SUB"

az group create -n $RG -l westus2 --tags managed-by=exempt
for w in plan apply; do
az identity create -g $RG -n devops-security-tf-$w -l westus2 --tags managed-by=exempt
done
pid() { az identity show -g $RG -n devops-security-tf-$1 --query principalId -o tsv; }
cid() { az identity show -g $RG -n devops-security-tf-$1 --query clientId -o tsv; }

# Federated credentials
IDS="claims['repository_id'] eq '$REPO_ID' and claims['repository_owner_id'] eq '$OWNER_ID'"
fic() {
python -c "import json,sys; print(json.dumps({'properties':{'issuer':'https://token.actions.githubusercontent.com','audiences':['api://AzureADTokenExchange'],'claimsMatchingExpression':{'value':sys.argv[1],'languageVersion':1}}}))" "$2 and $IDS" > "$WORK/fic.json"
az rest --method put --headers Content-Type=application/json --body @"$WORK/fic.json" \
--url "https://management.azure.com/subscriptions/$SUB/resourceGroups/$RG/providers/Microsoft.ManagedIdentity/userAssignedIdentities/devops-security-tf-$1/federatedIdentityCredentials/github-devops-security?api-version=2025-05-31-preview"
}
fic plan "claims['sub'] matches 'repo:*'"
fic apply "claims['sub'] eq 'repo:hackforla/devops-security:ref:refs/heads/main'"

# Azure roles
for r in "Contributor" "Role Based Access Control Administrator"; do
az role assignment create --assignee-object-id "$(pid apply)" --assignee-principal-type ServicePrincipal --role "$r" --scope "/subscriptions/$SUB"
done
az role assignment create --assignee-object-id "$(pid plan)" --assignee-principal-type ServicePrincipal --role Reader --scope "/subscriptions/$SUB"

# Graph permissions
GRAPH=00000003-0000-0000-c000-000000000000
GRAPH_SP=$(az ad sp show --id $GRAPH --query id -o tsv)
grant() {
local rid; rid=$(az ad sp show --id $GRAPH --query "appRoles[?value=='$2'].id | [0]" -o tsv)
printf '{"principalId":"%s","resourceId":"%s","appRoleId":"%s"}' "$(pid $1)" "$GRAPH_SP" "$rid" > "$WORK/ara.json"
az rest --method post --headers Content-Type=application/json --body @"$WORK/ara.json" \
--url "https://graph.microsoft.com/v1.0/servicePrincipals/$(pid $1)/appRoleAssignments"
}
grant apply AppRoleAssignment.ReadWrite.All
grant apply Application.Read.All
grant plan Application.Read.All

# Repository variables
gh variable set AZURE_TENANT_ID -R hackforla/devops-security --body "$TENANT"
gh variable set AZURE_SUBSCRIPTION_ID -R hackforla/devops-security --body "$SUB"
gh variable set AZURE_CLIENT_ID_PLAN -R hackforla/devops-security --body "$(cid plan)"
gh variable set AZURE_CLIENT_ID_APPLY -R hackforla/devops-security --body "$(cid apply)"
```

Recreating an identity gives it a new client ID and principal ID, so update the two `AZURE_CLIENT_ID_*`
variables afterwards. Anything that references the old principal ID breaks, including role assignments
that #238's Terraform makes.

## How to check it

Read-only:

```bash
export MSYS_NO_PATHCONV=1; SUB=<subscription id>
az identity list -g terraform-ci --query "[].[name, tags.\"managed-by\"]" -o tsv
for w in plan apply; do
P=$(az identity show -g terraform-ci -n devops-security-tf-$w --query principalId -o tsv)
az rest --method get --url "https://management.azure.com/subscriptions/$SUB/resourceGroups/terraform-ci/providers/Microsoft.ManagedIdentity/userAssignedIdentities/devops-security-tf-$w/federatedIdentityCredentials?api-version=2025-05-31-preview" \
--query 'value[].properties.claimsMatchingExpression.value' -o tsv
az role assignment list --assignee $P --all --query '[].roleDefinitionName' -o tsv
# 9a5d68dd-52b0-4cc2-bd40-abcf44ac3a30 = Application.Read.All
# 06b708a9-e830-4db3-a914-8e69da51d44f = AppRoleAssignment.ReadWrite.All
az rest --method get --url "https://graph.microsoft.com/v1.0/servicePrincipals/$P/appRoleAssignments" --query 'value[].appRoleId' -o tsv
done
```

Logins were tested on 2026-11-30 from a throwaway branch: plan logged in, and apply was refused with
`AADSTS7002131: No matching federated identity record found for presented assertion subject
'repo:hackforla/devops-security:ref:refs/heads/237-azure-login-test'`. Apply's successful login from
`main` is proven by the first apply run after #238 merges.