4
0

install-step-ra.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #!/bin/bash
  2. set -e
  3. # TODO:
  4. # - Parse params using argbash (argbash.io). Here's a template that I have tested but have not implemented yet:
  5. #
  6. # ARG_OPTIONAL_SINGLE([ca-url], , [the URL of the upstream (issuing) step-ca server])
  7. # ARG_OPTIONAL_SINGLE([fingerprint], , [the SHA256 fingerprint of the upstream peer step-ca server])
  8. # ARG_OPTIONAL_SINGLE([provisioner-name], , [the name of a JWK provisioner on the upstream CA that this RA will use])
  9. # ARG_OPTIONAL_SINGLE([provisioner-password-file], , [the name a file containing the upstream JWK provisioner password])
  10. # ARG_OPTIONAL_REPEATED([dns-name], , [DNS name of this RA that will appear on its TLS certificate; you may pass this flag multiple times])
  11. # ARG_OPTIONAL_SINGLE([listen-address], , [the address (and port #) this RA will listen on, eg. :443 or 127.0.0.1:4443])
  12. # ARG_HELP([This script will install and configure a Registration Authority that connects to an upstream CA running step-ca.])
  13. # ARGBASH_GO
  14. echo "This script will install and start a step-ca server running in Registration Authority (RA) mode."
  15. echo ""
  16. echo "You will need an upstream CA (URL and fingerprint)"
  17. echo "Don't have a CA? Sign up for a hosted CA at smallstep.com — or run your own."
  18. echo ""
  19. # Fail if this script is not run as root.
  20. if ! [ $(id -u) = 0 ]; then
  21. echo "This script must be run as root"
  22. exit 1
  23. fi
  24. # Architecture detection
  25. arch=$(uname -m)
  26. case $arch in
  27. x86_64) arch="amd64" ;;
  28. x86) arch="386" ;;
  29. i686) arch="386" ;;
  30. i386) arch="386" ;;
  31. aarch64) arch="arm64" ;;
  32. armv5*) arch="armv5" ;;
  33. armv6*) arch="armv6" ;;
  34. armv7*) arch="armv7" ;;
  35. esac
  36. if [ "$arch" = "armv5" ]; then
  37. echo "This script doesn't work on armv5 machines"
  38. exit 1
  39. fi
  40. if ! hash jq &> /dev/null; then
  41. echo "This script requires the jq commmand; please install it."
  42. exit 1
  43. fi
  44. if ! hash curl &> /dev/null; then
  45. echo "This script requires the curl commmand; please install it."
  46. exit 1
  47. fi
  48. if ! hash tar &> /dev/null; then
  49. echo "This script requires the tar commmand; please install it."
  50. exit 1
  51. fi
  52. while [ $# -gt 0 ]; do
  53. case "$1" in
  54. --ca-url)
  55. CA_URL="$2"
  56. shift
  57. shift
  58. ;;
  59. --fingerprint)
  60. CA_FINGERPRINT="$2"
  61. shift
  62. shift
  63. ;;
  64. --provisioner-name)
  65. CA_PROVISIONER_NAME="$2"
  66. shift
  67. shift
  68. ;;
  69. --provisioner-password-file)
  70. CA_PROVISIONER_JWK_PASSWORD_FILE="$2"
  71. shift
  72. shift
  73. ;;
  74. --dns-names)
  75. RA_DNS_NAMES="$2"
  76. shift
  77. shift
  78. ;;
  79. --listen-address)
  80. RA_ADDRESS="$2"
  81. shift
  82. shift
  83. ;;
  84. *)
  85. shift
  86. ;;
  87. esac
  88. done
  89. # Install step
  90. if ! hash step &> /dev/null; then
  91. echo "Installing 'step' in /usr/bin..."
  92. STEP_VERSION=$(curl -s https://api.github.com/repos/smallstep/cli/releases/latest | jq -r '.tag_name')
  93. curl -sLO https://github.com/smallstep/cli/releases/download/$STEP_VERSION/step_linux_${STEP_VERSION:1}_$arch.tar.gz
  94. tar xvzf step_linux_${STEP_VERSION:1}_$arch.tar.gz
  95. install -m 0755 -t /usr/bin step_${STEP_VERSION:1}/bin/step
  96. rm step_linux_${STEP_VERSION:1}_$arch.tar.gz
  97. rm -rf step_${STEP_VERSION:1}
  98. fi
  99. # Prompt for required parameters
  100. if [ -z "$CA_URL" ]; then
  101. CA_URL=""
  102. while [[ $CA_URL = "" ]]; do
  103. read -p "Issuing CA URL: " CA_URL < /dev/tty
  104. done
  105. fi
  106. if [ -z "$CA_FINGERPRINT" ]; then
  107. CA_FINGERPRINT=""
  108. while [[ $CA_FINGERPRINT = "" ]]; do
  109. read -p "Issuing CA Fingerprint: " CA_FINGERPRINT < /dev/tty
  110. done
  111. fi
  112. echo "Bootstrapping with the CA..."
  113. export STEPPATH=$(mktemp -d)
  114. step ca bootstrap --ca-url $CA_URL --fingerprint $CA_FINGERPRINT
  115. if [ -z "$CA_PROVISIONER_NAME" ]; then
  116. declare -a provisioners
  117. readarray -t provisioners < <(step ca provisioner list | jq -r '.[] | select(.type == "JWK") | .name')
  118. printf '%s\n' "${provisioners[@]}"
  119. printf "%b" "\nSelect a JWK provisioner:\n" >&2
  120. select provisioner in "${provisioners[@]}"; do
  121. if [ -n "$provisioner" ]; then
  122. echo "Using existing provisioner $provisioner."
  123. CA_PROVISIONER_NAME=$provisioner
  124. break
  125. else
  126. echo "Invalid selection!"
  127. fi
  128. done
  129. fi
  130. if [ -z "$RA_DNS_NAMES" ]; then
  131. RA_DNS_NAMES=""
  132. while [[ $RA_DNS_NAMES = "" ]]; do
  133. echo "What DNS names or IP addresses will your RA use?"
  134. read -p "(e.g. acme.example.com[,1.1.1.1,etc.]): " RA_DNS_NAMES < /dev/tty
  135. done
  136. fi
  137. count=0
  138. ra_dns_names_quoted=""
  139. for i in ${RA_DNS_NAMES//,/ }
  140. do
  141. if [ "$count" = "0" ]; then
  142. ra_dns_names_quoted="\"$i\""
  143. else
  144. ra_dns_names_quoted="${ra_dns_names_quoted}, \"$i\""
  145. fi
  146. count=$((count+1))
  147. done
  148. if [ "$count" = "0" ]; then
  149. echo "You must supply at least one RA DNS name"
  150. exit 1
  151. fi
  152. echo "Got here"
  153. if [ -z "$RA_ADDRESS" ]; then
  154. RA_ADDRESS=""
  155. while [[ $RA_ADDRESS = "" ]] ; do
  156. echo "What address should your RA listen on?"
  157. read -p "(e.g. :443 or 10.2.1.201:4430): " RA_ADDRESS < /dev/tty
  158. done
  159. fi
  160. if [ -z "$CA_PROVISIONER_JWK_PASSWORD_FILE" ]; then
  161. read -s -p "Enter the CA Provisioner Password: " CA_PROVISIONER_JWK_PASSWORD < /dev/tty
  162. printf "%b" "\n"
  163. fi
  164. echo "Installing 'step-ca' in /usr/bin..."
  165. CA_VERSION=$(curl -s https://api.github.com/repos/smallstep/certificates/releases/latest | jq -r '.tag_name')
  166. curl -sLO https://github.com/smallstep/certificates/releases/download/$CA_VERSION/step-ca_linux_${CA_VERSION:1}_$arch.tar.gz
  167. tar -xf step-ca_linux_${CA_VERSION:1}_$arch.tar.gz
  168. install -m 0755 -t /usr/bin step-ca_${CA_VERSION:1}/bin/step-ca
  169. setcap CAP_NET_BIND_SERVICE=+eip $(which step-ca)
  170. rm step-ca_linux_${CA_VERSION:1}_$arch.tar.gz
  171. rm -rf step-ca_${CA_VERSION:1}
  172. echo "Creating 'step' user..."
  173. export STEPPATH=/etc/step-ca
  174. useradd --system --home $(step path) --shell /bin/false step
  175. echo "Creating RA configuration..."
  176. mkdir -p $(step path)/db
  177. mkdir -p $(step path)/config
  178. cat <<EOF > $(step path)/config/ca.json
  179. {
  180. "address": "$RA_ADDRESS",
  181. "dnsNames": [$ra_dns_names_quoted],
  182. "db": {
  183. "type": "badgerV2",
  184. "dataSource": "/etc/step-ca/db"
  185. },
  186. "logger": {"format": "text"},
  187. "authority": {
  188. "type": "stepcas",
  189. "certificateAuthority": "$CA_URL",
  190. "certificateAuthorityFingerprint": "$CA_FINGERPRINT",
  191. "certificateIssuer": {
  192. "type" : "jwk",
  193. "provisioner": "$CA_PROVISIONER_NAME"
  194. },
  195. "provisioners": [{
  196. "type": "ACME",
  197. "name": "acme"
  198. }]
  199. },
  200. "tls": {
  201. "cipherSuites": [
  202. "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
  203. "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"
  204. ],
  205. "minVersion": 1.2,
  206. "maxVersion": 1.3,
  207. "renegotiation": false
  208. }
  209. }
  210. EOF
  211. if ! [ -z "$CA_PROVISIONER_JWK_PASSWORD" ]; then
  212. echo "Saving provisoiner password to $(step path)/password.txt..."
  213. echo $CA_PROVISIONER_JWK_PASSWORD > $(step path)/password.txt
  214. else
  215. echo "Copying provisioner password file to $(step path)/password.txt..."
  216. cp $CA_PROVISIONER_JWK_PASSWORD_FILE $(step path)/password.txt
  217. fi
  218. chmod 440 $(step path)/password.txt
  219. # Add a service to systemd for the RA.
  220. echo "Creating systemd service step-ca.service..."
  221. curl -sL https://raw.githubusercontent.com/smallstep/certificates/master/systemd/step-ca.service \
  222. -o /etc/systemd/system/step-ca.service
  223. echo "Creating RA mode override /etc/systemd/system/step-ca.service.d/local.conf..."
  224. mkdir /etc/systemd/system/step-ca.service.d
  225. cat <<EOF > /etc/systemd/system/step-ca.service.d/local.conf
  226. [Service]
  227. ; The empty ExecStart= clears the inherited ExecStart= value
  228. ExecStart=
  229. ExecStart=/usr/bin/step-ca config/ca.json --issuer-password-file password.txt
  230. EOF
  231. echo "Starting step-ca.service..."
  232. systemctl daemon-reload
  233. chown -R step:step $(step path)
  234. systemctl enable --now step-ca
  235. echo "Adding STEPPATH export to /root/.bash_profile..."
  236. echo "export STEPPATH=$STEPPATH" >> /root/.bash_profile
  237. echo "Finished. Check the journal with journalctl -fu step-ca.service"