Skip to content

Commit 8833adc

Browse files
install vault
1 parent 8dff4e6 commit 8833adc

1 file changed

Lines changed: 66 additions & 11 deletions

File tree

ci/get_dd_api_key.sh

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,78 @@
44
# under the Apache License Version 2.0.
55
# This product includes software developed at Datadog (https://www.datadoghq.com/).
66

7-
# Loads DD_API_KEY from Vault for CI jobs that need Datadog API access without
8-
# assuming an AWS role (e.g. unit-test Test Optimization agentless reporting).
7+
# Loads DD_API_KEY for CI jobs that need Datadog API access without assuming an
8+
# AWS role (e.g. unit-test Test Optimization agentless reporting).
9+
#
10+
# Resolution order:
11+
# 1. Use DD_API_KEY if already set (e.g. GitLab CI/CD project variable).
12+
# 2. Read from Vault via the gitlab-runner secrets path (requires vault CLI).
13+
#
14+
# Slim python CI images do not ship vault; the script installs a static binary
15+
# when needed. Runners must provide VAULT_ADDR / VAULT_TOKEN for Vault auth.
916

1017
set -e
1118

12-
printf "Getting DD API KEY...\n"
19+
VAULT_SECRETS_PATH="kv/k8s/gitlab-runner/datadog-lambda-python/secrets"
20+
VAULT_CLI_VERSION="${VAULT_CLI_VERSION:-1.18.5}"
1321

14-
DD_API_KEY=$(vault kv get -field=dd-api-key kv/k8s/gitlab-runner/datadog-lambda-python/secrets)
22+
_ensure_vault_cli() {
23+
if command -v vault >/dev/null 2>&1; then
24+
return 0
25+
fi
1526

16-
if [ -z "$DD_API_KEY" ]; then
17-
printf "[Error] DD_API_KEY is empty after Vault lookup.\n"
18-
exit 1
27+
local arch
28+
case "$(uname -m)" in
29+
x86_64 | amd64) arch=amd64 ;;
30+
aarch64 | arm64) arch=arm64 ;;
31+
*)
32+
printf "[Error] Unsupported architecture for vault install: %s\n" "$(uname -m)" >&2
33+
exit 1
34+
;;
35+
esac
36+
37+
local install_dir="${TMPDIR:-/tmp}/vault-cli-${VAULT_CLI_VERSION}-${arch}"
38+
mkdir -p "$install_dir"
39+
40+
if [ ! -x "${install_dir}/vault" ]; then
41+
if ! command -v curl >/dev/null 2>&1 || ! command -v unzip >/dev/null 2>&1; then
42+
apt-get update -qq
43+
apt-get install -y -qq curl unzip
44+
fi
45+
46+
local zip_url="https://releases.hashicorp.com/vault/${VAULT_CLI_VERSION}/vault_${VAULT_CLI_VERSION}_linux_${arch}.zip"
47+
printf "Installing vault CLI %s (%s)...\n" "$VAULT_CLI_VERSION" "$arch"
48+
curl -fsSL "$zip_url" -o "${install_dir}/vault.zip"
49+
unzip -qo "${install_dir}/vault.zip" -d "$install_dir"
50+
rm -f "${install_dir}/vault.zip"
51+
fi
52+
53+
export PATH="${install_dir}:${PATH}"
54+
}
55+
56+
_export_dd_api_key() {
57+
export DD_API_KEY
58+
59+
if [ -n "${GITLAB_ENV:-}" ]; then
60+
echo "DD_API_KEY=${DD_API_KEY}" >>"$GITLAB_ENV"
61+
fi
62+
}
63+
64+
if [ -n "${DD_API_KEY:-}" ]; then
65+
printf "Using DD_API_KEY from environment.\n"
66+
_export_dd_api_key
67+
exit 0
1968
fi
2069

21-
export DD_API_KEY
70+
printf "Getting DD API KEY from Vault...\n"
71+
72+
_ensure_vault_cli
73+
74+
DD_API_KEY=$(vault kv get -field=dd-api-key "$VAULT_SECRETS_PATH")
2275

23-
# Persist for later script steps when the runner uses separate shells per step.
24-
if [ -n "${GITLAB_ENV:-}" ]; then
25-
echo "DD_API_KEY=${DD_API_KEY}" >>"$GITLAB_ENV"
76+
if [ -z "$DD_API_KEY" ]; then
77+
printf "[Error] DD_API_KEY is empty after Vault lookup.\n" >&2
78+
exit 1
2679
fi
80+
81+
_export_dd_api_key

0 commit comments

Comments
 (0)