Skip to content

Commit 4e8c404

Browse files
authored
Merge pull request #1318 from cvvz/fix-workload-identity
fix: workload identity did not work
2 parents 4d4e1ca + c95933b commit 4e8c404

File tree

2 files changed

+94
-35
lines changed

2 files changed

+94
-35
lines changed

pkg/blob/azure.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,18 @@ func GetCloudProvider(ctx context.Context, kubeClient kubernetes.Interface, node
103103
} else {
104104
config.UserAgent = userAgent
105105
config.CloudProviderBackoff = true
106-
if err = az.InitializeCloudFromConfig(context.TODO(), config, fromSecret, false); err != nil {
106+
// these environment variables are injected by workload identity webhook
107+
if tenantID := os.Getenv("AZURE_TENANT_ID"); tenantID != "" {
108+
config.TenantID = tenantID
109+
}
110+
if clientID := os.Getenv("AZURE_CLIENT_ID"); clientID != "" {
111+
config.AADClientID = clientID
112+
}
113+
if federatedTokenFile := os.Getenv("AZURE_FEDERATED_TOKEN_FILE"); federatedTokenFile != "" {
114+
config.AADFederatedTokenFile = federatedTokenFile
115+
config.UseFederatedWorkloadIdentityExtension = true
116+
}
117+
if err = az.InitializeCloudFromConfig(ctx, config, fromSecret, false); err != nil {
107118
klog.Warningf("InitializeCloudFromConfig failed with error: %v", err)
108119
}
109120
}

pkg/blob/azure_test.go

Lines changed: 82 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,48 +77,87 @@ users:
7777
}()
7878

7979
tests := []struct {
80-
desc string
81-
createFakeCredFile bool
82-
createFakeKubeConfig bool
83-
kubeconfig string
84-
nodeID string
85-
userAgent string
86-
allowEmptyCloudConfig bool
87-
expectedErr error
80+
desc string
81+
createFakeCredFile bool
82+
createFakeKubeConfig bool
83+
setFederatedWorkloadIdentityEnv bool
84+
kubeconfig string
85+
nodeID string
86+
userAgent string
87+
allowEmptyCloudConfig bool
88+
expectedErr error
89+
aadFederatedTokenFile string
90+
useFederatedWorkloadIdentityExtension bool
91+
aadClientID string
92+
tenantID string
8893
}{
8994
{
90-
desc: "[success] out of cluster, no kubeconfig, no credential file",
91-
nodeID: "",
92-
allowEmptyCloudConfig: true,
93-
expectedErr: nil,
95+
desc: "[success] out of cluster, no kubeconfig, no credential file",
96+
nodeID: "",
97+
allowEmptyCloudConfig: true,
98+
aadFederatedTokenFile: "",
99+
useFederatedWorkloadIdentityExtension: false,
100+
aadClientID: "",
101+
tenantID: "",
102+
expectedErr: nil,
94103
},
95104
{
96-
desc: "[linux][failure][disallowEmptyCloudConfig] out of cluster, no kubeconfig, no credential file",
97-
nodeID: "",
98-
allowEmptyCloudConfig: false,
99-
expectedErr: syscall.ENOENT,
105+
desc: "[linux][failure][disallowEmptyCloudConfig] out of cluster, no kubeconfig, no credential file",
106+
nodeID: "",
107+
allowEmptyCloudConfig: false,
108+
aadFederatedTokenFile: "",
109+
useFederatedWorkloadIdentityExtension: false,
110+
aadClientID: "",
111+
tenantID: "",
112+
expectedErr: syscall.ENOENT,
100113
},
101114
{
102-
desc: "[windows][failure][disallowEmptyCloudConfig] out of cluster, no kubeconfig, no credential file",
103-
nodeID: "",
104-
allowEmptyCloudConfig: false,
105-
expectedErr: syscall.ENOTDIR,
115+
desc: "[windows][failure][disallowEmptyCloudConfig] out of cluster, no kubeconfig, no credential file",
116+
nodeID: "",
117+
allowEmptyCloudConfig: false,
118+
aadFederatedTokenFile: "",
119+
useFederatedWorkloadIdentityExtension: false,
120+
aadClientID: "",
121+
tenantID: "",
122+
expectedErr: syscall.ENOTDIR,
106123
},
107124
{
108-
desc: "[success] out of cluster & in cluster, specify a fake kubeconfig, no credential file",
109-
createFakeKubeConfig: true,
110-
kubeconfig: fakeKubeConfig,
111-
nodeID: "",
112-
allowEmptyCloudConfig: true,
113-
expectedErr: nil,
125+
desc: "[success] out of cluster & in cluster, specify a fake kubeconfig, no credential file",
126+
createFakeKubeConfig: true,
127+
kubeconfig: fakeKubeConfig,
128+
nodeID: "",
129+
allowEmptyCloudConfig: true,
130+
aadFederatedTokenFile: "",
131+
useFederatedWorkloadIdentityExtension: false,
132+
aadClientID: "",
133+
tenantID: "",
134+
expectedErr: nil,
114135
},
115136
{
116-
desc: "[success] out of cluster & in cluster, no kubeconfig, a fake credential file",
117-
createFakeCredFile: true,
118-
nodeID: "",
119-
userAgent: "useragent",
120-
allowEmptyCloudConfig: true,
121-
expectedErr: nil,
137+
desc: "[success] out of cluster & in cluster, no kubeconfig, a fake credential file",
138+
createFakeCredFile: true,
139+
nodeID: "",
140+
userAgent: "useragent",
141+
allowEmptyCloudConfig: true,
142+
aadFederatedTokenFile: "",
143+
useFederatedWorkloadIdentityExtension: false,
144+
aadClientID: "",
145+
tenantID: "",
146+
expectedErr: nil,
147+
},
148+
{
149+
desc: "[success] get azure client with workload identity",
150+
createFakeKubeConfig: true,
151+
createFakeCredFile: true,
152+
setFederatedWorkloadIdentityEnv: true,
153+
kubeconfig: fakeKubeConfig,
154+
nodeID: "",
155+
userAgent: "useragent",
156+
useFederatedWorkloadIdentityExtension: true,
157+
aadFederatedTokenFile: "fake-token-file",
158+
aadClientID: "fake-client-id",
159+
tenantID: "fake-tenant-id",
160+
expectedErr: nil,
122161
},
123162
}
124163

