Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: workload identity did not work #1783

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pkg/azurefile/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,18 @@ func getCloudProvider(ctx context.Context, kubeconfig, nodeID, secretName, secre
}
} else {
config.UserAgent = userAgent
if err = az.InitializeCloudFromConfig(context.TODO(), config, fromSecret, false); err != nil {
// these environment variables are injected by workload identity webhook
if tenantID := os.Getenv("AZURE_TENANT_ID"); tenantID != "" {
config.TenantID = tenantID
}
if clientID := os.Getenv("AZURE_CLIENT_ID"); clientID != "" {
config.AADClientID = clientID
}
if federatedTokenFile := os.Getenv("AZURE_FEDERATED_TOKEN_FILE"); federatedTokenFile != "" {
config.AADFederatedTokenFile = federatedTokenFile
config.UseFederatedWorkloadIdentityExtension = true
}
if err = az.InitializeCloudFromConfig(ctx, config, fromSecret, false); err != nil {
klog.Warningf("InitializeCloudFromConfig failed with error: %v", err)
}
}
Expand Down
46 changes: 37 additions & 9 deletions pkg/azurefile/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,18 @@ users:
}()

tests := []struct {
desc string
createFakeCredFile bool
createFakeKubeConfig bool
kubeconfig string
userAgent string
allowEmptyCloudConfig bool
expectedErr testutil.TestError
desc string
createFakeCredFile bool
createFakeKubeConfig bool
setFederatedWorkloadIdentityEnv bool
kubeconfig string
userAgent string
allowEmptyCloudConfig bool
aadFederatedTokenFile string
useFederatedWorkloadIdentityExtension bool
aadClientID string
tenantID string
expectedErr testutil.TestError
}{
{
desc: "out of cluster, no kubeconfig, no credential file",
Expand Down Expand Up @@ -137,6 +142,19 @@ users:
allowEmptyCloudConfig: true,
expectedErr: testutil.TestError{},
},
{
desc: "[success] get azure client with workload identity",
createFakeKubeConfig: true,
createFakeCredFile: true,
setFederatedWorkloadIdentityEnv: true,
kubeconfig: fakeKubeConfig,
userAgent: "useragent",
useFederatedWorkloadIdentityExtension: true,
aadFederatedTokenFile: "fake-token-file",
aadClientID: "fake-client-id",
tenantID: "fake-tenant-id",
expectedErr: testutil.TestError{},
},
}

for _, test := range tests {
Expand All @@ -145,7 +163,7 @@ users:
t.Error(err)
}
defer func() {
if err := os.Remove(fakeKubeConfig); err != nil {
if err := os.Remove(fakeKubeConfig); err != nil && !os.IsNotExist(err) {
t.Error(err)
}
}()
Expand All @@ -159,7 +177,7 @@ users:
t.Error(err)
}
defer func() {
if err := os.Remove(fakeCredFile); err != nil {
if err := os.Remove(fakeCredFile); err != nil && !os.IsNotExist(err) {
t.Error(err)
}
}()
Expand All @@ -172,6 +190,12 @@ users:
}
os.Setenv(DefaultAzureCredentialFileEnv, fakeCredFile)
}
if test.setFederatedWorkloadIdentityEnv {
t.Setenv("AZURE_TENANT_ID", test.tenantID)
t.Setenv("AZURE_CLIENT_ID", test.aadClientID)
t.Setenv("AZURE_FEDERATED_TOKEN_FILE", test.aadFederatedTokenFile)
}

cloud, err := getCloudProvider(context.Background(), test.kubeconfig, "", "", "", test.userAgent, test.allowEmptyCloudConfig, false, 5, 10)
if !testutil.AssertError(err, &test.expectedErr) && !strings.Contains(err.Error(), test.expectedErr.DefaultError.Error()) {
t.Errorf("desc: %s,\n input: %q, getCloudProvider err: %v, expectedErr: %v", test.desc, test.kubeconfig, err, test.expectedErr)
Expand All @@ -180,6 +204,10 @@ users:
t.Errorf("return value of getCloudProvider should not be nil even there is error")
} else {
assert.Equal(t, cloud.UserAgent, test.userAgent)
assert.Equal(t, cloud.AADFederatedTokenFile, test.aadFederatedTokenFile)
assert.Equal(t, cloud.UseFederatedWorkloadIdentityExtension, test.useFederatedWorkloadIdentityExtension)
assert.Equal(t, cloud.AADClientID, test.aadClientID)
assert.Equal(t, cloud.TenantID, test.tenantID)
}
}
}
Expand Down
Loading