#!/usr/bin/env bash # EasyLife 365 setup script. Use case: identity/credential-renewal-certificate (a renewed certificate). Version: v1. # Source: https://docs.easylife365.cloud/downloads/identity/credential-renewal-certificate/v1/grant.sh # # Gives the EasyLife 365 app access to the certificates of an existing key vault in your subscription. The script finds # the key vault --vault-name in the subscription you are signed in to and checks that it uses Azure role-based access # control (on access policies it stops). Then it assigns the role "Key Vault Certificates Officer" to the service # principal of the EasyLife 365 app (--app-id), on the key vault. It creates nothing else. # # The role is assigned on the vault, not on the certificate --certificate-name: EasyLife 365 checks the certificate role at # the vault. So the app can create, import and delete every certificate in this vault. Use a vault that holds only the # certificates EasyLife 365 renews. # # You can run it again: a role that is assigned already is left as it is and listed as skipped. The script never changes # the settings of a resource and never deletes anything. # # Run it as someone who can give access on the key vault: Owner, 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 ./grant.sh --vault-name contoso-renewal --certificate-name app-certificate \ # --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 ./grant.sh --vault-name --certificate-name --app-id [--subscription ] --vault-name The key vault. --certificate-name The certificate EasyLife 365 renews: 1 to 127 letters, digits and hyphens. The script checks the name only and shows it. A name that starts with a hyphen works as it is. --app-id The application (client) ID of the EasyLife 365 app in your tenant. --subscription Optional. The subscription that holds the vault. Default: the current subscription. 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:]'; } vault=""; certificate=""; app_id=""; subscription="" while [ $# -gt 0 ]; do case "$1" in -h|--help) usage; exit 0 ;; --vault-name|--certificate-name|--app-id|--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" ;; --certificate-name) certificate="$2" ;; --app-id) app_id="$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 "$certificate" ] && [ -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_certificate='^[A-Za-z0-9-]{1,127}$' 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}$' [[ "$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." [[ "$certificate" =~ $re_certificate ]] || fail "--certificate-name must be 1 to 127 letters, digits and hyphens." [[ "$app_id" =~ $re_guid ]] || fail "--app-id must be the application (client) ID, a GUID such as 00000000-0000-0000-0000-000000000000." role="Key Vault Certificates Officer" [ -z "$subscription" ] || az account set --subscription="$subscription" echo "Subscription: $(azt account show --query name -o tsv) ($(azt account show --query id -o tsv))" 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 changed. 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" vault_id=$(azt resource list --resource-type Microsoft.KeyVault/vaults --name "$vault" --query "[0].id" -o tsv) [ -n "$vault_id" ] || fail "Stopped. Nothing was changed. There is no key vault $vault in this subscription. Pass --subscription with the subscription that holds it." # EasyLife 365 works with Azure role-based access control only. A vault on access policies is never changed here. rbac=$(azt resource show --ids "$vault_id" --query properties.enableRbacAuthorization -o tsv) [ "$(lower "$rbac")" = "true" ] \ || fail "Stopped. Nothing was changed. 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." scope="$vault_id" 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 <