// Why this matters
If you manage Entra ID app registrations with Terraform and GitHub OIDC, the pipeline identity is the most privileged non-human in your tenant, and it is the thing attackers want. Most teams give it a client secret and tenant-wide write permission because that is the path of least resistance. Both choices are avoidable.
The engineering goal here is to make the factory incapable of holding a long-lived credential and incapable of changing anything it did not create. Those two properties together turn "compromise the pipeline" from a tenant takeover into a contained incident.
The business benefit is mundane but real: SSO and CI registrations stop being hand-built in a portal, so every app has a reviewed definition, a named owner and a predictable set of roles.
// The problem
Registrations were clicked together in the portal. Credentials were created ad hoc and pasted into whatever needed them, and nobody could say how many secrets existed or when they would expire. When automation finally replaced the portal, it inherited the same tenant-wide write power the humans had.
There is a circular dependency to solve too. The identity that creates registrations must itself be created by something, and that something cannot be the factory. Mixing both in one stack is how people end up with a "temporary" admin secret that lives for years.
Finally, Graph permissions are coarse. Application.ReadWrite.All can reset the credentials of any app in the tenant, including those with directory-wide privileges. That is effectively a privilege-escalation primitive handed to a pipeline.
// The solution
Two stacks, deliberately separate. The bootstrap stack is tiny: one application, one service principal, one federated credential and the Graph permission grant. It is applied rarely and by a human with admin rights. The factory stack is applied by the automation identity itself, through CI, and contains all the app declarations.
The federated credential trusts GitHub's OIDC issuer for one exact subject: a repository plus a protected environment. No wildcards, no branch patterns, no pull-request subject. A workflow run that is not inside that environment simply cannot obtain a token, and the environment requires a human reviewer.
The Graph permission is Application.ReadWrite.OwnedBy. The identity can create applications and then, as their owner, manage them; it cannot read or modify apps it does not own. The trade-off is that every app lists the automation identity as an owner, which is documented and detected rather than hidden. Role management, PIM and user-write permissions are explicitly out of scope.
For plan, a custom role at the tenant root grants only the read actions Terraform refresh requires. The built-in Reader role was rejected as broader than needed. Outputs for in-cluster consumers are written to the secrets manager per environment, so the registration factory never needs a place to keep a secret itself. See Watching the Watchers: Identity-Tampering Detection with ChatOps Triage for the rules that watch this identity.
- Bootstrapseparate stack creates the automation identity, avoiding a circular dependency
- Federated trustcredential bound to repo and environment subject claim
- Graph consentapplication permissions granted once by an admin
- Declare appmodule call adds app, roles, groups and redirect URIs; owner is the automation identity
- Planruns with a custom read-only role
- Reviewer approval gaterequired reviewer on the environment
- Applyapp, service principal, groups and role assignments created
- Publish credentialsclient details written to Vault per environment
- Detectdetection rules watch the automation identity itself
// Before you start
- Terraform 1.5+ with azuread and azurerm providers
- A Global Administrator or Privileged Role Administrator for the one-time Graph consent
- A GitHub repository with an environment named for production and required reviewers configured
- A secrets manager reachable from CI (Vault in the example) for publishing client details
- Understanding of OIDC subject claims:
repo:<org>/<repo>:environment:<name>
// Step-by-step implementation
01 Bootstrap the automation identity in its own stack
Create the application and service principal that will run the factory. Keep this stack separate, with its own state and its own (human-approved) apply, so the factory can never modify its own trust.
Do not create a password resource. The absence of
azuread_application_passwordis the design.02 Pin a federated credential to one repo and one environment
The subject string is the entire security boundary. Use the environment form, not the branch form: branch protection can be changed by repo admins, but an environment with required reviewers adds a human to the chain.
Use the exact subject. A wildcard such as
repo:<org>/*would let any repository in the organisation mint tokens for this identity.03 Grant Graph consent once, and a custom read-only role
Application permissions need admin consent. Do it as a one-time step in the bootstrap stack with
azuread_app_role_assignment, which records the grant as code rather than as a portal click.For Azure RBAC, define a custom role containing only the read actions that
terraform planneeds on the resources in this stack, and assign it at the tenant root group. Resist the shortcut of Reader: it exposes far more than refresh requires.04 Write the app module and make the identity the owner
The module declares an application, its service principal, optional app roles, group assignments and redirect URIs. Crucially it sets
ownersto include the automation identity, because OwnedBy permission only works for apps it owns.Callers pass a small object. Everything else (sign-in audience, token version, group claims) is an opinionated default, which is what keeps dozens of registrations consistent.
05 Declare the apps, and publish client details to the secrets manager
Each registration is one module call. For apps that need in-cluster credentials, write the client ID and tenant to the secrets manager under a per-environment path. The consumer reads from there, so the factory never needs to expose anything in a log or output.
Where a client secret is genuinely required by a third-party SSO integration, generate it in Terraform, write it straight to the secrets manager as a sensitive value and set a short expiry. It is the app's secret, never the automation identity's.
06 Wire the pipeline to the environment and add a backfill script
The workflow requests an OIDC token only inside the protected job. The
environment:line is what changes the subject claim, so it must match the credential in step two exactly.Existing apps that predate the factory need the identity added as an owner before it can manage them. Do that once with a script, support
--dry-run, and keep the output in the PR so a reviewer can see exactly what changes.
// How to verify it works
- Decode a token from a run outside the environment (for example from a branch): the login step must fail with an AADSTS subject-mismatch error.
- Run
az ad app credential list --id <factory-app-id>: it must return an empty list. - Attempt to patch an app the factory does not own with the factory token: Graph must return 403.
- Run the backfill with
--dry-runfirst and confirm it lists only the apps you expect. - Inspect the Vault path: client ID and tenant ID are present and no secret value appears in workflow logs.
// Design decisions and trade-offs
- Application.ReadWrite.OwnedBy instead of ReadWrite.All limits the blast radius to apps it owns; the trade-off (every app lists the identity as owner) is documented.
- Role management, PIM and user-write permissions are explicitly forbidden.
- A custom tenant-root role carries only the read actions Terraform refresh needs, instead of Reader.
- No wildcard federated credentials and no client secret on the automation identity.
- A one-time ownership backfill script supports --dry-run; a risk assessment and break-glass procedure are documented.
// Pitfalls and lessons learned
- Federated credentials match the subject exactly and case-sensitively; a trailing difference in the environment name produces a vague token error.
- OwnedBy permission silently does nothing on apps the identity does not own. If an update is a 403 on an old app, it is an ownership problem, not a permission problem.
- App role IDs generated with random UUIDs change on recreate and break every assignment. Make them deterministic.
- Granting consent in the same apply that uses the permission races; keep consent in the bootstrap stack.
- Do not allow pull requests from forks to use the write-capable identity. Use a separate read-only plan identity and environment.
- Owner lists become noisy once every app names the automation identity. Document the trade-off and detect owner changes by rule instead.
// Frequently asked questions
How do I use GitHub OIDC with Terraform and Entra ID?
Create a federated identity credential on an application with issuer https://token.actions.githubusercontent.com and a subject such as repo:org/repo:environment:name, then log in with azure/login using the client ID and no secret.
What is Application.ReadWrite.OwnedBy?
A Graph application permission that lets an identity manage only the app registrations it owns, unlike ReadWrite.All which covers every app in the tenant.
Can I avoid owners on every app?
Not with OwnedBy. If that is unacceptable you need ReadWrite.All and stronger compensating controls, which is a worse trade for most tenants.
Why separate bootstrap and factory stacks?
So the automation can never edit its own trust, and the stack that holds the privileged grants is applied rarely and by a human.
Do I still need secret rotation?
For the automation identity, no, because it has none. For third-party SSO secrets, use short expiries and publish them straight to a secrets manager.
// The outcome
About 34 module invocations owned by an identity that holds no secret.
// Where to take it next
- Add detection rules for any credential, owner or federation change on the factory identity, as in Watching the Watchers: Identity-Tampering Detection with ChatOps Triage.
- Use the same federation pattern for other pipelines, as in One Config, Two Clouds: Group and Role Management as Code.
- Move expiring third-party secrets to certificates or federation where the vendor allows.
- Document a break-glass procedure and rehearse it.