|
1 | 1 | #!/bin/bash |
2 | | - |
3 | | -set -eu -o pipefail |
| 2 | +set -e -o pipefail -u |
4 | 3 |
|
5 | 4 | basedir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" |
| 5 | +credhelper="$basedir/git-credential-s3-secrets" |
6 | 6 |
|
7 | | -# shellcheck disable=SC1090 |
8 | | -. "$basedir/lib/shared.bash" |
9 | | - |
10 | | -# For resiliency, increase the number of attempts to retrieve credentials for IAM roles |
11 | | -# The default value is 1 |
12 | | -export AWS_METADATA_SERVICE_NUM_ATTEMPTS=3 |
13 | | - |
14 | | -TMPDIR=${TMPDIR:-/tmp} |
15 | | -AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-us-east-1} |
16 | | - |
17 | | -s3_bucket="${BUILDKITE_PLUGIN_S3_SECRETS_BUCKET:-}" |
18 | | -s3_bucket_prefix="${BUILDKITE_PLUGIN_S3_SECRETS_BUCKET_PREFIX:-$BUILDKITE_PIPELINE_SLUG}" |
19 | | - |
20 | | -if [[ -z "$s3_bucket" ]] ; then |
21 | | - exit 0 |
22 | | -fi |
23 | | - |
24 | | -echo "~~~ Downloading secrets from :s3: $s3_bucket" >&2; |
25 | | - |
26 | | -if ! s3_bucket_exists "$s3_bucket" ; then |
27 | | - echo "+++ :warning: Bucket $s3_bucket doesn't exist" >&2; |
28 | | - exit 1 |
29 | | -fi |
30 | | - |
31 | | -ssh_key_paths=( |
32 | | - "$s3_bucket_prefix/private_ssh_key" |
33 | | - "$s3_bucket_prefix/id_rsa_github" |
34 | | - "private_ssh_key" |
35 | | - "id_rsa_github" |
36 | | -) |
37 | | - |
38 | | -for key in ${ssh_key_paths[*]} ; do |
39 | | - echo "Checking ${key}" >&2 |
40 | | - if s3_exists "$s3_bucket" "$key" ; then |
41 | | - echo "Found ${key}, downloading" >&2; |
42 | | - if ! ssh_key=$(s3_download "${s3_bucket}" "$key") ; then |
43 | | - echo "+++ :warning: Failed to download ssh-key $key" >&2; |
44 | | - exit 1 |
45 | | - fi |
46 | | - echo "Downloaded ${#ssh_key} bytes of ssh key" |
47 | | - add_ssh_private_key_to_agent "$ssh_key" |
48 | | - key_found=1 |
49 | | - elif [[ $? -eq 2 ]] ; then |
50 | | - echo "+++ :warning: Failed to check if $key exists" >&2; |
51 | | - exit 1 |
52 | | - fi |
53 | | -done |
54 | | - |
55 | | -if [[ -z "${key_found:-}" ]] && [[ "${BUILDKITE_REPO:-}" =~ ^git@ ]] ; then |
56 | | - echo >&2 "+++ :warning: Failed to find an SSH key in secret bucket" |
57 | | - echo >&2 "The repository '$BUILDKITE_REPO' appears to use SSH for transport, but the elastic-ci-stack-s3-secrets-hooks plugin did not find any SSH keys in the $s3_bucket S3 bucket." |
58 | | - echo >&2 "See https://github.com/buildkite/elastic-ci-stack-for-aws#build-secrets for more information." |
59 | | -fi |
60 | | - |
61 | | -env_paths=( |
62 | | - "env" |
63 | | - "environment" |
64 | | - "${s3_bucket_prefix}/env" |
65 | | - "${s3_bucket_prefix}/environment" |
66 | | -) |
| 7 | +# s3secrets-helper must be in PATH |
| 8 | +envscript="$( |
| 9 | + BUILDKITE_PLUGIN_S3_SECRETS_CREDHELPER="$credhelper" \ |
| 10 | + s3secrets-helper |
| 11 | +)" |
67 | 12 |
|
68 | 13 | env_before="$(env | sort)" |
69 | | - |
70 | | -for key in ${env_paths[*]} ; do |
71 | | - echo "Checking ${key}" >&2 |
72 | | - if s3_exists "$s3_bucket" "$key" ; then |
73 | | - echo "Downloading env file from ${key}" >&2; |
74 | | - if ! envscript=$(s3_download "${s3_bucket}" "$key") ; then |
75 | | - echo "+++ :warning: Failed to download env from $key" >&2; |
76 | | - exit 1 |
77 | | - fi |
78 | | - echo "Evaluating ${#envscript} bytes of env" |
79 | | - set -o allexport |
80 | | - eval "$envscript" |
81 | | - set +o allexport |
82 | | - elif [[ $? -eq 2 ]] ; then |
83 | | - echo "Failed to check if $key exists" >&2; |
84 | | - fi |
85 | | -done |
86 | | - |
87 | | -git_credentials_paths=( |
88 | | - "git-credentials" |
89 | | - "${s3_bucket_prefix}/git-credentials" |
90 | | -) |
91 | | - |
92 | | -git_credentials=() |
93 | | - |
94 | | -for key in ${git_credentials_paths[*]} ; do |
95 | | - if s3_exists "$s3_bucket" "$key" ; then |
96 | | - echo "Adding git-credentials in $key as a credential helper" >&2; |
97 | | - git_credentials+=("'credential.helper=$basedir/git-credential-s3-secrets ${s3_bucket} ${key}'") |
98 | | - fi |
99 | | -done |
100 | | - |
101 | | -if [[ "${#git_credentials[@]}" -gt 0 ]] ; then |
102 | | - export GIT_CONFIG_PARAMETERS |
103 | | - GIT_CONFIG_PARAMETERS=$( IFS=' '; echo -n "${git_credentials[*]}" ) |
104 | | -fi |
| 14 | +echo "Evaluating ${#envscript} bytes of env" |
| 15 | +set -o allexport |
| 16 | +eval "$envscript" |
| 17 | +set +o allexport |
105 | 18 |
|
106 | 19 | if [[ "${BUILDKITE_PLUGIN_S3_SECRETS_DUMP_ENV:-}" =~ ^(true|1)$ ]] ; then |
107 | 20 | echo "~~~ Environment variables that were set" >&2; |
|
0 commit comments