-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
az login support for Azure AD workload identity with Azure Kubernetes Service #26858
Comments
Thank you for opening this issue, we will look into it. |
Same issue, thanks for posting a workaround! Hope to see this fixed |
A similar feature request has been raised for Azure PowerShell: Azure/azure-powershell#22213
Supporting workload identity requires reading from these environment variables:
(Documented at https://azure.github.io/azure-workload-identity/docs/quick-start.html?highlight=AZURE_FEDERATED_TOKEN_FILE#7-deploy-workload) The AKS document https://learn.microsoft.com/en-us/azure/aks/workload-identity-overview#microsoft-authentication-library-msal provides samples that directly use MSAL for workload identity authentication. For example, the MSAL Python sample reads values from environment variables first: azure_client_id = os.getenv('AZURE_CLIENT_ID', '')
azure_tenant_id = os.getenv('AZURE_TENANT_ID', '')
azure_authority_host = os.getenv('AZURE_AUTHORITY_HOST', '')
azure_federated_token_file = os.getenv('AZURE_FEDERATED_TOKEN_FILE', '') Then it uses the same mechanism as Azure CLI - creating self.app = ConfidentialClientApplication(
azure_client_id,
client_credential={
'client_assertion': f.read().decode("utf-8")
},
authority="{}{}".format(azure_authority_host, azure_tenant_id)
) Azure CLI's code: azure-cli/src/azure-cli-core/azure/cli/core/auth/msal_authentication.py Lines 127 to 132 in 21266e0
The workaround provided in the issue description is the right path. In order for Azure CLI to natively support workload identity, it has to read those environment variables as the workaround does. But, we still need to work with MSAL team to decide whether this logic should be done by Azure CLI or MSAL. A prerequisite is to support environment credentials first: #10241 |
I'm interested in this as well, for the use case i am interested in, it would be to host azure devops agents in a k8s environment and use the workload identity for the service connection vs having to script this out and tightly couple the pipeline tasks to its hosting environment |
Same issue, thanks for posting a workaround! Hope to see this fixed |
Interested in this too. Would be great to have a native implementation of this in Azure CLI. |
az login --federated-token "$(cat $AZURE_FEDERATED_TOKEN_FILE)" --service-principal -u $AZURE_CLIENT_ID -t $AZURE_TENANT_ID how to get this federated token using the azure devops pipeline script task ? workload identity works fine with kubernetes , kubectl or helm task in the azure devops. But its not working in the azure cli task. |
|
Will this work in azure devops - AzureCLI@2 task ? |
Depends on your configuration - if you have followed the instructions on the workload identity page then it should work: https://azure.github.io/azure-workload-identity/docs/quick-start.html?highlight=AZURE_FEDERATED_TOKEN_FILE#7-deploy-workload. If you dont mind me asking have you configured workload identity in the agent? Otherwise you might have to enable an oidc auth task in the pipeline |
My scenario is different here, I have configured workload identity in azure devops service connection. this kubernetes plugin works fine using this aksinfralab service connection and can deploy the resource in k8s. (this service populating the federated token file in the agentpool)
when comes to azureCli task. I want to use this below. using this service connection I don't see the environment variables populated for the AZURE_FEDERATED_TOKEN_FILE in the agentpool
This is AD enabled cluster hence we need to use the kubelogin with any service principal. #agentpool is vm. |
How does this work with the |
For anyone interested in logging into the az cli using their own self generated tokens I've written up a series of blog posts on how to get the OIDC documents setup, provision associated azure resources, then login to az cli using the signed client assertion: https://finarne.wordpress.com/2024/07/25/acquire-an-entra-id-token-using-federated-credentials-part-1-oidc-discovery-documents/ |
+1 |
I was looking for an easy way to leverage a service account and to get a token for the managed identity. These two lines served my purpose. Maybe it provides help to others as well: $result = kubectl create token $SERVICEACCOUNT --namespace $NAMESPACE --audience api://AzureADTokenExchange
az login --federated-token $result --service-principal -u $CLIENT_ID -t $TENANT_ID The kubernetes cluster must have the OIDC token provider enabled. Interestingly the workload identity feature of AKS can be left disabled. |
I tried using the following but it does not work due to mismatch in audience. The audience in the token pointed by the az login --federated-token "$(cat $AZURE_FEDERATED_TOKEN_FILE)" --service-principal -u $AZURE_CLIENT_ID -t $AZURE_TENANT_ID The solution provided by @cveld works where it is getting token specifically for audience However, I cannot run |
Related command
az login
Is your feature request related to a problem? Please describe.
Currently if using workload identity and azure cli, az login must be specifically configured for login
Describe the solution you'd like
similar to az login --identity, maybe something like az login --workflow-identity
Describe alternatives you've considered
You can work around it currently using
az login --federated-token "$(cat $AZURE_FEDERATED_TOKEN_FILE)" --service-principal -u $AZURE_CLIENT_ID -t $AZURE_TENANT_ID
Additional context
For the client libraries a specific class is available
WorkloadIdentityCredential
More generally it would be nice if az login had a way to use something similar to
DefaultAzureCredential
which would allow scripts to be more portable depending on execution contextThe text was updated successfully, but these errors were encountered: