Skip to content

Commit 7de484d

Browse files
committed
Enable Azure MSI authentication
1 parent a308c62 commit 7de484d

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

pkg/storage/azure/azure.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -341,17 +341,33 @@ func (d *driver) storageAccountsClient(cfg *Azure, environment autorestazure.Env
341341
err error
342342
)
343343
if strings.TrimSpace(cfg.ClientSecret) == "" {
344-
options := azidentity.WorkloadIdentityCredentialOptions{
345-
ClientOptions: azcore.ClientOptions{
346-
Cloud: cloudConfig,
347-
},
348-
ClientID: cfg.ClientID,
349-
TenantID: cfg.TenantID,
350-
TokenFilePath: cfg.FederatedTokenFile,
351-
}
352-
cred, err = azidentity.NewWorkloadIdentityCredential(&options)
353-
if err != nil {
354-
return storage.AccountsClient{}, err
344+
if strings.TrimSpace(cfg.FederatedTokenFile) != "" {
345+
options := azidentity.WorkloadIdentityCredentialOptions{
346+
ClientOptions: azcore.ClientOptions{
347+
Cloud: cloudConfig,
348+
},
349+
ClientID: cfg.ClientID,
350+
TenantID: cfg.TenantID,
351+
TokenFilePath: cfg.FederatedTokenFile,
352+
}
353+
cred, err = azidentity.NewWorkloadIdentityCredential(&options)
354+
if err != nil {
355+
return storage.AccountsClient{}, err
356+
}
357+
} else {
358+
options := azidentity.ManagedIdentityCredentialOptions{
359+
ClientOptions: azcore.ClientOptions{
360+
Cloud: cloudConfig,
361+
},
362+
}
363+
if cfg.ClientID != "" {
364+
options.ID = azidentity.ClientID(cfg.ClientID)
365+
}
366+
var err error
367+
cred, err = azidentity.NewManagedIdentityCredential(&options)
368+
if err != nil {
369+
return storage.AccountsClient{}, err
370+
}
355371
}
356372
} else {
357373
options := azidentity.ClientSecretCredentialOptions{

pkg/storage/azure/azureclient/azureclient.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,31 @@ func New(opts *Options) (*Client, error) {
8686
if creds == nil {
8787
var err error
8888
if strings.TrimSpace(opts.ClientSecret) == "" {
89-
options := azidentity.WorkloadIdentityCredentialOptions{
90-
ClientOptions: coreOpts,
91-
ClientID: opts.ClientID,
92-
TenantID: opts.TenantID,
93-
TokenFilePath: opts.FederatedTokenFile,
94-
}
95-
creds, err = azidentity.NewWorkloadIdentityCredential(&options)
96-
if err != nil {
97-
return nil, err
89+
if strings.TrimSpace(opts.FederatedTokenFile) != "" {
90+
options := azidentity.WorkloadIdentityCredentialOptions{
91+
ClientOptions: coreOpts,
92+
ClientID: opts.ClientID,
93+
TenantID: opts.TenantID,
94+
TokenFilePath: opts.FederatedTokenFile,
95+
}
96+
creds, err = azidentity.NewWorkloadIdentityCredential(&options)
97+
if err != nil {
98+
return nil, err
99+
}
100+
} else {
101+
options := azidentity.ManagedIdentityCredentialOptions{
102+
ClientOptions: azcore.ClientOptions{
103+
Cloud: cloudConfig,
104+
},
105+
}
106+
if opts.ClientID != "" {
107+
options.ID = azidentity.ClientID(opts.ClientID)
108+
}
109+
var err error
110+
creds, err = azidentity.NewManagedIdentityCredential(&options)
111+
if err != nil {
112+
return nil, err
113+
}
98114
}
99115
} else {
100116
options := azidentity.ClientSecretCredentialOptions{

0 commit comments

Comments
 (0)