#!/usr/bin/env bash
#
# The identity Qry uses for its own work in Azure Databricks (catalog,
# profiling, anything not done as a person): an Entra service principal with
# OAuth client credentials, so no personal access token expires or carries an
# administrator's rights. Run by a workspace administrator with their own
# `az login`.
#
# Usage:
#   ./azure-databricks.sh --host adb-123.4.azuredatabricks.net --warehouse <id> \
#     --catalog main --schemas sales,finance [--write-schemas staging] \
#     [--name qry-databricks] [--apply]
#
# Without --apply it prints every command and changes nothing. With it, it
# creates the Entra app and its secret (written to ./qry-databricks-secret.txt,
# mode 600), adds the principal to the workspace and gives it Can use on the
# warehouse. The Unity Catalog grants are written to ./qry-databricks-grants.sql
# for the catalog owner to run: running SQL here would start the warehouse.
#
# Afterwards: `qry doctor --plane prerequisites --databricks-host <host>
# --databricks-sp <appId> --databricks-warehouse <id> --databricks-catalog <c>`.
set -euo pipefail

HOST="" WAREHOUSE="" CATALOG="" SCHEMAS="" WRITE_SCHEMAS="" NAME="qry-databricks" APPLY=0
while [[ $# -gt 0 ]]; do
  case "$1" in
    --host) HOST="${2#https://}"; HOST="${HOST%/}"; shift 2 ;;
    --warehouse) WAREHOUSE="$2"; shift 2 ;;
    --catalog) CATALOG="$2"; shift 2 ;;
    --schemas) SCHEMAS="$2"; shift 2 ;;
    --write-schemas) WRITE_SCHEMAS="$2"; shift 2 ;;
    --name) NAME="$2"; shift 2 ;;
    --apply) APPLY=1; shift ;;
    -h|--help) sed -n '2,22p' "$0"; exit 0 ;;
    *) echo "unknown option $1" >&2; exit 2 ;;
  esac
done
[[ -n "$HOST" && -n "$WAREHOUSE" && -n "$CATALOG" && -n "$SCHEMAS" ]] || {
  echo "--host, --warehouse, --catalog and --schemas are required" >&2; exit 2; }

DATABRICKS=2ff814a6-3304-4ab8-85cb-cd0e6f879c1d
api() {  # method path [json-body]
  local args=(az rest --method "$1" --resource "$DATABRICKS" --url "https://$HOST$2")
  [[ $# -ge 3 ]] && args+=(--headers Content-Type=application/json --body "$3")
  echo "+ ${args[*]}"
  if (( APPLY )); then "${args[@]}" -o none; fi
}

SECRET_FILE="./qry-databricks-secret.txt"
# The secret goes to a new file only: an existing one (or a symlink) could
# have wider permissions than this script would give it, or point elsewhere.
if (( APPLY )) && [[ -e "$SECRET_FILE" || -L "$SECRET_FILE" ]]; then
  echo "$SECRET_FILE already exists; move it away first" >&2; exit 1
fi

echo "# Qry's Databricks identity on $HOST$( ((APPLY)) || echo ' (dry run: nothing is changed)')"
if (( APPLY )); then
  APP_ID=$(az ad app create --display-name "$NAME" --sign-in-audience AzureADMyOrg --query appId -o tsv)
  az ad sp create --id "$APP_ID" -o none
  ( umask 077; set -o noclobber; az ad app credential reset --id "$APP_ID" --display-name qry \
      --years 1 --append --query password -o tsv > "$SECRET_FILE" )
  echo "+ created $NAME ($APP_ID); secret in ./qry-databricks-secret.txt"
else
  APP_ID="<appId>"
  echo "+ az ad app create --display-name $NAME --sign-in-audience AzureADMyOrg"
  echo "+ az ad sp create --id <appId>"
  echo "+ az ad app credential reset --id <appId> --display-name qry --years 1 > ./qry-databricks-secret.txt"
fi

# The order matters: Entra issues a token for the app straight away, but every
# call to Databricks answers 401 until the principal is in the workspace.
api post /api/2.0/preview/scim/v2/ServicePrincipals \
  "{\"schemas\":[\"urn:ietf:params:scim:schemas:core:2.0:ServicePrincipal\"],\"applicationId\":\"$APP_ID\",\"displayName\":\"$NAME\",\"active\":true}"
# PATCH adds to the warehouse's permissions; PUT would replace them.
api patch "/api/2.0/permissions/sql/warehouses/$WAREHOUSE" \
  "{\"access_control_list\":[{\"service_principal_name\":\"$APP_ID\",\"permission_level\":\"CAN_USE\"}]}"

GRANTS=./qry-databricks-grants.sql
{
  echo "-- Unity Catalog grants for Qry's service principal ($APP_ID)."
  echo "-- Read only, on the schemas Qry should answer from. Run as the catalog owner."
  echo "GRANT USE CATALOG ON CATALOG \`$CATALOG\` TO \`$APP_ID\`;"
  IFS=, read -ra list <<<"$SCHEMAS"
  for s in "${list[@]}"; do
    echo "GRANT USE SCHEMA, SELECT ON SCHEMA \`$CATALOG\`.\`$s\` TO \`$APP_ID\`;"
  done
  if [[ -n "$WRITE_SCHEMAS" ]]; then
    echo "-- Only where Qry pipelines write; never on the whole catalog."
    IFS=, read -ra wlist <<<"$WRITE_SCHEMAS"
    for s in "${wlist[@]}"; do
      echo "GRANT USE SCHEMA, SELECT, CREATE TABLE, MODIFY ON SCHEMA \`$CATALOG\`.\`$s\` TO \`$APP_ID\`;"
    done
  fi
} > "$GRANTS"
echo "+ wrote $GRANTS"

END="<one year from today>"
if (( APPLY )); then
  END=$(az ad app credential list --id "$APP_ID" \
    --query "sort_by([?displayName=='qry'], &endDateTime)[-1].endDateTime" -o tsv | cut -c1-10)
fi
TENANT=$(az account show --query tenantId -o tsv 2>/dev/null || echo "<tenantId>")
cat <<EOF

# For the Databricks datasource in Qry (authentication: OAuth M2M):
#   Client ID      $APP_ID
#   Tenant ID      $TENANT
#   Client secret  the content of ./qry-databricks-secret.txt
#   Expires on     $END  (Qry tells administrators before that date)
#
# Then check it:
#   qry doctor --plane prerequisites --databricks-host $HOST --databricks-sp $APP_ID \\
#     --databricks-warehouse $WAREHOUSE --databricks-catalog $CATALOG
EOF
