Skip to content

Commit 09f143b

Browse files
Merge pull request #380 from sunzhaohua2/41827
OCPBUGS-41827: update injector to use a secret rather than an environment variable
2 parents e938398 + d349235 commit 09f143b

6 files changed

Lines changed: 16 additions & 235 deletions

File tree

cmd/azure-config-credentials-injector/credentials_injector_test.go

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -69,112 +69,6 @@ func Test_mergeCloudConfig(t *testing.T) {
6969
args: []string{"--cloud-config-file-path", "foo"},
7070
expectedErrMsg: "stat foo: no such file or directory",
7171
},
72-
{
73-
name: "AZURE_CLIENT_ID not set",
74-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name()},
75-
expectedErrMsg: "azure_client_id should be set up",
76-
},
77-
{
78-
name: "AZURE_CLIENT_SECRET not set",
79-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name()},
80-
envVars: map[string]string{"AZURE_CLIENT_ID": "foo"},
81-
expectedErrMsg: "azure_client_secret should be set up",
82-
},
83-
{
84-
name: "input file content is not a valid json",
85-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name()},
86-
envVars: map[string]string{"AZURE_CLIENT_ID": "foo", "AZURE_CLIENT_SECRET": "bar"},
87-
fileContent: "{*&(&#@!}",
88-
expectedErrMsg: "couldn't read cloud config from file: invalid character '*' looking for beginning of object key string",
89-
},
90-
{
91-
name: "input file content is valid json, but format is unexpected",
92-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name()},
93-
envVars: map[string]string{"AZURE_CLIENT_ID": "foo", "AZURE_CLIENT_SECRET": "bar"},
94-
fileContent: "[1]",
95-
expectedErrMsg: "couldn't read cloud config from file: json: cannot unmarshal array into Go value of type map[string]interface {}",
96-
},
97-
{
98-
name: "all ok, file is empty json object",
99-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name()},
100-
envVars: map[string]string{"AZURE_CLIENT_ID": "foo", "AZURE_CLIENT_SECRET": "bar"},
101-
fileContent: "{}",
102-
expectedContent: "{\"aadClientId\":\"foo\",\"aadClientSecret\":\"bar\"}",
103-
},
104-
{
105-
name: "all ok, some content in json",
106-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name()},
107-
envVars: map[string]string{"AZURE_CLIENT_ID": "foo", "AZURE_CLIENT_SECRET": "bar"},
108-
fileContent: "{\"bar\": \"baz\"}",
109-
expectedContent: "{\"aadClientId\":\"foo\",\"aadClientSecret\":\"bar\",\"bar\":\"baz\"}",
110-
},
111-
{
112-
name: "all ok, client_id and client_secret overrides",
113-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name()},
114-
envVars: map[string]string{"AZURE_CLIENT_ID": "foo", "AZURE_CLIENT_SECRET": "bar"},
115-
fileContent: "{\"aadClientSecret\":\"fizz\",\"aadClientId\":\"baz\"}",
116-
expectedContent: "{\"aadClientId\":\"foo\",\"aadClientSecret\":\"bar\"}",
117-
},
118-
{
119-
name: "output file write error",
120-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", "/tmp"},
121-
envVars: map[string]string{"AZURE_CLIENT_ID": "foo", "AZURE_CLIENT_SECRET": "bar"},
122-
fileContent: "{}",
123-
expectedErrMsg: "couldn't write prepared cloud config to file: open /tmp: is a directory",
124-
},
125-
{
126-
name: "all ok, useManagedIdentityExtension not disabled",
127-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name()},
128-
envVars: map[string]string{"AZURE_CLIENT_ID": "foo", "AZURE_CLIENT_SECRET": "bar"},
129-
fileContent: "{\"aadClientSecret\":\"fizz\",\"aadClientId\":\"baz\",\"useManagedIdentityExtension\":true}",
130-
expectedContent: "{\"aadClientId\":\"foo\",\"aadClientSecret\":\"bar\",\"useManagedIdentityExtension\":true}",
131-
},
132-
{
133-
name: "all ok, useManagedIdentityExtension disabled",
134-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name(), "--disable-identity-extension-auth"},
135-
envVars: map[string]string{"AZURE_CLIENT_ID": "foo", "AZURE_CLIENT_SECRET": "bar"},
136-
fileContent: "{\"aadClientSecret\":\"fizz\",\"aadClientId\":\"baz\",\"useManagedIdentityExtension\":true}",
137-
expectedContent: "{\"aadClientId\":\"foo\",\"aadClientSecret\":\"bar\",\"useManagedIdentityExtension\":false}",
138-
},
139-
{
140-
name: "all ok, invalid useManagedIdentityExtension value",
141-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name(), "--disable-identity-extension-auth"},
142-
envVars: map[string]string{"AZURE_CLIENT_ID": "foo", "AZURE_CLIENT_SECRET": "bar"},
143-
fileContent: "{\"aadClientSecret\":\"fizz\",\"aadClientId\":\"baz\",\"useManagedIdentityExtension\":\"true\"}",
144-
expectedContent: "{\"aadClientId\":\"foo\",\"aadClientSecret\":\"bar\",\"useManagedIdentityExtension\":false}",
145-
},
146-
{
147-
name: "all ok, use workload identity while client secret is not present",
148-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name(), "--disable-identity-extension-auth", "--enable-azure-workload-identity=true"},
149-
envVars: map[string]string{"AZURE_TENANT_ID": "bar", "AZURE_CLIENT_ID": "buzz", "AZURE_FEDERATED_TOKEN_FILE": "baz"},
150-
fileContent: "{\"tenantId\":\"foo\",\"aadClientId\":\"fizz\"}",
151-
expectedContent: "{\"aadClientId\":\"buzz\",\"aadFederatedTokenFile\":\"baz\",\"tenantId\":\"bar\",\"useFederatedWorkloadIdentityExtension\":true}",
152-
},
153-
{
154-
name: "all ok, use workload identity while managed identity is not explicitly disabled",
155-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name(), "--enable-azure-workload-identity=true"},
156-
envVars: map[string]string{"AZURE_TENANT_ID": "bar", "AZURE_CLIENT_ID": "buzz", "AZURE_FEDERATED_TOKEN_FILE": "baz"},
157-
fileContent: "{\"tenantId\":\"foo\",\"aadClientId\":\"fizz\"}",
158-
expectedContent: "{\"aadClientId\":\"buzz\",\"aadFederatedTokenFile\":\"baz\",\"tenantId\":\"bar\",\"useFederatedWorkloadIdentityExtension\":true}",
159-
},
160-
{
161-
name: "should fail, client secret is present while federated token file is present",
162-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name(), "--disable-identity-extension-auth", "--enable-azure-workload-identity=true"},
163-
envVars: map[string]string{"AZURE_TENANT_ID": "baz", "AZURE_CLIENT_ID": "foo", "AZURE_CLIENT_SECRET": "bar", "AZURE_FEDERATED_TOKEN_FILE": "baz"},
164-
expectedErrMsg: "azure_client_secret is set while workload identity is enabled using azure_federated_token_file, this should never happen.\nPlease consider reporting a bug: https://issues.redhat.com",
165-
},
166-
{
167-
name: "should fail, tenant id missing while federated token file is present",
168-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name(), "--disable-identity-extension-auth", "--enable-azure-workload-identity=true"},
169-
envVars: map[string]string{"AZURE_CLIENT_ID": "buzz", "AZURE_FEDERATED_TOKEN_FILE": "baz"},
170-
expectedErrMsg: "azure_tenant_id should be set up while workload identity is enabled using azure_federated_token_file, this should never happen.\nPlease consider reporting a bug: https://issues.redhat.com",
171-
},
172-
{
173-
name: "should fail, workload identity can't be enabled because federated token missing, expect secret provided",
174-
args: []string{"--cloud-config-file-path", inputFile.Name(), "--output-file-path", outputFile.Name(), "--disable-identity-extension-auth", "--enable-azure-workload-identity=true"},
175-
envVars: map[string]string{"AZURE_TENANT_ID": "bar", "AZURE_CLIENT_ID": "buzz"},
176-
expectedErrMsg: "azure_client_secret should be set up",
177-
},
17872
}
17973

