Skip to content

Commit 3da3d57

Browse files
authored
Merge pull request #232505 from schaffererin/akskubeconfigaccess
Editing pass for consistent formatting
2 parents 77a6ec2 + e5338ec commit 3da3d57

File tree

1 file changed

+42
-50
lines changed

1 file changed

+42
-50
lines changed

articles/aks/control-kubeconfig-access.md

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,50 @@ title: Limit access to kubeconfig in Azure Kubernetes Service (AKS)
33
description: Learn how to control access to the Kubernetes configuration file (kubeconfig) for cluster administrators and cluster users
44
ms.topic: article
55
ms.custom: devx-track-azurecli
6-
ms.date: 05/06/2020
6+
ms.date: 03/28/2023
77
---
88

99
# Use Azure role-based access control to define access to the Kubernetes configuration file in Azure Kubernetes Service (AKS)
1010

11-
You can interact with Kubernetes clusters using the `kubectl` tool. The Azure CLI provides an easy way to get the access credentials and configuration information to connect to your AKS clusters using `kubectl`. To limit who can get that Kubernetes configuration (*kubeconfig*) information and to limit the permissions they then have, you can use Azure role-based access control (Azure RBAC).
11+
You can interact with Kubernetes clusters using the `kubectl` tool. The Azure CLI provides an easy way to get the access credentials and *kubeconfig* configuration file to connect to your AKS clusters using `kubectl`. You can use Azure role-based access control (Azure RBAC) to limit who can get access to the *kubeconfig* file and the permissions they have.
1212

1313
This article shows you how to assign Azure roles that limit who can get the configuration information for an AKS cluster.
1414

1515
## Before you begin
1616

17-
This article assumes that you have an existing AKS cluster. If you need an AKS cluster, see the AKS quickstart [using the Azure CLI][aks-quickstart-cli], [using Azure PowerShell][aks-quickstart-powershell], or [using the Azure portal][aks-quickstart-portal].
17+
* This article assumes that you have an existing AKS cluster. If you need an AKS cluster, create one using [Azure CLI][aks-quickstart-cli], [Azure PowerShell][aks-quickstart-powershell], or [the Azure portal][aks-quickstart-portal].
18+
* This article also requires that you're running Azure CLI version 2.0.65 or later. Run `az --version` to find the version. If you need to install or upgrade, see [Install Azure CLI][azure-cli-install].
1819

19-
This article also requires that you are running the Azure CLI version 2.0.65 or later. Run `az --version` to find the version. If you need to install or upgrade, see [Install Azure CLI][azure-cli-install].
20+
## Available permissions for cluster roles
2021

21-
## Available cluster roles permissions
22+
When you interact with an AKS cluster using the `kubectl` tool, a configuration file, called *kubeconfig*, defines cluster connection information. This configuration file is typically stored in *~/.kube/config*. Multiple clusters can be defined in this *kubeconfig* file. You can switch between clusters using the [`kubectl config use-context`][kubectl-config-use-context] command.
2223

23-
When you interact with an AKS cluster using the `kubectl` tool, a configuration file is used that defines cluster connection information. This configuration file is typically stored in *~/.kube/config*. Multiple clusters can be defined in this *kubeconfig* file. You switch between clusters using the [kubectl config use-context][kubectl-config-use-context] command.
24+
The [`az aks get-credentials`][az-aks-get-credentials] command lets you get the access credentials for an AKS cluster and merges these credentials into the *kubeconfig* file. You can use Azure RBAC to control access to these credentials. These Azure roles let you define who can retrieve the *kubeconfig* file and what permissions they have within the cluster.
2425

25-
The [az aks get-credentials][az-aks-get-credentials] command lets you get the access credentials for an AKS cluster and merges them into the *kubeconfig* file. You can use Azure role-based access control (Azure RBAC) to control access to these credentials. These Azure roles let you define who can retrieve the *kubeconfig* file, and what permissions they then have within the cluster.
26+
There are two Azure roles you can apply to an Azure Active Directory (Azure AD) user or group:
2627

27-
The two built-in roles are:
28+
- **Azure Kubernetes Service Cluster Admin Role**
2829

