Skip to content

Commit ea9026a

Browse files
authored
[3.0] Change default AZ creds to AZ CLI creds. (microsoft#14477)
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 66abf2e commit ea9026a

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()
@@ -80,7 +80,7 @@ func (abs *AzureBlobStorage) Download(
8080

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

8686
downloadEndTime := time.Now()
@@ -97,7 +97,7 @@ func (abs *AzureBlobStorage) Delete(
9797
deleteStartTime := time.Now()
9898
_, err = abs.theClient.DeleteBlob(ctx, containerName, blobName, nil)
9999
if err != nil {
100-
return fmt.Errorf("Failed to delete blob:\n%w", err)
100+
return fmt.Errorf("failed to delete blob:\n%w", err)
101101
}
102102
deleteEndTime := time.Now()
103103
logger.Log.Infof(" delete time: %v", deleteEndTime.Sub(deleteStartTime))
@@ -106,49 +106,45 @@ func (abs *AzureBlobStorage) Delete(
106106
}
107107

108108
func Create(tenantId string, userName string, password string, storageAccount string, authenticationType int) (abs *AzureBlobStorage, err error) {
109-
110109
url := "https://" + storageAccount + ".blob.core.windows.net/"
111110

112111
abs = &AzureBlobStorage{}
113112

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

121120
return abs, nil
122121

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

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

135133
return abs, nil
136134

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

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

149146
return abs, nil
150-
151147
}
152148

153-
return nil, errors.New("Unknown authentication type.")
149+
return nil, errors.New("unknown authentication type")
154150
}

toolkit/tools/internal/ccachemanager/ccachemanager.go

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

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

0 commit comments

Comments
 (0)