@@ -135,7 +174,7 @@ users:
135174
t.Error(err)
136175
}
137176
defer func() {
138-
if err := os.Remove(fakeKubeConfig); err != nil {
177+
if err := os.Remove(fakeKubeConfig); err != nil && !os.IsNotExist(err) {
139178
t.Error(err)
140179
}
141180
}()
@@ -156,7 +195,7 @@ users:
156195
t.Error(err)
157196
}
158197
defer func() {
159-
if err := os.Remove(fakeCredFile); err != nil {
198+
if err := os.Remove(fakeCredFile); err != nil && !os.IsNotExist(err) {
160199
t.Error(err)
161200
}
162201
}()
@@ -169,6 +208,11 @@ users:
169208
}
170209
os.Setenv(DefaultAzureCredentialFileEnv, fakeCredFile)
171210
}
211+
if test.setFederatedWorkloadIdentityEnv {
212+
t.Setenv("AZURE_TENANT_ID", test.tenantID)
213+
t.Setenv("AZURE_CLIENT_ID", test.aadClientID)
214+
t.Setenv("AZURE_FEDERATED_TOKEN_FILE", test.aadFederatedTokenFile)
215+
}
172216

173217
cloud, err := GetCloudProvider(context.Background(), kubeClient, test.nodeID, "", "", test.userAgent, test.allowEmptyCloudConfig)
174218
assert.ErrorIs(t, err, test.expectedErr)
@@ -178,6 +222,10 @@ users:
178222
} else {
179223
assert.Equal(t, cloud.Environment.StorageEndpointSuffix, storage.DefaultBaseURL)
180224
assert.Equal(t, cloud.UserAgent, test.userAgent)
225+
assert.Equal(t, cloud.AADFederatedTokenFile, test.aadFederatedTokenFile)
226+
assert.Equal(t, cloud.UseFederatedWorkloadIdentityExtension, test.useFederatedWorkloadIdentityExtension)
227+
assert.Equal(t, cloud.AADClientID, test.aadClientID)
228+
assert.Equal(t, cloud.TenantID, test.tenantID)
181229
}
182230
}
183231
}

0 commit comments

Comments
 (0)