Skip to content

Commit 61f756f

Browse files
committed
Add ManagedIdentity with AZURE_CLIENT_ID
This ensures the Managed Identity authentication works with multiple identities assigned to a single node. Signed-off-by: Hidde Beydals <[email protected]>
1 parent c4fb52f commit 61f756f

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

pkg/azure/blob.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,16 @@ type BlobClient struct {
6666
//
6767
// - azidentity.ClientSecretCredential when `tenantId`, `clientId` and
6868
// `clientSecret` fields are found.
69-
// - azidentity.ClientSecretCredential when `tenant`, `appId` and `password`
70-
// fields are found. To match with the JSON from:
71-
// https://docs.microsoft.com/en-us/azure/aks/kubernetes-service-principal?tabs=azure-cli#manually-create-a-service-principal
7269
// - azidentity.ClientCertificateCredential when `tenantId`,
7370
// `clientCertificate` (and optionally `clientCertificatePassword`) fields
7471
// are found.
7572
// - azidentity.ManagedIdentityCredential for a User ID, when a `clientId`
7673
// field but no `tenantId` is found.
77-
// - azidentity.ManagedIdentityCredential for a Resource ID, when a
78-
// `resourceId` field is found.
79-
// - azblob.SharedKeyCredential when an `accountKey` field is found.
74+
// - azblob.SharedKeyCredential when an `accountKey` field is found.
8075
// The account name is extracted from the endpoint specified on the Bucket
8176
// object.
8277
// - azidentity.ChainedTokenCredential with azidentity.EnvironmentCredential
83-
// and azidentity.ManagedIdentityCredential with defaults if no Secret is
84-
// given.
78+
// and azidentity.ManagedIdentityCredential.
8579
//
8680
// If no credentials are found, and the azidentity.ChainedTokenCredential can
8781
// not be established. A simple client without credentials is returned.
@@ -292,16 +286,11 @@ func (c *BlobClient) ObjectIsNotFound(err error) bool {
292286
// based on the data fields of the given Secret. It returns, in order:
293287
// - azidentity.ClientSecretCredential when `tenantId`, `clientId` and
294288
// `clientSecret` fields are found.
295-
// - azidentity.ClientSecretCredential when `tenant`, `appId` and `password`
296-
// fields are found. To match with the JSON from:
297-
// https://docs.microsoft.com/en-us/azure/aks/kubernetes-service-principal?tabs=azure-cli#manually-create-a-service-principal
298289
// - azidentity.ClientCertificateCredential when `tenantId`,
299290
// `clientCertificate` (and optionally `clientCertificatePassword`) fields
300291
// are found.
301292
// - azidentity.ManagedIdentityCredential for a User ID, when a `clientId`
302293
// field but no `tenantId` is found.
303-
// - azidentity.ManagedIdentityCredential for a Resource ID, when a
304-
// `resourceId` field is found.
305294
// - Nil, if no valid set of credential fields was found.
306295
func tokenCredentialFromSecret(secret *corev1.Secret) (azcore.TokenCredential, error) {
307296
if secret == nil {
@@ -357,10 +346,14 @@ func sharedCredentialFromSecret(endpoint string, secret *corev1.Secret) (*azblob
357346
// chainCredentialWithSecret tries to create a set of tokens, and returns an
358347
// azidentity.ChainedTokenCredential if at least one of the following tokens was
359348
// successfully created:
360-
// - azidentity.EnvironmentCredential
361-
// - azidentity.ManagedIdentityCredential
362-
// If a Secret with an `authorityHost` is provided, this is set on the
363-
// azidentity.EnvironmentCredentialOptions. It may return nil.
349+
//
350+
// - azidentity.EnvironmentCredential with `authorityHost` from Secret, if
351+
// provided.
352+
// - azidentity.ManagedIdentityCredential with Client ID from AZURE_CLIENT_ID
353+
// environment variable, if found.
354+
// - azidentity.ManagedIdentityCredential with defaults.
355+
//
356+
// If no valid token is created, it returns nil.
364357
func chainCredentialWithSecret(secret *corev1.Secret) (azcore.TokenCredential, error) {
365358
var creds []azcore.TokenCredential
366359

@@ -374,6 +367,13 @@ func chainCredentialWithSecret(secret *corev1.Secret) (azcore.TokenCredential, e
374367
if token, _ := azidentity.NewEnvironmentCredential(credOpts); token != nil {
375368
creds = append(creds, token)
376369
}
370+
if clientID := os.Getenv("AZURE_CLIENT_ID"); clientID != "" {
371+
if token, _ := azidentity.NewManagedIdentityCredential(&azidentity.ManagedIdentityCredentialOptions{
372+
ID: azidentity.ClientID(clientID),
373+
}); token != nil {
374+
creds = append(creds, token)
375+
}
376+
}
377377
if token, _ := azidentity.NewManagedIdentityCredential(nil); token != nil {
378378
creds = append(creds, token)
379379
}

0 commit comments

Comments
 (0)