Skip to content

Commit 48c2116

Browse files
author
James Healy
authored
Merge pull request #35 from buildkite/tidy-2020-10
🧹 early exit instead of indentation, unused grep_secrets removed.
2 parents 1af1b49 + c1334f0 commit 48c2116

File tree

2 files changed

+77
-79
lines changed

2 files changed

+77
-79
lines changed

hooks/environment

Lines changed: 77 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -17,91 +17,93 @@ AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-us-east-1}
1717
s3_bucket="${BUILDKITE_PLUGIN_S3_SECRETS_BUCKET:-}"
1818
s3_bucket_prefix="${BUILDKITE_PLUGIN_S3_SECRETS_BUCKET_PREFIX:-$BUILDKITE_PIPELINE_SLUG}"
1919

20-
if [[ -n "$s3_bucket" ]] ; then
21-
echo "~~~ Downloading secrets from :s3: $s3_bucket" >&2;
20+
if [[ -z "$s3_bucket" ]] ; then
21+
exit 0
22+
fi
2223

23-
if ! s3_bucket_exists "$s3_bucket" ; then
24-
echo "+++ :warning: Bucket $s3_bucket doesn't exist" >&2;
25-
exit 1
26-
fi
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
2730

28-
ssh_key_paths=(
29-
"$s3_bucket_prefix/private_ssh_key"
30-
"$s3_bucket_prefix/id_rsa_github"
31-
"private_ssh_key"
32-
"id_rsa_github"
33-
)
34-
35-
for key in ${ssh_key_paths[*]} ; do
36-
echo "Checking ${key}" >&2
37-
if s3_exists "$s3_bucket" "$key" ; then
38-
echo "Found ${key}, downloading" >&2;
39-
if ! ssh_key=$(s3_download "${s3_bucket}" "$key") ; then
40-
echo "+++ :warning: Failed to download ssh-key $key" >&2;
41-
exit 1
42-
fi
43-
echo "Downloaded ${#ssh_key} bytes of ssh key"
44-
add_ssh_private_key_to_agent "$ssh_key"
45-
key_found=1
46-
elif [[ $? -eq 2 ]] ; then
47-
echo "+++ :warning: Failed to check if $key exists" >&2;
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;
4844
exit 1
4945
fi
50-
done
51-
52-
if [[ -z "${key_found:-}" ]] && [[ "${BUILDKITE_REPO:-}" =~ ^git@ ]] ; then
53-
echo >&2 "+++ :warning: Failed to find an SSH key in secret bucket"
54-
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."
55-
echo >&2 "See https://github.com/buildkite/elastic-ci-stack-for-aws#build-secrets for more information."
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
5652
fi
53+
done
5754

58-
env_paths=(
59-
"env"
60-
"environment"
61-
"${s3_bucket_prefix}/env"
62-
"${s3_bucket_prefix}/environment"
63-
)
64-
65-
env_before="$(env | sort)"
66-
67-
for key in ${env_paths[*]} ; do
68-
echo "Checking ${key}" >&2
69-
if s3_exists "$s3_bucket" "$key" ; then
70-
echo "Downloading env file from ${key}" >&2;
71-
if ! envscript=$(s3_download "${s3_bucket}" "$key") ; then
72-
echo "+++ :warning: Failed to download env from $key" >&2;
73-
exit 1
74-
fi
75-
echo "Evaluating ${#envscript} bytes of env"
76-
set -o allexport
77-
eval "$envscript"
78-
set +o allexport
79-
elif [[ $? -eq 2 ]] ; then
80-
echo "Failed to check if $key exists" >&2;
81-
fi
82-
done
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
8360

84-
git_credentials_paths=(
85-
"git-credentials"
86-
"${s3_bucket_prefix}/git-credentials"
87-
)
61+
env_paths=(
62+
"env"
63+
"environment"
64+
"${s3_bucket_prefix}/env"
65+
"${s3_bucket_prefix}/environment"
66+
)
67+
68+
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
8886

89-
git_credentials=()
87+
git_credentials_paths=(
88+
"git-credentials"
89+
"${s3_bucket_prefix}/git-credentials"
90+
)
9091

91-
for key in ${git_credentials_paths[*]} ; do
92-
if s3_exists "$s3_bucket" "$key" ; then
93-
echo "Adding git-credentials in $key as a credential helper" >&2;
94-
git_credentials+=("'credential.helper=$basedir/git-credential-s3-secrets ${s3_bucket} ${key}'")
95-
fi
96-
done
92+
git_credentials=()
9793

98-
if [[ "${#git_credentials[@]}" -gt 0 ]] ; then
99-
export GIT_CONFIG_PARAMETERS
100-
GIT_CONFIG_PARAMETERS=$( IFS=' '; echo -n "${git_credentials[*]}" )
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}'")
10198
fi
99+
done
102100

103-
if [[ "${BUILDKITE_PLUGIN_S3_SECRETS_DUMP_ENV:-}" =~ ^(true|1)$ ]] ; then
104-
echo "~~~ Environment variables that were set" >&2;
105-
comm -13 <(echo "$env_before") <(env | sort) || true
106-
fi
101+
if [[ "${#git_credentials[@]}" -gt 0 ]] ; then
102+
export GIT_CONFIG_PARAMETERS
103+
GIT_CONFIG_PARAMETERS=$( IFS=' '; echo -n "${git_credentials[*]}" )
104+
fi
105+
106+
if [[ "${BUILDKITE_PLUGIN_S3_SECRETS_DUMP_ENV:-}" =~ ^(true|1)$ ]] ; then
107+
echo "~~~ Environment variables that were set" >&2;
108+
comm -13 <(echo "$env_before") <(env | sort) || true
107109
fi

lib/shared.bash

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,3 @@ add_ssh_private_key_to_agent() {
5050
echo "Loading ssh-key into ssh-agent (pid ${SSH_AGENT_PID:-})" >&2;
5151
echo "$ssh_key" | env SSH_ASKPASS="/bin/false" ssh-add -
5252
}
53-
54-
grep_secrets() {
55-
grep -E 'private_ssh_key|id_rsa_github|env|environment|git-credentials$' "$@"
56-
}

0 commit comments

Comments
 (0)