-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_user.py
executable file
·144 lines (126 loc) · 4.02 KB
/
create_user.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
#!/usr/bin/env python3
import sys
import json
import argparse
import requests
def setup(conf):
url_create_user = conf.url + '/api/v1/user'
url_enroll_user = conf.url + '/api/v1/user/%s/start_enrollment'
headers = {
'User-Agent': ('Mozilla/5.0 (X11; Linux x86_64; rv:125.0) '
'Gecko/20100101 Firefox/125.0'),
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'application/json',
'Origin': conf.url,
'Connection': 'keep-alive',
'Referer': conf.url + '/admin/users',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
}
cookies = {
'defguard_session': conf.token,
}
return (
url_create_user,
url_enroll_user,
headers,
cookies
)
def run(conf):
url_create_user, url_enroll_user, headers, cookies = setup(conf)
with open(conf.file, 'r') as fp:
lines = fp.readlines()
for user_entry in lines:
user_entry = user_entry.strip()
if user_entry == "":
continue
firstname, lastname, email = user_entry.split(";")
print(f"f: {firstname}, l: {lastname}, e: {email}")
username = email.split("@")[0]
if conf.create:
data = json.dumps({
"username": username,
"email": email,
"last_name": lastname,
"first_name": firstname,
"phone": "",
"groups": ["Users"],
})
# print("headers", headers, "cookies", cookies, "data", data)
r = requests.post(
url_create_user,
headers=headers,
cookies=cookies,
data=data
)
print(r, r.text)
if r.status_code == 201:
print((
f"User created: {username} - {email} - {firstname}"
f" - {lastname}"
))
elif r.status_code == 500:
print(f"User {username} already exists")
else:
print((
"Unknown status code for "
f"{username}: {r.status_code}"
))
if conf.enroll:
data = json.dumps({
"email": email,
"send_enrollment_notification": True,
})
url = url_enroll_user % username
r = requests.post(
url,
headers=headers,
cookies=cookies,
data=data
)
print(r, r.text)
print((
f"User {username} - {email} - {firstname}"
f" - {lastname}, enrolled"
))
if r.status_code == 500:
print(f"User {username} already exists")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-f", "--file",
required=False,
default="user_list.txt",
help="file containing a list of firstname;lastname;email"
)
parser.add_argument(
"-t", "--token",
required=True,
help="defguard admin session token"
)
parser.add_argument(
"-u", "--url",
help="defguard url, ex: https://defguard.example.com",
required=True
)
parser.add_argument(
"-d", "--dry-run",
default=False,
action="store_true"
)
parser.add_argument(
"-c", "--create",
default=False,
action="store_true"
)
parser.add_argument(
"-e", "--enroll",
default=False,
action="store_true"
)
args = parser.parse_args()
run(conf=args)
sys.exit(0)