You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/aks/workload-identity-cross-tenant.md
+95-25Lines changed: 95 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,13 +4,13 @@ description: Learn how to configure cross-tenant workload identity on Azure Kube
4
4
author: schaffererin
5
5
ms.topic: article
6
6
ms.subservice: aks-security
7
-
ms.date: 06/11/2024
7
+
ms.date: 07/02/2024
8
8
ms.author: schaffererin
9
9
---
10
10
11
11
# Configure cross-tenant workload identity on Azure Kubernetes Service (AKS)
12
12
13
-
In this article, you learn how to configure cross-tenant workload identity on Azure Kubernetes Service (AKS). Cross-tenant workload identity allows you to access resources in another tenant from your AKS cluster. In this example, you will create an Azure Service Bus in one tenant and send messages to it from a workload running in an AKS cluster in another tenant.
13
+
In this article, you learn how to configure cross-tenant workload identity on Azure Kubernetes Service (AKS). Cross-tenant workload identity allows you to access resources in another tenant from your AKS cluster. In this example, you create an Azure Service Bus in one tenant and send messages to it from a workload running in an AKS cluster in another tenant.
14
14
15
15
For more information on workload identity, see the [Workload identity overview][workload-identity-overview].
16
16
@@ -19,26 +19,32 @@ For more information on workload identity, see the [Workload identity overview][
19
19
****Two Azure subscriptions***, each in a separate tenant. In this article, we refer to these as *Tenant A* and *Tenant B*.
20
20
* Azure CLI installed on your local machine. If you don't have the Azure CLI installed, see [Install the Azure CLI][install-azure-cli].
21
21
* Bash shell environment. This article uses Bash shell syntax.
22
+
* You need to have the following subscription details:
22
23
23
-
In order to complete the steps in this article, you need to have the following information:
24
+
**Tenant A* tenant ID
25
+
**Tenant A* subscription ID
26
+
**Tenant B* tenant ID
27
+
**Tenant B* subscription ID
24
28
25
-
**Tenant A* tenant ID
26
-
**Tenant A* subscription ID
27
-
**Tenant B* tenant ID
28
-
**Tenant B* subscription ID
29
+
> [!IMPORTANT]
30
+
> Make sure you stay within the same terminal window for the duration of this article to retain the environment variables you set. If you close the terminal window, you need to set the environment variables again.
29
31
30
32
## Configure resources in Tenant A
31
33
32
34
In *Tenant A*, you create an AKS cluster with workload identity and OIDC issuer enabled. You use this cluster to deploy an application that attempts to access resources in *Tenant B*.
33
35
34
-
1. Log into your *Tenant A* subscription using the [`az login`][az-login-interactively] command and pass in the tenant ID of *Tenant A*.
36
+
### Log in to Tenant A
37
+
38
+
1. Log in to your *Tenant A* subscription using the [`az login`][az-login-interactively] command.
35
39
36
40
```azurecli-interactive
41
+
# Set environment variable
37
42
TENANT_A_ID=<tenant-id>
43
+
38
44
az login --tenant $TENANT_A_ID
39
45
```
40
46
41
-
1. Ensure you are working with the correct subscription in *Tenant A* by using the [`az account set`][az-account-set] command.
47
+
1. Ensure you're working with the correct subscription in *Tenant A* using the [`az account set`][az-account-set] command.
42
48
43
49
```azurecli-interactive
44
50
# Set environment variable
@@ -48,6 +54,8 @@ In *Tenant A*, you create an AKS cluster with workload identity and OIDC issuer
48
54
az account set --subscription $TENANT_A_SUBSCRIPTION_ID
49
55
```
50
56
57
+
### Create resources in Tenant A
58
+
51
59
1. Create a resource group in *Tenant A* to host the AKS cluster using the [`az group create`][az-group-create] command.
52
60
53
61
```azurecli-interactive
@@ -62,7 +70,7 @@ In *Tenant A*, you create an AKS cluster with workload identity and OIDC issuer
62
70
1. Create an AKS cluster in *Tenant A* with workload identity and OIDC issuer enabled using the [`az aks create`][az-aks-create] command.
63
71
64
72
```azurecli-interactive
65
-
# Set environment variables
73
+
# Set environment variable
66
74
CLUSTER_NAME=<cluster-name>
67
75
68
76
# Create an AKS cluster
@@ -74,7 +82,9 @@ In *Tenant A*, you create an AKS cluster with workload identity and OIDC issuer
74
82
--generate-ssh-keys
75
83
```
76
84
77
-
1. Get the OIDC issuer URL from the cluster in *Tenant A* using the [`az aks show`][az-aks-show] command.
85
+
### Get OIDC issuer URL from AKS cluster
86
+
87
+
* Get the OIDC issuer URL from the cluster in *Tenant A* using the [`az aks show`][az-aks-show] command.
78
88
79
89
```azurecli-interactive
80
90
OIDC_ISSUER_URL=$(az aks show --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --query "oidcIssuerProfile.issuerUrl" --output tsv)
@@ -84,20 +94,24 @@ In *Tenant A*, you create an AKS cluster with workload identity and OIDC issuer
84
94
85
95
In *Tenant B*, you create an Azure Service Bus, a managed identity and assign it permissions to read and write messages to the service bus, and establish the trust between the managed identity and the AKS cluster in *Tenant A*.
86
96
97
+
### Log in to Tenant B
98
+
87
99
1. Log out of your *Tenant A* subscription using the [`az logout`][az-logout] command.
88
100
89
101
```azurecli-interactive
90
102
az logout
91
103
```
92
104
93
-
1. Log into your *Tenant B* subscription using the [`az login`][az-login-interactively] command and pass in the tenant ID of *Tenant B*.
105
+
1. Log in to your *Tenant B* subscription using the [`az login`][az-login-interactively] command.
94
106
95
107
```azurecli-interactive
108
+
# Set environment variable
96
109
TENANT_B_ID=<tenant-id>
110
+
97
111
az login --tenant $TENANT_B_ID
98
112
```
99
113
100
-
1. Ensure you are working with the correct subscription in *Tenant A* by using the [`az account set`][az-account-set] command.
114
+
1. Ensure you're working with the correct subscription in *Tenant B* using the [`az account set`][az-account-set] command.
101
115
102
116
```azurecli-interactive
103
117
# Set environment variable
@@ -107,6 +121,8 @@ In *Tenant B*, you create an Azure Service Bus, a managed identity and assign it
107
121
az account set --subscription $TENANT_B_SUBSCRIPTION_ID
108
122
```
109
123
124
+
### Create resources in Tenant B
125
+
110
126
1. Create a resource group in *Tenant B* to host the managed identity using the [`az group create`][az-group-create] command.
111
127
112
128
```azurecli-interactive
@@ -117,10 +133,11 @@ In *Tenant B*, you create an Azure Service Bus, a managed identity and assign it
117
133
# Create a resource group
118
134
az group create --name $RESOURCE_GROUP --location $LOCATION
119
135
```
136
+
120
137
1. Create a service bus and queue in *Tenant B* using the [`az servicebus namespace create`][az-servicebus-namespace-create] and [`az servicebus queue create`][az-servicebus-queue-create] commands.
121
138
122
139
```azurecli-interactive
123
-
# Set a unique name for the servicebus
140
+
# Set environment variable
124
141
SERVICEBUS_NAME=sb-crosstenantdemo-$RANDOM
125
142
126
143
# Create a new service bus namespace and and return the service bus hostname
@@ -141,12 +158,15 @@ In *Tenant B*, you create an Azure Service Bus, a managed identity and assign it
141
158
1. Create a user-assigned managed identity in *Tenant B* using the [`az identity create`][az-identity-create] command.
142
159
143
160
```azurecli-interactive
144
-
# Set user-assigned managed identity name
161
+
# Set environment variable
145
162
IDENTITY_NAME=${SERVICEBUS_NAME}-identity
146
163
147
164
# Create a user-assigned managed identity
148
165
az identity create --resource-group $RESOURCE_GROUP --name $IDENTITY_NAME
149
166
```
167
+
168
+
### Get resource IDs and assign permissions in Tenant B
169
+
150
170
1. Get the principal ID of the managed identity in *Tenant B* using the [`az identity show`][az-identity-show] command.
151
171
152
172
```azurecli-interactive
@@ -178,7 +198,7 @@ In *Tenant B*, you create an Azure Service Bus, a managed identity and assign it
178
198
--output tsv)
179
199
```
180
200
181
-
6. Assign the managed identity in *Tenant B* permissions to read and write service bus messages using the [`az role assignment create`][az-role-assignment-create] command.
201
+
1. Assign the managed identity in *Tenant B* permissions to read and write service bus messages using the [`az role assignment create`][az-role-assignment-create] command.
182
202
183
203
```azurecli-interactive
184
204
az role assignment create \
@@ -203,36 +223,40 @@ In this section, you create the federated identity credential needed to establis
`--subject system:serviceaccount:default:myserviceaccount` is the name of the Kubernetes service account that you will create later in *Tenant A*. When your application pod makes authentication requests, this value is sent to Microsoft Entra ID as the `subject` in the authorization request. Microsoft Entra ID determines eligibility based on whether this value matches what you set when you created the federated identity credential, so it's important to ensure the value matches.
226
+
`--subject system:serviceaccount:default:myserviceaccount` is the name of the Kubernetes service account that you create in *Tenant A* later in the article. When your application pod makes authentication requests, this value is sent to Microsoft Entra ID as the `subject` in the authorization request. Microsoft Entra ID determines eligibility based on whether this value matches what you set when you created the federated identity credential, so it's important to ensure the value matches.
207
227
208
228
## Deploy application to send messages to Azure Service Bus queue
209
229
210
230
In this section, you deploy an application to your AKS cluster in *Tenant A* that sends messages to the Azure Service Bus queue in *Tenant B*.
211
231
232
+
### Log in to Tenant A and get AKS credentials
233
+
212
234
1. Log out of your *Tenant B* subscription using the [`az logout`][az-logout] command.
213
235
214
236
```azurecli-interactive
215
237
az logout
216
238
```
217
239
218
-
1. Log into your *Tenant A* subscription using the [`az login`][az-login-interactively] command and pass in the tenant ID of *Tenant A*.
240
+
1. Log in to your *Tenant A* subscription using the [`az login`][az-login-interactively] command.
219
241
220
242
```azurecli-interactive
221
243
az login --tenant $TENANT_A_ID
222
244
```
223
245
224
-
1. Ensure you are working with the correct subscription in *Tenant A* by using the [`az account set`][az-account-set] command.
246
+
1. Ensure you're working with the correct subscription in *Tenant A* using the [`az account set`][az-account-set] command.
225
247
226
248
```azurecli-interactive
227
249
az account set --subscription $TENANT_A_SUBSCRIPTION_ID
228
250
```
229
251
230
-
1. Get the cluster credentials for the AKS cluster in *Tenant A* using the [`az aks get-credentials`][az-aks-get-credentials] command.
252
+
1. Get the credentials for the AKS cluster in *Tenant A* using the [`az aks get-credentials`][az-aks-get-credentials] command.
231
253
232
254
```azurecli-interactive
233
255
az aks get-credentials --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME
234
256
```
235
257
258
+
### Create Kubernetes resources to send messages to Azure Service Bus queue
259
+
236
260
1. Create a new Kubernetes ServiceAccount in the `default` namespace and pass in the client ID of your managed identity in *Tenant B* to the `kubectl apply` command. The client ID is used to authenticate the pod to the Azure Service Bus.
237
261
238
262
```azurecli-interactive
@@ -246,7 +270,7 @@ In this section, you deploy an application to your AKS cluster in *Tenant A* tha
246
270
EOF
247
271
```
248
272
249
-
4. Create a new Kubernetes Job in the `default` namespace to send 100 messages to your Azure Service Bus queue. The Pod template is configured to use workload identity and the service account you created in the previous step. Also note that the `AZURE_TENANT_ID` environment variable is set to the tenant ID of *Tenant B*. This is required as workload identity defaults to the tenant of the AKS cluster, so you need to explicitly set the tenant ID of *Tenant B*.
273
+
1. Create a new Kubernetes Job in the `default` namespace to send 100 messages to your Azure Service Bus queue. The Pod template is configured to use workload identity and the service account you created in the previous step. Also note that the `AZURE_TENANT_ID` environment variable is set to the tenant ID of *Tenant B*. This is required as workload identity defaults to the tenant of the AKS cluster, so you need to explicitly set the tenant ID of *Tenant B*.
250
274
251
275
```azurecli-interactive
252
276
kubectl apply -f - <<EOF
@@ -280,7 +304,9 @@ In this section, you deploy an application to your AKS cluster in *Tenant A* tha
280
304
EOF
281
305
```
282
306
283
-
5. Verify that the pod is configured correctly to interact with the Azure Service Bus queue in *Tenant B* by checking the status of the pod using the `kubectl describe pod` command.
307
+
## Verify the deployment
308
+
309
+
1. Verify that the pod is correctly configured to interact with the Azure Service Bus queue in *Tenant B* by checking the status of the pod using the `kubectl describe pod` command.
284
310
285
311
```azurecli-interactive
286
312
# Get the dynamically generated pod name
@@ -290,7 +316,7 @@ In this section, you deploy an application to your AKS cluster in *Tenant A* tha
290
316
kubectl describe pod $POD_NAME | grep AZURE_TENANT_ID
291
317
```
292
318
293
-
6. Check the logs of the pod to see if the application was able to send messages across tenants using the `kubectl logs` command.
319
+
1. Check the logs of the pod to see if the application was able to send messages across tenants using the `kubectl logs` command.
294
320
295
321
```azurecli-interactive
296
322
kubectl logs $POD_NAME
@@ -306,6 +332,50 @@ In this section, you deploy an application to your AKS cluster in *Tenant A* tha
306
332
Sent 100 messages
307
333
```
308
334
335
+
## Clean up resources
336
+
337
+
After you verify that the deployment is successful, you can clean up the resources to avoid incurring Azure costs.
338
+
339
+
### Delete resources in Tenant A
340
+
341
+
1. Log in to your *Tenant A* subscription using the [`az login`][az-login-interactively] command.
342
+
343
+
```azurecli-interactive
344
+
az login --tenant $TENANT_A_ID
345
+
```
346
+
347
+
1. Ensure you're working with the correct subscription in *Tenant A* using the [`az account set`][az-account-set] command.
348
+
349
+
```azurecli-interactive
350
+
az account set --subscription $TENANT_A_SUBSCRIPTION_ID
351
+
```
352
+
353
+
1. Delete the Azure resource group and all resources in it using the [`az group delete`][az-group-delete] command.
354
+
355
+
```azurecli-interactive
356
+
az group delete --name $RESOURCE_GROUP --yes --no-wait
357
+
```
358
+
359
+
### Delete resources in Tenant B
360
+
361
+
1. Log in to your *Tenant B* subscription using the [`az login`][az-login-interactively] command.
362
+
363
+
```azurecli-interactive
364
+
az login --tenant $TENANT_B_ID
365
+
```
366
+
367
+
1. Ensure you're working with the correct subscription in *Tenant B* using the [`az account set`][az-account-set] command.
368
+
369
+
```azurecli-interactive
370
+
az account set --subscription $TENANT_B_SUBSCRIPTION_ID
371
+
```
372
+
373
+
1. Delete the Azure resource group and all resources in it using the [`az group delete`][az-group-delete] command.
374
+
375
+
```azurecli-interactive
376
+
az group delete --name $RESOURCE_GROUP --yes --no-wait
377
+
```
378
+
309
379
## Next steps
310
380
311
381
In this article, you learned how to configure cross-tenant workload identity on Azure Kubernetes Service (AKS). To learn more about workload identity, see the following articles:
@@ -327,9 +397,9 @@ In this article, you learned how to configure cross-tenant workload identity on
0 commit comments