diff --git a/.gitignore b/.gitignore index 300f95f0..ee4336e6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ node_modules/ /lib/ *.dll /coverage/ -.env \ No newline at end of file +.env* \ No newline at end of file diff --git a/scripts/generate-service-principal.sh b/scripts/generate-service-principal.sh new file mode 100755 index 00000000..7df6adf2 --- /dev/null +++ b/scripts/generate-service-principal.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +### +# This script generate a service principal using your currently set subscription +# then write out the credentials of your new sp in a file +# that you can export as environment variables +### +set -e + +if ! [[ -x "$(command -v jq)" ]]; then + echo "Please install [jq] before continuing -> https://stedolan.github.io/jq/download/. Aborting." + exit 1 +fi + +SP_NAME=$1 + +if [[ -z $SP_NAME ]]; then + SP_NAME="FunctionsTestingPrincipal" + echo "You didn't pass in a name for the service principal. We'll use the default: ${SP_NAME}" + echo +fi + +SUBSCRIPTION=$(az account show | jq .name) +echo "You're using subscription: ${SUBSCRIPTION}" +echo "--> Creating service principal with name: ${SP_NAME}" +SP_RESPONSE=$(az ad sp create-for-rbac --name "http://${SP_NAME}") + +FILE=".env.servicePrincipal" + +echo "--> Writing creds to '${FILE}'" +cat <${FILE} +export azureSubId=$(az account show | jq .id) +export azureServicePrincipalTenantId=$(echo "${SP_RESPONSE}" | jq .tenant) +export azureServicePrincipalClientId=$(echo "${SP_RESPONSE}" | jq .name) +export azureServicePrincipalPassword=$(echo "${SP_RESPONSE}" | jq .password) +EOF + +echo "Run \"source ${FILE}\" to set your Azure account credentials "