@@ -115,10 +115,15 @@ func NewClient(obj *sourcev1.Bucket, secret *corev1.Secret) (c *BlobClient, err
115
115
// Compose token chain based on environment.
116
116
// This functions as a replacement for azidentity.NewDefaultAzureCredential
117
117
// to not shell out.
118
- if token , err = chainCredentialWithSecret (secret ); err != nil {
118
+ token , err = chainCredentialWithSecret (secret )
119
+ if err != nil {
119
120
err = fmt .Errorf ("failed to create environment credential chain: %w" , err )
120
121
return nil , err
121
122
}
123
+ if token != nil {
124
+ c .ServiceClient , err = azblob .NewServiceClient (obj .Spec .Endpoint , token , nil )
125
+ return
126
+ }
122
127
123
128
// Fallback to simple client.
124
129
c .ServiceClient , err = azblob .NewServiceClientWithNoCredential (obj .Spec .Endpoint , nil )
@@ -353,6 +358,8 @@ func sharedCredentialFromSecret(endpoint string, secret *corev1.Secret) (*azblob
353
358
// azidentity.ChainedTokenCredential if at least one of the following tokens was
354
359
// successfully created:
355
360
// - azidentity.EnvironmentCredential
361
+ // - azidentity.ManagedIdentityCredential with Client ID from AZURE_CLIENT_ID
362
+ // environment variable, if found.
356
363
// - azidentity.ManagedIdentityCredential
357
364
// If a Secret with an `authorityHost` is provided, this is set on the
358
365
// azidentity.EnvironmentCredentialOptions. It may return nil.
@@ -369,6 +376,13 @@ func chainCredentialWithSecret(secret *corev1.Secret) (azcore.TokenCredential, e
369
376
if token , _ := azidentity .NewEnvironmentCredential (credOpts ); token != nil {
370
377
creds = append (creds , token )
371
378
}
379
+ if clientID := os .Getenv ("AZURE_CLIENT_ID" ); clientID != "" {
380
+ if token , _ := azidentity .NewManagedIdentityCredential (& azidentity.ManagedIdentityCredentialOptions {
381
+ ID : azidentity .ClientID (clientID ),
382
+ }); token != nil {
383
+ creds = append (creds , token )
384
+ }
385
+ }
372
386
if token , _ := azidentity .NewManagedIdentityCredential (nil ); token != nil {
373
387
creds = append (creds , token )
374
388
}
0 commit comments