Skip to content

Commit cc805b4

Browse files
committed
Change Azure authentication order
Based on recommendations from Microsoft, change the order valid authentication options are taken into account. Mainly to ensure it works as expected when multiple Managed Identities are bound on the same VM node. Signed-off-by: Hidde Beydals <[email protected]>
1 parent 37e602a commit cc805b4

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

pkg/azure/blob.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ type BlobClient struct {
6363
// Bucket and Secret. It detects credentials in the Secret in the following
6464
// order:
6565
//
66-
// - azidentity.ManagedIdentityCredential for a Resource ID, when a
67-
// `resourceId` field is found.
68-
// - azidentity.ManagedIdentityCredential for a User ID, when a `clientId`
69-
// field but no `tenantId` is found.
66+
// - azidentity.ClientSecretCredential when `tenantId`, `clientId` and
67+
// `clientSecret` fields are found.
7068
// - azidentity.ClientCertificateCredential when `tenantId`,
7169
// `clientCertificate` (and optionally `clientCertificatePassword`) fields
7270
// are found.
73-
// - azidentity.ClientSecretCredential when `tenantId`, `clientId` and
74-
// `clientSecret` fields are found.
71+
// - azidentity.ManagedIdentityCredential for a User ID, when a `clientId`
72+
// field but no `tenantId` is found.
73+
// - azidentity.ManagedIdentityCredential for a Resource ID, when a
74+
// `resourceId` field is found.
7575
// - azblob.SharedKeyCredential when an `accountKey` field is found.
7676
// The account name is extracted from the endpoint specified on the Bucket
7777
// object.
@@ -271,31 +271,30 @@ func (c *BlobClient) ObjectIsNotFound(err error) bool {
271271
}
272272

273273
func tokenCredentialFromSecret(secret *corev1.Secret) (azcore.TokenCredential, error) {
274-
var token azcore.TokenCredential
275-
if resourceID, ok := secret.Data[resourceIDField]; ok {
276-
return azidentity.NewManagedIdentityCredential(&azidentity.ManagedIdentityCredentialOptions{
277-
ID: azidentity.ResourceID(resourceID),
278-
})
279-
}
280-
if clientID, hasClientID := secret.Data[clientIDField]; hasClientID {
281-
tenantID, hasTenantID := secret.Data[tenantIDField]
282-
if !hasTenantID {
283-
return azidentity.NewManagedIdentityCredential(&azidentity.ManagedIdentityCredentialOptions{
284-
ID: azidentity.ClientID(clientID),
285-
})
274+
clientID, hasClientID := secret.Data[clientIDField]
275+
if tenantID, hasTenantID := secret.Data[tenantIDField]; hasTenantID && hasClientID {
276+
if clientSecret, hasClientSecret := secret.Data[clientSecretField]; hasClientSecret && len(clientSecret) > 0 {
277+
return azidentity.NewClientSecretCredential(string(tenantID), string(clientID), string(clientSecret), nil)
286278
}
287-
if clientCertificate, hasClientCertificate := secret.Data[clientCertificateField]; hasClientCertificate {
279+
if clientCertificate, hasClientCertificate := secret.Data[clientCertificateField]; hasClientCertificate && len(clientCertificate) > 0 {
288280
certs, key, err := azidentity.ParseCertificates(clientCertificate, secret.Data[clientCertificatePasswordField])
289281
if err != nil {
290282
return nil, fmt.Errorf("failed to parse client certificates: %w", err)
291283
}
292284
return azidentity.NewClientCertificateCredential(string(tenantID), string(clientID), certs, key, nil)
293285
}
294-
if clientSecret, hasClientSecret := secret.Data[clientSecretField]; hasClientSecret {
295-
return azidentity.NewClientSecretCredential(string(tenantID), string(clientID), string(clientSecret), nil)
296-
}
297286
}
298-
return token, nil
287+
if hasClientID {
288+
return azidentity.NewManagedIdentityCredential(&azidentity.ManagedIdentityCredentialOptions{
289+
ID: azidentity.ClientID(clientID),
290+
})
291+
}
292+
if resourceID, hasResourceID := secret.Data[resourceIDField]; hasResourceID {
293+
return azidentity.NewManagedIdentityCredential(&azidentity.ManagedIdentityCredentialOptions{
294+
ID: azidentity.ResourceID(resourceID),
295+
})
296+
}
297+
return nil, nil
299298
}
300299

301300
func sharedCredentialFromSecret(endpoint string, secret *corev1.Secret) (*azblob.SharedKeyCredential, error) {

0 commit comments

Comments
 (0)