|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2020 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | +set -o pipefail |
| 20 | + |
| 21 | +run_exec_credentials_tests() { |
| 22 | + set -o nounset |
| 23 | + set -o errexit |
| 24 | + |
| 25 | + kube::log::status "Testing kubectl with configured exec credentials plugin" |
| 26 | + |
| 27 | + cat > "${TMPDIR:-/tmp}"/invalid_exec_plugin.yaml << EOF |
| 28 | +apiVersion: v1 |
| 29 | +clusters: |
| 30 | +- cluster: |
| 31 | + name: test |
| 32 | +contexts: |
| 33 | +- context: |
| 34 | + cluster: test |
| 35 | + user: invalid_token_user |
| 36 | + name: test |
| 37 | +current-context: test |
| 38 | +kind: Config |
| 39 | +preferences: {} |
| 40 | +users: |
| 41 | +- name: invalid_token_user |
| 42 | + user: |
| 43 | + exec: |
| 44 | + apiVersion: client.authentication.k8s.io/v1beta1 |
| 45 | + # Any invalid exec credential plugin will do to demonstrate |
| 46 | + command: ls |
| 47 | +EOF |
| 48 | + |
| 49 | + ### Provided --token should take precedence, thus not triggering the (invalid) exec credential plugin |
| 50 | + # Pre-condition: Client certificate authentication enabled on the API server |
| 51 | + kube::util::test_client_certificate_authentication_enabled |
| 52 | + # Command |
| 53 | + output=$(kubectl "${kube_flags_with_token[@]:?}" --kubeconfig="${TMPDIR:-/tmp}"/invalid_exec_plugin.yaml get namespace kube-system -o name || true) |
| 54 | + |
| 55 | + if [[ "${output}" == "namespace/kube-system" ]]; then |
| 56 | + kube::log::status "exec credential plugin not triggered since kubectl was called with provided --token" |
| 57 | + else |
| 58 | + kube::log::status "Unexpected output when providing --token for authentication - exec credential plugin likely triggered. Output: ${output}" |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | + # Post-condition: None |
| 62 | + |
| 63 | + ### Without provided --token, the exec credential plugin should be triggered |
| 64 | + # Pre-condition: Client certificate authentication enabled on the API server - already checked by positive test above |
| 65 | + |
| 66 | + kube_flags_without_token=('-s' "https://127.0.0.1:${SECURE_API_PORT}" '--insecure-skip-tls-verify=true') |
| 67 | + |
| 68 | + # Command |
| 69 | + output2=$(kubectl "${kube_flags_without_token[@]:?}" --kubeconfig="${TMPDIR:-/tmp}"/invalid_exec_plugin.yaml get namespace kube-system -o name 2>&1 || true) |
| 70 | + |
| 71 | + if [[ "${output2}" =~ "json parse error" ]]; then |
| 72 | + kube::log::status "exec credential plugin triggered since kubectl was called without provided --token" |
| 73 | + else |
| 74 | + kube::log::status "Unexpected output when not providing --token for authentication - exec credential plugin not triggered. Output: ${output2}" |
| 75 | + exit 1 |
| 76 | + fi |
| 77 | + # Post-condition: None |
| 78 | + |
| 79 | + rm "${TMPDIR:-/tmp}"/invalid_exec_plugin.yaml |
| 80 | + |
| 81 | + set +o nounset |
| 82 | + set +o errexit |
| 83 | +} |
0 commit comments