autocert.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/bin/bash
  2. #set -x
  3. echo "Welcome to Autocert configuration. Press return to begin."
  4. read ANYKEY
  5. STEPPATH=/home/step/.step
  6. CA_PASSWORD=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32 ; echo '')
  7. AUTOCERT_PASSWORD=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32 ; echo '')
  8. echo -e "\e[1mChecking cluster permissions...\e[0m"
  9. function permission_error {
  10. # TODO: Figure out the actual service account instead of assuming default.
  11. echo
  12. echo -e "\033[0;31mPERMISSION ERROR\033[0m"
  13. echo "Set permissions by running the following command, then try again:"
  14. echo -e "\e[1m"
  15. echo " kubectl create clusterrolebinding autocert-init-binding \\"
  16. echo " --clusterrole cluster-admin \\"
  17. echo " --user \"system:serviceaccount:default:default\""
  18. echo -e "\e[0m"
  19. echo "Once setup is complete you can remove this binding by running:"
  20. echo -e "\e[1m"
  21. echo " kubectl delete clusterrolebinding autocert-init-binding"
  22. echo -e "\e[0m"
  23. exit 1
  24. }
  25. echo -n "Checking for permission to create step namespace: "
  26. kubectl auth can-i create namespaces
  27. if [ $? -ne 0 ]; then
  28. permission_error "create step namespace"
  29. fi
  30. echo -n "Checking for permission to create configmaps in step namespace: "
  31. kubectl auth can-i create configmaps --namespace step
  32. if [ $? -ne 0 ]; then
  33. permission_error "create configmaps"
  34. fi
  35. echo -n "Checking for permission to create secrets in step namespace: "
  36. kubectl auth can-i create secrets --namespace step
  37. if [ $? -ne 0 ]; then
  38. permission_error "create secrets"
  39. fi
  40. echo -n "Checking for permission to create deployments in step namespace: "
  41. kubectl auth can-i create deployments --namespace step
  42. if [ $? -ne 0 ]; then
  43. permission_error "create deployments"
  44. fi
  45. echo -n "Checking for permission to create services in step namespace: "
  46. kubectl auth can-i create services --namespace step
  47. if [ $? -ne 0 ]; then
  48. permission_error "create services"
  49. fi
  50. echo -n "Checking for permission to create cluster role: "
  51. kubectl auth can-i create clusterrole
  52. if [ $? -ne 0 ]; then
  53. permission_error "create cluster roles"
  54. fi
  55. echo -n "Checking for permission to create cluster role binding:"
  56. kubectl auth can-i create clusterrolebinding
  57. if [ $? -ne 0 ]; then
  58. permission_error "create cluster role bindings"
  59. exit 1
  60. fi
  61. # Setting this here on purpose, after the above section which explicitly checks
  62. # for and handles exit errors.
  63. set -e
  64. step ca init \
  65. --name "$CA_NAME" \
  66. --dns "$CA_DNS" \
  67. --address "$CA_ADDRESS" \
  68. --provisioner "$CA_DEFAULT_PROVISIONER" \
  69. --with-ca-url "$CA_URL" \
  70. --password-file <(echo "$CA_PASSWORD")
  71. echo
  72. echo -e "\e[1mCreating autocert provisioner...\e[0m"
  73. expect <<EOD
  74. spawn step ca provisioner add autocert --create
  75. expect "Please enter a password to encrypt the provisioner private key? \\\\\\[leave empty and we'll generate one\\\\\\]: "
  76. send "${AUTOCERT_PASSWORD}\n"
  77. expect eof
  78. EOD
  79. echo
  80. echo -e "\e[1mCreating step namespace and preparing environment...\e[0m"
  81. kubectl create namespace step
  82. kubectl -n step create configmap config --from-file $(step path)/config
  83. kubectl -n step create configmap certs --from-file $(step path)/certs
  84. kubectl -n step create configmap secrets --from-file $(step path)/secrets
  85. kubectl -n step create secret generic ca-password --from-literal "password=${CA_PASSWORD}"
  86. kubectl -n step create secret generic autocert-password --from-literal "password=${AUTOCERT_PASSWORD}"
  87. # Deploy CA and wait for rollout to complete
  88. echo
  89. echo -e "\e[1mDeploying certificate authority...\e[0m"
  90. kubectl apply -f https://raw.githubusercontent.com/smallstep/certificates/autocert/autocert/install/01-step-ca.yaml
  91. kubectl -n step rollout status deployment/ca
  92. # Deploy autocert, setup RBAC, and wait for rollout to complete
  93. echo
  94. echo -e "\e[1mDeploying autocert...\e[0m"
  95. kubectl apply -f https://raw.githubusercontent.com/smallstep/certificates/autocert/autocert/install/02-autocert.yaml
  96. kubectl apply -f https://raw.githubusercontent.com/smallstep/certificates/autocert/autocert/install/03-rbac.yaml
  97. kubectl -n step rollout status deployment/autocert
  98. # Some `base64`s wrap lines... no thanks!
  99. CA_BUNDLE=$(cat $(step path)/certs/root_ca.crt | base64 | tr -d '\n')
  100. cat <<EOF | kubectl apply -f -
  101. apiVersion: admissionregistration.k8s.io/v1beta1
  102. kind: MutatingWebhookConfiguration
  103. metadata:
  104. name: autocert-webhook-config
  105. labels: {app: autocert}
  106. webhooks:
  107. - name: autocert.step.sm
  108. clientConfig:
  109. service:
  110. name: autocert
  111. namespace: step
  112. path: "/mutate"
  113. caBundle: $CA_BUNDLE
  114. rules:
  115. - operations: ["CREATE"]
  116. apiGroups: [""]
  117. apiVersions: ["v1"]
  118. resources: ["pods"]
  119. failurePolicy: Ignore
  120. namespaceSelector:
  121. matchLabels:
  122. autocert.step.sm: enabled
  123. EOF
  124. FINGERPRINT=$(step certificate fingerprint $(step path)/certs/root_ca.crt)
  125. echo
  126. echo -e "\e[1mAutocert installed!\e[0m"
  127. echo
  128. echo "Store this information somewhere safe:"
  129. echo " CA & admin provisioner password: ${CA_PASSWORD}"
  130. echo " Autocert password: ${AUTOCERT_PASSWORD}"
  131. echo " CA Fingerprint: ${FINGERPRINT}"
  132. echo