-
Notifications
You must be signed in to change notification settings - Fork 2
/
userImport.sh
200 lines (198 loc) · 7.5 KB
/
userImport.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
# ------------------------------------------
# Import Users & Groups in Bulk Script
# Author : Prince Nyeche
# Platform: Atlassian Cloud Users
# Version: 0.8
# ------------------------------------------
# If you want to debug Script uncomment set -x
# set -x
printf "####################################################################### \n"
printf " CLOUD USERS & GROUPS IMPORT SCRIPT \n"
printf " Functions of this Script \n"
printf " ------------------------- \n"
printf " 1. VERIFY Credentials to Login \n"
printf " 2. VERIFY Users is valid before creating \n"
printf " 3. Creates Groups from CSV file \n"
printf " 4. Adds Users into Groups from CSV file \n"
printf " \n"
printf " To suspend the Script... [Press Ctrl + Z] \n"
printf "####################################################################### \n"
#########################################
# Change Log 14 Dec 2019
# 1. Added a means to validate login
# 2. rewrote the script into functions
# 3. Add more UI features
#
#########################################
# Set delimiter
IFS=","
OLDIFS=$IFS
#### ---- Configuration ---####
credentials="credential.csv"
fname="CLOUDIMPORT"
folder=$(mkdir -pv $fname)
path=$(pwd)
CONN_END=4 #terminated connection
# we get our details and insert into a file
read -p "Enter your Atlassian Account Email Address: " email
read -p "Enter API token : " apitoken
printf $email','$apitoken | awk -F "," '{print $0}' > $fname/$credentials
SLEEP=2
content="Content-Type: application/json"
regex="^[a-z0-9!#\$%&'*+/=?^_\`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?\$"
userlog="userlog.txt"
grouplog="grouplog.txt"
groupfile="groupfile.csv"
inst="atlassian.net"
#### ---- End Configuration ---####
# format : nexusfive
read -p "Enter Issue Instance Name: " ins_name
# declare file path to users file
read -p "Enter Users File Name: " userFileName
# declare file path to group file
read -p "Enter Groups File Name: " memFileName
url="https://$ins_name.atlassian.net/rest/api/3/user"
DeleteLog() {
# Delete log file
files=$path/$fname/[ug]*.[a-z][a-z][a-z]
for x in $files
do
rm -f $x
echo "Deleting log \"$x\"..."
done
# we remove our credential file and validators
rm -f $fname/$credentials
echo "removing file... \"$credentials\""
rm -f $fname/${email}.txt
echo "removing file... \"${email}.txt\""
# then we remove the Directory
rm -d $fname
echo "removing Directory \"$fname\""
sleep $SLEEP
}
# We define a function to Create Groups
CreateGroup() {
# Start creating a Group, mention something on screen
echo "Starting Group Import..."
if [[ -f $file2 ]]; then
cat $file2 | while read -r groupname id name emailAddress active created
do
echo "Creating Groups..." '\n'
if [[ -n $groupname ]]; then
curl --silent -u "$email":"$apitoken" -d '{"name": "'$groupname'" }' -X POST -H "$content" "https://$ins_name.$inst/rest/api/3/group" 2>> $fname/$grouplog
# print stdout to screen and stderr to grouplog
# print out the success
printf 'Group Created... Group Name: "'$groupname'"\n'
fi
done < $file2
else
echo "****************************************************"
echo "No $file2 file exist, No Group creation"
echo "****************************************************"
fi
# End Group creation
addUserToGroup
DeleteLog
}
# We define a function to Add created users to Groups
addUserToGroup() {
# ----------------------------------------------
# Start the Conditions to check
# 1. Start checking the accountId and add the groups
# ----------------------------------------------
if [[ -f $file2 ]]; then
# Accounted for space characters, will need to write a function
cat $file2 | sed -e 's/ /+/g'| awk -F "," '{print $1","$2}' > $fname/$groupfile
# if want to manipulate other special characters
# I think POSIX will work though
# e.g. sed -e 's/[:punct:]/+/g' => Future test
cat $path/$fname/$groupfile| while read -r groupname id
do
accountId=$id
echo "Checking Groups..." '\n'
curl -u "$email":"$apitoken" -d '{"accountId": "'$accountId'"}' -X POST -H "$content" "https://$ins_name.$inst/rest/api/3/group/user?groupname=${groupname}"
# display the values of the file
printf 'Matching accountId to Group... Groups: "'$groupname'"\n'
done < $fname/$groupfile
else
echo "****************************************************************"
echo "No $file2 file exist, cannot create groups to user...Try Again."
echo "****************************************************************"
fi
echo "*********************************************"
echo " User & Group Addition Complete "
echo "*********************************************"
}
# We define a function to Create Users
CreateUser() {
# file variables and path to file
file="$path/$userFileName"
file2="$path/$memFileName"
# Show something to the user to read while waiting
echo "Checking user file, Please wait..."
sleep $SLEEP
### ---- Start the Conditions ----###
# ----------------------------------------------
# Start the Conditions to check
# 1. Validate email in CSV file ( we only check this)
# 2. Download a list of each user accountId
# 3. Validate if the user has been created before
# 4. Output if data has been created, create user
# ----------------------------------------------
if [[ -f $file ]]; then
# Start the loop
cat $file | while read -r id displayName emailAddress active date LastLogin
do
if [[ $emailAddress =~ $regex ]]; then
# Start Creating the user in the instance
echo "$url"
curl --silent -u "$email":"$apitoken" -d '{"emailAddress": "'$emailAddress'", "displayName": '$displayName' }' -X POST -H "$content" "$url" >> $fname/$userlog 2>&1
# display the values of the user created
printf 'Creating User... emailAddress: "'$emailAddress'", displayName: '$displayName'\n'
else
echo "**************************************************"
echo "$emailAddress doesn't seem valid...Check the CSV file..."
echo "**************************************************"
fi
done < $file
# Close reading file, print results
echo "*********************************************"
echo " User Creation Complete"
echo "*********************************************"
else
# if file not present display message
echo "****************************************************"
echo "No $file file exist, cannot create user...Try Again."
echo "****************************************************"
fi
# End Bulk User Creation
CreateGroup
}
# Our Basic Authentication to login
BasicAuthJira() {
cat $fname/$credentials | while read -r email apitoken
do
grabUser=$(curl --silent -u "$email":"$apitoken" -X GET -H "Content-Type: application/json" "https://$ins_name.$inst/rest/api/3/myself" 1>> $fname/${email}.txt)
loginOkay=$(grep '"active":*' $fname/${email}.txt | cut -d ',' -f9)
loginErr=$(grep 'Unauthorized (401)' $fname/${email}.txt |cut -d "<" -f2)
if [[ "$email" =~ $regex ]] && [[ "$loginOkay" == "\"active\":true" ]]; then
echo "CREDENTIALS ACCEPTED, Login Successful..."
#start reading the function
CreateUser
elif [[ "$email" =~ $regex ]] && [[ "$loginErr" == "title>Unauthorized (401)
h1>Unauthorized (401)" ]]; then
echo "INVALID CREDENTIALS, Authentication Failed..."
DeleteLog
exit $CONN_END
else
echo "Something else went wrong, check the Script..."
DeleteLog
exit $CONN_END
fi
done < $fname/$credentials
}
# We Initialize our Script below
BasicAuthJira
IFS=$OLDIFS
exit $?