From c1ac0a2fc0831d3f197c65a7072617c98628dd5c Mon Sep 17 00:00:00 2001 From: My Ho Date: Thu, 20 Jun 2019 10:45:08 -0700 Subject: [PATCH 1/2] add script for generating service principal so user don't have to login everytime they run `sls deploy` --- .gitignore | 2 +- scripts/generateSP.sh | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100755 scripts/generateSP.sh 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/generateSP.sh b/scripts/generateSP.sh new file mode 100755 index 00000000..af3c32ec --- /dev/null +++ b/scripts/generateSP.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +set -e + +SP_NAME=${1:-"http://FunctionsTestingPrincipal"} + +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 ${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 " From 3b372392ab4926e4751c8f9be1383e0776376e58 Mon Sep 17 00:00:00 2001 From: My Ho Date: Thu, 20 Jun 2019 14:50:22 -0700 Subject: [PATCH 2/2] add comments to explain script usage --- scripts/generate-service-principal.sh | 38 +++++++++++++++++++++++++++ scripts/generateSP.sh | 22 ---------------- 2 files changed, 38 insertions(+), 22 deletions(-) create mode 100755 scripts/generate-service-principal.sh delete mode 100755 scripts/generateSP.sh 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 " diff --git a/scripts/generateSP.sh b/scripts/generateSP.sh deleted file mode 100755 index af3c32ec..00000000 --- a/scripts/generateSP.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/sh - -set -e - -SP_NAME=${1:-"http://FunctionsTestingPrincipal"} - -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 ${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 "