29-
* **Azure Kubernetes Service Cluster Admin Role**
30-
* Allows access to *Microsoft.ContainerService/managedClusters/listClusterAdminCredential/action* API call. This API call [lists the cluster admin credentials][api-cluster-admin].
31-
* Downloads *kubeconfig* for the *clusterAdmin* role.
32-
* **Azure Kubernetes Service Cluster User Role**
33-
* Allows access to *Microsoft.ContainerService/managedClusters/listClusterUserCredential/action* API call. This API call [lists the cluster user credentials][api-cluster-user].
34-
* Downloads *kubeconfig* for *clusterUser* role.
30+
* Allows access to `Microsoft.ContainerService/managedClusters/listClusterAdminCredential/action` API call. This API call [lists the cluster admin credentials][api-cluster-admin].
31+
* Downloads *kubeconfig* for the *clusterAdmin* role.
3532

36-
These Azure roles can be applied to an Azure Active Directory (AD) user or group.
33+
- **Azure Kubernetes Service Cluster User Role**
34+
35+
* Allows access to `Microsoft.ContainerService/managedClusters/listClusterUserCredential/action` API call. This API call [lists the cluster user credentials][api-cluster-user].
36+
* Downloads *kubeconfig* for *clusterUser* role.
3737

3838
> [!NOTE]
39-
> On clusters that use Azure AD, users with the *clusterUser* role have an empty *kubeconfig* file that prompts a log in. Once logged in, users have access based on their Azure AD user or group settings. Users with the *clusterAdmin* role have admin access.
39+
> On clusters that use Azure AD, users with the *clusterUser* role have an empty *kubeconfig* file that prompts a login. Once logged in, users have access based on their Azure AD user or group settings. Users with the *clusterAdmin* role have admin access.
4040
>
41-
> On clusters that do not use Azure AD, the *clusterUser* role has same effect of *clusterAdmin* role.
41+
> On clusters that don't use Azure AD, the *clusterUser* role has same effect of *clusterAdmin* role.
4242
4343
## Assign role permissions to a user or group
4444

45-
To assign one of the available roles, you need to get the resource ID of the AKS cluster and the ID of the Azure AD user account or group. The following example commands:
45+
To assign one of the available roles, you need to get the resource ID of the AKS cluster and the ID of the Azure AD user account or group using the following steps:
4646

47-
* Get the cluster resource ID using the [az aks show][az-aks-show] command for the cluster named *myAKSCluster* in the *myResourceGroup* resource group. Provide your own cluster and resource group name as needed.
48-
* Use the [az account show][az-account-show] and [az ad user show][az-ad-user-show] commands to get your user ID.
49-
* Finally, assign a role using the [az role assignment create][az-role-assignment-create] command.
47+
1. Get the cluster resource ID using the [`az aks show`][az-aks-show] command for the cluster named *myAKSCluster* in the *myResourceGroup* resource group. Provide your own cluster and resource group name as needed.
48+
2. Use the [`az account show`][az-account-show] and [`az ad user show`][az-ad-user-show] commands to get your user ID.
49+
3. Assign a role using the [`az role assignment create`][az-role-assignment-create] command.
5050

5151
The following example assigns the *Azure Kubernetes Service Cluster Admin Role* to an individual user account:
5252

