-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_user_certs.sh
executable file
·79 lines (66 loc) · 1.94 KB
/
create_user_certs.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
#!/bin/bash
if [[ -n "${CLEAR_ALL}" ]]; then
rm -rf ./certs
minikube delete
minikube start --driver=docker
fi
if [ "$#" -ne 2 ]; then
echo "Illegal number of parameters. Usage: create_user.sh <usern_name> <group1,group2,...>"
exit 1
fi
mkdir certs
cd certs
USER=$1
IFS=',' read -r -a array <<< "$2"
for group in "${array[@]}"
do
USER_GROUPS=${USER_GROUPS}/O=$group
done
openssl genrsa -out ${USER}.key 2048
openssl req -new -key ${USER}.key -out ${USER}.csr -subj "/CN=${USER}${USER_GROUPS}"
# https://kubernetes.io/docs/reference/access-authn-authz/certificate-signing-requests/#create-certificatesigningrequest
cat <<EOF | kubectl apply -f -
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
name: ${USER}
spec:
groups:
- system:authenticated
request: $(cat ${USER}.csr | base64 | tr -d '\n')
signerName: kubernetes.io/kube-apiserver-client
expirationSeconds: 600
usages:
- client auth
EOF
kubectl certificate approve ${USER}
until [[ -s "${USER}.crt" ]]
do
kubectl get csr ${USER} -o jsonpath='{.status.certificate}' | base64 --decode > ${USER}.crt
done
# Create the kubeconfig file
CONTEXT=$(kubectl config current-context)
CLUSTER=$(kubectl config view -o jsonpath="{.contexts[?(@.name == \"$CONTEXT\"})].context.cluster}")
SERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name == \"${CLUSTER}\"})].cluster.server}")
CA=$(kubectl config view --flatten -o jsonpath="{.clusters[?(@.name == \"${CLUSTER}\"})].cluster.certificate-authority-data}")
cat > ${USER}.kubeconfig <<EOF
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: $CA
server: ${SERVER}
name: ${CLUSTER}
contexts:
- context:
cluster: ${CLUSTER}
user: ${USER}
name: ${USER}
current-context: ${USER}
kind: Config
preferences: {}
users:
- name: ${USER}
user:
client-certificate-data: $(cat ${USER}.crt | base64 | tr -d '\n')
client-key-data: $(cat ${USER}.key | base64 | tr -d '\n')
EOF