Skip to content

Commit

Permalink
blob/azureblob: Use azidentity.NewDefaultAzureCredential the default/…
Browse files Browse the repository at this point in the history
…fallback (#3161)
  • Loading branch information
vangent committed Aug 11, 2022
1 parent bb5165b commit 58fd166
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
26 changes: 13 additions & 13 deletions blob/azureblob/azureblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
// - If none of the above are provided, azureblob defaults to
// azidentity.NewDefaultAzureCredential:
// https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#NewDefaultAzureCredential.
// See the documentation there for the environment variables it supports,
// including AZURE_CLIENT_ID, AZURE_TENANT_ID, etc.
// See the documentation there for the credential types it supports, including
// CLI creds, environment variables like AZURE_CLIENT_ID, AZURE_TENANT_ID, etc.
//
// In addition, the environment variables AZURE_STORAGE_DOMAIN,
// In addition, the environment variables AZURE_STORAGE_ACCOUNT, AZURE_STORAGE_DOMAIN,
// AZURE_STORAGE_PROTOCOL, AZURE_STORAGE_IS_CDN, and AZURE_STORAGE_IS_LOCAL_EMULATOR
// can be used to configure how the default URLOpener generates the Azure
// Service URL via ServiceURLOptions. These can all be configured via URL
Expand Down Expand Up @@ -289,10 +289,10 @@ func (o *lazyOpener) OpenBucketURL(ctx context.Context, u *url.URL) (*blob.Bucke
type credTypeEnumT int

const (
credTypeSharedKey credTypeEnumT = iota
credTypeDefault credTypeEnumT = iota
credTypeSharedKey
credTypeSASViaNone
credTypeConnectionString
credTypeIdentityFromEnv
)

type credInfoT struct {
Expand Down Expand Up @@ -327,7 +327,7 @@ func newCredInfoFromEnv() *credInfoT {
credInfo.CredType = credTypeConnectionString
credInfo.ConnectionString = connectionString
} else {
credInfo.CredType = credTypeIdentityFromEnv
credInfo.CredType = credTypeDefault
}
return credInfo
}
Expand All @@ -341,6 +341,13 @@ func (i *credInfoT) NewServiceClient(svcURL ServiceURL) (*azblob.ServiceClient,
}

switch i.CredType {
case credTypeDefault:
log.Println("azureblob.URLOpener: using NewDefaultAzureCredential")
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return nil, fmt.Errorf("failed azidentity.NewDefaultAzureCredential: %v", err)
}
return azblob.NewServiceClient(string(svcURL), cred, azClientOpts)
case credTypeSharedKey:
log.Println("azureblob.URLOpener: using shared key credentials")
sharedKeyCred, err := azblob.NewSharedKeyCredential(i.AccountName, i.AccountKey)
Expand All @@ -354,13 +361,6 @@ func (i *credInfoT) NewServiceClient(svcURL ServiceURL) (*azblob.ServiceClient,
case credTypeConnectionString:
log.Println("azureblob.URLOpener: using connection string")
return azblob.NewServiceClientFromConnectionString(i.ConnectionString, azClientOpts)
case credTypeIdentityFromEnv:
log.Println("azureblob.URLOpener: using NewEnvironmentCredentials")
cred, err := azidentity.NewEnvironmentCredential(nil)
if err != nil {
return nil, fmt.Errorf("failed azidentity.NewEnvironmentCredential: %v", err)
}
return azblob.NewServiceClient(string(svcURL), cred, azClientOpts)
default:
return nil, errors.New("internal error, unknown cred type")
}
Expand Down
6 changes: 3 additions & 3 deletions blob/azureblob/azureblob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func TestOpenerFromEnv(t *testing.T) {
// Default.
accountName: "anotheraccount",
want: &credInfoT{
CredType: credTypeIdentityFromEnv,
CredType: credTypeDefault,
AccountName: "anotheraccount",
},
wantOpts: &ServiceURLOptions{
Expand All @@ -383,7 +383,7 @@ func TestOpenerFromEnv(t *testing.T) {
protocol: "http",
domain: "foo.bar.com",
want: &credInfoT{
CredType: credTypeIdentityFromEnv,
CredType: credTypeDefault,
AccountName: "myaccount",
},
wantOpts: &ServiceURLOptions{
Expand All @@ -397,7 +397,7 @@ func TestOpenerFromEnv(t *testing.T) {
accountName: "myaccount",
isLocalEmulator: true,
want: &credInfoT{
CredType: credTypeIdentityFromEnv,
CredType: credTypeDefault,
AccountName: "myaccount",
},
wantOpts: &ServiceURLOptions{
Expand Down

0 comments on commit 58fd166

Please sign in to comment.