-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-env.sh
More file actions
58 lines (50 loc) · 2.25 KB
/
Copy pathsetup-env.sh
File metadata and controls
58 lines (50 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash
# This script loads credentials from Hashicorp Vault into (exported) env vars from the secret specified below:
vaultSecret=concourse/meshstack-dev/trial-cloudfoundation
# Needs:
# * Hashicorp Vault CLI (command vault)
# * kubectl with access to the meshcloud internal infra kubernetes cluster hosting the Vault.
# Source this script into your local shell:
# > source setup-env.sh
# DO NOT execute this script (this script is intentionally marked non-executable).
set -a
# shellcheck disable=SC1090
source <(
set -euo pipefail
# Find the correct kubectl context created by gcloud
kubeContext=$(kubectl config get-contexts -o name | grep '^gke_meshstack-infra.*_meshstack-infra$' | head -n1)
if [[ -z "$kubeContext" ]]; then
echo "Failed to find kubectl context with prefix gke_meshstack-infra, try connecting by running: " >&2
echo "gcloud container clusters get-credentials meshstack-infra --region europe-west3 --project meshstack-infra" >&2
exit 1
fi
# Set up kubectl port-forward to vault with random local port
kubectlOutput=$(mktemp)
kubectl port-forward --context "$kubeContext" -n vault-new svc/vault :8200 >"$kubectlOutput" 2>&1 &
kubectlPid=$!
# shellcheck disable=SC2064
trap "kill $kubectlPid 2>/dev/null || true; rm -f '$kubectlOutput'" EXIT
# Wait for port-forward to establish and extract the local port
for i in $(seq 10 -1 0); do
localVaultPort=$(sed -n 's/.*Forwarding from 127.0.0.1:\([0-9]*\).*/\1/p' "$kubectlOutput" 2>/dev/null || true)
if [[ -n "$localVaultPort" ]]; then
export VAULT_ADDR=http://localhost:$localVaultPort
echo "Opened port forward to meshstack-infra vault at $VAULT_ADDR (attempts left: $i)" >&2
break
elif ((i == 0)); then
echo "$i: Failed to establish port-forward:" >&2
cat "$kubectlOutput" >&2
exit 1
fi
sleep 0.5
done
# Check if vault is already logged in, if not login with OIDC
if ! vault token lookup &>/dev/null; then
vault login -method=oidc >&2
fi
echo "Exporting the following SENSITIVE environment variables: " >&2
vault kv get -format=json "$vaultSecret" |
jq -r '.data.data | to_entries[] | "\(.key)='\''\(.value)'\''"' |
tee >(tr '\n' '\0' | sed "s/='[^']*'//g" | tr '\0' '\n' >/dev/stderr)
)
set +a