doppler-env.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Fetch named secrets from Doppler into the environment of a Spacelift run.
  2. #
  3. # Usage, from a Spacelift hook (see .spacelift/config.yml), as two commands:
  4. #
  5. # . /mnt/workspace/source/scripts/spacelift/doppler-env.sh
  6. # cah_doppler_env ARG [ARG ...]
  7. #
  8. # Each ARG is either NAME (fetch NAME from Doppler, export it as NAME) or
  9. # VAR=NAME (fetch NAME from Doppler, export it as VAR). The second form exists
  10. # because Doppler only allows uppercase secret names, while OpenTofu reads a
  11. # variable named foo only from the exact environment variable TF_VAR_foo.
  12. #
  13. # It is sourced, not executed, so the exported variables reach the phase that
  14. # follows. Sourcing only defines functions: arguments to a sourced file are not
  15. # portable (dash ignores them), so the call is a separate command. POSIX sh
  16. # only: the Spacelift runner image is Alpine (busybox ash).
  17. #
  18. # What it does, in order:
  19. # 1. Skips unless the run is allowed to hold credentials (see below).
  20. # 2. Checks the Doppler settings that the Spacelift context provides.
  21. # 3. Installs the pinned Doppler CLI, verified against a pinned SHA-256.
  22. # 4. For each NAME, fetches it, exports it, and masks it in the run logs.
  23. #
  24. # Credentials are only fetched for a tracked or destroy run on the main branch.
  25. # This repository is public, so a pull request (proposed run) must never see
  26. # them: planning executes provider and data-source code from the pull request.
  27. #
  28. # Needs DOPPLER_TOKEN (a read-only service token, secret), DOPPLER_PROJECT and
  29. # DOPPLER_CONFIG, all from the cah-doppler Spacelift context.
  30. #
  31. # To change the pinned CLI, follow docs/spacelift-secrets.md.
  32. CAH_DOPPLER_VERSION="3.77.0"
  33. CAH_DOPPLER_SHA256_AMD64="92587cb89c2ff82fc2ddec2b2a9f275ff320994a44e16cd9310b55a429a82084"
  34. CAH_DOPPLER_SHA256_ARM64="3aa603d60bc8502ec67d9a57e917925f357ac2822eead484acda3875fe6a4d95"
  35. # CAH_DOPPLER_DIR is optional. When it is not set, a new private folder is made
  36. # with mktemp -d (never a fixed name in the shared /tmp, where another local user
  37. # could plant the file this script then runs).
  38. cah_doppler_log() {
  39. printf 'doppler-env: %s\n' "$*"
  40. }
  41. # The placeholder rule of scripts/secret_rule.py (the one definition) as POSIX
  42. # shell: succeeds when the value is empty or a placeholder. tests/test_secret_rule.py
  43. # pins this copy to the Python one. Compared in lower case.
  44. cah_doppler_is_placeholder() {
  45. case "$1" in
  46. *[![:space:]]*) ;;
  47. *) return 0 ;;
  48. esac
  49. _lc=$(printf '%s' "$1" | tr 'A-Z' 'a-z')
  50. case "$_lc" in
  51. your_* | your-* | changeme* | change-me* | replace-with* | replace_with* | xxx* | dp.st.your*) return 0 ;;
  52. '<'*'>' | *example* | *placeholder* | *changeme*) return 0 ;;
  53. todo | tbd | admin | password | secret | test) return 0 ;;
  54. esac
  55. return 1
  56. }
  57. # True for the Doppler names that hold a credential. A public value (an SSH
  58. # public key, a tenant name) may legitimately contain a word such as "example",
  59. # so the rule is applied to credentials only.
  60. cah_doppler_is_credential_name() {
  61. case "$1" in
  62. *_TOKEN | *_PASSWORD | *_PASSWD | *_SECRET | *_API_KEY | *_ACCESS_KEY | *_SECRET_KEY) return 0 ;;
  63. esac
  64. return 1
  65. }
  66. # Prints the name of the first failure and returns non-zero.
  67. cah_doppler_install() {
  68. _arch=$(uname -m)
  69. case "$_arch" in
  70. x86_64 | amd64) _arch=amd64 _sha=$CAH_DOPPLER_SHA256_AMD64 ;;
  71. aarch64 | arm64) _arch=arm64 _sha=$CAH_DOPPLER_SHA256_ARM64 ;;
  72. *)
  73. cah_doppler_log "unsupported architecture: $_arch"
  74. return 1
  75. ;;
  76. esac
  77. if command -v doppler >/dev/null 2>&1 &&
  78. [ "$(doppler --version 2>/dev/null | sed 's/^v//')" = "$CAH_DOPPLER_VERSION" ]; then
  79. return 0
  80. fi
  81. _tar="doppler_${CAH_DOPPLER_VERSION}_linux_${_arch}.tar.gz"
  82. _url="https://github.com/DopplerHQ/cli/releases/download/${CAH_DOPPLER_VERSION}/${_tar}"
  83. if [ -z "${CAH_DOPPLER_DIR:-}" ]; then
  84. CAH_DOPPLER_DIR=$(mktemp -d) || return 1
  85. else
  86. mkdir -p -m 700 "$CAH_DOPPLER_DIR" || return 1
  87. fi
  88. if [ -L "$CAH_DOPPLER_DIR" ] || [ ! -d "$CAH_DOPPLER_DIR" ] || [ ! -O "$CAH_DOPPLER_DIR" ]; then
  89. cah_doppler_log "refusing: $CAH_DOPPLER_DIR is a symlink, not a folder, or not yours"
  90. return 1
  91. fi
  92. if ! curl -fsSL --proto '=https' --tlsv1.2 --retry 3 -o "$CAH_DOPPLER_DIR/$_tar" "$_url"; then
  93. cah_doppler_log "download failed: $_url"
  94. return 1
  95. fi
  96. if ! printf '%s %s\n' "$_sha" "$CAH_DOPPLER_DIR/$_tar" | sha256sum -c - >/dev/null 2>&1; then
  97. cah_doppler_log "checksum mismatch for $_tar; refusing to run it"
  98. rm -f "$CAH_DOPPLER_DIR/$_tar"
  99. return 1
  100. fi
  101. tar -xzf "$CAH_DOPPLER_DIR/$_tar" -C "$CAH_DOPPLER_DIR" doppler || return 1
  102. rm -f "$CAH_DOPPLER_DIR/$_tar"
  103. PATH="$CAH_DOPPLER_DIR:$PATH"
  104. export PATH
  105. }
  106. cah_doppler_env() {
  107. _run_type="${TF_VAR_spacelift_run_type:-}"
  108. _branch="${TF_VAR_spacelift_commit_branch:-}"
  109. case "$_run_type" in
  110. TRACKED | DESTROY) ;;
  111. *)
  112. cah_doppler_log "run type '${_run_type:-unset}' does not get secrets; skipping"
  113. return 0
  114. ;;
  115. esac
  116. if [ "$_branch" != "main" ]; then
  117. cah_doppler_log "branch '${_branch:-unset}' is not main; skipping"
  118. return 0
  119. fi
  120. if [ "$#" -eq 0 ]; then
  121. cah_doppler_log "no secret names given"
  122. return 1
  123. fi
  124. for _arg in "$@"; do
  125. case "$_arg" in
  126. *=*) _var=${_arg%%=*} _src=${_arg#*=} ;;
  127. *) _var=$_arg _src=$_arg ;;
  128. esac
  129. for _n in "$_var" "$_src"; do
  130. case "$_n" in
  131. '' | [0-9]* | *[!A-Za-z0-9_]*)
  132. cah_doppler_log "invalid secret name: $_arg"
  133. return 1
  134. ;;
  135. esac
  136. done
  137. done
  138. for _var in DOPPLER_TOKEN DOPPLER_PROJECT DOPPLER_CONFIG; do
  139. eval "_val=\${$_var:-}"
  140. if [ -z "$_val" ]; then
  141. cah_doppler_log "$_var is not set; is the cah-doppler context attached?"
  142. return 1
  143. fi
  144. done
  145. if cah_doppler_is_placeholder "$DOPPLER_TOKEN"; then
  146. cah_doppler_log "DOPPLER_TOKEN is a placeholder; set the real read-only service token in the cah-doppler context"
  147. return 1
  148. fi
  149. cah_doppler_install || return 1
  150. for _arg in "$@"; do
  151. case "$_arg" in
  152. *=*) _var=${_arg%%=*} _src=${_arg#*=} ;;
  153. *) _var=$_arg _src=$_arg ;;
  154. esac
  155. if ! _val=$(doppler secrets get "$_src" --plain --no-check-version \
  156. --project "$DOPPLER_PROJECT" --config "$DOPPLER_CONFIG" 2>/dev/null); then
  157. cah_doppler_log "could not read $_src from Doppler"
  158. return 1
  159. fi
  160. if [ -z "$_val" ]; then
  161. cah_doppler_log "$_src is empty in Doppler"
  162. return 1
  163. fi
  164. if cah_doppler_is_credential_name "$_src" && cah_doppler_is_placeholder "$_val"; then
  165. cah_doppler_log "$_src in Doppler is a placeholder; set the real value (the value is not shown)"
  166. unset _val
  167. return 1
  168. fi
  169. echo "::add-mask $_val"
  170. export "$_var=$_val"
  171. cah_doppler_log "loaded $_src as $_var"
  172. done
  173. unset _val
  174. }