@@ -65,55 +65,48 @@ az role assignment create \
6565
--role "Azure Kubernetes Service Cluster Admin Role"
6666
```
6767

68+
If you want to assign permissions to an Azure AD group, update the `--assignee` parameter shown in the previous example with the object ID for the *group* rather than the *user*.
69+
70+
To get the object ID for a group, use the [`az ad group show`][az-ad-group-show] command. The following command gets the object ID for the Azure AD group named *appdev*:
71+
72+
```azurecli-interactive
73+
az ad group show --group appdev --query objectId -o tsv
74+
```
75+
6876
> [!IMPORTANT]
69-
> In some cases, the *user.name* in the account is different than the *userPrincipalName*, such as with Azure AD guest users:
77+
> In some cases, such as Azure AD guest users, the *user.name* in the account is different than the *userPrincipalName*.
7078
>
71-
> ```output
79+
> ```azurecli-interactive
7280
> $ az account show --query user.name -o tsv
7381
82+
>
7483
> $ az ad user list --query "[?contains(otherMails,'[email protected]')].{UPN:userPrincipalName}" -o tsv
7584
> user_contoso.com#EXT#@contoso.onmicrosoft.com
7685
> ```
7786
>
78-
> In this case, set the value of *ACCOUNT_UPN* to the *userPrincipalName* from the Azure AD user. For example, if your account *user.name* is *user\@contoso.com*:
79-
>
87+
> In this case, set the value of *ACCOUNT_UPN* to the *userPrincipalName* from the Azure AD user. For example, if your account *user.name* is *user\@contoso.com*, this action would look like the following example:
88+
>
8089
> ```azurecli-interactive
8190
> ACCOUNT_UPN=$(az ad user list --query "[?contains(otherMails,'[email protected]')].{UPN:userPrincipalName}" -o tsv)
8291
> ```
8392
84-
> [!TIP]
85-
> If you want to assign permissions to an Azure AD group, update the `--assignee` parameter shown in the previous example with the object ID for the *group* rather than a *user*. To obtain the object ID for a group, use the [az ad group show][az-ad-group-show] command. The following example gets the object ID for the Azure AD group named *appdev*: `az ad group show --group appdev --query objectId -o tsv`
86-
87-
You can change the previous assignment to the *Cluster User Role* as needed.
88-
89-
The following example output shows the role assignment has been successfully created:
90-
91-
```
92-
{
93-
"canDelegate": null,
94-
"id": "/subscriptions/<guid>/resourcegroups/myResourceGroup/providers/Microsoft.ContainerService/managedClusters/myAKSCluster/providers/Microsoft.Authorization/roleAssignments/b2712174-5a41-4ecb-82c5-12b8ad43d4fb",
95-
"name": "b2712174-5a41-4ecb-82c5-12b8ad43d4fb",
96-
"principalId": "946016dd-9362-4183-b17d-4c416d1f8f61",
97-
"resourceGroup": "myResourceGroup",
98-
"roleDefinitionId": "/subscriptions/<guid>/providers/Microsoft.Authorization/roleDefinitions/0ab01a8-8aac-4efd-b8c2-3ee1fb270be8",
99-
"scope": "/subscriptions/<guid>/resourcegroups/myResourceGroup/providers/Microsoft.ContainerService/managedClusters/myAKSCluster",
100-
"type": "Microsoft.Authorization/roleAssignments"
101-
}
102-
```
103-
10493
## Get and verify the configuration information
10594
106-
With Azure roles assigned, use the [az aks get-credentials][az-aks-get-credentials] command to get the *kubeconfig* definition for your AKS cluster. The following example gets the *--admin* credentials, which work correctly if the user has been granted the *Cluster Admin Role*:
95+
Once the roles are assigned, use the [`az aks get-credentials`][az-aks-get-credentials] command to get the *kubeconfig* definition for your AKS cluster. The following example gets the *--admin* credentials, which works correctly if the user has been granted the *Cluster Admin Role*:
10796
10897
```azurecli-interactive
10998
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster --admin
11099
```
111100
112-
You can then use the [kubectl config view][kubectl-config-view] command to verify that the *context* for the cluster shows that the admin configuration information has been applied:
101+
You can then use the [`kubectl config view`][kubectl-config-view] command to verify that the *context* for the cluster shows that the admin configuration information has been applied.
113102

114-
```
103+
```azurecli-interactive
115104
$ kubectl config view
105+
```
116106

107+
Your output should look similar to the following example output:
108+
109+
```azurecli-interactive
117110
apiVersion: v1
118111
clusters:
119112
- cluster:
@@ -138,7 +131,7 @@ users:
138131

139132
## Remove role permissions
140133

141-
To remove role assignments, use the [az role assignment delete][az-role-assignment-delete] command. Specify the account ID and cluster resource ID, as obtained in the previous commands. If you assigned the role to a group rather than a user, specify the appropriate group object ID rather than account object ID for the `--assignee` parameter:
134+
To remove role assignments, use the [`az role assignment delete`][az-role-assignment-delete] command. Specify the account ID and cluster resource ID that you obtained in the previous steps. If you assigned the role to a group rather than a user, specify the appropriate group object ID rather than account object ID for the `--assignee` parameter.
142135

143136
```azurecli-interactive
144137
az role assignment delete --assignee $ACCOUNT_ID --scope $AKS_CLUSTER
@@ -158,7 +151,6 @@ For enhanced security on access to AKS clusters, [integrate Azure Active Directo
158151
[aks-quickstart-powershell]: ./learn/quick-kubernetes-deploy-powershell.md
159152
[azure-cli-install]: /cli/azure/install-azure-cli
160153
[az-aks-get-credentials]: /cli/azure/aks#az_aks_get_credentials
161-
[azure-rbac]: ../role-based-access-control/overview.md
162154
[api-cluster-admin]: /rest/api/aks/managedclusters/listclusteradmincredentials
163155
[api-cluster-user]: /rest/api/aks/managedclusters/listclusterusercredentials
164156
[az-aks-show]: /cli/azure/aks#az_aks_show

0 commit comments

Comments
 (0)