-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcreate-cert.sh
executable file
·40 lines (30 loc) · 2.02 KB
/
create-cert.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
#!/bin/sh
/usr/bin/openssl genrsa -out /certs/${USER_NAME}.pem 2048 && \
/usr/bin/openssl req -new -key /certs/${USER_NAME}.pem -out /certs/${USER_NAME}.csr -subj "/CN=users:${USER_NAME}${USER_GROUPS}"
encoded_string=$(cat /certs/${USER_NAME}.csr | base64 | tr -d '\n')
echo 'apiVersion: certificates.k8s.io/v1beta1\nkind: CertificateSigningRequest\nmetadata:\n name: user-request-'${USER_NAME}'\nspec:\n groups:\n - system:authenticated\n request: '$encoded_string'\n usages:\n - digital signature\n - key encipherment\n - client auth' > /certs/cert-request-${USER_NAME}.yaml
echo "Creating Cert Request in Cluster"
kubectl create -f /certs/cert-request-${USER_NAME}.yaml
echo "Approving the Cert Request"
kubectl certificate approve user-request-${USER_NAME}
kubectl get csr user-request-${USER_NAME} -o jsonpath='{.status.certificate}' | base64 --decode > /certs/${USER_NAME}.crt
echo "Fetching Certificate:"
cat /certs/${USER_NAME}.crt
echo
echo Creating /certs/config-${USER_NAME}
touch /certs/config-${USER_NAME}
echo
echo Setting cluster in /certs/config-${USER_NAME}
kubectl config set-cluster ${CLUSTER_NAME} --kubeconfig /certs/config-${USER_NAME} --server ${SERVER_URL} --certificate-authority='/usr/src/certs/ca.crt' --embed-certs=true
echo
echo Setting credentials for user ${USER_NAME} in /certs/config-${USER_NAME}
kubectl --kubeconfig /certs/config-${USER_NAME} config set-credentials ${USER_NAME} --client-certificate=/certs/${USER_NAME}.crt --client-key=/certs/${USER_NAME}.pem --embed-certs=true
echo
echo Setting the context ${CLUSTER_NAME}-${USER_NAME} for user ${USER_NAME} and cluster ${CLUSTER_NAME} in /certs/config-${USER_NAME}
kubectl --kubeconfig /certs/config-${USER_NAME} config set-context ${CLUSTER_NAME}-${USER_NAME} --cluster=${CLUSTER_NAME} --user=${USER_NAME}
echo
echo Using Context ${CLUSTER_NAME}-${USER_NAME}
kubectl --kubeconfig /certs/config-${USER_NAME} config use-context ${CLUSTER_NAME}-${USER_NAME}
echo
echo kubeconfig file /certs/config-${USER_NAME} created
echo