Skip to main content

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​

DataAzure permissionAccess
All Kubernetes objects of the cluster (pods, pod logs, deployments, nodes, namespaces, ConfigMaps and the other object types), Secrets excludedMicrosoft.ContainerService/managedClusters/*/read (dataActions)get, list, watch
SecretsMicrosoft.ContainerService/managedClusters/secrets/read (notDataActions)Not granted by this role
ConfigMaps, pod specs and pod logs

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}"
  • entraId and azureRbac must be true. A cluster with Kubernetes RBAC only ignores the Azure role of this page, so CXM does not support it.
  • If authorizedIpRanges is not empty, add the CXM egress addresses to it. Ask CXM for these addresses.
  • If privateCluster is true, 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​

  1. Save this file as cxm-aks-reader.json. Put one /subscriptions/<subscription-id> line in AssignableScopes for 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>"
    ]
    }
  2. Run:

    az role definition create --role-definition cxm-aks-reader.json

With the Azure Portal​

  1. Open one subscription that holds an AKS cluster.

  2. Go to Access control (IAM) > Add > Add custom role.

  3. Open the JSON tab and select Edit.

  4. Replace the JSON with this body. Put one /subscriptions/<subscription-id> line in assignableScopes for 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"
    ]
    }
    ]
    }
    }
  5. 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:

  1. Open the subscription and go to Access control (IAM) > Add > Add role assignment.
  2. Search for CXM AKS Reader, select it, and select Next.
  3. Select Select members, search for cxm-asset-crawler, and select it.
  4. 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 each Enabled subscription that is not in subscription_exclusions.
  • use_management_group = true: the root module includes each Enabled subscription that is not in subscription_exclusions and is under the management group management_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_ids of the root module. If subscription_ids is 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:

  1. Add /subscriptions/<subscription-id> to the assignable scopes of the role. With the Azure CLI, add it to AssignableScopes in cxm-aks-reader.json and run az 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.
  2. Do Step 2 for this subscription.
  3. 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.

  1. 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
  2. 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.

  3. Make the kubeconfig use the token of the Service Principal:

    kubelogin convert-kubeconfig -l azurecli --kubeconfig cxm-check.kubeconfig
  4. Check the access:

    kubectl --kubeconfig cxm-check.kubeconfig auth can-i list pods --all-namespaces
    kubectl --kubeconfig cxm-check.kubeconfig auth can-i get secrets --all-namespaces

    The first command must print yes. The second command must print no.

  5. Sign out of the Service Principal with az logout, then delete the check file with rm cxm-check.kubeconfig.

Security Considerations​

  1. 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.
  2. Read data actions only: The role holds only read dataActions. It does not grant create, update or delete on a Kubernetes object.
  3. Secrets not granted: This role does not grant Secrets through the Kubernetes API. notDataActions removes Microsoft.ContainerService/managedClusters/secrets/read from this role only. It does not deny Secrets to other roles.
  4. 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.
  5. Audit trail: To record each Kubernetes request of CXM, send the kube-audit log category of the cluster to a Log Analytics workspace or a storage account in Diagnostic settings.