Microsoft 365 Security as Code — Part 4: Deploying to a production tenant with almost no secrets
A pipeline that changes a production tenant is a good target. This is how ours signs in without stored secrets, holds admin roles only while a job runs, and what is still weak: the people who approve are also the people who can change the rules.
This is part 4 of a series on managing Microsoft 365 security as code, based on the CIS Microsoft 365 Foundations Benchmark. Each part can be read on its own, and all parts are listed on the series page.

A pipeline that can change the security settings of a production Microsoft 365 tenant is a very good target. If someone takes it over, they do not need to attack the tenant. They only need to use what the pipeline can already do. So the question for this part of the work was not "how do we log in", but "what does the pipeline hold, and for how long".
TLDR: the runners hold no passwords or keys. They sign in with a token that Kubernetes hands to the job, each repository runs on its own identity, and the admin roles that identity needs are switched on when a job starts and switched off when it ends. There is one stored secret left, and the people who can approve a deployment can also change the rules around it. I describe both below, because they are the parts that matter most.
No secrets: workload identity
Our pipelines run on GitLab runners on AKS. AKS can give each runner pod an identity through Azure workload identity federation. The pod does not get a password. It gets a short-lived token in a file, plus three environment variables that say which identity it is: the client ID, the tenant ID and the path of the token file. A webhook in the cluster adds them to the pod. They are not GitLab CI/CD variables, so there is nothing in GitLab to leak. The one tenant-related variable that I set in GitLab is the tenant's domain name, which is not a secret.
The script that signs in is short. It reads the token from the file, exchanges it for an Azure token, and asks for a token for the service it needs. This is the Exchange Online version:
$federatedToken = Get-Content -Path $env:AZURE_FEDERATED_TOKEN_FILE -Raw
Connect-AzAccount `
-ApplicationId $env:AZURE_CLIENT_ID `
-TenantId $env:AZURE_TENANT_ID `
-FederatedToken $federatedToken `
-Scope Process | Out-Null
# Exchange Online's app-only token resource
$tokenResponse = Get-AzAccessToken -ResourceUrl 'https://outlook.office365.com'
For Exchange Online, the token goes to Connect-ExchangeOnline -AccessToken, and for Microsoft365DSC it goes in the AccessTokens parameter. I do not use the -ManagedIdentity switch, even though it looks like the obvious choice. That switch asks the instance metadata service for a token, and a pod that uses workload identity does not have that service. The Terraform jobs use the same idea through OpenID Connect (ARM_USE_OIDC).
Tokens are fresh for every job (every job gets a new, fresh container), so nothing is cached between runs. And the script stops with a clear message when the three variables are missing, because that means the job did not land on a runner with the right identity.
One identity per job
In part 2 I explained that the repositories are split to limit the damage when one runner is compromised. In the pipeline, this is a single line. Every job names a runner tag, and each tag belongs to exactly one managed identity:
.dsc_runner: &dsc_runner
tags:
- exo
The Exchange Online jobs run on the exo runner and can use only what that runners' associated identity holds. The Conditional Access jobs run on a different runner with a different identity. A job in one repository cannot ask for the permissions of another repository by accident, because the identity comes from the runner and not from the code.
Admin roles only while a job runs
The identity of a runner needs more than API permissions. To change an Exchange Online or a SharePoint setting, it also needs an administrator role in Entra ID. Giving that role to the identity permanently would mean that anyone who gets code running on the runner has an admin role at any time of the day.
So the roles are eligible, not active. The pipeline switches a role on when a job starts, using Privileged Identity Management (PIM), and switches it off again when the job ends. A small, custom built, command-line tool in our job image does this. These are the two commands, with a justification text that records which project and which job asked:
pim elevate --client-id "$AZURE_CLIENT_ID" --role "$ELEVATE_ROLE" \
--duration "PT1H" --justification "M365 $CI_PROJECT_PATH job $CI_JOB_ID" \
--wait-for-activation
pim revoke --client-id "$AZURE_CLIENT_ID" --role "$ELEVATE_ROLE"
Which roles a job gets depends on the job. The Exchange Online pipeline activates the Exchange Administrator role. The SharePoint and Teams pipeline activates three roles for those services, and a separate role for the Defender for Cloud Apps jobs. The Conditional Access pipeline activates the Conditional Access Administrator role and a read-only security role. Nothing activates a role that its own job does not need. The runner identities hold no permanent roles in the tenant. The only thing they can do is switch on the roles they are eligible for, through two Graph permissions that allow it.
Some details make this safe in practice:
- Revoke twice. The first revoke is in a trap that runs when the job shell ends, including when a command fails. A second attempt runs in the
after_script, which starts a fresh shell, so it also works after a cancel or a timeout. If the runner itself disappears, the role still ends by itself when the hour is over. - Never overlap. Jobs that use the same identity share a GitLab
resource_group, so only one runs at a time. Without this, one job could revoke the role while another job still needs it. - Elevate inside the job. In one repository the plan and apply jobs wait for a person to start them. If the role were switched on in an earlier stage, it could run out while the job waited. So each job switches on its own role as its first step.
- Not interruptible. A job that is halfway through an apply is not cancelled by a newer pipeline.
Where the human is
Switching roles on and off is automatic. The original plan was to introduce a human approval when the pipeline PIM-elevates, but that is not possible when you use managed identities. The decision to deploy is manual. On the default branch, which is protected, the apply job is a manual job: it waits until a person starts it. Merge request pipelines only compile the configuration and write a drift report. A scheduled pipeline on the default branch checks for drift and never applies anything. A failed job also sends a message to a Teams channel.
A managed identity cannot approve a PIM activation. So I could not use the approval step that PIM itself offers for these roles, and I did not want to switch approvals off. The approval is in GitLab instead. The jobs that elevate run in a protected GitLab environment, and the approval rule on that environment says that only maintainers and owners of the repository can approve. The person who approves is therefore recorded in GitLab, next to the pipeline run that used the role.
What is still weak
This is an important part to take from this post.
The people who approve can also change the rules. A maintainer or owner can approve a deployment, and can also change the pipeline file, the protected branch settings and the approval rule itself. GitLab does not offer a way to stop that. The pipeline definition and the shared template are in a central place with the same problem. What I can do is make it visible. My plan is to stream the GitLab audit events to our security monitoring and raise an alert when an approval setting changes, when someone becomes a maintainer or owner, or when a person approves a deployment they started themselves. You can implement CODEOWNERS and set a CI/CD configuration file in CI/CD settings for a repo (in stead of using a .gitlab-ci.yaml file), there is a certain level of trust in the team, but it is important to understand that making these changes is possible.
One job has a strong role. The Entra ID settings pipeline activates a role that can manage role assignments. That is the most powerful job in the set, and it is the one where the approval and the audit trail matter most. Limiting each identity to one job reduces the damage of one compromised runner. It does not make every runner harmless.
Checking and applying use the same identity. Inside one repository, the job that checks for drift and the job that applies changes run on the same runner and activate the same role. The difference between them is the manual step and the protected branch, not a second identity. A separate read-only identity exists for the assessment in the reporting repository, and it holds only read permissions.
One secret is left. The policy interface of Defender for Cloud Apps does not accept the token of an application identity. For those jobs the pipeline reads an API token from a key vault, using the workload identity to do so. The token has an expiry date, and it is only sent to the policy endpoints.
Some permissions are still manual. The Exchange Online roles for the application identity cannot be created with the Entra ID Terraform provider, so they are created manually. Part 3 explains why this matters and how the pipeline checks for it.
If you are building this
- Use workload identity federation, and remove every secret that exists only to log in. Check where the pipeline still stores one, and write it down.
- Give each pipeline its own identity, and tie the identity to the runner, not to the code.
- Make admin roles eligible, and let the job switch them on and off. Revoke in more than one place, and let the role expire by itself as the last defence.
- Serialise jobs that share an identity.
- Put a person in front of every apply, and put the approval where you can see who gave it.
- Look at who can change the pipeline and the approval rules. If the answer is the same people who approve, add a way to see it when they do.
⚠️ We use Gitlab as our Git repository, with self-hosted runners. Both Github and Azure DevOps offer the same functionality for self-hosting runners.
In part 5 I move to the policies themselves: Conditional Access as code, and the places where a policy looks right but is not.
Get in touch
I help teams that need Microsoft 365 security to be written down as code, reviewed like code, and provable to an auditor, especially in financial services and other regulated environments. If you are moving a pipeline to workload identity, or you are working out how to give it admin rights safely, I would like to hear about it. Connect with me on LinkedIn, or reach out via ascode.nl.
This post is part of the series Microsoft 365 Security as Code, where you can find all the parts.