forked from Twingate-Labs/Twingate-CLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateusers.py
166 lines (117 loc) · 5.02 KB
/
createusers.py
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
import csv
import subprocess
import os
import random
import string
import re
import secrets
import json
# Handy function for GOVC and assume GOVC is on your $PATH
def govc_runner(command):
my_env = os.environ.copy()
# Admin user will need to perform the commmands
my_env["GOVC_USERNAME"] = os.environ['GOVC_USERNAME']
my_env["GOVC_PASSWORD"] = os.environ['GOVC_PASSWORD']
my_env["GOVC_URL"] = os.environ['GOVC_URL']
my_env["GOVC_INSECURE"] = "true"
process = subprocess.Popen(command, env=my_env, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
return output, error
# New group and user info
filename = "../usernames.csv"
with open(filename, 'r') as f:
reader = csv.reader(f)
for row in reader:
newUserUsername = row[0]
newUserEmail = row[1]
newGroup = row[2]
newFirst = row[3]
newLast = row[4]
print(f"Username: {newUserUsername}, Email:{newUserEmail}, Group:{newGroup}")
userexists = govc_runner("govc sso.user.id " + newUserUsername)
ueresult = re.search(r'no such user', str(userexists))
if ueresult:
characters = string.ascii_letters + string.digits + '!@#$%^&*()'
symbols = ['*', '%', "@", "#", "^", "&"] # Can add more
newUserPassword = ""
for _ in range(3):
newUserPassword += secrets.choice(string.ascii_lowercase)
newUserPassword += secrets.choice(string.ascii_uppercase)
newUserPassword += secrets.choice(string.digits)
newUserPassword += secrets.choice(symbols)
# Creating new group and user
govc_runner("govc sso.group.create " + newGroup)
govc_runner("govc sso.user.create -f '" + newFirst + "' -l '" + newLast + "' -m '" + newUserEmail + "' -p '" + newUserPassword + "' '" + newUserUsername + "'")
govc_runner("govc sso.group.update -a " + newUserUsername + " " + newGroup)
# Check if it has worked
output, error = govc_runner("govc sso.user.id " + newUserUsername)
testoutput = output.decode("utf-8")
print(testoutput)
print(newGroup)
if newGroup.strip() in testoutput:
print("Yay, it worked:\n" + output.decode("utf-8") + "user password " + newUserPassword)
else:
print("Something went wrong :( " + error.decode("utf-8"))
#Twingate-CLI
logintenat = os.environ["TG_TENANT"]
loginapi = os.environ["TG_API"]
loginoutput = subprocess.check_output('python3 ./tgcli.py auth login -t ' + logintenat + ' -a ' + loginapi, shell=True)
session = loginoutput.decode("utf-8").split(":")[1].strip()
print(session)
filename = "../usernames.csv"
with open(filename, 'r') as f:
reader = csv.reader(f)
for row in reader:
newUserUsername = row[0]
newUserEmail = row[1]
newGroup = row[2]
newFirst = row[3]
newLast = row[4]
print(f"First name:{newFirst}, Last name:{newLast}, Email:{newUserEmail}")
command = ["python3", "./tgcli.py", "-s", session, "user", "create", "-e", newUserEmail, "-r", "MEMBER", "-s", "true", "-f", newFirst, "-l", newLast]
subprocess.call(command)
datainput = ["python3", "./tgcli.py", "-s", session, "group", "list"]
data = subprocess.check_output(datainput, encoding='UTF-8')
with open("group.txt", "w") as f:
# Write the variable to the file
f.write(str(data))
# Close the file
f.close()
# Load the JSON file.
with open("group.txt", "r") as f:
data0 = json.load(f)
f.close()
netname = "IBMC-devqe"
for node in data0[0]:
if node['node']['name'] == netname:
# Print the node
print(node['node']['id'])
groupnodeid = node['node']['id']
datainput = ["python3", "./tgcli.py", "-s", session, "user", "list"]
data = subprocess.check_output(datainput, encoding='UTF-8')
with open("email.txt", "w") as f:
# Write the variable to the file
f.write(str(data))
# Close the file
f.close()
# Load the JSON file.
with open("email.txt", "r") as f:
data0 = json.load(f)
f.close()
email = newUserEmail.replace(" ", "")
print(email)
for node in data0[0]:
if node['node']['email'] == email:
# Print the node
print(node['node']['id'])
emailnodeid = node['node']['id']
command2 = ["python3", "./tgcli.py", "-s", session, "group", "addUsers", "-g", groupnodeid, "-u", emailnodeid]
subprocess.call(command2)
command3 = ["python3", "./tgcli.py", "auth", "logout", "-s", session]
subprocess.call(command3)
os.remove("group.txt")
os.remove("email.txt")
#animals = ['BlueFly', 'BlackEel', 'RedBoa', 'BlackBat', 'BlackBoa', 'OrangeFox', 'OrangeApe', 'GreenApe', 'WhiteApe', 'PurpleElk', 'RedCow', 'GreenFox', 'YellowFox', 'PinkBoa', 'YellowElk', 'PinkFox', 'GreenBoa', 'RedBat', 'PurpleApe', 'OrangeBat', 'YellowEel', 'OrangeYak', 'RedDog', 'PinkEel', 'PurpleBat', 'OrangeElk', 'BlueBoa', 'OrangeEel', 'GreenCat', 'WhiteDog', 'OrangeCat', 'BlueCat', 'YellowCat', 'GreenCow', 'BlackYak', 'RedCat', 'WhiteFox']
# Print the list
#for animal in animals:
# subprocess.call(["python3", "./tgcli.py", "auth", "logout", "-s", animal])