@@ -22,6 +22,7 @@ import (
22
22
"strings"
23
23
24
24
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
25
+ "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
25
26
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
26
27
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
27
28
log "github.com/sirupsen/logrus"
@@ -65,10 +66,16 @@ func getConfig(configFile, resourceGroup, userAssignedIdentityClientID string) (
65
66
}
66
67
67
68
// getAccessToken retrieves Azure API access token.
68
- func getCredentials (cfg config ) (azcore.TokenCredential , error ) {
69
+ func getCredentials (cfg config ) (azcore.TokenCredential , * arm. ClientOptions , error ) {
69
70
cloudCfg , err := getCloudConfiguration (cfg .Cloud )
70
71
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 ,
72
79
}
73
80
74
81
// Try to retrieve token with service principal credentials.
@@ -83,25 +90,21 @@ func getCredentials(cfg config) (azcore.TokenCredential, error) {
83
90
! strings .EqualFold (cfg .ClientSecret , "msi" ) {
84
91
log .Info ("Using client_id+client_secret to retrieve access token for Azure API." )
85
92
opts := & azidentity.ClientSecretCredentialOptions {
86
- ClientOptions : azcore.ClientOptions {
87
- Cloud : cloudCfg ,
88
- },
93
+ ClientOptions : clientOpts ,
89
94
}
90
95
cred , err := azidentity .NewClientSecretCredential (cfg .TenantID , cfg .ClientID , cfg .ClientSecret , opts )
91
96
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 )
93
98
}
94
- return cred , nil
99
+ return cred , armClientOpts , nil
95
100
}
96
101
97
102
// Try to retrieve token with Workload Identity.
98
103
if cfg .UseWorkloadIdentityExtension {
99
104
log .Info ("Using workload identity extension to retrieve access token for Azure API." )
100
105
101
106
wiOpt := azidentity.WorkloadIdentityCredentialOptions {
102
- ClientOptions : azcore.ClientOptions {
103
- Cloud : cloudCfg ,
104
- },
107
+ ClientOptions : clientOpts ,
105
108
// In a standard scenario, Client ID and Tenant ID are expected to be read from environment variables.
106
109
// Though, in certain cases, it might be important to have an option to override those (e.g. when AZURE_TENANT_ID is not set
107
110
// 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) {
112
115
113
116
cred , err := azidentity .NewWorkloadIdentityCredential (& wiOpt )
114
117
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 )
116
119
}
117
120
118
- return cred , nil
121
+ return cred , armClientOpts , nil
119
122
}
120
123
121
124
// Try to retrieve token with MSI.
122
125
if cfg .UseManagedIdentityExtension {
123
126
log .Info ("Using managed identity extension to retrieve access token for Azure API." )
124
127
msiOpt := azidentity.ManagedIdentityCredentialOptions {
125
- ClientOptions : azcore.ClientOptions {
126
- Cloud : cloudCfg ,
127
- },
128
+ ClientOptions : clientOpts ,
128
129
}
129
130
if cfg .UserAssignedIdentityID != "" {
130
131
msiOpt .ID = azidentity .ClientID (cfg .UserAssignedIdentityID )
131
132
}
132
133
cred , err := azidentity .NewManagedIdentityCredential (& msiOpt )
133
134
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 )
135
136
}
136
- return cred , nil
137
+ return cred , armClientOpts , nil
137
138
}
138
139
139
- return nil , fmt .Errorf ("no credentials provided for Azure API" )
140
+ return nil , nil , fmt .Errorf ("no credentials provided for Azure API" )
140
141
}
141
142
142
143
func getCloudConfiguration (name string ) (cloud.Configuration , error ) {
0 commit comments