Skip to content
This repository was archived by the owner on Apr 27, 2020. It is now read-only.

Commit 9112a93

Browse files
lmdehaassuperbrothers
authored andcommitted
Revert "Use token with kubeconfig set (#46)" (#48)
This reverts commit 160a836.
1 parent 160a836 commit 9112a93

File tree

4 files changed

+20
-41
lines changed

4 files changed

+20
-41
lines changed

AUTHORS

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ Tanner Bruce
77
Takuhiro Yoshida
88
O. Yuanying
99
Anne Schuth
10-
Werner Buck

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ The version of this resource corresponds to the version of kubectl. We recommend
3535

3636
### cluster configs
3737

38-
- `server`: *Optional.* The address and port of the API server.
39-
- `token`: *Optional.* Bearer token for authentication to the API server.
38+
- `server`: *Optional.* The address and port of the API server. Requires `token`.
39+
- `token`: *Optional.* Bearer token for authentication to the API server. Requires `server`.
4040
- `namespace`: *Optional.* The namespace scope. Defaults to `default`. If set along with `kubeconfig`, `namespace` will override the namespace in the current-context
4141
- `certificate_authority`: *Optional.* A certificate file for the certificate authority.
4242
```yaml

assets/common.sh

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ setup_kubectl() {
3434
# Optional. The address and port of the API server. Requires token.
3535
local server
3636
server="$(jq -r '.source.server // ""' < "$payload")"
37+
# Optional. Bearer token for authentication to the API server. Requires server.
38+
local token
39+
token="$(jq -r '.source.token // ""' < "$payload")"
3740
# Optional. A certificate file for the certificate authority.
3841
local certificate_authority
3942
certificate_authority="$(jq -r '.source.certificate_authority // ""' < "$payload")"
@@ -42,9 +45,23 @@ setup_kubectl() {
4245
local insecure_skip_tls_verify
4346
insecure_skip_tls_verify="$(jq -r '.source.insecure_skip_tls_verify // ""' < "$payload")"
4447

48+
if [[ -z "$server" || -z "$token" ]]; then
49+
echoerr 'You must specify "server" and "token", if not specify "kubeconfig".'
50+
exit 1
51+
fi
52+
53+
local -r AUTH_NAME=auth
4554
local -r CLUSTER_NAME=cluster
4655
local -r CONTEXT_NAME=kubernetes-resource
4756

57+
# Build options for kubectl config set-credentials
58+
# Avoid to expose the token string by using placeholder
59+
local set_credentials_opts
60+
set_credentials_opts=("--token=**********")
61+
exe kubectl config set-credentials "$AUTH_NAME" "${set_credentials_opts[@]}"
62+
# placeholder is replaced with actual token string
63+
sed -i -e "s/[*]\\{10\\}/$token/" "$KUBECONFIG"
64+
4865
# Build options for kubectl config set-cluster
4966
local set_cluster_opts
5067
set_cluster_opts=("--server=$server")
@@ -59,7 +76,7 @@ setup_kubectl() {
5976
fi
6077
exe kubectl config set-cluster "$CLUSTER_NAME" "${set_cluster_opts[@]}"
6178

62-
exe kubectl config set-context "$CONTEXT_NAME" --cluster="$CLUSTER_NAME"
79+
exe kubectl config set-context "$CONTEXT_NAME" --user="$AUTH_NAME" --cluster="$CLUSTER_NAME"
6380

6481
exe kubectl config use-context "$CONTEXT_NAME"
6582

@@ -94,24 +111,6 @@ setup_kubectl() {
94111
if [[ -n "$namespace" ]]; then
95112
exe kubectl config set-context "$(kubectl config current-context)" --namespace="$namespace"
96113
fi
97-
98-
# if providing a token we set a user and override context to support both kubeconfig and generated config
99-
local token
100-
token="$(jq -r '.source.token // ""' < "$payload")"
101-
if [[ -n "$token" ]]; then
102-
local -r AUTH_NAME=auth
103-
104-
# Build options for kubectl config set-credentials
105-
# Avoid to expose the token string by using placeholder
106-
local set_credentials_opts
107-
set_credentials_opts=("--token=**********")
108-
exe kubectl config set-credentials "$AUTH_NAME" "${set_credentials_opts[@]}"
109-
# placeholder is replaced with actual token string
110-
sed -i -e "s/[*]\\{10\\}/$token/" "$KUBECONFIG"
111-
112-
# override user of context to one with token
113-
exe kubectl config set-context "$(kubectl config current-context)" --user="$AUTH_NAME"
114-
fi
115114

116115
# Optional. The name of the kubeconfig context to use.
117116
local context

test/suite.bats

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@ setup() {
1212
kubectl config view --flatten --minify > "$kubeconfig_file"
1313
# Change the current-context to $namespace
1414
kubectl --kubeconfig "$kubeconfig_file" config set-context ${current_context} --namespace "$namespace"
15-
# Create a kubeconfig json without users (no token)
16-
kubeconfig_file_no_token="$(mktemp)"
17-
kubectl config view --flatten --minify -o json | jq -r 'del(.contexts[0].context.user,.users)' > "$kubeconfig_file_no_token"
18-
# create rolebinding for full namespace access to default service account in namespace to avoid forbidden errors with token
19-
kubectl create -n $namespace rolebinding --clusterrole=cluster-admin --serviceaccount=$namespace:default testaccount
20-
# get default service account
21-
serviceaccount=$(kubectl get -n $namespace serviceaccount default -o json | jq -r '.secrets[0].name')
22-
# Extract token from service account for testing
23-
token="$(kubectl get -n $namespace secret "$serviceaccount" -o json | jq -r '.data["token"]' | base64 -d)"
2415
}
2516

2617
teardown() {
@@ -66,16 +57,6 @@ teardown() {
6657
assert_failure
6758
}
6859

69-
@test "with no credentials in outputs.kubeconfig_file and source.token" {
70-
run assets/out <<< "$(jq -n '{"source": {"token": $token}, "params": {"kubectl": $kubectl, "kubeconfig_file": $kubeconfig_file, "namespace": $namespace}}' \
71-
--arg token "$token" \
72-
--arg kubeconfig_file "$kubeconfig_file_no_token" \
73-
--arg kubectl "get ns $namespace -o name" \
74-
--arg namespace "$namespace")"
75-
assert_match "namespace/$namespace" "$output"
76-
assert_success
77-
}
78-
7960
@test "command substitution in outputs.kubectl" {
8061
run kubectl --kubeconfig "$kubeconfig_file" run nginx --image=nginx
8162
assert_success

0 commit comments

Comments
 (0)