Chapter 7 · This site
Zero Stored Secrets: How I Wired Entra ID Sign-In to a Managed Identity
I've spent a good chunk of my career cleaning up after expired secrets. At a previous job, service principal secrets quietly expiring caused P1 incidents until we automated expiry notifications across Key Vault, Event Grid, a Logic App, and ServiceNow. It works, but it's a workaround for a problem that shouldn't exist in the first place: a secret that has to be rotated is a secret that will eventually be forgotten.
So when I built my own portfolio site — a small Flask app with one gated page behind Entra ID sign-in — I set myself a constraint: no stored client secret, anywhere, for anything. Not in Key Vault, not in an App Setting, not rotated on a timer. Here's how that's actually possible, and the mechanism that makes it work: Managed Identity as a Federated Identity Credential.
The problem with "just use a client secret"
A confidential OAuth2 client — an app that signs users in via the Authorization Code flow — normally has two options for proving its own identity to the identity provider during the token exchange:
- A client secret: a string, generated once, that has to be stored somewhere the app can read it, and rotated before it expires.
- A client certificate: same problem, different file format.
Azure App Service's Easy Auth (auth_settings_v2) only supports these two.
Its configuration takes a client_id and a client_secret_setting_name —
a name pointing at an App Setting holding either a secret or a certificate
reference. There's no third option in that schema. If you want to
eliminate the stored secret entirely, Easy Auth can't get you there.
The actual mechanism: a managed identity as the credential
Azure has a much less commonly discussed feature: an App Registration can trust a federated identity credential instead of a stored secret. This is the same mechanism GitHub Actions and Azure DevOps use for secretless deployment — the identity provider (GitHub/ADO) issues a short-lived OIDC token, and your App Registration is configured to trust tokens from that specific issuer instead of checking a static secret.
The part that's less obvious: the external issuer doesn't have to be GitHub or ADO. It can be Entra ID itself, issuing a token on behalf of your own Azure resource — specifically, a user-assigned managed identity. That identity becomes, functionally, a live, auto-rotating, non-exportable "credential" that your App Registration trusts.
Concretely, three pieces:
-
A user-assigned managed identity, attached to the App Service alongside its own system-assigned identity (which stays scoped to ordinary things like Key Vault and Application Insights access — a separate, unrelated identity from the one doing the sign-in work).
-
A federated identity credential on the App Registration, trusting that managed identity's principal ID as the subject:
hcl
resource "azuread_application_federated_identity_credential" "main" {
application_id = azuread_application.main.id
display_name = "app-mi-fic"
audiences = ["api://AzureADTokenExchange"]
issuer = "https://login.microsoftonline.com/<tenant-id>/v2.0"
subject = "<managed-identity-principal-id>"
}
The issuer here is Entra ID's own tenant endpoint — not GitHub's, not
ADO's — because the token being trusted is one Entra ID issues about
your own managed identity.
- App code that fetches a token from the managed identity and hands it to MSAL as the client assertion, instead of a secret string:
```python from azure.identity import ManagedIdentityCredential import msal
def client_assertion(): credential = ManagedIdentityCredential(client_id=UAI_CLIENT_ID) return credential.get_token("api://AzureADTokenExchange/.default").token
msal_app = msal.ConfidentialClientApplication( client_id=APP_REGISTRATION_CLIENT_ID, authority=f"https://login.microsoftonline.com/{TENANT_ID}", client_credential={"client_assertion": client_assertion}, ) ```
MSAL calls client_assertion() when it actually needs to authenticate —
fetching a fresh token from the managed identity each time, rather than
reading a static value. There's nothing to leak, because there's nothing
stored.
What this looks like in practice
The sign-in flow itself is completely ordinary from a user's perspective —
click "Sign in," land on the real login.microsoftonline.com page, sign
in, get redirected back. All three routes are standard Authorization Code
flow:
@auth_bp.route("/login")
def login():
flow = msal_app.initiate_auth_code_flow(
scopes=[], redirect_uri=REDIRECT_URI
)
session["auth_flow"] = flow
return redirect(flow["auth_uri"])
@auth_bp.route("/callback")
def callback():
flow = session.pop("auth_flow", {})
result = msal_app.acquire_token_by_auth_code_flow(flow, request.args)
session["user"] = result.get("id_token_claims")
return redirect(url_for("routes.private"))
The only thing that's different from a textbook MSAL confidential-client
example is that client_credential is a callable instead of a string. The
managed identity step happens entirely inside acquire_token_by_auth_code_flow
— MSAL calls it, gets a token, uses it as the assertion, and the rest of
the OAuth exchange proceeds exactly as it would with a secret.
The tradeoffs, honestly
This isn't free. It's more moving parts than client_secret = "xyz":
- It only works when the app is actually running on Azure with the
managed identity attached —
ManagedIdentityCredentialtalks to Azure's instance metadata endpoint, which doesn't exist on a laptop. Local development can exercise every other route, but sign-in itself has to be tested on the deployed app. - It's one extra Azure resource (the user-assigned identity) and one extra Terraform resource (the federated credential) compared to a secret. For a five-minute prototype, a secret is still faster to wire up.
What you get in exchange: there is no secret to leak in a repo, no expiry date to track, no rotation job to build, and no App Setting that becomes sensitive the moment it's populated. For a personal project, that trade was worth it immediately. For anything with an SRE on call for expiring credentials, I think it's worth it even more.
This is one piece of a larger personal cloud platform, built from scratch: a landing zone, a private Terraform module registry, shared pipeline templates and Azure DevOps managed as code.