-
Notifications
You must be signed in to change notification settings - Fork 20
/
configure-auth.sh
executable file
·130 lines (113 loc) · 4.52 KB
/
configure-auth.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
################################################################################################
#- Purpose: Script is used to create an Azure AD App Registration, set Azure
#- Keyvault secrets & restart the EventGrid Viewer Blazor Azure Web App Service.
#- Parameters are:
#- [-s] azure subscription - The Azure subscription to use (required)"
#- [-g] resource group name - The name of the Azure resource group ie rg-cse-egvb-dev (required)"
#- [-a] site name - The name of the Azure web app ie as-cse-egvb-dev (required)"
#- [-k] keyvault name - The name of the Azure Keyvault to store Azure AD secrets (required) ie kv-cse-egvb-dev"
#- [-h] help - Help (optional)"
################################################################################################
set -eu
###############################################################
#- function used to print out script usage
###############################################################
function usage() {
echo
echo "Arguments:"
echo -e "\t-s \t The Azure subscription to use (required)"
echo -e "\t-g \t The name of the Azure resource group ie rg-cse-egvb-dev (required)"
echo -e "\t-a \t The name of the Azure web app ie as-cse-egvb-dev (required)"
echo -e "\t-k \t The name of the Azure Keyvault to store Azure AD secrets ie kv-cse-egvb-dev (required)"
echo -e "\t-h \t Help (optional)"
echo
echo "Example:"
echo -e "./configure-auth.sh -s 00000000-1111-2222-3333-444444444444 -g rg-cse-egvb-dev -a as-cse-egvb-dev -k kv-cse-egvb-dev"
}
#######################################################
#- function used to print messages
#######################################################
function print() {
echo "$1"
}
###############################################################
#- function used to create a random password
###############################################################
function generateRandomPassword() {
local password=$(openssl rand -base64 16 | colrm 17 | sed 's/$/!/')
echo "#$password!2"
}
parent_path=$(
cd "$(dirname "${BASH_SOURCE[0]}")"
pwd -P
)
cd "$parent_path"
# Loop, get parameters & remove any spaces from input
while getopts "s:g:a:k:h" opt; do
case $opt in
s)
# subscription
subscription=$OPTARG
;;
g)
# resource group
resourceGroup=$OPTARG
;;
a)
# site name
siteName=$OPTARG
;;
k)
# keyvault name
keyvaultName=$OPTARG
;;
:)
echo "Error: -${OPTARG} requires a value"
exit 1
;;
*)
usage
exit 1
;;
esac
done
# validate parameters
if [[ $# -eq 0 || -z $subscription || -z $resourceGroup || -z $siteName || -z $keyvaultName ]]; then
error "Required parameters are missing"
usage
exit 1
fi
# login to azure
az login -o none
az account set -s $subscription
# get configuration values
managedIdentityPrincipalId=$(az webapp identity assign -n $siteName -g $resourceGroup --query 'principalId' -o tsv)
signedInUserPrincipalName=$(az ad signed-in-user show --query 'userPrincipalName' -o tsv)
secret=$(generateRandomPassword)
# create app registration
print "Creating app registration for $siteName"
appId=$(az ad app create --display-name $siteName \
--identifier-uris https://$siteName.azurewebsites.net \
--reply-urls https://$siteName.azurewebsites.net/signin-oidc \
--password $secret \
--required-resource-accesses @configure-auth-manifest.json \
--query 'appId' -o tsv)
# set access policies
print "Setting the keyvault access policies"
az keyvault set-policy --name $keyvaultName --upn $signedInUserPrincipalName --secret-permissions set get list delete -o none
az keyvault set-policy --name $keyvaultName --object-id $managedIdentityPrincipalId --secret-permissions set get list -o none
# setting keyvault secrets
print "Setting keyvault secrets"
az keyvault secret set --vault-name $keyvaultName -n 'sct-egvb-azad-client-id' --value $appId -o none
# restart the app
print "Stopping and starting the app"
az webapp stop -n $siteName -g $resourceGroup -o none
az webapp start -n $siteName -g $resourceGroup -o none
# output
print "#######################---OUTPUT---##################################"
print "Signed-In User: $signedInUserPrincipalName"
print "Managed Identity: $managedIdentityPrincipalId"
print "App Registration Secret: $secret"
print "App Registration AppId: $appId"
print "#####################################################################"