Skip to content

Commit 58fd166

Browse files
authored
blob/azureblob: Use azidentity.NewDefaultAzureCredential the default/fallback (#3161)
1 parent bb5165b commit 58fd166

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

blob/azureblob/azureblob.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
// - If none of the above are provided, azureblob defaults to
4141
// azidentity.NewDefaultAzureCredential:
4242
// https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#NewDefaultAzureCredential.
43-
// See the documentation there for the environment variables it supports,
44-
// including AZURE_CLIENT_ID, AZURE_TENANT_ID, etc.
43+
// See the documentation there for the credential types it supports, including
44+
// CLI creds, environment variables like AZURE_CLIENT_ID, AZURE_TENANT_ID, etc.
4545
//
46-
// In addition, the environment variables AZURE_STORAGE_DOMAIN,
46+
// In addition, the environment variables AZURE_STORAGE_ACCOUNT, AZURE_STORAGE_DOMAIN,
4747
// AZURE_STORAGE_PROTOCOL, AZURE_STORAGE_IS_CDN, and AZURE_STORAGE_IS_LOCAL_EMULATOR
4848
// can be used to configure how the default URLOpener generates the Azure
4949
// Service URL via ServiceURLOptions. These can all be configured via URL
@@ -289,10 +289,10 @@ func (o *lazyOpener) OpenBucketURL(ctx context.Context, u *url.URL) (*blob.Bucke
289289
type credTypeEnumT int
290290

291291
const (
292-
credTypeSharedKey credTypeEnumT = iota
292+
credTypeDefault credTypeEnumT = iota
293+
credTypeSharedKey
293294
credTypeSASViaNone
294295
credTypeConnectionString
295-
credTypeIdentityFromEnv
296296
)
297297

298298
type credInfoT struct {
@@ -327,7 +327,7 @@ func newCredInfoFromEnv() *credInfoT {
327327
credInfo.CredType = credTypeConnectionString
328328
credInfo.ConnectionString = connectionString
329329
} else {
330-
credInfo.CredType = credTypeIdentityFromEnv
330+
credInfo.CredType = credTypeDefault
331331
}
332332
return credInfo
333333
}
@@ -341,6 +341,13 @@ func (i *credInfoT) NewServiceClient(svcURL ServiceURL) (*azblob.ServiceClient,
341341
}
342342

343343
switch i.CredType {
344+
case credTypeDefault:
345+
log.Println("azureblob.URLOpener: using NewDefaultAzureCredential")
346+
cred, err := azidentity.NewDefaultAzureCredential(nil)
347+
if err != nil {
348+
return nil, fmt.Errorf("failed azidentity.NewDefaultAzureCredential: %v", err)
349+
}
350+
return azblob.NewServiceClient(string(svcURL), cred, azClientOpts)
344351
case credTypeSharedKey:
345352
log.Println("azureblob.URLOpener: using shared key credentials")
346353
sharedKeyCred, err := azblob.NewSharedKeyCredential(i.AccountName, i.AccountKey)
@@ -354,13 +361,6 @@ func (i *credInfoT) NewServiceClient(svcURL ServiceURL) (*azblob.ServiceClient,
354361
case credTypeConnectionString:
355362
log.Println("azureblob.URLOpener: using connection string")
356363
return azblob.NewServiceClientFromConnectionString(i.ConnectionString, azClientOpts)
357-
case credTypeIdentityFromEnv:
358-
log.Println("azureblob.URLOpener: using NewEnvironmentCredentials")
359-
cred, err := azidentity.NewEnvironmentCredential(nil)
360-
if err != nil {
361-
return nil, fmt.Errorf("failed azidentity.NewEnvironmentCredential: %v", err)
362-
}
363-
return azblob.NewServiceClient(string(svcURL), cred, azClientOpts)
364364
default:
365365
return nil, errors.New("internal error, unknown cred type")
366366
}

blob/azureblob/azureblob_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ func TestOpenerFromEnv(t *testing.T) {
370370
// Default.
371371
accountName: "anotheraccount",
372372
want: &credInfoT{
373-
CredType: credTypeIdentityFromEnv,
373+
CredType: credTypeDefault,
374374
AccountName: "anotheraccount",
375375
},
376376
wantOpts: &ServiceURLOptions{
@@ -383,7 +383,7 @@ func TestOpenerFromEnv(t *testing.T) {
383383
protocol: "http",
384384
domain: "foo.bar.com",
385385
want: &credInfoT{
386-
CredType: credTypeIdentityFromEnv,
386+
CredType: credTypeDefault,
387387
AccountName: "myaccount",
388388
},
389389
wantOpts: &ServiceURLOptions{
@@ -397,7 +397,7 @@ func TestOpenerFromEnv(t *testing.T) {
397397
accountName: "myaccount",
398398
isLocalEmulator: true,
399399
want: &credInfoT{
400-
CredType: credTypeIdentityFromEnv,
400+
CredType: credTypeDefault,
401401
AccountName: "myaccount",
402402
},
403403
wantOpts: &ServiceURLOptions{

0 commit comments

Comments
 (0)