AKS Access
CXM reads the Kubernetes API of your AKS clusters to find cost optimizations in your workloads. This guide adds AKS access to an existing CXM Azure integration. You do not create a new identity or a new secret: CXM uses the same Service Principal (cxm-asset-crawler). You make no change inside the clusters.
What CXM Reads
| Data | Azure permission | Access |
|---|---|---|
| All Kubernetes objects of the cluster (pods, pod logs, deployments, nodes, namespaces, ConfigMaps and the other object types), Secrets excluded | Microsoft.ContainerService/managedClusters/*/read (dataActions) | get, list, watch |
| Secrets | Microsoft.ContainerService/managedClusters/secrets/read (notDataActions) | Not granted by this role |
This role does not grant Secrets through the Kubernetes API. It grants ConfigMaps, pod specs and pod logs. A ConfigMap, a pod spec (environment variables included) or a pod log can hold a plain-text value. If a workload keeps a password or a token in a ConfigMap, in an environment variable or in its log, CXM can read it. Keep these values in Secrets.
Supported Clusters
CXM supports a cluster only when these two settings are enabled:
- Microsoft Entra ID authentication
- Azure RBAC for Kubernetes Authorization
To see the two settings and the network access of a cluster, run:
az aks show \
--subscription <subscription-id> \
--resource-group <resource-group> \
--name <cluster-name> \
--query "{entraId: aadProfile.managed, azureRbac: aadProfile.enableAzureRbac, privateCluster: apiServerAccessProfile.enablePrivateCluster, authorizedIpRanges: apiServerAccessProfile.authorizedIpRanges}"
entraIdandazureRbacmust betrue. A cluster with Kubernetes RBAC only ignores the Azure role of this page, so CXM does not support it.- If
authorizedIpRangesis not empty, add the CXM egress addresses to it. Ask CXM for these addresses. - If
privateClusteristrue, CXM needs a network path to the private endpoint. An address allowlist does not give one. CXM does not support a private cluster without such a path.
Prerequisites
- The CXM Azure integration is set up (Terraform or Manual). Keep its Client ID and its Tenant ID at hand.
- The Reader role of the integration covers each subscription that holds an AKS cluster. The AKS role holds no action, so CXM finds the clusters and their API server addresses only through Reader.
- A user with the Owner or the User Access Administrator role on each subscription that holds an AKS cluster does Step 1 and Step 2.
With Terraform
Set enable_aks_access = true on the root module github.com/cxmlabs/terraform-azure-cxm-integration. This flag needs enable_asset_discovery = true (the default), because the Reader role gives CXM the clusters. The root module then calls the submodule terraform-azure-aks-access, which creates the role of Step 1 and the assignments of Step 2. The submodule README gives its inputs. If the submodule created the role, skip Step 1 and Step 2, and do Step 3. The manual steps below are for a reader without Terraform.
Step 1: Create the Custom Role
Create the role one time. The role name is unique in the tenant. The role lists each subscription that holds an AKS cluster in its assignable scopes.
With the Azure CLI
-
Save this file as
cxm-aks-reader.json. Put one/subscriptions/<subscription-id>line inAssignableScopesfor each subscription that holds an AKS cluster:{"Name": "CXM AKS Reader","IsCustom": true,"Description": "Read access for CXM to the Kubernetes API of AKS clusters, all objects except Secrets.","Actions": [],"NotActions": [],"DataActions": ["Microsoft.ContainerService/managedClusters/*/read"],"NotDataActions": ["Microsoft.ContainerService/managedClusters/secrets/read"],"AssignableScopes": ["/subscriptions/<subscription-id>"]} -
Run:
az role definition create --role-definition cxm-aks-reader.json
With the Azure Portal
-
Open one subscription that holds an AKS cluster.
-
Go to Access control (IAM) > Add > Add custom role.
-
Open the JSON tab and select Edit.
-
Replace the JSON with this body. Put one
/subscriptions/<subscription-id>line inassignableScopesfor each subscription that holds an AKS cluster:{"properties": {"roleName": "CXM AKS Reader","description": "Read access for CXM to the Kubernetes API of AKS clusters, all objects except Secrets.","assignableScopes": ["/subscriptions/<subscription-id>"],"permissions": [{"actions": [],"notActions": [],"dataActions": ["Microsoft.ContainerService/managedClusters/*/read"],"notDataActions": ["Microsoft.ContainerService/managedClusters/secrets/read"]}]}} -
Select Save, then Review + create > Create.
Step 2: Assign the Role to the CXM Service Principal
Assign the role on each subscription that holds an AKS cluster. Azure cannot assign a custom role with dataActions at a management group scope (Custom role limits). So an assignment on a management group does not work.
With the Azure CLI, run this command one time for each subscription:
az role assignment create \
--assignee <client-id> \
--role "CXM AKS Reader" \
--scope /subscriptions/<subscription-id>
With the Azure Portal:
- Open the subscription and go to Access control (IAM) > Add > Add role assignment.
- Search for CXM AKS Reader, select it, and select Next.
- Select Select members, search for
cxm-asset-crawler, and select it. - Select Review + assign.
Add a Subscription Later
A subscription that you add later gets no AKS access by itself. Without Terraform, a subscription that gets its first AKS cluster later gets none either.
If the Terraform submodule manages the role, make the root module include the subscription, then run terraform apply. The way to include it depends on the mode of the root module:
all_subscriptions = true: the root module includes eachEnabledsubscription that is not insubscription_exclusions.use_management_group = true: the root module includes eachEnabledsubscription that is not insubscription_exclusionsand is under the management groupmanagement_group_id, its child management groups included. If the subscription is not under that management group, move it there first.- Otherwise: add the subscription to
subscription_idsof the root module. Ifsubscription_idsis empty, the module uses only the subscription of the provider: keep that subscription in the list.
Do not edit the role manually: the next terraform apply removes the change.
Without Terraform, do these steps for this subscription:
- Add
/subscriptions/<subscription-id>to the assignable scopes of the role. With the Azure CLI, add it toAssignableScopesincxm-aks-reader.jsonand runaz role definition update --role-definition cxm-aks-reader.json. With the Azure Portal, open the role in Access control (IAM) > Roles, select Edit, and add the subscription in the Assignable scopes tab. - Do Step 2 for this subscription.
- If the Reader role of the integration does not cover this subscription, assign it as Step 3 of the Manual Setup says.
Step 3: Verify the Access
Azure can take up to 10 minutes to apply a new role assignment. Do this check on one cluster. You need the Azure CLI, kubectl and kubelogin. The command az aks install-cli installs kubectl and kubelogin. The machine that does the check must reach the API server of the cluster. On a cluster with authorized IP ranges, add the address of this machine. A private cluster needs a network path from this machine.
-
With your own Azure identity, get the kubeconfig of the cluster. The role of this page holds no action, so the Service Principal cannot get it. Your identity needs the Azure Kubernetes Service Cluster User Role, or a role that includes it, such as Owner:
az aks get-credentials --subscription <subscription-id> --resource-group <resource-group> --name <cluster-name> --file cxm-check.kubeconfig -
Sign in as the Service Principal:
az login --service-principal \--username <client-id> \--password <client-secret> \--tenant <tenant-id>If you do not have the client secret, create a temporary client secret for this check in App registrations >
cxm-asset-crawler> Certificates & secrets. Delete it after the check. Or ask CXM to do the check. -
Make the kubeconfig use the token of the Service Principal:
kubelogin convert-kubeconfig -l azurecli --kubeconfig cxm-check.kubeconfig -
Check the access:
kubectl --kubeconfig cxm-check.kubeconfig auth can-i list pods --all-namespaceskubectl --kubeconfig cxm-check.kubeconfig auth can-i get secrets --all-namespacesThe first command must print
yes. The second command must printno. -
Sign out of the Service Principal with
az logout, then delete the check file withrm cxm-check.kubeconfig.
Security Considerations
- What this page grants: the custom role CXM AKS Reader on the subscriptions of Step 2. It lets CXM read the Kubernetes objects of each AKS cluster in these subscriptions, ConfigMaps, pod specs and pod logs included (What CXM Reads). The role holds no action, so it cannot get the cluster credentials.
- Read data actions only: The role holds only
readdataActions. It does not grantcreate,updateordeleteon a Kubernetes object. - Secrets not granted: This role does not grant Secrets through the Kubernetes API.
notDataActionsremovesMicrosoft.ContainerService/managedClusters/secrets/readfrom this role only. It does not deny Secrets to other roles. - Scoped access: CXM reads only the clusters of the subscriptions of Step 2. A subscription that you add later needs the steps of Add a Subscription Later.
- Audit trail: To record each Kubernetes request of CXM, send the
kube-auditlog category of the cluster to a Log Analytics workspace or a storage account in Diagnostic settings.