-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch-env.sh
65 lines (52 loc) · 2.09 KB
/
fetch-env.sh
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
59
60
61
62
63
64
65
#!/bin/bash
# Accept RESOURCE_GROUP and APP_NAME as arguments
RESOURCE_GROUP=$1
APP_NAME=$2
OUTPUT_FILE=".env"
# Check if required arguments are provided
if [ -z "$RESOURCE_GROUP" ] || [ -z "$APP_NAME" ]; then
echo "Usage: bash ./resolve-env.sh <RESOURCE_GROUP> <APP_NAME>"
exit 1
fi
# Function to fetch secrets from Azure Key Vault
fetch_keyvault_secret() {
local secret_reference="$1"
local vault_name
local secret_name
# Extract the Key Vault name and secret name
vault_name=$(echo "$secret_reference" | sed -n 's/.*VaultName=\([^;]*\).*/\1/p')
secret_name=$(echo "$secret_reference" | sed -n 's/.*SecretName=\([^)]*\).*/\1/p')
if [ -z "$vault_name" ] || [ -z "$secret_name" ]; then
echo "Error parsing Key Vault reference: $secret_reference"
return 1
fi
# Fetch the secret value
az keyvault secret show --vault-name "$vault_name" --name "$secret_name" --query "value" -o tsv 2>/dev/null
}
# Fetch App Settings from Azure App Service
echo "Fetching environment variables from App Service..."
app_settings=$(az webapp config appsettings list --resource-group "$RESOURCE_GROUP" --name "$APP_NAME" --query "[].{name:name, value:value}" -o json)
if [ -z "$app_settings" ]; then
echo "Failed to fetch app settings. Ensure the Azure CLI is configured and you have access to the App Service."
exit 1
fi
# Write to .env file
echo "# Fetched Environment Variables" > "$OUTPUT_FILE"
# Process each setting
echo "$app_settings" | jq -c '.[]' | while read -r setting; do
name=$(echo "$setting" | jq -r '.name')
value=$(echo "$setting" | jq -r '.value')
# Check if the value is a Key Vault reference
if [[ "$value" == @Microsoft.KeyVault* ]]; then
echo "Fetching Key Vault reference for $name..."
resolved_value=$(fetch_keyvault_secret "$value")
if [ $? -ne 0 ]; then
resolved_value="ERROR_FETCHING_SECRET"
fi
else
resolved_value="$value"
fi
# Write to the .env file
echo "${name}=${resolved_value}" >> "$OUTPUT_FILE"
done
echo "Fetched environment variables written to $OUTPUT_FILE"