Skip to content

Commit 23b07c4

Browse files
authored
[2.0] Change default AZ creds to AZ CLI creds. (#14478)
Internal Microsoft policy requires us to not use NewDefaultAzureCredential when logging into Azure. In all cases where we used the default method in our builds we relied on Azure CLI credentials, thus the switch to NewAzureCLICredential. For more information see the AzureCLICredential docs. The change also has minor Go linting clean-up.
1 parent 6c66789 commit 23b07c4

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

toolkit/tools/internal/azureblobstorage/azureblobstorage.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
const (
2020
AnonymousAccess = 0
2121
ServicePrincipalAccess = 1
22-
ManagedIdentityAccess = 2
22+
AzureCLIAccess = 2
2323
)
2424

2525
type AzureBlobStorage struct {
@@ -36,13 +36,13 @@ func (abs *AzureBlobStorage) Upload(
3636

3737
localFile, err := os.OpenFile(localFileName, os.O_RDONLY, 0)
3838
if err != nil {
39-
return fmt.Errorf("Failed to open local file for upload:\n%w", err)
39+
return fmt.Errorf("failed to open local file for upload:\n%w", err)
4040
}
4141
defer localFile.Close()
4242

4343
_, err = abs.theClient.UploadFile(ctx, containerName, blobName, localFile, nil)
4444
if err != nil {
45-
return fmt.Errorf("Failed to upload local file to blob:\n%w", err)
45+
return fmt.Errorf("failed to upload local file to blob:\n%w", err)
4646
}
4747

4848
uploadEndTime := time.Now()
@@ -77,7 +77,7 @@ func (abs *AzureBlobStorage) Download(
7777

7878
_, err = abs.theClient.DownloadFile(ctx, containerName, blobName, localFile, nil)
7979
if err != nil {
80-
return fmt.Errorf("Failed to download blob to local file:\n%w", err)
80+
return fmt.Errorf("failed to download blob to local file:\n%w", err)
8181
}
8282

8383
downloadEndTime := time.Now()
@@ -94,7 +94,7 @@ func (abs *AzureBlobStorage) Delete(
9494
deleteStartTime := time.Now()
9595
_, err = abs.theClient.DeleteBlob(ctx, containerName, blobName, nil)
9696
if err != nil {
97-
return fmt.Errorf("Failed to delete blob:\n%w", err)
97+
return fmt.Errorf("failed to delete blob:\n%w", err)
9898
}
9999
deleteEndTime := time.Now()
100100
logger.Log.Infof(" delete time: %v", deleteEndTime.Sub(deleteStartTime))
@@ -103,49 +103,45 @@ func (abs *AzureBlobStorage) Delete(
103103
}
104104

105105
func Create(tenantId string, userName string, password string, storageAccount string, authenticationType int) (abs *AzureBlobStorage, err error) {
106-
107106
url := "https://" + storageAccount + ".blob.core.windows.net/"
108107

109108
abs = &AzureBlobStorage{}
110109

111-
if authenticationType == AnonymousAccess {
112-
110+
switch authenticationType {
111+
case AnonymousAccess:
113112
abs.theClient, err = azblob.NewClientWithNoCredential(url, nil)
114113
if err != nil {
115-
return nil, fmt.Errorf("Unable to init azure blob storage read-only client:\n%w", err)
114+
return nil, fmt.Errorf("unable to init azure blob storage read-only client:\n%w", err)
116115
}
117116

118117
return abs, nil
119118

120-
} else if authenticationType == ServicePrincipalAccess {
121-
119+
case ServicePrincipalAccess:
122120
credential, err := azidentity.NewClientSecretCredential(tenantId, userName, password, nil)
123121
if err != nil {
124-
return nil, fmt.Errorf("Unable to init azure service principal identity:\n%w", err)
122+
return nil, fmt.Errorf("unable to init azure service principal identity:\n%w", err)
125123
}
126124

127125
abs.theClient, err = azblob.NewClient(url, credential, nil)
128126
if err != nil {
129-
return nil, fmt.Errorf("Unable to init azure blob storage read-write client:\n%w", err)
127+
return nil, fmt.Errorf("unable to init azure blob storage read-write client:\n%w", err)
130128
}
131129

132130
return abs, nil
133131

134-
} else if authenticationType == ManagedIdentityAccess {
135-
136-
credential, err := azidentity.NewDefaultAzureCredential(nil)
132+
case AzureCLIAccess:
133+
credential, err := azidentity.NewAzureCLICredential(nil)
137134
if err != nil {
138-
return nil, fmt.Errorf("Unable to init azure managed identity:\n%w", err)
135+
return nil, fmt.Errorf("unable to init azure managed identity:\n%w", err)
139136
}
140137

141138
abs.theClient, err = azblob.NewClient(url, credential, nil)
142139
if err != nil {
143-
return nil, fmt.Errorf("Unable to init azure blob storage read-write client:\n%w", err)
140+
return nil, fmt.Errorf("unable to init azure blob storage read-write client:\n%w", err)
144141
}
145142

146143
return abs, nil
147-
148144
}
149145

150-
return nil, errors.New("Unknown authentication type.")
146+
return nil, errors.New("unknown authentication type")
151147
}

toolkit/tools/internal/ccachemanager/ccachemanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ func CreateManager(rootDir string, configFileName string) (m *CCacheManager, err
456456
logger.Log.Infof(" creating blob storage client...")
457457
accessType := azureblobstorage.AnonymousAccess
458458
if configuration.RemoteStoreConfig.UploadEnabled {
459-
accessType = azureblobstorage.ManagedIdentityAccess
459+
accessType = azureblobstorage.AzureCLIAccess
460460
}
461461

462462
azureBlobStorage, err := azureblobstorage.Create(configuration.RemoteStoreConfig.TenantId, configuration.RemoteStoreConfig.UserName, configuration.RemoteStoreConfig.Password, configuration.RemoteStoreConfig.StorageAccount, accessType)

0 commit comments

Comments
 (0)