-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrole_domains.py
459 lines (394 loc) · 21.5 KB
/
role_domains.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import time
import socket
import paramiko
from shell_handler import ShellHandler
# from password import generate_password
domain_safe_mode_password = 'hello!321' # generate_password(12)
verbose = False
def deploy_forest(cloud_config, name, control_ipv4_addr, game_ipv4_addr, password, domain):
user = 'Administrator'
domain_name = domain + '.' + cloud_config['enterprise_url']
print("Setting safe-mode password for domain to " + password)
cmd = (
"Install-windowsfeature AD-domain-services ; "
"Import-Module ADDSDeployment ; "
"$secure=ConvertTo-SecureString -asplaintext -string {} -force ; "
"Install-ADDSForest -domainname {} -SafeModeAdministratorPassword $secure -verbose -NoRebootOnCompletion:$true -Force:$true ; "
"wget https://www.python.org/ftp/python/3.12.1/python-3.12.1-embed-amd64.zip -Outfile python.zip; "
"Expand-Archive -force .\python.zip; "
"mv python c:\\ ; "
"icacls \"c:\\python\" /grant:r \"users:(RX)\" /C ; "
"$oldpath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path; "
"$newpath = \"$oldpath;C:\python\" ; "
"Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newpath "
).format(domain_safe_mode_password, domain_name)
if verbose:
print(" Register forest command:" + cmd)
shell = ShellHandler(control_ipv4_addr, user, password)
stdout, stderr, exit_status = shell.execute_powershell(cmd, verbose=verbose)
try:
shell.execute_powershell('Restart-computer -force', verbose=verbose)
except socket.error:
pass
print(" Waiting for reboot of domain controller leader with ip={} (Expect socket closed by peer messages).".format(control_ipv4_addr))
time.sleep(10)
status_received = False
attempts = 0
while not status_received and attempts < 60:
try:
attempts += 1
shell = ShellHandler(control_ipv4_addr, user, password)
stdout2, stderr2, exit_status2 = shell.execute_powershell("get-addomain", verbose=verbose)
if 'Attempting to perform the' in str(stdout2) + str(stderr2):
# server is starting up, try again.
status_received = False
time.sleep(10)
else:
status_received = True
except paramiko.ssh_exception.SSHException:
time.sleep(10)
pass
except paramiko.ssh_exception.NoValidConnectionsError:
time.sleep(10)
pass
except ConnectionResetError:
time.sleep(10)
pass
if 'ReplicaDirectoryServers' not in str(stdout2):
print("Stdout2: " + str(stdout2))
print("Stderr2: " + str(stderr2))
errstr = 'Cannot get domain information from ' + name
raise RuntimeError(errstr)
print(" Reboot Complete. Waiting for domain controller service to start.")
# wait for domain controller to be up/ready.
remove_control_network_from_dns_cmd = (
"set-dnsclient -interfacealias 'control-adapter' -registerthisconnectionsaddress 0 ; "
" $srv=$(get-dnsserversetting -all) ;"
f" $srv.ListeningIPAddress=@( {game_ipv4_addr} ) ;"
" set-dnsserversetting -inputobject $srv; "
" ipconfig /flushdns ; "
" ipconfig /registerdns ; "
" dcdiag /fix "
)
shell = ShellHandler(control_ipv4_addr, user, password)
stdout3, stderr3, exit_status3 = shell.execute_powershell(remove_control_network_from_dns_cmd, verbose=verbose)
return {
"deploy_forest_results": {"name": name, "control_addr": control_ipv4_addr, "game_addr": game_ipv4_addr, "password": password, "domain": domain},
"install_forest": {"stdout": stdout, "stderr": stderr, "exit_status": exit_status},
"verify_forest": {"stdout": stdout2, "stderr": stderr2, "exit_status": exit_status2},
"cleanup_control_from_dns": {"stdout": stdout3, "stderr": stderr3, "exit_status": exit_status3},
"domain_safe_mode_password": domain_safe_mode_password
}
def add_domain_controller(cloud_config, leader_details, name, control_ipv4_addr, game_ipv4_addr, password, domain):
user = 'Administrator'
domain_name = domain + '.' + cloud_config['enterprise_url']
leader_admin_password = leader_details['admin_pass']
game_leader_ip = leader_details['game_addr'][0]
control_leader_ip = leader_details['control_addr'][0]
print(' domain-controller leader (control): ' + control_leader_ip)
print(' domain-controller leader: (game)' + game_leader_ip)
print(' domain-controller password: ' + leader_admin_password)
pycmd = (
"wget https://www.python.org/ftp/python/3.12.1/python-3.12.1-embed-amd64.zip -Outfile python.zip; "
"Expand-Archive -force .\python.zip; "
"mv python c:\\ ; "
"icacls \"c:\\python\" /grant:r \"users:(RX)\" /C ; "
)
adcmd = (
"Install-windowsfeature AD-domain-services ; "
"Import-Module ADDSDeployment ; "
"Set-DnsClientServerAddress -serveraddress ('{}') -interfacealias 'game-adapter' ; "
"Set-DnsClientServerAddress -serveraddress ('{}') -interfacealias 'control-adapter' ; "
"$passwd = convertto-securestring -AsPlainText -Force -String '{}' ; "
"$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist '{}\\administrator',$passwd ; "
"$secure=ConvertTo-SecureString -asplaintext -string '{}' -force ; "
"sleep 60; "
"Install-ADDSDomainController -DomainName {} -SafeModeAdministratorPassword $secure -verbose -NoRebootOnCompletion:$true -confirm:$false -credential $cred; "
"$oldpath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path; "
"$newpath = \"$oldpath;C:\python\" ; "
"Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newpath "
).format(game_leader_ip, game_leader_ip, leader_admin_password, domain_name, domain_safe_mode_password, domain_name)
if verbose:
print(" Register as domain comtroller command:" + adcmd)
try:
shell = ShellHandler(control_ipv4_addr, user, password)
stdout2, stderr2, exit_status2 = shell.execute_powershell(pycmd, verbose=verbose)
except paramiko.ssh_exception.AuthenticationException:
return {}
stdout = [stdout2]
stderr = [stderr2]
exit_status = [exit_status2]
attempts = 0
while attempts < 10:
shell = ShellHandler(control_ipv4_addr, user, password)
attempts += 1
# if name == 'dc3':
# print('adcmd='+ str(adcmd))
# sys.exit(1)
stdout2, stderr2, exit_status2 = shell.execute_powershell(adcmd, verbose=verbose)
stdout.append(stdout2)
stderr.append(stderr2)
exit_status.append(exit_status2)
# stop if successful
if 'A domain controller could not be contacted' not in str(stderr2) and 'A domain controller could not be contacted' not in str(stdout2):
break
print("Domain controler registration failed, rebooting and retrying (Expect socket error messages)")
# print(str(stdout2 + stderr2))
shell.execute_powershell('Restart-computer -force', verbose=verbose)
time.sleep(60)
if attempts > 9:
raise RuntimeError("Could not join domain on machine " + name)
try:
shell = ShellHandler(control_ipv4_addr, user, password)
shell.execute_powershell('Restart-computer -force', verbose=verbose)
# we expect a forced reboot to end in a socket error because the socket will
# forceably disconnect as the machine reboots.
except socket.error:
pass
print(" Waiting for reboot of windows node with ip={} (Expect socket error messages).".format(control_ipv4_addr))
time.sleep(10)
status_received = False
attempts = 0
while not status_received and attempts < 60:
try:
attempts += 1
shell = ShellHandler(control_ipv4_addr, user, leader_admin_password)
stdout2, stderr2, exit_status2 = shell.execute_powershell("get-addomain", verbose=verbose)
if 'ReplicaDirectoryServers' not in str(stdout2):
print("Connected, waiting for AD to start up.")
time.sleep(10)
continue
status_received = True
stdout.append(stdout2)
stderr.append(stderr2)
exit_status.append(exit_status2)
except paramiko.ssh_exception.SSHException:
time.sleep(10)
pass
except paramiko.ssh_exception.NoValidConnectionsError:
time.sleep(10)
pass
if "stdout2" not in locals() or 'ReplicaDirectoryServers' not in str(stdout2):
if "stdout" in locals():
print("add-dc-stdout:" + str(stdout))
if "stderr" in locals():
print("add-dc-stderr:" + str(stderr))
if "stdout2" in locals():
print("verify-stdout:" + str(stdout2))
if "stderr2" in locals():
print("verify-stderr:" + str(stderr2))
errstr = 'Cannot get domain information from ' + name
raise RuntimeError(errstr)
print(" Reboot Complete")
return {
"add_domain_results": {"name": name, "control_addr": control_ipv4_addr, "game_addr": game_ipv4_addr, "password": password, "domain": domain},
"install_domain_controller": {"stdout": stdout, "stderr": stderr, "exit_status": exit_status},
"verify_domain_controller": {"stdout": stdout2, "stderr": stderr2, "exit_status": exit_status2}
}
def join_domain(obj):
cloud_config = obj['cloud_config']
node = obj['node']
name = node['name']
domain_name = obj['domain']
enterprise_name = cloud_config['enterprise_url']
fqdn_domain_name = domain_name + '.' + enterprise_name
leader = obj['domain_leader']
leader_admin_password = leader['admin_pass']
game_leader_addrs = leader['game_addr']
control_ipv4_addr = obj['control_addr']
game_ipv4_addr = obj['game_addr']
password = obj['password']
roles = node['roles']
iswindows = len(list(filter(lambda role: 'windows' == role, roles))) == 1
islinux = len(list(filter(lambda role: 'linux' == role, roles))) == 1
# convert array into string for powershell.
domain_ips = str(game_leader_addrs).replace("[", "").replace(']', '').replace("'", '"')
if verbose:
print(" Domain controller leader:" + leader['name'])
print(" Domain controller IPs (game):" + str(game_leader_addrs))
if iswindows:
print("Windows join-domain for node " + name)
return join_domain_windows(name, leader_admin_password, control_ipv4_addr, game_ipv4_addr, domain_ips, fqdn_domain_name, domain_name, password)
elif islinux:
print("Linux join-domain for node " + name)
return join_domain_linux(name, leader_admin_password, control_ipv4_addr, game_ipv4_addr, domain_ips, fqdn_domain_name, domain_name, password, enterprise_name)
else:
errstr = " No endpoint/domain enrollment for node " + name
raise RuntimeError(errstr)
def join_domain_windows(name, leader_admin_password, control_ipv4_addr, game_ipv4_addr, domain_ips, fqdn_domain_name, domain_name, password):
print("Windows join-domain for node " + name)
user = 'Administrator'
cmd = (
"$passwd = convertto-securestring -AsPlainText -Force -String {} ; "
"$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist 'administrator@{}',$passwd ; "
"Set-DnsClientServerAddress -serveraddress ({}) -interfacealias 'game-adapter' ; "
"Add-Computer -Credential $cred -domainname {};"
"wget https://www.python.org/ftp/python/3.12.1/python-3.12.1-embed-amd64.zip -Outfile python.zip; "
"Expand-Archive -force .\python.zip; "
"mv python c:\\ ; "
"icacls \"c:\\python\" /grant:r \"users:(RX)\" /C ; "
"$oldpath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path; "
"$newpath = \"$oldpath;C:\python\" ; "
"Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newpath "
).format(leader_admin_password, domain_name, domain_ips, fqdn_domain_name)
print(" Joining an existing domain: " + domain_name)
shell = ShellHandler(control_ipv4_addr, user, password)
stdout, stderr, exit_status = shell.execute_powershell(cmd, verbose=verbose)
try:
shell = ShellHandler(control_ipv4_addr, user, password)
shell.execute_powershell('Restart-computer -force', verbose=verbose)
except socket.error:
pass
print(" Waiting for reboot of windows domain member with ip={} (Expect socket closed by peer messages).".format(control_ipv4_addr))
time.sleep(10)
status_received = False
attempts = 0
stdout2 = ""
stderr2 = ""
while not status_received and attempts < 60:
try:
attempts += 1
shell = ShellHandler(control_ipv4_addr, domain_name + '\\' + user, leader_admin_password)
stdout2, stderr2, exit_status2 = shell.execute_powershell(
'echo "the domain is $env:userdomain" ', verbose=verbose)
status_received = True
print(f" Reboot Completed for {name} by verifying computer is in the domain")
except paramiko.ssh_exception.SSHException:
time.sleep(5)
pass
except paramiko.ssh_exception.NoValidConnectionsError:
time.sleep(5)
pass
if stdout2 == "":
raise RuntimeError("Could not verify machine {} was on domain: unable to connect".format(name))
if not 'the domain is {}'.format(domain_name.upper()) in str(stdout2):
print("join_domain_stdout:" + str(stdout))
print("join_domain_stderr:" + str(stderr))
print("verify_domain_stdout:" + str(stdout2))
print("verify_domain_stderr:" + str(stderr2))
errstr = 'Cannot get domain information from ' + name
raise RuntimeError(errstr)
return {
"join_domain": {"join-cmd": cmd, "stdout": stdout, "stderr": stderr, "exit_status": exit_status},
"verify_join_domain": {"stdout": stdout2, "stderr": stderr2, "exit_status": exit_status2}
}
def join_domain_linux(name, leader_admin_password, control_ipv4_addr, game_ipv4_addr, domain_ips, fqdn_domain_name, domain_name, password, enterprise_name):
netplan_config_path = '/etc/netplan/50-cloud-init.yaml'
chrony_config_path = '/etc/chrony/chrony.conf'
domain_ips_formated = str(domain_ips).replace('[', '').replace(']', '').replace('"', '')
krdb_config_path = '/etc/krb5.conf'
set_allow_password = (
"set -x ; ip a ; ping -c 3 google.com; ping -c 3 nova.clouds.archive.ubuntu.com ; "
"sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config; "
"sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config; "
"sudo sed -i 's/#PasswordAuthentication /PasswordAuthentication /' /etc/ssh/sshd_config; "
"sudo sed -i 's/KbdInteractiveAuthentication no/KbdInteractiveAuthentication yes/' /etc/ssh/sshd_config; "
"sudo rm /etc/ssh/sshd_config.d/60-cloudimg-settings.conf "
)
set_dns_command = (
"sudo sed -i '/dhcp4: true/a \ nameservers:\\n addresses: \[ {} \]' {} ; "
"cat {}; sudo netplan apply ; echo Hostname=$(hostname); sudo resolvectl status "
).format(domain_ips_formated, netplan_config_path, netplan_config_path)
install_packages_cmd = "sudo apt update && sudo env DEBIAN_FRONTEND=noninteractive apt install -y dnsutils iputils-ping traceroute telnet tcpdump python-is-python3 chrony krb5-user realmd sssd sssd-tools adcli samba-common-bin"
set_chrony_command = (
"sudo sed -i '/pool ntp.ubuntu.com iburst maxsources 4/i pool {} iburst maxsources 5' {} ; ".format(fqdn_domain_name, chrony_config_path) +
"sudo systemctl enable chrony ; " +
"sudo systemctl restart chrony; " +
"while ! sudo chronyc tracking|grep 'Leap status : Normal'; do echo waiting for chrony to sync time; sleep 1; done "
)
krb5_cmd = (
f"sudo sed -i 's/default_realm = .*/default_realm = {enterprise_name.upper()}/' {krdb_config_path} ; " +
f"sudo sed -i '/\\[libdefaults\\]/a \ rdns=false ' {krdb_config_path} ; " +
f"count=1 ; while (( count < 30 )) ; do echo {leader_admin_password} | sudo kinit administrator@{fqdn_domain_name.upper()} 2>&1 " +
"|grep 'Cannot find KDC' ; res=${PIPESTATUS[2]} ; if (( res != 0 )) ; then break; fi ; echo waiting for kinit to succeed; " +
"sudo netplan apply; sleep 5; count=$(( count + 1 )) ; done ; " +
"sudo klist "
)
realm_cmd = (
"sudo realm discover {};"
"echo {}| sudo realm join -U administrator {} -v;"
).format(fqdn_domain_name, leader_admin_password, fqdn_domain_name.upper())
cmds = '(' + set_allow_password + ';' + set_dns_command + ';' + install_packages_cmd + ';' + \
set_chrony_command + ';' + krb5_cmd + ';' + realm_cmd + ') 2>&1 | tee /tmp/join_domain.log '
shell = ShellHandler(control_ipv4_addr, 'ubuntu', None)
stdout, stderr, exit_status = shell.execute_cmd(cmds, verbose=verbose)
shell.execute_cmd("sudo reboot now", verbose=verbose)
print(
f" Waiting for reboot of {name} linux domain member with ip={control_ipv4_addr}(Expect socket closed by peer messages).")
time.sleep(5)
status_received = False
attempts = 0
stdout2 = None
stderr2 = None
exit_status2 = None
while not status_received and attempts < 300:
attempts += 1
try:
admin_user = 'administrator@' + fqdn_domain_name
print(" Trying to verify reboot of {}... creds={}:{}:{}".format(
name, control_ipv4_addr, admin_user, leader_admin_password))
shell = ShellHandler(control_ipv4_addr, admin_user, leader_admin_password)
stdout2, stderr2, exit_status2 = shell.execute_cmd('sudo netplan apply; realm list', verbose=verbose)
status_received = True
except paramiko.ssh_exception.SSHException:
print(" Waiting for reboot of linux domain member, {}, with ip={}(Expect socket closed by peer messages).".format(
name, control_ipv4_addr))
time.sleep(5)
pass
except paramiko.ssh_exception.NoValidConnectionsError:
print(" Waiting for reboot of linux domain member, {} with ip={}(Expect socket closed by peer messages).".format(
name, control_ipv4_addr))
time.sleep(5)
pass
try:
stdout2
except Exception as _: # noqa: F841
errstr = 'Connect after reboot.'
raise RuntimeError(errstr)
if stdout2 is None or not 'realm-name: {}'.format(fqdn_domain_name.upper()) in str(stdout2):
print("join_domain_stdout:" + str(stdout))
print("join_domain_stderr:" + str(stderr))
print("verify_domain_stdout:" + str(stdout2))
print("verify_domain_stderr:" + str(stderr2))
errstr = 'Cannot detect domain information from ' + name
if stdout2 is None:
errstr += ". Could not connect"
else:
errstr += ". Missing domain information."
raise RuntimeError(errstr)
print(f" Reboot Completed for {name} by verifying computer is in the domain")
return {
"join_domain": {"join-cmd": cmds, "stdout": stdout, "stderr": stderr, "exit_status": exit_status},
"verify_join_domain": {"stdout": stdout2, "stderr": stderr2, "exit_status": exit_status2}
}
def deploy_users(users, built):
deploy_users = {}
domain_leaders = built['setup']['setup_domains']['domain_leaders']
domain_commands = {}
for user in users:
username = user['user_profile']['username']
domain = user['domain']
print("Preparing to install user " + username + " in domain " + domain)
install_one_user = (
'$secure=ConvertTo-SecureString -asplaintext -string "{}" -force; '
'New-ADUser -samaccountname "{}" -name "{}" -accountpassword $secure -enabled $true'
).format(user['user_profile']['password'], user['user_profile']['username'], user['user_profile']['name'])
if domain in domain_commands:
domain_commands[domain] += '; ' + install_one_user
else:
domain_commands[domain] = install_one_user
deploy_users['cmds'] = domain_commands
deploy_users['add_users'] = {}
for domain in domain_commands:
cmd = domain_commands[domain]
controller_name = domain_leaders[domain]['name']
print(domain_leaders[domain])
controller_addr = domain_leaders[domain]['control_addr'][0] # uses control address
domain_password = domain_leaders[domain]['admin_pass']
print("Installing users for domain " + domain + " on server " + controller_addr)
print(" controller name,addr:" + controller_name + "(" + controller_addr + ")")
qualified_username = 'administrator@' + domain
shell = ShellHandler(controller_addr, qualified_username, domain_password)
stdout, stderr, exit_status = shell.execute_powershell(cmd, verbose=verbose)
deploy_users['add_users'][domain] = {"cmd": cmd, "stdout": stdout, "stderr": stderr, "exit_status": exit_status}
return deploy_users