DEV Community

Cover image for SC-500 deep dive #2: the best secret is the one you never had
CD Security Lab
CD Security Lab

Posted on Originally published at gitlab.com

SC-500 deep dive #2: the best secret is the one you never had

Every Thursday I post SC-500 practice questions. Every Saturday I take one exam topic and go deep: the real problem, how it works, the fix, and the exam traps. Week 2: managed identities, and why "no secret at all" beats "a secret in a well-locked vault".

Lab story, lab names only (contoso.com). Screens are drawings, numbers are invented.

The problem: one connection string, three copies, zero owners

In this lab story, Contoso runs an order app, app-lab-orders, on Azure App Service. It talks to two things:

  • the Azure SQL database sqldb-lab-orders, with the SQL login app_orders and its password
  • the storage account stlaborders01, with the account key

Both live in the app's application settings as plain text. During a review, the team found the same SQL password in three places:

  1. the App Service configuration, readable by everyone with Contributor on the resource group (12 people)
  2. a appsettings.Development.json file in the Git repository, "only for local testing"
  3. a pipeline variable that was never marked as secret

And the storage account key gives full access to every container, not just the one the app needs. Nobody had rotated either secret in 14 months, because nobody knew what would break.

Moving these secrets into Key Vault would help (deep dive #1 was about that). But the vault still holds a password that someone, someday, copies. The better question is: why does the app need a password at all?

Before: secrets in plain text in the app settings

How it works: the platform proves who the app is

A managed identity is an identity in Microsoft Entra ID that Azure creates and manages for a resource. The app asks the platform for a token, and the platform hands it one. There is no password, no certificate, nothing for the team to store or rotate.

System-assigned User-assigned
Created Turned on for one resource Created as its own Azure resource
Lifecycle Deleted when the resource is deleted Lives until you delete it
Shared? No, one resource only Yes, can be attached to many resources
Good for One app, one identity Many instances that need the same access, or access ready before the resource exists

The flow is always the same:

  1. The app asks the local token endpoint for a token for a target, for example Azure SQL or Azure Storage. On App Service it is the identity endpoint; on a VM it is the Instance Metadata Service (IMDS).
  2. Microsoft Entra ID issues a token for the managed identity.
  3. The app sends the token to the target. The target checks the identity's role or database user.

The important part is step 3. A managed identity on its own can do nothing. It is a name badge, not a key. Access comes only from the roles you give it on each target.

How the token flow works, and where access is decided

The fix: identity on, access granted, secrets gone

1. Find the secrets

List the app settings and connection strings, and look for passwords and keys:

az webapp config appsettings list -g rg-lab-orders -n app-lab-orders --query "[].name" -o tsv
az webapp config connection-string list -g rg-lab-orders -n app-lab-orders --query "[].name" -o tsv
Enter fullscreen mode Exit fullscreen mode

In this lab: SqlConnection (with a password) and StorageConnection (with the account key).

2. Turn on the managed identity

az webapp identity assign -g rg-lab-orders -n app-lab-orders
Enter fullscreen mode Exit fullscreen mode

This is a system-assigned identity: it is created with the app and deleted with it. If the app runs in several regions and every copy needs the same access, use a user-assigned identity instead.

3. Give it the smallest access, on each target

For Azure SQL, create a database user for the identity (the database must have a Microsoft Entra admin), then give it only the roles it needs:

CREATE USER [app-lab-orders] FROM EXTERNAL PROVIDER;
ALTER ROLE db_datareader ADD MEMBER [app-lab-orders];
ALTER ROLE db_datawriter ADD MEMBER [app-lab-orders];
Enter fullscreen mode Exit fullscreen mode

For storage, a data role on the one container the app uses, not on the whole account:

SCOPE=$(az storage account show -n stlaborders01 --query id -o tsv)/blobServices/default/containers/invoices
az role assignment create --role "Storage Blob Data Contributor" \
  --assignee "$(az webapp identity show -g rg-lab-orders -n app-lab-orders --query principalId -o tsv)" \
  --scope "$SCOPE"
Enter fullscreen mode Exit fullscreen mode

4. Change the app to use tokens

The SQL connection string loses its password:

Server=tcp:sql-lab-01.database.windows.net;Database=sqldb-lab-orders;Authentication=Active Directory Default;
Enter fullscreen mode Exit fullscreen mode

For storage, the Azure SDK's DefaultAzureCredential picks up the managed identity on Azure and the developer's own sign-in on a laptop. Same code in both places, no key in either.

If one secret really must stay (for example, an API key from a payment partner), keep it in Key Vault and point to it with a Key Vault reference in the app settings, read with the same managed identity:

@Microsoft.KeyVault(SecretUri=https://kv-lab-orders.vault.azure.net/secrets/PartnerApiKey/)
Enter fullscreen mode Exit fullscreen mode

5. Close the old doors

Removing the secret from the app is not enough. The old password and key still work for anyone who copied them:

  • Drop the SQL login app_orders, or turn on Microsoft Entra-only authentication on the server (SC-500 practice Q014).
  • Rotate both storage account keys, then set AllowSharedKeyAccess to false so no key works at all.
  • Delete the appsettings.Development.json secret from the repository history, and the pipeline variable.

After: identity on, roles per target, no secrets left

6. Keep it that way

  • Azure Policy has built-in definitions to audit App Service apps that do not use a managed identity, and to audit storage accounts that allow Shared Key access. Start in Audit.
  • In Defender for Cloud, follow the matching recommendations.

SC-500 exam traps

  1. "Which identity type is deleted with the VM?" System-assigned. A user-assigned identity survives, and keeps its role assignments.
  2. "The app has a managed identity but gets 403." The identity has no role on the target. Turning the identity on grants nothing.
  3. "You removed a role, but the app still has access for a while." Managed identity tokens are cached by the platform for around 24 hours per target. Role and group changes can take that long to bite. Plan for it in an incident.
  4. "Assign an existing user-assigned identity to a VM. Which role do you need on the identity?" Managed Identity Operator (plus rights on the VM). Managed Identity Contributor is for creating and deleting identities.
  5. "Can a managed identity sign in to another tenant?" No, managed identities do not support cross-directory scenarios. Code that runs outside Azure (on-premises, another cloud, GitHub) uses workload identity federation or Azure Arc instead (SC-500 practice Q021).

Before and after in this lab story

Before After
Secrets in app settings 2 (SQL password, storage key) 0
Copies of the SQL password 3 0, the login is dropped
Access to storage Full account, by key One container, by role
Rotation work per year "Nobody dares" None needed

Cheat sheet

Coming next

Next deep dive: private endpoints vs service endpoints, and why "private" does not always mean what you think. On Thursday: SC-500 practice questions, as usual.

Sources


Thanks for reading! I'm Cuong, and on CD Security Lab I rebuild real Microsoft security cases in a safe lab and explain them simply. Follow along:

Got a question about your own tenant? Drop it in the comments.

Top comments (0)