Skip to content

Commit 668f372

Browse files
committed
feat(scripts): Enhance akv_auth script to detect run environment.
Signed-off-by: spbsoluble <[email protected]>
1 parent de029a3 commit 668f372

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,18 @@ jobs:
187187
run: |
188188
git config --global url."https://$GITHUB_TOKEN:[email protected]/".insteadOf "https://github.com/"
189189
190+
190191
- name: Install dependencies
191192
run: go mod download && go mod tidy
192193

193194
- name: Get secret from Azure Key Vault
194195
run: |
195-
. ./examples/auth/akv/akv_auth.sh
196+
. ./examples/auth/akv/akv_auth_v2.sh
196197
cat $HOME/.keyfactor/command_config.json
197198
198199
- name: Install kfutil
199200
run: |
201+
echo "Installing kfutil on self-hosted runner"
200202
make install
201203
202204
- name: Run tests

examples/auth/akv/akv_auth_v2.sh

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env bash
2+
set -e -o pipefail
3+
4+
# Define the default values using environment variables
5+
default_vault_name="${VAULT_NAME:-kfutil}"
6+
default_secret_name="${SECRET_NAME:-integration-labs}"
7+
echo "Default vault name: $default_vault_name"
8+
echo "Default secret name: $default_secret_name"
9+
10+
export METADATA_URL="http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://vault.azure.net"
11+
12+
read_keyvault_secret_azure() {
13+
local vault_name="$1"
14+
local secret_name="$2"
15+
16+
echo "Vault Name: $vault_name"
17+
echo "Secret Name: $secret_name"
18+
19+
# Make a request to the metadata endpoint
20+
echo "Querying metadata endpoint for access token..."
21+
echo "Metadata URL: $METADATA_URL"
22+
token_json=$(curl -H "Metadata: true" $METADATA_URL)
23+
24+
echo "Exporting access token to access_token variable..."
25+
# Parse the access token from the response JSON
26+
access_token=$(echo $token_json | jq -r .access_token)
27+
28+
# Now you can use the $access_token to authenticate and access Azure Key Vault
29+
echo "Access Token: $access_token"
30+
31+
secret_url="https://${vault_name}.vault.azure.net/secrets/${secret_name}?api-version=7.0"
32+
echo "Secret URL: $secret_url"
33+
34+
# Get the secret value from Azure Key Vault
35+
echo "Querying Azure Key Vault for secret value..."
36+
secret_value=$(curl -H "Authorization: Bearer ${access_token}" "$secret_url" | jq -r .value)
37+
38+
mkdir -p ~/.keyfactor
39+
echo "${secret_value}" | jq -r . > "${secret_name}.json"
40+
rm -f "${HOME}/.keyfactor/command_config.json" || true
41+
echo "${secret_value}" | jq -r . > "${HOME}/.keyfactor/command_config.json"
42+
}
43+
44+
read_keyvault_secret_cli() {
45+
local vault_name="$1"
46+
local secret_name="$2"
47+
48+
echo "Vault Name: $vault_name"
49+
echo "Secret Name: $secret_name"
50+
51+
# Check if the user is logged in to Azure CLI
52+
if ! az account show &> /dev/null; then
53+
echo "You are not logged in to Azure CLI. Please run 'az login' to continue."
54+
exit 1
55+
fi
56+
57+
# Get the secret value from Azure Key Vault using Azure CLI
58+
echo "Querying Azure Key Vault for secret value using Azure CLI..."
59+
secret_value=$(az keyvault secret show --vault-name "$vault_name" --name "$secret_name" --query value -o tsv)
60+
61+
mkdir -p ~/.keyfactor
62+
echo "${secret_value}" | jq -r . > "${secret_name}.json"
63+
rm -f "${HOME}/.keyfactor/command_config.json" || true
64+
echo "${secret_value}" | jq -r . > "${HOME}/.keyfactor/command_config.json"
65+
}
66+
67+
# Main script logic
68+
if curl -H "Metadata: true" --max-time 5 $METADATA_URL &> /dev/null; then
69+
# Running in Azure Cloud
70+
read_keyvault_secret_azure "$default_vault_name" "$default_secret_name"
71+
else
72+
# Running on a workstation
73+
if [[ $# -eq 0 ]]; then
74+
# No arguments provided, use default values from environment variables
75+
read_keyvault_secret_cli "$default_vault_name" "$default_secret_name"
76+
elif [[ $# -eq 2 ]]; then
77+
# Two arguments provided: vault_name and secret_name
78+
read_keyvault_secret_cli "$1" "$2"
79+
else
80+
echo "Usage: $0 [vault_name secret_name]"
81+
exit 1
82+
fi
83+
fi

0 commit comments

Comments
 (0)