#!/usr/bin/env bash # EasyLife 365 setup script. Use case: audit/audit-workspace (the audit log). Version: v1. # Source: https://docs.easylife365.cloud/downloads/audit/audit-workspace/v1/grant.sh # # Lets the EasyLife 365 app read an existing Log Analytics workspace in your subscription. The script finds the workspace # --workspace-name in the subscription you are signed in to. Then it assigns the role "Log Analytics Reader" to the # service principal of the EasyLife 365 app (--app-id), on the workspace only. It creates nothing else. # # Each EasyLife 365 product has its own app. For a workspace that several products share, run the script once with the # app ID of each product. # # 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 workspace: 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 --workspace-name contoso-audit --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 --workspace-name --app-id [--resource-group ] [--subscription ] [--yes] --workspace-name The workspace. --app-id The application (client) ID of the EasyLife 365 app in your tenant. --resource-group Optional. When several workspaces in the subscription have the name, the resource group of the one you mean. --subscription Optional. The subscription that holds the workspace. Without it the script uses the current subscription and asks you to confirm it, because workspace names are not unique in Azure. --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:]'; } workspace=""; app_id=""; resource_group=""; subscription=""; assume_yes="false" while [ $# -gt 0 ]; do case "$1" in -h|--help) usage; exit 0 ;; --yes) assume_yes="true"; shift ;; --workspace-name|--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 --workspace-name) workspace="$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 "$workspace" ] && [ -n "$app_id" ]; }; then usage >&2; exit 2; fi re_workspace='^[A-Za-z0-9][A-Za-z0-9-]{2,61}[A-Za-z0-9]$' 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}$' [[ "$workspace" =~ $re_workspace ]] || fail "--workspace-name must be 4 to 63 letters, digits and hyphens, and start and end with a letter or digit." [[ "$app_id" =~ $re_guid ]] || fail "--app-id must be the application (client) ID, a GUID such as 00000000-0000-0000-0000-000000000000." [ -z "$resource_group" ] || [[ "$resource_group" =~ $re_group ]] || fail "--resource-group is not a valid resource group name." role="Log Analytics Reader" [ -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 "Give access on a workspace named $workspace in this subscription? [y/N] " answer [ "$answer" = "y" ] || [ "$answer" = "Y" ] || fail "Stopped. Nothing was changed. Pass --subscription to choose another subscription." fi 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" # Workspace names are unique per resource group only, so the same name can exist more than once. workspace_id=""; count=0 ids=$(azt resource list --resource-type Microsoft.OperationalInsights/workspaces --name "$workspace" --query "[].id" -o tsv) while IFS= read -r line; do [ -n "$line" ] || continue if [ -z "$resource_group" ] || [ "$(lower "$(echo "$line" | cut -d/ -f5)")" = "$(lower "$resource_group")" ]; then workspace_id="$line"; count=$((count + 1)) fi done <