@@ -22,6 +22,7 @@ import (
2222 "strings"
2323
2424 "github.com/Azure/azure-sdk-for-go/sdk/azcore"
25+ "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
2526 "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
2627 "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
2728 log "github.com/sirupsen/logrus"
@@ -65,10 +66,16 @@ func getConfig(configFile, resourceGroup, userAssignedIdentityClientID string) (
6566}
6667
6768// getAccessToken retrieves Azure API access token.
68- func getCredentials (cfg config ) (azcore.TokenCredential , error ) {
69+ func getCredentials (cfg config ) (azcore.TokenCredential , * arm. ClientOptions , error ) {
6970 cloudCfg , err := getCloudConfiguration (cfg .Cloud )
7071 if err != nil {
71- return nil , fmt .Errorf ("failed to get cloud configuration: %w" , err )
72+ return nil , nil , fmt .Errorf ("failed to get cloud configuration: %w" , err )
73+ }
74+ clientOpts := azcore.ClientOptions {
75+ Cloud : cloudCfg ,
76+ }
77+ armClientOpts := & arm.ClientOptions {
78+ ClientOptions : clientOpts ,
7279 }
7380
7481 // Try to retrieve token with service principal credentials.
@@ -83,25 +90,21 @@ func getCredentials(cfg config) (azcore.TokenCredential, error) {
8390 ! strings .EqualFold (cfg .ClientSecret , "msi" ) {
8491 log .Info ("Using client_id+client_secret to retrieve access token for Azure API." )
8592 opts := & azidentity.ClientSecretCredentialOptions {
86- ClientOptions : azcore.ClientOptions {
87- Cloud : cloudCfg ,
88- },
93+ ClientOptions : clientOpts ,
8994 }
9095 cred , err := azidentity .NewClientSecretCredential (cfg .TenantID , cfg .ClientID , cfg .ClientSecret , opts )
9196 if err != nil {
92- return nil , fmt .Errorf ("failed to create service principal token: %w" , err )
97+ return nil , nil , fmt .Errorf ("failed to create service principal token: %w" , err )
9398 }
94- return cred , nil
99+ return cred , armClientOpts , nil
95100 }
96101
97102 // Try to retrieve token with Workload Identity.
98103 if cfg .UseWorkloadIdentityExtension {
99104 log .Info ("Using workload identity extension to retrieve access token for Azure API." )
100105
101106 wiOpt := azidentity.WorkloadIdentityCredentialOptions {
102- ClientOptions : azcore.ClientOptions {
103- Cloud : cloudCfg ,
104- },
107+ ClientOptions : clientOpts ,
105108 // In a standard scenario, Client ID and Tenant ID are expected to be read from environment variables.
106109 // Though, in certain cases, it might be important to have an option to override those (e.g. when AZURE_TENANT_ID is not set
107110 // through a webhook or azure.workload.identity/client-id service account annotation is absent). When any of those values are
@@ -112,31 +115,29 @@ func getCredentials(cfg config) (azcore.TokenCredential, error) {
112115
113116 cred , err := azidentity .NewWorkloadIdentityCredential (& wiOpt )
114117 if err != nil {
115- return nil , fmt .Errorf ("failed to create a workload identity token: %w" , err )
118+ return nil , nil , fmt .Errorf ("failed to create a workload identity token: %w" , err )
116119 }
117120
118- return cred , nil
121+ return cred , armClientOpts , nil
119122 }
120123
121124 // Try to retrieve token with MSI.
122125 if cfg .UseManagedIdentityExtension {
123126 log .Info ("Using managed identity extension to retrieve access token for Azure API." )
124127 msiOpt := azidentity.ManagedIdentityCredentialOptions {
125- ClientOptions : azcore.ClientOptions {
126- Cloud : cloudCfg ,
127- },
128+ ClientOptions : clientOpts ,
128129 }
129130 if cfg .UserAssignedIdentityID != "" {
130131 msiOpt .ID = azidentity .ClientID (cfg .UserAssignedIdentityID )
131132 }
132133 cred , err := azidentity .NewManagedIdentityCredential (& msiOpt )
133134 if err != nil {
134- return nil , fmt .Errorf ("failed to create the managed service identity token: %w" , err )
135+ return nil , nil , fmt .Errorf ("failed to create the managed service identity token: %w" , err )
135136 }
136- return cred , nil
137+ return cred , armClientOpts , nil
137138 }
138139
139- return nil , fmt .Errorf ("no credentials provided for Azure API" )
140+ return nil , nil , fmt .Errorf ("no credentials provided for Azure API" )
140141}
141142
142143func getCloudConfiguration (name string ) (cloud.Configuration , error ) {
0 commit comments