#!/usr/bin/env bash # EasyLife 365 setup script. Use case: identity/credential-renewal-secret (a renewed client secret). Version: v1. # Source: https://docs.easylife365.cloud/downloads/identity/credential-renewal-secret/v1/create.sh # # Creates a key vault and a secret in your subscription and gives the EasyLife 365 app access to the secret. # In the subscription you are signed in to, the script: # 1. looks up the service principal of the EasyLife 365 app (--app-id) in your tenant; # 2. creates the resource group --resource-group in --region, when the key vault and the group don't exist yet; # 3. creates the key vault --vault-name: standard tier, Azure role-based access control, soft delete kept 90 days. # An existing vault must use Azure role-based access control: on access policies the script stops; # 4. creates the secret --secret-name with a random value, through Azure Resource Manager. The value goes straight into # the vault and is never shown. The script doesn't need a role that reads or writes secrets for this; # 5. assigns the role "Key Vault Secrets Officer" to the service principal, on that secret only. # # You can run it again: whatever exists already is left as it is and listed as skipped. An existing secret keeps its # value. The script never changes the settings of an existing resource and never deletes anything. # # Run it as someone who can create resources in the subscription and give access there: Owner, or Contributor together # with User Access Administrator or Role Based Access Control Administrator. # # Needs Bash and the Azure CLI, signed in with `az login`. Azure Cloud Shell has everything. # # Example: # bash ./create.sh --vault-name contoso-renewal --secret-name app-secret --region westeurope \ # --app-id 00000000-0000-0000-0000-000000000000 set -euo pipefail # Git Bash on Windows rewrites arguments that start with a slash, such as a resource ID. Switch that off. export MSYS_NO_PATHCONV=1 usage() { cat <<'USAGE' Usage: bash ./create.sh --vault-name --secret-name --region --app-id [--resource-group ] [--subscription ] [--yes] --vault-name The key vault: 3 to 24 letters, digits and hyphens, starting with a letter, ending with a letter or digit, with no two hyphens in a row. Unique in Azure. --secret-name The secret: 1 to 127 letters, digits and hyphens. A name that starts with a hyphen works as it is. --region The Azure region of a new key vault, for example westeurope. --app-id The application (client) ID of the EasyLife 365 app in your tenant. --resource-group Optional. The resource group of a new key vault. Default: rg-easylife365. --subscription Optional. Without it the script uses the current subscription and asks you to confirm it. --yes Optional. Uses the current subscription without asking. USAGE } fail() { echo "Error: $*" >&2; exit 1; } # Git Bash adds a carriage return to every line the Azure CLI prints. azt() { az "$@" | tr -d '\r'; } lower() { printf '%s' "$1" | tr '[:upper:]' '[:lower:]'; } # The Azure CLI on Windows needs a Windows path for a file it reads. native_path() { if command -v cygpath >/dev/null 2>&1; then cygpath -w "$1"; else printf '%s' "$1"; fi; } vault=""; secret=""; region=""; app_id=""; resource_group="rg-easylife365"; subscription=""; assume_yes="false" while [ $# -gt 0 ]; do case "$1" in -h|--help) usage; exit 0 ;; --yes) assume_yes="true"; shift ;; --vault-name|--secret-name|--region|--app-id|--resource-group|--subscription) # The next argument is the value, even when it starts with a hyphen. [ $# -ge 2 ] || fail "$1 needs a value." case "$1" in --vault-name) vault="$2" ;; --secret-name) secret="$2" ;; --region) region="$2" ;; --app-id) app_id="$2" ;; --resource-group) resource_group="$2" ;; --subscription) subscription="$2" ;; esac shift 2 ;; *) echo "Unknown argument: $1" >&2; usage >&2; exit 2 ;; esac done # Every value is checked before anything is sent to Azure. if ! { [ -n "$vault" ] && [ -n "$secret" ] && [ -n "$region" ] && [ -n "$app_id" ]; }; then usage >&2; exit 2; fi re_vault='^[A-Za-z][A-Za-z0-9-]{1,22}[A-Za-z0-9]$' re_secret='^[A-Za-z0-9-]{1,127}$' re_region='^[A-Za-z0-9]{2,40}$' re_guid='^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$' re_group='^[-A-Za-z0-9._()]{1,90}$' [[ "$vault" =~ $re_vault && "$vault" != *--* ]] \ || fail "--vault-name must be 3 to 24 letters, digits and hyphens, start with a letter, end with a letter or digit, and have no two hyphens in a row." [[ "$secret" =~ $re_secret ]] || fail "--secret-name must be 1 to 127 letters, digits and hyphens." [[ "$region" =~ $re_region ]] || fail "--region must be the name of an Azure region, for example westeurope." [[ "$app_id" =~ $re_guid ]] || fail "--app-id must be the application (client) ID, a GUID such as 00000000-0000-0000-0000-000000000000." [[ "$resource_group" =~ $re_group ]] || fail "--resource-group is not a valid resource group name." role="Key Vault Secrets Officer" api_version="2023-07-01" created=""; skipped="" note_created() { created="${created}Created: $1"$'\n'; } note_skipped() { skipped="${skipped}Skipped: $1"$'\n'; } [ -z "$subscription" ] || az account set --subscription="$subscription" echo "Subscription: $(azt account show --query name -o tsv) ($(azt account show --query id -o tsv))" if [ -z "$subscription" ] && [ "$assume_yes" != "true" ]; then [ -t 0 ] || fail "Pass --subscription , or --yes to use the subscription above." read -r -p "Create the resources in this subscription? [y/N] " answer [ "$answer" = "y" ] || [ "$answer" = "Y" ] || fail "Stopped. Nothing was created. Pass --subscription to choose another subscription." fi # The service principal exists once an administrator has consented to the EasyLife 365 app in your tenant. sp_info=$(azt ad sp show --id "$app_id" --query "[id, displayName]" -o tsv 2>/dev/null) || sp_info="" sp_id=$(printf '%s\n' "$sp_info" | sed -n 1p) sp_name=$(printf '%s\n' "$sp_info" | sed -n 2p) [ -n "$sp_id" ] || fail "Stopped. Nothing was created. There is no service principal for the app $app_id in this tenant. Check that you're signed in to the right tenant, and that an administrator has consented to the EasyLife 365 app." echo "EasyLife 365 app: $sp_name, app ID $app_id, service principal $sp_id" # EasyLife 365 works with Azure role-based access control only. A vault on access policies is never changed here. assert_role_based_access() { rbac=$(azt resource show --ids "$1" --query properties.enableRbacAuthorization -o tsv) if [ "$(lower "$rbac")" != "true" ]; then fail "Stopped. The key vault $vault uses access policies. EasyLife 365 works with Azure role-based access control only. Switch the vault's permission model to Azure role-based access control (Access configuration in the Azure portal), or use another vault, then run the script again. The script changes nothing on this vault." fi } vault_id=$(azt resource list --resource-type Microsoft.KeyVault/vaults --name "$vault" --query "[0].id" -o tsv) if [ -n "$vault_id" ]; then assert_role_based_access "$vault_id" note_skipped "key vault $vault (exists in resource group $(echo "$vault_id" | cut -d/ -f5))" else if [ "$(azt group exists --name="$resource_group")" = "true" ]; then note_skipped "resource group $resource_group (exists)" else az group create --name="$resource_group" --location="$region" --only-show-errors -o none note_created "resource group $resource_group" fi echo "Creating the key vault $vault ..." az keyvault create --name="$vault" --resource-group="$resource_group" --location="$region" \ --sku standard --enable-rbac-authorization true --retention-days 90 --only-show-errors -o none note_created "key vault $vault" vault_id=$(azt resource show --resource-group="$resource_group" --resource-type Microsoft.KeyVault/vaults --name="$vault" --query id -o tsv) assert_role_based_access "$vault_id" fi # The secret is read and created through Azure Resource Manager, so the person running the script needs no data role. secret_url="https://management.azure.com$vault_id/secrets/$secret?api-version=$api_version" if answer=$(az rest --method get --url "$secret_url" -o none 2>&1); then note_skipped "secret $secret (exists, its value is kept)" else case "$answer" in *"Not Found"*|*NotFound*) ;; *) fail "Azure didn't answer whether the secret $secret exists: $answer" ;; esac body_file=$(mktemp) trap 'rm -f "$body_file"' EXIT chmod 600 "$body_file" # 32 random bytes, Base64. The value goes into a file only the current user can read and is removed at the end. printf '{"properties":{"value":"%s"}}' "$(head -c 32 /dev/urandom | base64 | tr -d '\r\n')" > "$body_file" az rest --method put --url "$secret_url" --body "@$(native_path "$body_file")" --only-show-errors -o none rm -f "$body_file" note_created "secret $secret (with a random value)" fi scope="$vault_id/secrets/$secret" assigned="false" scopes=$(azt role assignment list --scope="$scope" --role "$role" --query "[?principalId=='$sp_id'].scope" -o tsv) while IFS= read -r line; do if [ "$(lower "$line")" = "$(lower "$scope")" ]; then assigned="true"; fi done <