Bubble proxy service
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.8 KiB

  1. #!/bin/bash
  2. #
  3. # Utility to initialize a bubble-flexrouter.
  4. #
  5. # Note: Initialization requires the `htpasswd` program to be installed, to compute the bcrypted password
  6. #
  7. # Usage:
  8. #
  9. # flex_init.sh [-f|--force] [-b|--bcrypt] [flex-password-env-var]
  10. #
  11. # -f or --force : Recreate token file, password file and SSH key, even if present
  12. #
  13. # -b or --bcrypt : If set, this means that the contents of the flex-password-env-var
  14. # is a bcrypted password, not a plaintext password
  15. #
  16. # flex-password-env-var : Name of environment variable containing password to bcrypt and write to password file
  17. # Default value is BUBBLE_FR_PASS, or if not set, from prompt
  18. # Ignored if password file already exists and -f / --force was not specified
  19. #
  20. # The init command will create the following files:
  21. # ${FLEX_HOME}/.bfr_pass # the bcrypted password
  22. # ${FLEX_HOME}/.bfr_token # the flex router token
  23. # ${FLEX_HOME}/.ssh/flex.pub # the SSH public key
  24. # ${FLEX_HOME}/.ssh/flex # the SSH private key
  25. #
  26. # Environment variables:
  27. #
  28. # FLEX_HOME : the base directory for files. Default is ${HOME}
  29. #
  30. SCRIPT="${0}"
  31. function die {
  32. echo 1>&2 "${1}"
  33. exit 1
  34. }
  35. function log {
  36. echo 1>&2 "${SCRIPT} : ${1}"
  37. }
  38. case "$(uname -a | awk '{print $1}')" in
  39. Linux*)
  40. if [[ -z "${BUBBLE_DIST_HOME}" ]] ; then
  41. SHA_CMD="sha256sum"
  42. fi
  43. ;;
  44. Darwin*)
  45. SHA_CMD="shasum -a 256"
  46. ;;
  47. CYGWIN*)
  48. SHA_CMD="sha256sum"
  49. ;;
  50. esac
  51. function rand_string() {
  52. cat /dev/random | strings | head -c 1000 | ${SHA_CMD}
  53. }
  54. function write_ssh_key() {
  55. KEY_FILE="${1}"
  56. KEY_DIR="$(dirname ${KEY_FILE})"
  57. if [[ ! -d "${KEY_DIR}" ]] ; then
  58. mkdir -p "${KEY_DIR}" && chmod 700 ${KEY_DIR} || die "Error creating SSH key directory: ${KEY_DIR}"
  59. fi
  60. ssh-keygen -t rsa -q -N '' -C 'bubble-flexrouter' -f ${KEY_FILE} || die "Error generating SSH key: ${KEY_FILE}"
  61. }
  62. log "Initializing flex-router"
  63. FORCE=0
  64. DO_BCRYPT=1
  65. while [[ ! -z "${1}" && ${1} == -* ]] ; do
  66. if [[ ${1} == "--force" || ${1} == "-f" ]] ; then
  67. FORCE=1
  68. shift
  69. elif [[ ${1} == "--bcrypt" || ${1} == "-b" ]] ; then
  70. DO_BCRYPT=0
  71. shift
  72. else
  73. die "Only allowed options are: --force / -f and --bcrypt / -b"
  74. fi
  75. done
  76. if [[ -z "${FLEX_HOME}" ]] ; then
  77. FLEX_HOME="${HOME}"
  78. fi
  79. BFR_PASSWORD_FILE="${HOMEFLEX_HOME}/.bfr_pass"
  80. BFR_TOKEN_FILE="${FLEX_HOME}/.bfr_token"
  81. BFR_SSH_KEY_FILE="${FLEX_HOME}/.ssh/flex"
  82. WRITE_PASS=0
  83. if [[ -s ${BFR_PASSWORD_FILE} ]] ; then
  84. if [[ ${FORCE} -eq 1 ]] ; then
  85. log "Password file exists but -f / --force was set, overwriting: ${BFR_PASSWORD_FILE}"
  86. WRITE_PASS=1
  87. else
  88. log "Password file exists, not overwriting: ${BFR_PASSWORD_FILE}"
  89. fi
  90. else
  91. WRITE_PASS=1
  92. fi
  93. if [[ ${WRITE_PASS} -eq 1 ]] ; then
  94. if [[ $DO_BCRYPT -eq 1 ]] ; then
  95. if [[ -z "$(which htpasswd)" ]] ; then
  96. die "htpasswd command not found, cannot bcrypt password"
  97. fi
  98. fi
  99. BFR_PASSWORD_VAR="${1}"
  100. if [[ -z "${BFR_PASSWORD_VAR}" ]] ; then
  101. BFR_PASSWORD_VAR="BUBBLE_FR_PASS"
  102. fi
  103. BFR_PASSWORD="${!BFR_PASSWORD_VAR}"
  104. if [[ -z "${BFR_PASSWORD}" ]] ; then
  105. read -sp "Bubble Flex Router Password: " BFR_PASSWORD
  106. # trim leading and trailing whitespace
  107. BFR_PASSWORD="$(echo -n "${BFR_PASSWORD}" | awk '{$1=$1};1')"
  108. if [[ -z "${BFR_PASSWORD}" ]] ; then
  109. die "No password set"
  110. fi
  111. fi
  112. if [[ $DO_BCRYPT -eq 1 ]] ; then
  113. echo "$(htpasswd -nbBC 12 USER "${BFR_PASSWORD}" | awk -F ':' '{print $2}')" > ${BFR_PASSWORD_FILE} || die "Error writing password file"
  114. else
  115. echo "${BFR_PASSWORD}" > ${BFR_PASSWORD_FILE} || die "Error writing password file"
  116. fi
  117. chmod 600 ${BFR_PASSWORD_FILE} || die "Error setting permission on password file: ${BFR_PASSWORD_FILE}"
  118. log "Wrote bcrypted password to ${BFR_PASSWORD_FILE}"
  119. fi
  120. if [[ -s ${BFR_TOKEN_FILE} ]] ; then
  121. if [[ ${FORCE} -eq 0 ]] ; then
  122. log "Token file exists, not overwriting: ${BFR_TOKEN_FILE}"
  123. else
  124. log "Token file exists but -f / --force was set, overwriting: ${BFR_TOKEN_FILE}"
  125. echo "$(rand_string)" > "${BFR_TOKEN_FILE}"
  126. fi
  127. else
  128. log "Token file not found or empty, creating: ${BFR_TOKEN_FILE}"
  129. echo "$(rand_string)" > "${BFR_TOKEN_FILE}"
  130. fi
  131. chmod 600 ${BFR_TOKEN_FILE} || die "Error setting permission on token file: ${BFR_TOKEN_FILE}"
  132. if [[ -s ${BFR_SSH_KEY_FILE} ]] ; then
  133. if [[ ${FORCE} -eq 0 ]] ; then
  134. log "SSH key file exists, not overwriting: ${BFR_SSH_KEY_FILE}"
  135. else
  136. log "SSH key file exists but -f / --force was set, overwriting: ${BFR_SSH_KEY_FILE}"
  137. rm -f ${BFR_SSH_KEY_FILE} ${BFR_SSH_KEY_FILE}.pub || die "Error removing existing key file: ${BFR_SSH_KEY_FILE} and ${BFR_SSH_KEY_FILE}.pub"
  138. write_ssh_key ${BFR_SSH_KEY_FILE}
  139. fi
  140. else
  141. log "SSH key file not found or empty, creating: ${BFR_SSH_KEY_FILE}"
  142. write_ssh_key ${BFR_SSH_KEY_FILE}
  143. fi
  144. log "Initialization completed successfully"