18074
for _, tc := range testCases {

cmd/azure-config-credentials-injector/main.go

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ import (
1111
)
1212

1313
const (
14-
clientIDEnvKey = "AZURE_CLIENT_ID"
15-
clientSecretEnvKey = "AZURE_CLIENT_SECRET"
16-
tenantIDEnvKey = "AZURE_TENANT_ID"
17-
federatedTokenEnvKey = "AZURE_FEDERATED_TOKEN_FILE"
18-
1914
clientIDCloudConfigKey = "aadClientId"
2015
clientSecretCloudConfigKey = "aadClientSecret"
2116
useManagedIdentityExtensionConfigKey = "useManagedIdentityExtension"
@@ -60,7 +55,6 @@ func main() {
6055
func mergeCloudConfig(_ *cobra.Command, args []string) error {
6156
var (
6257
azureClientId string
63-
azureClientIdFound bool
6458
tenantId string
6559
tenantIdFound bool
6660
federatedTokenFile string
@@ -74,54 +68,31 @@ func mergeCloudConfig(_ *cobra.Command, args []string) error {
7468
return err
7569
}
7670

77-
// Read credentials from mounted Secret files or environment variables
71+
// Read credentials from mounted Secret files
7872
azureClientId, err = readSecretFile(fmt.Sprintf("%s/azure_client_id", injectorOpts.credentialsPath))
7973
if err != nil {
80-
if os.IsNotExist(err) {
81-
// Fallback to environment variable if secret file does not exist
82-
azureClientId, azureClientIdFound = mustLookupEnvValue(clientIDEnvKey)
83-
if !azureClientIdFound {
84-
return fmt.Errorf("azure_client_id should be set up")
85-
}
86-
} else {
87-
return fmt.Errorf("failed to read azure_client_id from secret: %w", err)
88-
}
74+
return fmt.Errorf("failed to read azure_client_id from secret: %w", err)
8975
}
9076

9177
azureClientSecret, err = readSecretFile(fmt.Sprintf("%s/azure_client_secret", injectorOpts.credentialsPath))
92-
if err != nil {
93-
if os.IsNotExist(err) {
94-
// Fallback to environment variable if secret file does not exist
95-
azureClientSecret, secretFound = mustLookupEnvValue(clientSecretEnvKey)
96-
} else {
97-
return fmt.Errorf("failed to read azure_client_secret from secret: %w", err)
98-
}
99-
} else {
78+
if err == nil {
10079
secretFound = true
101-
}
102-
103-
tenantId, err = readSecretFile(fmt.Sprintf("%s/azure_tenant_id", injectorOpts.credentialsPath))
104-
if err != nil {
105-
if os.IsNotExist(err) {
106-
// Fallback to environment variable if secret file does not exist
107-
tenantId, tenantIdFound = mustLookupEnvValue(tenantIDEnvKey)
108-
} else {
109-
return fmt.Errorf("failed to read azure_tenant_id from secret: %w", err)
110-
}
111-
} else {
112-
tenantIdFound = true
80+
} else if !os.IsNotExist(err) {
81+
return fmt.Errorf("failed to read azure_client_secret from secret: %w", err)
11382
}
11483

11584
federatedTokenFile, err = readSecretFile(fmt.Sprintf("%s/azure_federated_token_file", injectorOpts.credentialsPath))
116-
if err != nil {
117-
if os.IsNotExist(err) {
118-
// Fallback to environment variable if secret file does not exist
119-
federatedTokenFile, federatedTokenFileFound = mustLookupEnvValue(federatedTokenEnvKey)
120-
} else {
121-
return fmt.Errorf("failed to read azure_federated_token_file from secret: %w", err)
122-
}
123-
} else {
85+
if err == nil {
12486
federatedTokenFileFound = true
87+
} else if !os.IsNotExist(err) {
88+
return fmt.Errorf("failed to read azure_federated_token_file from secret: %w", err)
89+
}
90+
91+
tenantId, err = readSecretFile(fmt.Sprintf("%s/azure_tenant_id", injectorOpts.credentialsPath))
92+
if err == nil {
93+
tenantIdFound = true
94+
} else if !os.IsNotExist(err) {
95+
return fmt.Errorf("failed to read azure_tenant_id from secret: %w", err)
12596
}
12697

12798
// If federatedTokenFile found, workload identity should be used
@@ -199,7 +170,7 @@ func prepareCloudConfig(cloudConfig map[string]interface{}, clientId, clientSecr
199170
cloudConfig[aadFederatedTokenFileConfigKey] = federatedTokenFile
200171
cloudConfig[useFederatedWorkloadIdentityExtensionConfigKey] = true
201172
} else {
202-
klog.V(4).Infof("%s env variable is set, client secret authentication will be used", clientSecretEnvKey)
173+
klog.V(4).Infof("azure_client_secret is set, client secret authentication will be used")
203174
cloudConfig[clientSecretCloudConfigKey] = clientSecret
204175
}
205176

@@ -217,11 +188,3 @@ func writeCloudConfig(path string, preparedConfig []byte) error {
217188
}
218189
return nil
219190
}
220-
221-
func mustLookupEnvValue(key string) (string, bool) {
222-
value, found := os.LookupEnv(key)
223-
if !found || len(value) == 0 {
224-
return "", false
225-
}
226-
return value, true
227-
}

pkg/cloud/azure/assets/cloud-controller-manager-deployment.yaml

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,6 @@ spec:
7171
--disable-identity-extension-auth \
7272
--enable-azure-workload-identity=true \
7373
--creds-path=/etc/azure/credentials
74-
env:
75-
- name: AZURE_CLIENT_ID
76-
valueFrom:
77-
secretKeyRef:
78-
name: azure-cloud-credentials
79-
key: azure_client_id
80-
- name: AZURE_CLIENT_SECRET
81-
valueFrom:
82-
secretKeyRef:
83-
name: azure-cloud-credentials
84-
key: azure_client_secret
85-
optional: true
86-
- name: AZURE_TENANT_ID
87-
valueFrom:
88-
secretKeyRef:
89-
name: azure-cloud-credentials
90-
key: azure_tenant_id
91-
optional: true
92-
- name: AZURE_FEDERATED_TOKEN_FILE
93-
valueFrom:
94-
secretKeyRef:
95-
name: azure-cloud-credentials
96-
key: azure_federated_token_file
97-
optional: true
9874
terminationMessagePolicy: FallbackToLogsOnError
9975
volumeMounts:
10076
- name: config-accm
@@ -114,12 +90,6 @@ spec:
11490
value: /etc/kubernetes-cloud-config/cloud.conf
11591
- name: OCP_INFRASTRUCTURE_NAME
11692
value: {{ .infrastructureName }}
117-
- name: AZURE_FEDERATED_TOKEN_FILE
118-
valueFrom:
119-
secretKeyRef:
120-
name: azure-cloud-credentials
121-
key: azure_federated_token_file
122-
optional: true
12393
resources:
12494
requests:
12595
cpu: 200m

pkg/cloud/azure/assets/cloud-node-manager-daemonset.yaml

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,6 @@ spec:
6363
--disable-identity-extension-auth \
6464
--enable-azure-workload-identity=true \
6565
--creds-path=/etc/azure/credentials
66-
env:
67-
- name: AZURE_CLIENT_ID
68-
valueFrom:
69-
secretKeyRef:
70-
name: azure-cloud-credentials
71-
key: azure_client_id
72-
- name: AZURE_CLIENT_SECRET
73-
valueFrom:
74-
secretKeyRef:
75-
name: azure-cloud-credentials
76-
key: azure_client_secret
77-
optional: true
78-
- name: AZURE_TENANT_ID
79-
valueFrom:
80-
secretKeyRef:
81-
name: azure-cloud-credentials
82-
key: azure_tenant_id
83-
optional: true
84-
- name: AZURE_FEDERATED_TOKEN_FILE
85-
valueFrom:
86-
secretKeyRef:
87-
name: azure-cloud-credentials
88-
key: azure_federated_token_file
89-
optional: true
9066
terminationMessagePolicy: FallbackToLogsOnError
9167
volumeMounts:
9268
- name: host-etc-kube

pkg/cloud/azurestack/assets/cloud-controller-manager-deployment.yaml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,6 @@ spec:
6262
- --cloud-config-file-path=/tmp/cloud-config/cloud.conf
6363
- --output-file-path=/tmp/merged-cloud-config/cloud.conf
6464
- --creds-path=/etc/azure/credentials
65-
env:
66-
- name: AZURE_CLIENT_ID
67-
valueFrom:
68-
secretKeyRef:
69-
name: azure-cloud-credentials
70-
key: azure_client_id
71-
- name: AZURE_CLIENT_SECRET
72-
valueFrom:
73-
secretKeyRef:
74-
name: azure-cloud-credentials
75-
key: azure_client_secret
7665
terminationMessagePolicy: FallbackToLogsOnError
7766
volumeMounts:
7867
- name: config-accm

pkg/cloud/azurestack/assets/cloud-node-manager-daemonset.yaml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,6 @@ spec:
5454
- --cloud-config-file-path=/tmp/cloud-config/cloud.conf
5555
- --output-file-path=/tmp/merged-cloud-config/cloud.conf
5656
- --creds-path=/etc/azure/credentials
57-
env:
58-
- name: AZURE_CLIENT_ID
59-
valueFrom:
60-
secretKeyRef:
61-
name: azure-cloud-credentials
62-
key: azure_client_id
63-
- name: AZURE_CLIENT_SECRET
64-
valueFrom:
65-
secretKeyRef:
66-
name: azure-cloud-credentials
67-
key: azure_client_secret
6857
terminationMessagePolicy: FallbackToLogsOnError
6958
volumeMounts:
7059
- name: config-accm

0 commit comments

Comments
 (0)