-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-user.sh
executable file
·181 lines (159 loc) · 3.95 KB
/
add-user.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
#!/usr/bin/env bash
set -euo pipefail
set -x
if ! which openssl; then
echo "Can't find the openssl tool"
exit 1
fi
if ! which jq; then
echo "Can't find the jq tool"
exit 1
fi
KUBECONFIG="${KUBECONFIG:-${HOME}/.kube/config}"
NAME="${1:-${USER}}"
CSR_NAME="${NAME}-csr"
KUBECTX=$(kubectl config view -o json | jq -r '."current-context"')
CLUSTER_NAME=$(kubectl config view -o json | jq --arg kubectx "${KUBECTX}" -r '.contexts[] | select(.name == $kubectx) | .context.cluster')
CLUSTER_ENDPOINT=$(kubectl config view -o json | jq --arg cluster "${CLUSTER_NAME}" -r '.clusters[] | select(.name == $cluster) | .cluster.server')
echo "Fetching cluster CA"
CERTIFICATE_AUTHORITY_DATA=$(kubectl --insecure-skip-tls-verify -n kube-public get cm kube-root-ca.crt -o json | jq -r '.data."ca.crt"' | base64 -w0)
echo "Generating private key for user"
USER_KEY="${PWD}/${NAME}-rsa.key"
openssl genrsa -out "${USER_KEY}" 4096 2>/dev/null 1>/dev/null
echo "Wrote private key to ${USER_KEY}"
echo "Generating signing request."
OPENSSL_CNF_DATA=$(cat <<EOF
[req]
default_bits = 2048
prompt = no
default_md = sha256
distinguished_name = dn
[dn]
CN=${NAME}
O=dev
[v3_ext]
authorityKeyIdentifier=keyid,issuer=always
basicConstraints=CA:FALSE
keyUsage=keyEncipherment,dataEncipherment
extendedKeyUsage=serverAuth,clientAuth
EOF
)
OPENSSL_CNF="${PWD}/${NAME}-openssl.cnf"
echo "${OPENSSL_CNF_DATA}" > "${OPENSSL_CNF}"
USER_CSR="${PWD}/${NAME}.csr"
openssl req -config "${OPENSSL_CNF}" -new -key "${USER_KEY}" -nodes -out "${USER_CSR}"
rm -f "${OPENSSL_CNF}"
CSR_BASE64=$(base64 < "${USER_CSR}" | tr -d '\n')
cat <<EOF | kubectl create -f -
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
name: ${CSR_NAME}
spec:
request: ${CSR_BASE64}
signerName: kubernetes.io/kube-apiserver-client
usages:
- client auth
EOF
echo
echo "Approving signing request."
kubectl certificate approve "${CSR_NAME}"
echo
echo "Creating namespace for user"
USER_NS="${NAME}-ns"
kubectl create ns "${USER_NS}"
echo "Creating role for user"
cat <<EOF | kubectl create -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: ${NAME}-read-write
namespace: ${USER_NS}
rules:
- apiGroups:
- ""
resources: ["*"]
verbs:
- get
- list
- watch
- create
- update
- patch
- delete
- deletecollection
- apiGroups:
- "rbac.authorization.k8s.io"
resources:
- "*"
verbs:
- get
- list
- watch
- create
- update
- patch
- delete
- apiGroups:
- "apps"
resources:
- "*"
verbs:
- get
- list
- watch
- create
- update
- patch
- delete
EOF
echo "Creating rolebinding for user"
cat <<EOF | kubectl create -f -
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: ${NAME}
namespace: ${USER_NS}
subjects:
- kind: User
name: ${NAME}
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: ${NAME}-read-write
apiGroup: rbac.authorization.k8s.io
EOF
echo "Generating kubeconfig for user ${NAME}"
CLIENT_KEY_DATA=$(base64 < "${USER_KEY}" | tr -d '\n')
CLIENT_CERTIFICATE_DATA=$(kubectl get csr "${CSR_NAME}" -o jsonpath='{.status.certificate}' | tr -d '\n')
[[ -z "${CLIENT_CERTIFICATE_DATA}" ]] && echo "Error fetching signed certificate from cluster" && exit 1
KUBECONFIG_USER="${PWD}/${NAME}.kubeconfig"
KUBECONFIG_DATA=$(cat <<EOF
apiVersion: v1
kind: Config
clusters:
- cluster:
certificate-authority-data: ${CERTIFICATE_AUTHORITY_DATA}
server: ${CLUSTER_ENDPOINT}
name: ${CLUSTER_NAME}
users:
- name: ${NAME}
user:
client-key-data: ${CLIENT_KEY_DATA}
client-certificate-data: ${CLIENT_CERTIFICATE_DATA}
contexts:
- context:
cluster: ${CLUSTER_NAME}
user: ${NAME}
namespace: ${USER_NS}
name: ${NAME}-${CLUSTER_NAME}
current-context: ${NAME}-${CLUSTER_NAME}
EOF
)
echo "${KUBECONFIG_DATA}" > "${KUBECONFIG_USER}"
chmod 0600 "${KUBECONFIG_USER}
echo "Cleaning up"
rm -fv "${USER_KEY}"
rm -fv "${USER_CSR}"
kubectl delete csr "${CSR_NAME}"
echo "Done!"