DEV Community

Cover image for Read-only Azure service principal: Reader role, costs, secret expiry and how to check it cannot write
Gonzalo Gjuka
Gonzalo Gjuka

Posted on Originally published at controlcloud.dev on

Read-only Azure service principal: Reader role, costs, secret expiry and how to check it cannot write

This site's Azure guide explains in five steps how to create the service principal in the portal and paste it into Control Cloud. Search Console shows searches arriving with no page that answers them: "azure service principal", "service principal azure", "azure access control". This note answers them with the CLI: what a service principal is and how it differs from an app registration and a managed identity, how to create one with the Reader role in a single command, which role adds cost reading, when the secret expires, and how to check that it really cannot write. Commands and role names come from Microsoft Learn; the source list is at the end.

App registration, service principal and managed identity

  • App registration (the application object): the global definition of the application, living in the tenant where it was registered. That is where the client ID and the credentials are.
  • Service principal: according to Microsoft Learn, the local representation of that application object in a tenant. It is the identity Azure RBAC assigns roles to. When you register an application, the service principal is created automatically, which is why the portal makes them look like one thing.
  • Managed identity: a service principal that Azure manages for code running inside Azure (a VM, a Function). There is no secret to store or rotate. If the tool that reads your account runs outside Azure, it does not apply: you need a service principal with a secret or a certificate.

Create it with the Reader role in one command

az ad sp create-for-rbac registers the application, creates the service principal and assigns the role in one step. Microsoft Learn recommends always passing --role and --scopes: without them the service principal exists but has no access to any resource. To create it you need User Access Administrator or Role Based Access Control Administrator (or higher) on the subscription.

# the active subscription of your az session
SUB=$(az account show --query id --output tsv)

az ad sp create-for-rbac --name "inventory-read-only" \
  --role Reader \
  --scopes "/subscriptions/$SUB"
Enter fullscreen mode Exit fullscreen mode

The output is a JSON document with four fields:

{
  "appId": "00000000-0000-0000-0000-000000000000",
  "displayName": "inventory-read-only",
  "password": "...",
  "tenant": "00000000-0000-0000-0000-000000000000"
}
Enter fullscreen mode Exit fullscreen mode

appId is the client ID, password is the client secret and tenant is the tenant ID. The secret cannot be read again: if you lose it, generate another with az ad sp credential reset. Two warnings from the documentation: if another directory object shares the same display name, the command can modify it, so use a unique name; and if your directory has "Users can register applications" set to No, the command fails with "Insufficient privileges to complete the operation" until an Entra admin gives you a role such as Application Developer.

Reader is the built-in role Microsoft describes as "View all resources, but does not allow you to make any changes". Its only action is */read on the control plane and it has no DataActions: it sees that a storage account exists, but does not read the blobs inside it.

Less reach: resource groups instead of the subscription

--scopes takes several space-separated scopes. If the tool only needs to see two resource groups, grant it those and not the whole subscription:

az ad sp create-for-rbac --name "inventory-two-rgs" \
  --role Reader \
  --scopes "/subscriptions/$SUB/resourceGroups/prod-web" \
           "/subscriptions/$SUB/resourceGroups/prod-db"
Enter fullscreen mode Exit fullscreen mode

The price of a smaller scope is a smaller inventory: anything outside those resource groups does not show up. For a full inventory the right scope is the subscription; for a team that only runs its own part, the resource group.

Costs: Reader is not enough

To see costs at subscription or resource group scope, Microsoft Learn requires at least Cost Management Reader (or Contributor). Enterprise Agreements also need "AO view charges" turned on, and Microsoft Customer Agreements need the billing profile's "Azure charges" setting. In Control Cloud's code, the Cost Management API query from a service principal that only has Reader comes back as 403. The role is added to the same service principal:

az role assignment create --assignee "$APP_ID" \
  --role "Cost Management Reader" \
  --scope "/subscriptions/$SUB"
Enter fullscreen mode Exit fullscreen mode

Assigning it requires Owner or User Access Administrator on that scope. Cost Management Reader reads costs and does not modify resources, so the credential still cannot write.

The secret expires: how long it lasts and how to rotate it

create-for-rbac creates the secret with one year of validity unless you pass --years. In the portal, under Certificates & secrets, the maximum is 24 months and Microsoft recommends less than 12. The same page recommends certificates or federated credentials instead of secrets for production. If you use a secret, rotating without downtime means adding a new one before deleting the old one:

# when each credential expires (the value cannot be retrieved)
az ad app credential list --id "$APP_ID"

# a new one, WITHOUT deleting the current one
az ad app credential reset --id "$APP_ID" --append --years 1

# once the consumer uses the new one, delete the old one by its keyId
az ad app credential delete --id "$APP_ID" --key-id "$OLD_KEY_ID"
Enter fullscreen mode Exit fullscreen mode

Without --append, reset replaces the existing credentials and whatever used the previous one stops authenticating at that moment. Write down the expiry date: the day it expires, the integration stops reading with no warning other than a sign-in error.

Check that it only reads

Before pasting it anywhere, sign in as the service principal and test both things. read -sp keeps the secret out of the shell history:

read -sp "client secret: " AZ_PASS && echo
az login --service-principal -u "$APP_ID" -p "$AZ_PASS" --tenant "$TENANT"

# read: must work
az group list --output table

# write: must fail with AuthorizationFailed
az group create --name write-test --location eastus

# which roles it has, and on which scope
az role assignment list --assignee "$APP_ID" --all --output table
Enter fullscreen mode Exit fullscreen mode

Creating the resource group must fail with AuthorizationFailed and a message saying the client "does not have authorization to perform action" over that scope. If it gets created, the service principal has a write role inherited from somewhere else: the last line shows which one and on which scope. Delete the test resource group with your own user and remove that role.

Using it in Control Cloud

Control Cloud's Azure form asks for the tenant ID, client ID and client secret; the subscription ID is optional. Left empty, Control Cloud discovers every subscription that service principal can access, so the scope you granted above decides what shows up on the map; with an ID, it reads only that subscription. Credentials are stored encrypted. With Reader it draws resource groups, VMs and networks; the cost panel also needs Cost Management Reader and, when it is missing, shows these same steps. Starting or stopping VMs from Control Cloud requires a role with write permissions on the VMs, as the Azure guide explains. If you want to try it with your subscription, request access.

Sources

  • Microsoft Learn, "Create Azure service principals using the Azure CLI" and "Reset service principal credentials using the Azure CLI".
  • Microsoft Learn, "Application and service principal objects in Microsoft Entra ID" and "What are managed identities for Azure resources?".
  • Microsoft Learn, "Add and manage app credentials in Microsoft Entra ID" (client secret expiry).
  • Microsoft Learn, "Azure built-in roles for General" (Reader role) and "Assign access to Cost Management data".
  • CLI reference: az ad sp, az ad app credential and az role assignment.
  • In this repository: the Azure connection form (ConnectionsPage.tsx), subscription discovery (orgcreds/livefetch.go) and the cost query (azuremap/cost.go).

Top comments (0)