4
0

install-step-ra.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 ! hash jq &> /dev/null; then
  37. echo "This script requires the jq commmand; please install it."
  38. exit 1
  39. fi
  40. if ! hash curl &> /dev/null; then
  41. echo "This script requires the curl commmand; please install it."
  42. exit 1
  43. fi
  44. if ! hash tar &> /dev/null; then
  45. echo "This script requires the tar commmand; please install it."
  46. exit 1
  47. fi
  48. while [ $# -gt 0 ]; do
  49. case "$1" in
  50. --ca-url)
  51. CA_URL="$2"
  52. shift
  53. shift
  54. ;;
  55. --fingerprint)
  56. CA_FINGERPRINT="$2"
  57. shift
  58. shift
  59. ;;
  60. --provisioner-name)
  61. CA_PROVISIONER_NAME="$2"
  62. shift
  63. shift
  64. ;;
  65. --provisioner-password-file)
  66. CA_PROVISIONER_JWK_PASSWORD_FILE="$2"
  67. shift
  68. shift
  69. ;;
  70. --dns-names)
  71. RA_DNS_NAMES="$2"
  72. shift
  73. shift
  74. ;;
  75. --listen-address)
  76. RA_ADDRESS="$2"
  77. shift
  78. shift
  79. ;;
  80. *)
  81. shift
  82. ;;
  83. esac
  84. done
  85. # Install step
  86. if ! hash step &> /dev/null; then
  87. echo "Installing 'step' in /usr/bin..."
  88. STEP_VERSION=$(curl -s https://api.github.com/repos/smallstep/cli/releases/latest | jq -r '.tag_name')
  89. curl -sLO https://github.com/smallstep/cli/releases/download/$STEP_VERSION/step_linux_${STEP_VERSION:1}_$arch.tar.gz
  90. tar xvzf step_linux_${STEP_VERSION:1}_$arch.tar.gz
  91. install -m 0755 -t /usr/bin step_${STEP_VERSION:1}/bin/step
  92. rm step_linux_${STEP_VERSION:1}_$arch.tar.gz
  93. rm -rf step_${STEP_VERSION:1}
  94. fi
  95. # Prompt for required parameters
  96. if [ -z "$CA_URL" ]; then
  97. CA_URL=""
  98. while [[ $CA_URL = "" ]]; do
  99. read -p "Issuing CA URL: " CA_URL < /dev/tty
  100. done
  101. fi
  102. if [ -z "$CA_FINGERPRINT" ]; then
  103. CA_FINGERPRINT=""
  104. while [[ $CA_FINGERPRINT = "" ]]; do
  105. read -p "Issuing CA Fingerprint: " CA_FINGERPRINT < /dev/tty
  106. done
  107. fi
  108. echo "Bootstrapping with the CA..."
  109. export STEPPATH=$(mktemp -d)
  110. step ca bootstrap --ca-url $CA_URL --fingerprint $CA_FINGERPRINT
  111. if [ -z "$CA_PROVISIONER_NAME" ]; then
  112. declare -a provisioners
  113. readarray -t provisioners < <(step ca provisioner list | jq -r '.[] | select(.type == "JWK") | .name')
  114. printf '%s\n' "${provisioners[@]}"
  115. printf "%b" "\nSelect a JWK provisioner:\n" >&2
  116. select provisioner in "${provisioners[@]}"; do
  117. if [ -n "$provisioner" ]; then
  118. echo "Using existing provisioner $provisioner."
  119. CA_PROVISIONER_NAME=$provisioner
  120. break
  121. else
  122. echo "Invalid selection!"
  123. fi
  124. done
  125. fi
  126. if [ -z "$RA_DNS_NAMES" ]; then
  127. RA_DNS_NAMES=""
  128. while [[ $RA_DNS_NAMES = "" ]]; do
  129. echo "What DNS names or IP addresses will your RA use?"
  130. read -p "(e.g. acme.example.com[,1.1.1.1,etc.]): " RA_DNS_NAMES < /dev/tty
  131. done
  132. fi
  133. count=0
  134. ra_dns_names_quoted=""
  135. for i in ${RA_DNS_NAMES//,/ }
  136. do
  137. if [ "$count" = "0" ]; then
  138. ra_dns_names_quoted="\"$i\""
  139. else
  140. ra_dns_names_quoted="${ra_dns_names_quoted}, \"$i\""
  141. fi
  142. count=$((count+1))
  143. done
  144. if [ "$count" = "0" ]; then
  145. echo "You must supply at least one RA DNS name"
  146. exit 1
  147. fi
  148. echo "Got here"
  149. if [ -z "$RA_ADDRESS" ]; then
  150. RA_ADDRESS=""
  151. while [[ $RA_ADDRESS = "" ]] ; do
  152. echo "What address should your RA listen on?"
  153. read -p "(e.g. :443 or 10.2.1.201:4430): " RA_ADDRESS < /dev/tty
  154. done
  155. fi
  156. if [ -z "$CA_PROVISIONER_JWK_PASSWORD_FILE" ]; then
  157. read -s -p "Enter the CA Provisioner Password: " CA_PROVISIONER_JWK_PASSWORD < /dev/tty
  158. printf "%b" "\n"
  159. fi
  160. echo "Installing 'step-ca' in /usr/bin..."
  161. curl -sLO https://dl.smallstep.com/certificates/ra-installer/latest/step-ca_linux_$arch.tar.gz
  162. tar -xf step-ca_linux_$arch.tar.gz
  163. install -m 0755 -t /usr/bin step-ca_linux_$arch/step-ca
  164. setcap CAP_NET_BIND_SERVICE=+eip $(which step-ca)
  165. rm step-ca_linux_$arch.tar.gz
  166. rm -rf step-ca_linux_$arch
  167. echo "Creating 'step' user..."
  168. export STEPPATH=/etc/step-ca
  169. useradd --system --home $(step path) --shell /bin/false step
  170. echo "Creating RA configuration..."
  171. mkdir -p $(step path)/db
  172. mkdir -p $(step path)/config
  173. cat <<EOF > $(step path)/config/ca.json
  174. {
  175. "address": "$RA_ADDRESS",
  176. "dnsNames": [$ra_dns_names_quoted],
  177. "db": {
  178. "type": "badgerV2",
  179. "dataSource": "/etc/step-ca/db"
  180. },
  181. "logger": {"format": "text"},
  182. "authority": {
  183. "type": "stepcas",
  184. "certificateAuthority": "$CA_URL",
  185. "certificateAuthorityFingerprint": "$CA_FINGERPRINT",
  186. "certificateIssuer": {
  187. "type" : "jwk",
  188. "provisioner": "$CA_PROVISIONER_NAME"
  189. },
  190. "provisioners": [{
  191. "type": "ACME",
  192. "name": "acme"
  193. }]
  194. },
  195. "tls": {
  196. "cipherSuites": [
  197. "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
  198. "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"
  199. ],
  200. "minVersion": 1.2,
  201. "maxVersion": 1.3,
  202. "renegotiation": false
  203. }
  204. }
  205. EOF
  206. if ! [ -z "$CA_PROVISIONER_JWK_PASSWORD" ]; then
  207. echo "Saving provisoiner password to $(step path)/password.txt..."
  208. echo $CA_PROVISIONER_JWK_PASSWORD > $(step path)/password.txt
  209. else
  210. echo "Copying provisioner password file to $(step path)/password.txt..."
  211. cp $CA_PROVISIONER_JWK_PASSWORD_FILE $(step path)/password.txt
  212. fi
  213. chmod 440 $(step path)/password.txt
  214. # Add a service to systemd for the RA.
  215. echo "Creating systemd service step-ca.service..."
  216. curl -sL https://raw.githubusercontent.com/smallstep/certificates/master/systemd/step-ca.service \
  217. -o /etc/systemd/system/step-ca.service
  218. echo "Creating RA mode override /etc/systemd/system/step-ca.service.d/local.conf..."
  219. mkdir /etc/systemd/system/step-ca.service.d
  220. cat <<EOF > /etc/systemd/system/step-ca.service.d/local.conf
  221. [Service]
  222. ; The empty ExecStart= clears the inherited ExecStart= value
  223. ExecStart=
  224. ExecStart=/usr/bin/step-ca config/ca.json --issuer-password-file password.txt
  225. EOF
  226. echo "Starting step-ca.service..."
  227. systemctl daemon-reload
  228. chown -R step:step $(step path)
  229. systemctl enable --now step-ca
  230. echo "Adding STEPPATH export to /root/.bash_profile..."
  231. echo "export STEPPATH=$STEPPATH" >> /root/.bash_profile
  232. echo "Finished. Check the journal with journalctl -fu step-ca.service"