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
# 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.
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.
14
14
15
15
For more information on workload identity, see the [Workload identity overview][workload-identity-overview].
16
16
17
17
## Prerequisites
18
18
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
-
* X
22
-
* Y
23
-
* Z
21
+
* Bash shell environment. This article uses Bash shell syntax.
22
+
23
+
In order to complete the steps in this article, you need to have the following information:
24
+
25
+
**Tenant A* tenant ID
26
+
**Tenant A* subscription ID
27
+
**Tenant B* tenant ID
28
+
**Tenant B* subscription ID
24
29
25
30
## Configure resources in Tenant A
26
31
27
32
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*.
28
33
29
-
1. Log in to your *Tenant A* subscription using the [`az account set`][az-account-set] command.
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*.
35
+
36
+
```azurecli-interactive
37
+
TENANT_A_ID=<tenant-id>
38
+
az login --tenant $TENANT_A_ID
39
+
```
40
+
41
+
1. Ensure you are working with the correct subscription in *Tenant A* by using the [`az account set`][az-account-set] command.
30
42
31
43
```azurecli-interactive
32
44
# Set environment variable
@@ -36,7 +48,7 @@ In *Tenant A*, you create an AKS cluster with workload identity and OIDC issuer
36
48
az account set --subscription $TENANT_A_SUBSCRIPTION_ID
37
49
```
38
50
39
-
2. Create a resource group in *Tenant A* to host the AKS cluster using the [`az group create`][az-group-create] command.
51
+
1. Create a resource group in *Tenant A* to host the AKS cluster using the [`az group create`][az-group-create] command.
40
52
41
53
```azurecli-interactive
42
54
# Set environment variables
@@ -47,27 +59,45 @@ In *Tenant A*, you create an AKS cluster with workload identity and OIDC issuer
47
59
az group create --name $RESOURCE_GROUP --location $LOCATION
48
60
```
49
61
50
-
3. Create an AKS cluster in *Tenant A* with workload identity and OIDC issuer enabled using the [`az aks create`][az-aks-create] command.
62
+
1. Create an AKS cluster in *Tenant A* with workload identity and OIDC issuer enabled using the [`az aks create`][az-aks-create] command.
51
63
52
64
```azurecli-interactive
53
65
# Set environment variables
54
66
CLUSTER_NAME=<cluster-name>
55
67
56
68
# Create an AKS cluster
57
-
az aks create --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --enable-oidc-issuer --enable-workload-identity --generate-ssh-keys
69
+
az aks create \
70
+
--resource-group $RESOURCE_GROUP \
71
+
--name $CLUSTER_NAME \
72
+
--enable-oidc-issuer \
73
+
--enable-workload-identity \
74
+
--generate-ssh-keys
58
75
```
59
76
60
-
4. Get the OIDC issuer URL from the cluster in *Tenant A* using the [`az aks show`][az-aks-show] command.
77
+
1. Get the OIDC issuer URL from the cluster in *Tenant A* using the [`az aks show`][az-aks-show] command.
61
78
62
79
```azurecli-interactive
63
80
OIDC_ISSUER_URL=$(az aks show --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --query "oidcIssuerProfile.issuerUrl" --output tsv)
64
81
```
65
82
66
83
## Configure resources in Tenant B
67
84
68
-
In *Tenant B*, you create a managed identity, assign it permissions to read subscription information, and establish the trust between the managed identity and the AKS cluster in *Tenant A*.
85
+
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
+
87
+
1. Log out of your *Tenant A* subscription using the [`az logout`][az-logout] command.
88
+
89
+
```azurecli-interactive
90
+
az logout
91
+
```
92
+
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*.
94
+
95
+
```azurecli-interactive
96
+
TENANT_B_ID=<tenant-id>
97
+
az login --tenant $TENANT_B_ID
98
+
```
69
99
70
-
1. Log in to your *Tenant B* subscription using the [`az account set`][az-account-set] command.
100
+
1. Ensure you are working with the correct subscription in *Tenant A* by using the [`az account set`][az-account-set] command.
71
101
72
102
```azurecli-interactive
73
103
# Set environment variable
@@ -77,7 +107,7 @@ In *Tenant B*, you create a managed identity, assign it permissions to read subs
77
107
az account set --subscription $TENANT_B_SUBSCRIPTION_ID
78
108
```
79
109
80
-
2. Create a resource group in *Tenant B* to host the managed identity using the [`az group create`][az-group-create] command.
110
+
1. Create a resource group in *Tenant B* to host the managed identity using the [`az group create`][az-group-create] command.
81
111
82
112
```azurecli-interactive
83
113
# Set environment variables
@@ -87,33 +117,75 @@ In *Tenant B*, you create a managed identity, assign it permissions to read subs
87
117
# Create a resource group
88
118
az group create --name $RESOURCE_GROUP --location $LOCATION
89
119
```
120
+
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.
90
121
91
-
3. Create a user-assigned managed identity in *Tenant B* using the [`az identity create`][az-identity-create] command.
122
+
```azurecli-interactive
123
+
# Set a unique name for the servicebus
124
+
SERVICEBUS_NAME=sb-crosstenantdemo-$RANDOM
125
+
126
+
# Create a new service bus namespace and and return the service bus hostname
6. Assign the managed identity in *Tenant B* permissions to read subscription information using the [`az role assignment create`][az-role-assignment-create] command.
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.
114
182
115
183
```azurecli-interactive
116
-
az role assignment create --role "Reader" --assignee $PRINCIPAL_ID --scope /subscriptions/$TENANT_A_SUBSCRIPTION_ID
184
+
az role assignment create \
185
+
--role "Azure Service Bus Data Owner" \
186
+
--assignee-object-id $PRINCIPAL_ID \
187
+
--assignee-principal-type ServicePrincipal \
188
+
--scope $SERVICEBUS_ID
117
189
```
118
190
119
191
## Establish trust between AKS cluster and managed identity
@@ -123,121 +195,115 @@ In this section, you create the federated identity credential needed to establis
123
195
* Create a federated identity credential using the [`az aks federated-identity add`][az-aks-federated-identity-add] command.
`--subject system:serviceaccount:default:wi-demo-account` 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 Azure AD as the `subject` in the authorization request. Azure AD 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.
130
-
131
-
## Create application to read subscription information
132
-
133
-
XYZ
134
-
135
-
## Test application
136
-
137
-
Before you deploy the application to your AKS cluster, you test the application locally to make sure it works. Since your application will read subscription quota information using [Azure Quota API](/rest/api/reserved-vm-instances/quotaapi), you also need to make sure the `Microsoft.Quota` resource provider is registered in your subscription.
138
-
139
-
1. Log in to either *Tenant A* or *Tenant B* using the [`az account set`][az-account-set] command.
140
-
141
-
```azurecli-interactive
142
-
az account set --subscription $TENANT_A_SUBSCRIPTION_ID
143
-
```
206
+
`--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.
144
207
145
-
2. Check if the `Microsoft.Quota` resource provider is registered in your subscription using the [`az provider show`][az-provider-show] command.
208
+
## Deploy application to send messages to Azure Service Bus queue
146
209
147
-
```azurecli-interactive
148
-
az provider show --namespace Microsoft.Quota
149
-
```
210
+
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*.
150
211
151
-
If the `registrationState` is `Registered`, the resource provider is registered. If it's not registered, you can register it using the [`az provider register`][az-provider-register] command.
212
+
1. Log out of your *Tenant B* subscription using the [`az logout`][az-logout] command.
152
213
153
214
```azurecli-interactive
154
-
az provider register --namespace Microsoft.Quota
215
+
az logout
155
216
```
156
217
157
-
3. Once the registration state is `Registered`, you can test the application locally using the following commands:
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*.
158
219
159
220
```azurecli-interactive
160
-
XYZ
221
+
az login --tenant $TENANT_A_ID
161
222
```
162
223
163
-
## Deploy application to AKS cluster
164
-
165
-
Now that you confirmed the application works locally, you can push it to a container registry so that it can be pulled from within your AKS cluster.
166
-
167
-
1. XYZ
224
+
1. Ensure you are working with the correct subscription in *Tenant A* by using the [`az account set`][az-account-set] command.
168
225
169
226
```azurecli-interactive
170
-
XYZ
227
+
az account set --subscription $TENANT_A_SUBSCRIPTION_ID
171
228
```
172
229
173
-
2. Get the cluster credentials for the AKS cluster in *Tenant A* using the [`az aks get-credentials`][az-aks-get-credentials] command.
230
+
1. Get the cluster credentials for the AKS cluster in *Tenant A* using the [`az aks get-credentials`][az-aks-get-credentials] command.
174
231
175
232
```azurecli-interactive
176
233
az aks get-credentials --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME
177
234
```
178
235
179
-
3. Create a new Kubernetes service account in the `default` namespace with the client ID of your managed identity using the `kubectl apply` command. Make sure to replace the `<YOUR_USER_ASSIGNED_MANAGED_IDENTITY>` placeholder with the client ID of your managed identity in *Tenant B*.
236
+
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.
4. Create a new pod in the `default` namespace with the image name, the *Tenant B* tenant ID, and the *Tenant B* subscription ID using the `kubectl apply` command. Make sure to replace the placeholders with your own values.
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*.
5. Verify that the pod is running using the `kubectl get pods` command.
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.
225
284
226
285
```azurecli-interactive
227
-
kubectl get pods
286
+
# Get the dynamically generated pod name
287
+
POD_NAME=$(kubectl get po --selector job-name=myproducer -o jsonpath='{.items[0].metadata.name}')
288
+
289
+
# Get the tenant ID environment variable
290
+
kubectl describe pod $POD_NAME | grep AZURE_TENANT_ID
228
291
```
229
292
230
-
6. Check the logs of the pod to see if the application was able to read subscription information using the `kubectl logs` command.
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.
231
294
232
295
```azurecli-interactive
233
-
kubectl logs wi-demo-app
296
+
kubectl logs $POD_NAME
234
297
```
235
298
236
299
Your output should look similar to the following example output:
0 commit comments