-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure-service-principal.sh
147 lines (129 loc) · 4.45 KB
/
azure-service-principal.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
#-------------------------------------------------------------------------------------------------------------
# AUTHOR: JEFFREY CHIJIOKE-UCHE, DIT, MSIT, MSIS, DS
# USAGE: AZURE PLATFORM
# AUTHOR INITIAL CREATION: AUG 31, 2017
# LAST UPDATED: SEP. 1, 2022
# COMPANY: IBM
#--------------------------------------------------------------------------------------------------------------
#INSTALL: AZ CLI(WINDOWS) https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli
#INSTALL: AZ CLI (LINUX) https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-linux?pivots=apt
function azLogin(){
az login
}
# IBM Trigger v3.4.0
function azureServicePrincipal(){
while true; do
read -p "Do You Wish to login to Your Azure Account? [y|n] " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
#Login to your Azure Account && Configure Service Principal.
if [[ $yn == "n" || $yn == "N" || $yn == "No" || $yn == "no" || $yn == "NO" ]]
then
indiCators
echo "Ok, thats fine, you said that you do not need to sign-in to Azure account at this time!"
echo "Checking to ensure that you are already signed-in to Azure."
CheckState
servicePrincipal
exportIDs
elif [[ $yn == "y" || $yn == "Y" || $yn == "Yes" || $yn == "yes" || $yn == "YES" ]]
then
indiCators
echo "Ok, follow the browser prompt to sign-in to your Azure account!"
echo "Remember to return to this terminal after signing in to Azure."
azLogin
CheckState
servicePrincipal
exportIDs
else
echo "No matching Response!"
exit;
fi
}
function configRun(){
azureServicePrincipal
}
function servicePrincipal(){
RAND=$((1 + $RANDOM % 13455533234))
APP_NAME="mySvcPr-$RAND"
SUBSCRIPTION_ID=$(az account show --query id --output tsv)
SVCP=$(az ad sp create-for-rbac -n $APP_NAME --role Contributor --scopes /subscriptions/$SUBSCRIPTION_ID --output json --only-show-errors)
APP_ID=$(echo $SVCP | jq -r .appId)
OBJ_ID=$(az ad sp list --filter "appId eq '$APP_ID'" --output json | jq '.[0].objectId' -r)
PASS_ID=$(echo $SVCP | jq -r .password)
TENANT_ID=$(echo $SVCP | jq -r .tenant)
echo "Creating Role assinment.."
az role assignment create --role "User Access Administrator" --assignee-object-id $OBJ_ID --only-show-errors
CheckState
echo "Listing Role Assignment..."
az role assignment list --assignee $APP_ID --query [].roleDefinitionName --output json --only-show-errors
}
function exportIDs(){
reportBar
export ARM_CLIENT_ID=${APP_ID}
export ARM_CLIENT_SECRET=${PASS_ID}
export ARM_TENANT_ID=${TENANT_ID}
export ARM_SUBSCRIPTION_ID=${SUBSCRIPTION_ID}
echo "APP NAME: $APP_NAME"
echo "CLIENT ID: ${APP_ID}"
echo "CLIENT SECRET: ${PASS_ID}"
echo "TENANT ID: ${TENANT_ID}"
echo "USER OR APP PRINCIPAL OBJECT ID: $OBJ_ID"
echo "SUBSCRIPTION ID: ${SUBSCRIPTION_ID}"
azure
}
function CheckState(){
sleep 12s & PID=$!
echo -e "${CYAN}Checking, please wait....${NOCOLOR}"
printf "["
while kill -0 $PID 2> /dev/null; do
printf "${GREEN}|||||||"
sleep 2
done
printf "done!${NOCOLOR}]"
echo -e ""
}
function indiCators(){
NOCOLOR='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
LIGHTGRAY='\033[0;37m'
DARKGRAY='\033[1;30m'
LIGHTRED='\033[1;31m'
LIGHTGREEN='\033[1;32m'
YELLOW='\033[1;33m'
LIGHTBLUE='\033[1;34m'
LIGHTPURPLE='\033[1;35m'
LIGHTCYAN='\033[1;36m'
WHITE='\033[1;37m'
}
function reportBar(){
echo -ne '"[|||||||| (33%)\r'
sleep 1
echo -ne '[||||||||||||||||||||] (66%)\r'
sleep 2
echo -ne ${GREEN} '[||||||||||||||||||||||||||||||||||||] (100%) completed! \r' ${NOCOLOR}
echo -ne '\n'
}
function azure(){
echo -e "${GREEN}Azure Cloud Configuration Completed!${NOCOLOR}"
echo -e "${GREEN}
/---\ M I C R O S O F T A Z U R E
/ _ \ __________ _________ ____
/ /_\ \\___ / | \_ __ \_/ __ \
/ | \/ /| | /| | \/\ ___/
\____|__ /_____ \____/ |__| \___ >
\/ \/ \/
A Z U R E I N F R A S T R U C T U R E AS C O D E P R E-R E Q ${NOCOLOR}"
}
#Exec::::::::::::::::::::::#
configRun
#Exec::::::::::::::::::::::#