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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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
  18. # Ignored if password file already exists and -f / --force was not specified
  19. #
  20. # The init command will create the following files:
  21. # ${HOME}/.bfr_pass # the bcrypted password
  22. # ${HOME}/.bfr_token # the flex router token
  23. # ${HOME}/.ssh/flex.pub # the SSH public key
  24. # ${HOME}/.ssh/flex # the SSH private key
  25. #
  26. function die {
  27. echo 1>&2 "${1}"
  28. exit 1
  29. }
  30. function rand_string() {
  31. LEN=${1:-50}
  32. cat /dev/random | strings | tr -d [[:space:]] | head -c ${LEN}
  33. }
  34. function write_ssh_key() {
  35. KEY_FILE="${1}"
  36. KEY_DIR="$(dirname ${KEY_FILE})"
  37. if [[ ! -d "${KEY_DIR}" ]] ; then
  38. mkdir -p "${KEY_DIR}" && chmod 700 ${KEY_DIR} || die "Error creating SSH key directory: ${KEY_DIR}"
  39. fi
  40. ssh-keygen -t rsa -q -N '' -C 'bubble-flexrouter' -f ${KEY_FILE} || die "Error generating SSH key: ${KEY_FILE}"
  41. }
  42. echo "Initializing flex-router"
  43. FORCE=0
  44. DO_BCRYPT=1
  45. while [[ ! -z "${1}" && ${1} == -* ]] ; then
  46. if [[ ${1} == "--force" || ${1} == "-f" ]] ; then
  47. FORCE=1
  48. shift
  49. elif [[ ${1} == "--bcrypt" || ${1} == "-b" ]] ; then
  50. DO_BCRYPT=0
  51. shift
  52. else
  53. die "Only allowed options are: --force / -f and --bcrypt / -b"
  54. fi
  55. done
  56. BFR_PASSWORD_FILE="${HOME}/.bfr_pass"
  57. BFR_TOKEN_FILE="${HOME}/.bfr_token"
  58. BFR_SSH_KEY_FILE="${HOME}/.ssh/flex"
  59. WRITE_PASS=0
  60. if [[ -s ${BFR_PASSWORD_FILE} ]] ; then
  61. if [[ ${FORCE} -eq 1 ]] ; then
  62. echo "Password file exists but -f / --force was set, overwriting: ${BFR_PASSWORD_FILE}"
  63. WRITE_PASS=1
  64. else
  65. echo "Password file exists, not overwriting: ${BFR_PASSWORD_FILE}"
  66. fi
  67. else
  68. WRITE_PASS=1
  69. fi
  70. if [[ ${WRITE_PASS} -eq 1 ]] ; then
  71. if [[ $DO_BCRYPT -eq 1 ]] ; then
  72. if [[ -z "$(which htpasswd)" ]] ; then
  73. die "htpasswd command not found, cannot bcrypt password"
  74. fi
  75. fi
  76. BFR_PASSWORD_VAR="${1}"
  77. if [[ -z "${BFR_PASSWORD_VAR}" ]] ; then
  78. BFR_PASSWORD_VAR="BUBBLE_FR_PASS"
  79. fi
  80. BFR_PASSWORD="${!BFR_PASSWORD_VAR}"
  81. if [[ -z "${BFR_PASSWORD}" ]] ; then
  82. die "Environment variable ${BFR_PASSWORD_VAR} was not defined or was empty"
  83. fi
  84. if [[ $DO_BCRYPT -eq 1 ]] ; then
  85. echo "$(htpasswd -nbBC 12 USER "${BFR_PASSWORD}" | awk -F ':' '{print $2}')" > ${BFR_PASSWORD_FILE} || die "Error writing password file"
  86. else
  87. echo "${BFR_PASSWORD}" > ${BFR_PASSWORD_FILE} || die "Error writing password file"
  88. fi
  89. chmod 600 ${BFR_PASSWORD_FILE} || die "Error setting permission on password file: ${BFR_PASSWORD_FILE}"
  90. echo "Wrote bcrypted password to ${BFR_PASSWORD_FILE}"
  91. fi
  92. if [[ -s ${BFR_TOKEN_FILE} ]] ; then
  93. if [[ ${FORCE} -eq 0 ]] ; then
  94. echo "Token file exists, not overwriting: ${BFR_TOKEN_FILE}"
  95. else
  96. echo "Token file exists but -f / --force was set, overwriting: ${BFR_TOKEN_FILE}"
  97. echo "$(rand_string)" > "${BFR_TOKEN_FILE}"
  98. fi
  99. else
  100. echo "Token file not found or empty, creating: ${BFR_TOKEN_FILE}"
  101. echo "$(rand_string)" > "${BFR_TOKEN_FILE}"
  102. fi
  103. chmod 600 ${BFR_TOKEN_FILE} || die "Error setting permission on token file: ${BFR_TOKEN_FILE}"
  104. if [[ -s ${BFR_SSH_KEY_FILE} ]] ; then
  105. if [[ ${FORCE} -eq 0 ]] ; then
  106. echo "SSH key file exists, not overwriting: ${BFR_SSH_KEY_FILE}"
  107. else
  108. 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"
  109. write_ssh_key ${BFR_SSH_KEY_FILE}
  110. fi
  111. else
  112. write_ssh_key ${BFR_SSH_KEY_FILE}
  113. fi
  114. echo "Initialization completed successfully"