-
Notifications
You must be signed in to change notification settings - Fork 38.7k
/
authentication.sh
328 lines (281 loc) · 12.7 KB
/
authentication.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env bash
# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
run_exec_credentials_tests() {
run_exec_credentials_tests_version client.authentication.k8s.io/v1beta1
run_exec_credentials_tests_version client.authentication.k8s.io/v1
}
run_exec_credentials_tests_version() {
set -o nounset
set -o errexit
local -r apiVersion="$1"
kube::log::status "Testing kubectl with configured ${apiVersion} exec credentials plugin"
cat > "${TMPDIR:-/tmp}"/invalid_exec_plugin.yaml << EOF
apiVersion: v1
clusters:
- cluster:
name: test
contexts:
- context:
cluster: test
user: invalid_token_user
name: test
current-context: test
kind: Config
preferences: {}
users:
- name: invalid_token_user
user:
exec:
apiVersion: ${apiVersion}
# Any invalid exec credential plugin will do to demonstrate
command: ls
interactiveMode: IfAvailable
EOF
### Provided --token should take precedence, thus not triggering the (invalid) exec credential plugin
# Pre-condition: Client certificate authentication enabled on the API server
kube::util::test_client_certificate_authentication_enabled
# Command
output=$(kubectl "${kube_flags_with_token[@]:?}" --kubeconfig="${TMPDIR:-/tmp}"/invalid_exec_plugin.yaml get namespace kube-system -o name || true)
if [[ "${output}" == "namespace/kube-system" ]]; then
kube::log::status "exec credential plugin not triggered since kubectl was called with provided --token"
else
kube::log::status "Unexpected output when providing --token for authentication - exec credential plugin likely triggered. Output: ${output}"
exit 1
fi
# Post-condition: None
### Without provided --token, the exec credential plugin should be triggered
# Pre-condition: Client certificate authentication enabled on the API server - already checked by positive test above
# Command
output2=$(kubectl "${kube_flags_without_token[@]:?}" --kubeconfig="${TMPDIR:-/tmp}"/invalid_exec_plugin.yaml get namespace kube-system -o name 2>&1 || true)
if [[ "${output2}" =~ "json parse error" ]]; then
kube::log::status "exec credential plugin triggered since kubectl was called without provided --token"
else
kube::log::status "Unexpected output when not providing --token for authentication - exec credential plugin not triggered. Output: ${output2}"
exit 1
fi
# Post-condition: None
cat >"${TMPDIR:-/tmp}"/valid_exec_plugin.yaml <<EOF
apiVersion: v1
clusters:
- cluster:
name: test
contexts:
- context:
cluster: test
user: valid_token_user
name: test
current-context: test
kind: Config
preferences: {}
users:
- name: valid_token_user
user:
exec:
apiVersion: ${apiVersion}
command: echo
args:
- '{"apiVersion":"${apiVersion}","status":{"token":"admin-token"}}'
interactiveMode: IfAvailable
EOF
### Valid exec plugin should authenticate user properly
# Pre-condition: Client certificate authentication enabled on the API server - already checked by positive test above
# Command
output3=$(kubectl "${kube_flags_without_token[@]:?}" --kubeconfig="${TMPDIR:-/tmp}"/valid_exec_plugin.yaml get namespace kube-system -o name 2>&1 || true)
if [[ "${output3}" == "namespace/kube-system" ]]; then
kube::log::status "exec credential plugin triggered and provided valid credentials"
else
kube::log::status "Unexpected output when using valid exec credential plugin for authentication. Output: ${output3}"
exit 1
fi
# Post-condition: None
### Provided --username/--password should take precedence, thus not triggering the (valid) exec credential plugin
# Pre-condition: Client certificate authentication enabled on the API server - already checked by positive test above
# Command
output4=$(kubectl "${kube_flags_without_token[@]:?}" --username bad --password wrong --kubeconfig="${TMPDIR:-/tmp}"/valid_exec_plugin.yaml get namespace kube-system -o name 2>&1 || true)
if [[ "${output4}" =~ "Unauthorized" ]]; then
kube::log::status "exec credential plugin not triggered since kubectl was called with provided --username/--password"
else
kube::log::status "Unexpected output when providing --username/--password for authentication - exec credential plugin likely triggered. Output: ${output4}"
exit 1
fi
# Post-condition: None
### Provided --client-certificate/--client-key should take precedence on the cli, thus not triggering the (invalid) exec credential plugin
# contained in the kubeconfig.
# Use CSR to get a valid certificate
cat <<EOF | kubectl create -f -
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
name: testuser
spec:
request: $(base64 < hack/testdata/auth/testuser.csr | tr -d '\n')
signerName: kubernetes.io/kube-apiserver-client
usages: [client auth]
EOF
kube::test::wait_object_assert 'csr/testuser' '{{range.status.conditions}}{{.type}}{{end}}' ''
kubectl certificate approve testuser
kube::test::wait_object_assert 'csr/testuser' '{{range.status.conditions}}{{.type}}{{end}}' 'Approved'
# wait for certificate to not be empty
kube::test::wait_object_assert 'csr/testuser' '{{.status.certificate}}' '.+'
kubectl get csr testuser -o jsonpath='{.status.certificate}' | base64 -d > "${TMPDIR:-/tmp}"/testuser.crt
output5=$(kubectl "${kube_flags_without_token[@]:?}" --client-certificate="${TMPDIR:-/tmp}"/testuser.crt --client-key="hack/testdata/auth/testuser.key" --kubeconfig="${TMPDIR:-/tmp}"/invalid_exec_plugin.yaml get namespace kube-system -o name)
if [[ "${output5}" =~ "Unauthorized" ]]; then
kube::log::status "Unexpected output when providing --client-certificate/--client-key for authentication - exec credential plugin likely triggered. Output: ${output5}"
exit 1
else
kube::log::status "exec credential plugin not triggered since kubectl was called with provided --client-certificate/--client-key"
fi
### Provided --client-certificate/--client-key should take precedence in the kubeconfig, thus not triggering the (invalid) exec credential plugin.
cat >"${TMPDIR:-/tmp}"/invalid_execcredential.sh <<EOF
#!/bin/bash
echo '{"kind":"ExecCredential","apiVersion":"client.authentication.k8s.io/v1beta1","status":{"clientKeyData":"bad","clientCertificateData":"bad"}}'
EOF
chmod +x "${TMPDIR:-/tmp}"/invalid_execcredential.sh
kubectl config set-credentials testuser --client-certificate="${TMPDIR:-/tmp}"/testuser.crt --client-key="hack/testdata/auth/testuser.key" --exec-api-version=client.authentication.k8s.io/v1beta1 --exec-command=/tmp/invalid_execcredential.sh
output6=$(kubectl "${kube_flags_without_token[@]:?}" --user testuser get namespace kube-system -o name)
if [[ "${output6}" =~ "Unauthorized" ]]; then
kube::log::status "Unexpected output when kubeconfig was configured with --client-certificate/--client-key for authentication - exec credential plugin likely triggered. Output: ${output6}"
exit 1
else
kube::log::status "exec credential plugin not triggered since kubeconfig was configured with --client-certificate/--client-key for authentication"
fi
kubectl delete csr testuser
rm "${TMPDIR:-/tmp}"/invalid_execcredential.sh
rm "${TMPDIR:-/tmp}"/invalid_exec_plugin.yaml
rm "${TMPDIR:-/tmp}"/valid_exec_plugin.yaml
set +o nounset
set +o errexit
}
run_exec_credentials_interactive_tests() {
run_exec_credentials_interactive_tests_version client.authentication.k8s.io/v1beta1
run_exec_credentials_interactive_tests_version client.authentication.k8s.io/v1
}
run_exec_credentials_interactive_tests_version() {
set -o nounset
set -o errexit
local -r apiVersion="$1"
kube::log::status "Testing kubectl with configured ${apiVersion} interactive exec credentials plugin"
cat >"${TMPDIR:-/tmp}"/always_interactive_exec_plugin.yaml <<EOF
apiVersion: v1
clusters:
- cluster:
name: test
contexts:
- context:
cluster: test
user: always_interactive_token_user
name: test
current-context: test
kind: Config
preferences: {}
users:
- name: always_interactive_token_user
user:
exec:
apiVersion: ${apiVersion}
command: echo
args:
- '{"apiVersion":"${apiVersion}","status":{"token":"admin-token"}}'
interactiveMode: Always
EOF
### The exec credential plugin should not be run if it kubectl already uses standard input
# Pre-condition: The kubectl command requires standard input
some_resource='{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"some-resource"}}'
# Declare map from kubectl command to standard input data
declare -A kubectl_commands
kubectl_commands["apply -f -"]="$some_resource"
kubectl_commands["set env deployment/some-deployment -"]="SOME_ENV_VAR_KEY=SOME_ENV_VAR_VAL"
kubectl_commands["replace -f - --force"]="$some_resource"
failure=
for kubectl_command in "${!kubectl_commands[@]}"; do
# Use a separate bash script for the command here so that script(1) will not get confused with kubectl flags
script_file="${TMPDIR:-/tmp}/test-cmd-exec-credentials-script-file.sh"
cat <<EOF >"$script_file"
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
kubectl ${kube_flags_without_token[*]:?} --kubeconfig=${TMPDIR:-/tmp}/always_interactive_exec_plugin.yaml ${kubectl_command} 2>&1 || true
EOF
chmod +x "$script_file"
# Run kubectl as child of script(1) so kubectl will always run with a PTY
# Dynamically build script(1) command so that we can conditionally add flags on Linux
script_command="script -q /dev/null"
if [[ "$(uname)" == "Linux" ]]; then script_command="${script_command} -c"; fi
script_command="${script_command} ${script_file}"
# Specify SHELL env var when we call script(1) since it is picky about the format of the env var
shell="$(which bash)"
kube::log::status "Running command '$script_command' (kubectl command: '$kubectl_command') with input '${kubectl_commands[$kubectl_command]}'"
output=$(echo "${kubectl_commands[$kubectl_command]}" | SHELL="$shell" $script_command)
if [[ "${output}" =~ "used by stdin resource manifest reader" ]]; then
kube::log::status "exec credential plugin not run because kubectl already uses standard input"
else
kube::log::status "Unexpected output when running kubectl command that uses standard input. Output: ${output}"
failure=yup
fi
done
if [[ -n "$failure" ]]; then
exit 1
fi
# Post-condition: None
cat >"${TMPDIR:-/tmp}"/missing_interactive_exec_plugin.yaml <<EOF
apiVersion: v1
clusters:
- cluster:
name: test
contexts:
- context:
cluster: test
user: missing_interactive_token_user
name: test
current-context: test
kind: Config
preferences: {}
users:
- name: missing_interactive_token_user
user:
exec:
apiVersion: ${apiVersion}
command: echo
args:
- '{"apiVersion":"${apiVersion}","status":{"token":"admin-token"}}'
EOF
### The kubeconfig will fail to be loaded if a v1 exec credential plugin is missing an interactiveMode field, otherwise it will default to IfAvailable
# Pre-condition: The exec credential plugin is missing an interactiveMode setting
output2=$(kubectl "${kube_flags_without_token[@]:?}" --kubeconfig="${TMPDIR:-/tmp}"/missing_interactive_exec_plugin.yaml get namespace kube-system -o name 2>&1 || true)
if [[ "$apiVersion" == "client.authentication.k8s.io/v1" ]]; then
if [[ "${output2}" =~ "error: interactiveMode must be specified for missing_interactive_token_user to use exec authentication plugin" ]]; then
kube::log::status "kubeconfig was not loaded successfully because ${apiVersion} exec credential plugin is missing interactiveMode"
else
kube::log::status "Unexpected output when running kubectl command that uses a ${apiVersion} exec credential plugin without an interactiveMode. Output: ${output2}"
exit 1
fi
else
if [[ "${output2}" == "namespace/kube-system" ]]; then
kube::log::status "${apiVersion} exec credential plugin triggered and provided valid credentials"
else
kube::log::status "Unexpected output when using valid exec credential plugin for authentication. Output: ${output2}"
exit 1
fi
fi
# Post-condition: None
rm "${TMPDIR:-/tmp}"/always_interactive_exec_plugin.yaml
rm "${TMPDIR:-/tmp}"/missing_interactive_exec_plugin.yaml
set +o nounset
set +o errexit
}