forked from picoCTF/picoCTF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.py
executable file
·1211 lines (983 loc) · 39.3 KB
/
deploy.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
from subprocess import CalledProcessError
import tarfile
"""
Handles deployment of an installed problem.
Deploying a problem means creating one or more instances, which are each
templated with flags, the shell server URL, etc., and assigned a port
(if required for their problem type).
Flags and assigned ports will remain consistent for (problem, instance) pairs
across any shell servers that share the SHARED_ROOT directory.
However, instances must still be created individually on each shell server,
as server URLs must be templated appropriately, dependencies potentially
need to be installed on each server, and the underlying files, users and
service definitions that make up a deployed instance are specific to each
shell server.
"""
HIGHEST_PORT = 65535
LOWEST_PORT = 1025
CONTAINER_PORT = 5000
LOCALHOST = "127.0.0.1"
PROBLEM_FILES_DIR = "problem_files"
STATIC_FILE_ROOT = "static"
XINETD_SERVICE_PATH = "/etc/xinetd.d/"
TEMP_DEB_DIR = "/tmp/picoctf_debs/"
FLAG_FMT = "%s"
# will be set to the configuration module during deployment
shared_config = None
local_config = None
port_map = {}
current_problem = None
current_instance = None
containerize = False
logger = logging.getLogger(__name__)
def get_deploy_context():
"""
Returns the deployment context, a dictionary containing the current
config, port_map, problem, instance
"""
global shared_config, local_config, port_map, current_problem, current_instance
return {
"shared_config": shared_config,
"local_config": local_config,
"port_map": port_map,
"problem": current_problem,
"instance": current_instance,
}
port_random = None
# checks if the port is being used by a system process
def check_if_port_in_use(port):
import socket, errno
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((LOCALHOST, port))
except socket.error as e:
return True
s.close()
return False
def flag_fmt():
"""Used to shim the command line passed flag format into the challenge class"""
return FLAG_FMT
def give_port():
"""
Returns a random port and registers it, unless running in a container which
always sets the port to a constant CONTAINER_PORT.
"""
global port_random
if containerize:
logger.debug(
f"Running in a container. Assigning fixed port: {CONTAINER_PORT}"
)
return CONTAINER_PORT
context = get_deploy_context()
# default behavior
if context["shared_config"] is None:
return randint(LOWEST_PORT, HIGHEST_PORT)
if "banned_ports_parsed" not in context["shared_config"]:
banned_ports_result = []
for port_range in context["shared_config"].banned_ports:
banned_ports_result.extend(
list(range(port_range["start"], port_range["end"] + 1))
)
context["shared_config"]["banned_ports_parsed"] = banned_ports_result
# during real deployment, let's register a port
if port_random is None:
port_random = Random(context["shared_config"].deploy_secret)
# if this instance already has a port, reuse it
if (context["problem"], context["instance"]) in context["port_map"]:
assigned_port = context["port_map"][(context["problem"], context["instance"])]
if assigned_port is not None:
logger.debug(
f"This problem instance ({context['problem']}: {str(context['instance'])}) already has an assigned port: {str(assigned_port)}"
)
return assigned_port
used_ports = [port for port in context["port_map"].values() if port is not None]
if (
len(used_ports) + len(context["shared_config"].banned_ports_parsed)
== HIGHEST_PORT + 1
):
raise Exception("All usable ports are taken. Cannot deploy any more instances.")
# Added used ports to banned_ports_parsed.
for port in used_ports:
context["shared_config"].banned_ports_parsed.append(port)
# in case the port chosen is in use, try again.
loop_var = HIGHEST_PORT - len(context["shared_config"].banned_ports_parsed) + 1
while loop_var > 0:
# Get a random port that is random, not in the banned list, not in use, and not assigned before.
port = port_random.choice(
[
i
for i in range(LOWEST_PORT, HIGHEST_PORT)
if i not in context["shared_config"].banned_ports_parsed
]
)
if check_if_port_in_use(port):
loop_var -= 1
context["shared_config"].banned_ports_parsed.append(port)
continue
return port
raise Exception(
"Unable to assigned a port to this problem. All ports are either taken or used by the system."
)
import functools
import json
import os
import shutil
import subprocess
import traceback
from abc import ABCMeta
from ast import literal_eval
from copy import copy, deepcopy
from grp import getgrnam
from hashlib import md5, sha1
from importlib.machinery import SourceFileLoader
# These are below because of a circular import issue with problem.py and give_port
# [TODO] cleanup
from os.path import commonprefix, isdir, isfile, join
from pwd import getpwnam
from random import randint, Random
from time import sleep
from hacksport.operations import create_user, execute
from hacksport.problem import (
Compiled,
Directory,
ExecutableFile,
File,
FlaskApp,
GroupWriteDirectory,
PHPApp,
WebService,
PreTemplatedFile,
ProtectedFile,
Remote,
Service,
)
# must follow hacksport.problem due to dependency on Challenge
from hacksport.docker import DockerChallenge
from hacksport.status import get_all_problem_instances, get_all_problems
from jinja2 import Environment, FileSystemLoader, Template
from shell_manager.package import package_problem
from shell_manager.util import (
DEPLOYED_ROOT,
FatalException,
get_attributes,
get_problem,
get_problem_root,
sanitize_name,
STAGING_ROOT,
get_problem_root_hashed,
get_pid_hash,
get_bundle,
DEB_ROOT,
SHARED_ROOT,
get_shared_config,
get_local_config,
acquire_lock,
release_lock,
)
from spur import RunProcessError
PORT_MAP_PATH = join(SHARED_ROOT, "port_map.json")
def challenge_meta(attributes):
"""
Returns a metaclass that will introduce the given attributes into the class
namespace.
Args:
attributes: The dictionary of attributes
Returns:
The metaclass described above
"""
class ChallengeMeta(ABCMeta):
def __new__(cls, name, bases, attr):
attrs = dict(attr)
attrs.update(attributes)
return super().__new__(cls, name, bases, attrs)
return ChallengeMeta
def update_problem_class(Class, problem_object, seed, user, instance_directory):
"""
Changes the metaclass of the given class to introduce necessary fields before
object instantiation.
Args:
Class: The problem class to be updated
problem_name: The problem name
seed: The seed for the Random object
user: The linux username for this challenge instance
instance_directory: The deployment directory for this instance
Returns:
The updated class described above
"""
random = Random(seed)
attributes = deepcopy(problem_object)
# pass configuration options in as class fields
attributes.update(dict(shared_config))
attributes.update(dict(local_config))
attributes.update(
{
"random": random,
"user": user,
"directory": instance_directory,
"server": local_config.hostname,
}
)
return challenge_meta(attributes)(Class.__name__, Class.__bases__, Class.__dict__)
def get_username(problem_name, instance_number):
"""
Determine the username for a given problem instance.
Given limitation of 32char linux usernames with useradd, truncates generated
username to 28chars. This allows up to 1000 instances of problems with
usernames that do require truncation.
"""
username = "{}_{}".format(sanitize_name(problem_name)[0:28], instance_number)
if len(username) > 32:
raise Exception(
"Unable to create more than 1000 instances of this problem. Shorten problem name.")
return username
def create_service_files(problem, instance_number, path):
"""
Creates xinetd service files for the given problem.
Creates a service file for a problem
Args:
problem: the instantiated problem object
instance_number: the instance number
path: the location to drop the service file
Returns:
A tuple containing (service_file_path, socket_file_path).
socket_file_path will be None if the problem is not a service.
"""
# See https://github.com/puppetlabs/puppetlabs-xinetd/blob/master/templates/service.erb
# and https://linux.die.net/man/5/xinetd.conf
xinetd_template = """
service %s
{
type = UNLISTED
port = %d
disable = no
socket_type = stream
protocol = tcp
wait = %s
user = %s
group = %s
log_type = FILE /var/log/xinetd-hacksport-%s.log
log_on_success = HOST EXIT DURATION
log_on_failure = HOST
cps = 50 3
rlimit_cpu = %s
per_source = 100
server = %s
}
"""
is_service = isinstance(problem, Service)
is_web = isinstance(problem, WebService)
if not is_service and not is_web:
return (None, None)
if getattr(problem, "skip_service_file_creation", False):
return (None, None)
problem_service_info = problem.service()
service_content = xinetd_template % (
problem.user,
problem.port,
"no" if problem_service_info["Type"] == "oneshot" else "yes",
problem.user,
problem.user,
problem.user,
"100" if problem_service_info["Type"] == "oneshot" else "UNLIMITED",
problem_service_info["ExecStart"],
)
service_file_path = join(path, "{}".format(problem.user))
with open(service_file_path, "w") as f:
f.write(service_content)
return (service_file_path, None)
def create_instance_user(problem_name, instance_number):
"""
Generates a random username based on the problem name. The username returned is guaranteed to
not exist.
Args:
problem_name: The name of the problem
instance_number: The unique number for this instance
Returns:
The created username
"""
converted_name = sanitize_name(problem_name)
username = get_username(converted_name, instance_number)
try:
# Check if the user already exists.
user = getpwnam(username)
new = False
except KeyError:
create_user(username)
new = True
return username, new
def generate_instance_deployment_directory(username):
"""
Generates the instance deployment directory for the given username
"""
directory = username
if shared_config.obfuscate_problem_directories:
directory = (
username
+ "_"
+ md5((username + shared_config.deploy_secret).encode()).hexdigest()
)
root_dir = shared_config.problem_directory_root
if not isdir(root_dir):
os.makedirs(root_dir)
# make the root not world readable
os.chmod(root_dir, 0o751)
path = join(root_dir, directory)
if not isdir(path):
os.makedirs(path)
return path
def generate_seed(*args):
"""
Generates a seed using the list of string arguments
"""
return md5("".join(args).encode("utf-8")).hexdigest()
def generate_staging_directory(
root=STAGING_ROOT, problem_name=None, instance_number=None
):
"""
Creates a random, empty staging directory
Args:
root: The parent directory for the new directory. Defaults to join(SHARED_ROOT, "staging")
Optional prefixes to help identify the staging directory: problem_name, instance_number
Returns:
The path of the generated directory
"""
if not os.path.isdir(root):
os.makedirs(root)
# ensure that the staging files are not world-readable
os.chmod(root, 0o750)
def get_new_path():
prefix = ""
if problem_name is not None:
prefix += problem_name + "_"
if instance_number is not None:
prefix += str(instance_number) + "_"
path = join(root, prefix + str(randint(0, 1e16)))
if os.path.isdir(path):
return get_new_path()
return path
path = get_new_path()
os.makedirs(path)
return path
def template_string(template, **kwargs):
"""
Templates the given string with the keyword arguments
Args:
template: The template string
**kwards: Variables to use in templating
"""
temp = Template(template)
return temp.render(**kwargs)
def template_file(in_file_path, out_file_path, **kwargs):
"""
Templates the given file with the keyword arguments.
Args:
in_file_path: The path to the template
out_file_path: The path to output the templated file
**kwargs: Variables to use in templating
"""
env = Environment(
loader=FileSystemLoader(os.path.dirname(in_file_path)),
keep_trailing_newline=True,
)
template = env.get_template(os.path.basename(in_file_path))
output = template.render(**kwargs)
with open(out_file_path, "w") as f:
f.write(output)
def template_staging_directory(staging_directory, problem):
"""
Templates every file in the staging directory recursively other than
problem.json and challenge.py.
Args:
staging_directory: The path of the staging directory
problem: The problem object
"""
# prepend the staging directory to all
dont_template = copy(problem.dont_template) + [
"app/templates",
"problem.json",
"challenge.py",
"templates",
"__pre_templated",
]
dont_template_files = list(filter(isfile, dont_template))
dont_template_directories = list(filter(isdir, dont_template))
dont_template_directories = [
join(staging_directory, directory) for directory in dont_template_directories
]
for root, dirnames, filenames in os.walk(staging_directory):
if any(
os.path.commonprefix([root, path]) == path
for path in dont_template_directories
):
logger.debug(
"....Not templating anything in the directory '{}'".format(root)
)
continue
for filename in filenames:
if filename in dont_template_files:
logger.debug("....Not templating the file '{}'".format(filename))
continue
fullpath = join(root, filename)
try:
template_file(fullpath, fullpath, **get_attributes(problem))
except UnicodeDecodeError as e:
# tried templating binary file
pass
def deploy_files(
staging_directory, instance_directory, file_list, username, problem_class
):
"""
Copies the list of files from the staging directory to the instance directory.
Will properly set permissions and setgid files based on their type.
"""
# get uid and gid for default and problem user
user = getpwnam(username)
default = getpwnam(shared_config.default_user)
for f in file_list:
# copy the file over, making the directories as needed
output_path = join(instance_directory, f.path)
if not os.path.isdir(os.path.dirname(output_path)):
os.makedirs(os.path.dirname(output_path))
if not isinstance(f, Directory):
if isinstance(f, PreTemplatedFile):
file_source = join(staging_directory, "__pre_templated", f.path)
else:
file_source = join(staging_directory, f.path)
shutil.copy2(file_source, output_path)
# set the ownership based on the type of file
if isinstance(f, ProtectedFile) or isinstance(f, ExecutableFile) or \
isinstance(f, GroupWriteDirectory):
os.chown(output_path, default.pw_uid, user.pw_gid)
else:
uid = default.pw_uid if f.user is None else getpwnam(f.user).pw_uid
gid = default.pw_gid if f.group is None else getgrnam(f.group).gr_gid
os.chown(output_path, uid, gid)
# set the permissions appropriately
os.chmod(output_path, f.permissions)
if issubclass(problem_class, Service):
os.chown(instance_directory, default.pw_uid, user.pw_gid)
os.chmod(instance_directory, 0o750)
def install_user_service(service_file, socket_file):
"""
Installs the service file and socket file into the xinetd
service directory, sets the service to start on boot, and
starts the service now.
Args:
service_file: The path to the systemd service file to install
socket_file: The path to the systemd socket file to install
"""
if service_file is None:
return
service_name = os.path.basename(service_file)
logger.debug("...Installing user service '%s'.", service_name)
# copy service file
service_path = os.path.join(XINETD_SERVICE_PATH, service_name)
shutil.copy2(service_file, service_path)
def generate_instance(
problem_object,
problem_directory,
instance_number,
staging_directory,
deployment_directory=None,
):
"""
Runs the setup functions of Problem in the correct order
Args:
problem_object: The contents of the problem.json
problem_directory: The directory to the problem
instance_number: The instance number to be generated
staging_directory: The temporary directory to store files in
deployment_directory: The directory that will be deployed to. Defaults to a deterministic, unique
directory generated for each problem,instance pair using the configuration options
PROBLEM_DIRECTORY_ROOT and OBFUSCATE_PROBLEM_DIRECTORIES
Returns:
A dict containing (problem, staging_directory, deployment_directory, files,
web_accessible_files, service_file, socket_file)
"""
logger.debug(
"Generating instance %d of problem '%s'.",
instance_number,
problem_object["unique_name"],
)
logger.debug("...Using staging directory %s", staging_directory)
username, new = create_instance_user(problem_object["name"], instance_number)
if new:
logger.debug("...Created problem user '%s'.", username)
else:
logger.debug("...Using existing problem user '%s'.", username)
if deployment_directory is None:
deployment_directory = generate_instance_deployment_directory(username)
logger.debug("...Using deployment directory '%s'.", deployment_directory)
seed = generate_seed(
problem_object["name"], shared_config.deploy_secret, str(instance_number)
)
logger.debug("...Generated random seed '%s' for deployment.", seed)
copy_path = join(staging_directory, PROBLEM_FILES_DIR)
shutil.copytree(problem_directory, copy_path)
pretemplated_directory = join(copy_path, "__pre_templated")
if isdir(pretemplated_directory):
shutil.rmtree(pretemplated_directory)
# store cwd to restore later
cwd = os.getcwd()
os.chdir(copy_path)
challenge = SourceFileLoader(
"challenge", join(copy_path, "challenge.py")
).load_module()
Problem = update_problem_class(
challenge.Problem, problem_object, seed, username, deployment_directory
)
# run methods in proper order
problem = Problem()
# reseed and generate flag
problem.flag = problem.generate_flag(Random(seed))
problem.flag_sha1 = sha1(problem.flag.encode("utf-8")).hexdigest()
logger.debug("...Instance %d flag is '%s'.", instance_number, problem.flag)
logger.debug("...Running problem initialize.")
problem.initialize()
shutil.copytree(copy_path, pretemplated_directory)
web_accessible_files = []
def url_for(
web_accessible_files, source_name, display=None, raw=False, pre_templated=False
):
if pre_templated:
source_path = join(copy_path, "__pre_templated", source_name)
else:
source_path = join(copy_path, source_name)
problem_hash = (
problem_object["name"] + shared_config.deploy_secret + str(instance_number)
)
problem_hash = md5(problem_hash.encode("utf-8")).hexdigest()
destination_path = join(STATIC_FILE_ROOT, problem_hash, source_name)
link_template = "<a href='{}'>{}</a>"
web_accessible_files.append(
(source_path, join(shared_config.web_root, destination_path))
)
uri_prefix = "//"
uri = join(uri_prefix, local_config.hostname, destination_path)
if not raw:
return link_template.format(
uri, source_name if display is None else display
)
return uri
problem.url_for = functools.partial(url_for, web_accessible_files)
logger.debug("...Templating the staging directory")
template_staging_directory(copy_path, problem)
if isinstance(problem, Compiled):
problem.compiler_setup()
if isinstance(problem, Remote):
problem.remote_setup()
if isinstance(problem, FlaskApp):
problem.flask_setup()
if isinstance(problem, PHPApp):
problem.php_setup()
if isinstance(problem, Service):
problem.service_setup()
logger.debug("...Running problem setup.")
problem.setup()
os.chdir(cwd)
all_files = copy(problem.files)
if isinstance(problem, Compiled):
all_files.extend(problem.compiled_files)
if isinstance(problem, Service):
all_files.extend(problem.service_files)
if not all([isinstance(f, File) for f in all_files]):
logger.error("All files must be created using the File class!")
raise FatalException
for f in all_files:
if not isinstance(f, Directory) and not os.path.isfile(join(copy_path, f.path)):
logger.error("File '%s' does not exist on the file system!", f)
service_file, socket_file = create_service_files(
problem, instance_number, staging_directory
)
logger.debug("...Created service files '%s','%s'.", service_file, socket_file)
# template the description
# change newline for <br>, otherwise it won't render on the pico website
problem.description = template_string(
problem.description, **get_attributes(problem)
).replace("\n", "<br>")
problem.hints = [template_string(hint, **get_attributes(problem)).replace("\n", "<br>") for hint in problem.hints]
logger.debug("...Instance description: %s", problem.description)
logger.debug("...Instance hints: %s", problem.hints)
# Steps to meet cmgr interface
if containerize:
# Create /challenge directory
try:
os.mkdir("/challenge", 0o700)
except FileExistsError:
logger.warn("/challenge already exists in container")
# Write flag into /challenge/metadata.json
with open("/challenge/metadata.json", "w") as out:
metadata = {"flag": problem.flag}
json.dump(metadata, out)
# Collect web_accessible_files into /challenge/artifacts.tar.gz
if len(web_accessible_files) >= 1:
logger.debug(f"Collecting web accessible files to artifacts.tar.gz")
with tarfile.open("/challenge/artifacts.tar.gz", "w:gz") as tar:
for f, _ in web_accessible_files:
tar.add(f, arcname=os.path.basename(f))
return {
"problem": problem,
"staging_directory": staging_directory,
"deployment_directory": deployment_directory,
"files": all_files,
"web_accessible_files": web_accessible_files,
"service_file": service_file,
"socket_file": socket_file,
}
def deploy_problem(
problem_directory,
instances=None,
test=False,
deployment_directory=None,
debug=False,
restart_xinetd=True,
containerize=False,
):
"""
Deploys the problem specified in problem_directory.
Args:
problem_directory: The directory storing the problem
instances: The list of instances to deploy. Defaults to [0]
test: Whether the instances are test instances. Defaults to False.
deployment_directory: If not None, the challenge will be deployed here
instead of their home directory
debug: Output debug info
restart_xinetd: Whether to restart xinetd upon deployment of this set
of instances for a problem. Defaults True as used by
tests, but typically is used with False from
deploy_problems, which takes in multiple problems.
containerize: Deployment is occuring in a container. This flag is used
by containerize and external tools like cmgr that deploy
challenges in an isolated environment.
"""
if instances is None:
instances = [0]
global current_problem, current_instance, port_map
problem_object = get_problem(problem_directory)
current_problem = problem_object["unique_name"]
instance_list = []
need_restart_xinetd = False
logger.debug("Beginning to deploy problem '%s'.", problem_object["name"])
problem_deb_location = (
os.path.join(DEB_ROOT, sanitize_name(problem_object["unique_name"])) + ".deb"
)
try:
subprocess.run(
"DEBIAN_FRONTEND=noninteractive apt-get -y install "
+ f"--reinstall {problem_deb_location}",
shell=True,
check=True,
stdout=subprocess.PIPE,
)
except subprocess.CalledProcessError:
logger.error("An error occurred while installing problem packages.")
raise FatalException
logger.debug("Reinstalled problem's deb package to fulfill dependencies")
for instance_number in instances:
current_instance = instance_number
staging_directory = generate_staging_directory(
problem_name=problem_object["name"], instance_number=instance_number
)
if test and deployment_directory is None:
deployment_directory = join(staging_directory, "deployed")
instance = generate_instance(
problem_object,
problem_directory,
instance_number,
staging_directory,
deployment_directory=deployment_directory,
)
instance_list.append((instance_number, instance))
deployment_json_dir = join(
DEPLOYED_ROOT,
"{}-{}".format(
sanitize_name(problem_object["name"]), get_pid_hash(problem_object, True)
),
)
if not os.path.isdir(deployment_json_dir):
os.makedirs(deployment_json_dir)
# ensure that the deployed files are not world-readable
os.chmod(DEPLOYED_ROOT, 0o750)
# all instances generated without issue. let's do something with them
for instance_number, instance in instance_list:
problem_path = join(instance["staging_directory"], PROBLEM_FILES_DIR)
problem = instance["problem"]
deployment_directory = instance["deployment_directory"]
logger.debug(
"...Copying problem files %s to deployment directory %s.",
instance["files"],
deployment_directory,
)
deploy_files(
problem_path,
deployment_directory,
instance["files"],
problem.user,
problem.__class__,
)
if test:
logger.info("Test instance %d information:", instance_number)
logger.info("...Description: %s", problem.description)
logger.info("...Deployment Directory: %s", deployment_directory)
logger.debug("Cleaning up test instance side-effects.")
logger.debug("...Killing user processes.")
# This doesn't look great.
try:
execute("killall -u {}".format(problem.user))
sleep(0.1)
except RunProcessError as e:
pass
logger.debug("...Removing test user '%s'.", problem.user)
execute(["userdel", problem.user])
deployment_json_dir = instance["staging_directory"]
else:
# copy files to the web root
logger.debug(
"...Copying web accessible files: %s", instance["web_accessible_files"]
)
for source, destination in instance["web_accessible_files"]:
if not os.path.isdir(os.path.dirname(destination)):
os.makedirs(os.path.dirname(destination))
shutil.copy2(source, destination)
if instance["service_file"] is not None:
install_user_service(instance["service_file"], instance["socket_file"])
# set to true, this will signal restart xinetd
need_restart_xinetd = True
# keep the staging directory if run with debug flag
# this can still be cleaned up by running "shell_manager clean"
if not debug:
shutil.rmtree(instance["staging_directory"])
deployment_info = {
"user": problem.user,
"deployment_directory": deployment_directory,
"service": None
if instance["service_file"] is None
else os.path.basename(instance["service_file"]),
"socket": None
if instance["socket_file"] is None
else os.path.basename(instance["socket_file"]),
"server": problem.server,
"description": problem.description,
"hints": problem.hints,
"flag": problem.flag,
"flag_sha1": problem.flag_sha1,
"instance_number": instance_number,
"should_symlink": not isinstance(problem, Service)
and len(instance["files"]) > 0,
"files": [f.to_dict() for f in instance["files"]],
"docker_challenge": isinstance(problem, DockerChallenge)
}
if isinstance(problem, Service):
deployment_info["port"] = problem.port
logger.debug("...Port %d has been allocated.", problem.port)
# pass along image digest so webui can launch the correct image
if isinstance(problem, DockerChallenge):
deployment_info["instance_digest"] = problem.image_digest
deployment_info["port_info"] = {n: p.dict() for n, p in problem.ports.items()}
port_map[(current_problem, instance_number)] = deployment_info.get("port", None)
instance_info_path = os.path.join(
deployment_json_dir, "{}.json".format(instance_number)
)
with open(instance_info_path, "w") as f:
f.write(json.dumps(deployment_info, indent=4, separators=(", ", ": ")))
logger.debug(
"The instance deployment information can be found at '%s'.",
instance_info_path,
)
# restart xinetd
if restart_xinetd and need_restart_xinetd:
execute(["service", "xinetd", "restart"], timeout=60)
logger.info(
"Problem instances %s were successfully deployed for '%s'.",
instances,
problem_object["unique_name"],
)
return need_restart_xinetd
def deploy_init(contain):
global shared_config, local_config, port_map, containerize
containerize = contain
shared_config = get_shared_config()
local_config = get_local_config()
# Attempt to load the port_map from file
try:
with open(PORT_MAP_PATH, "r") as f:
port_map = json.load(f)
port_map = {literal_eval(k): v for k, v in port_map.items()}
except FileNotFoundError:
# If it does not exist, create it
for path, problem in get_all_problems().items():
for instance in get_all_problem_instances(path):
port_map[
(problem["unique_name"], instance["instance_number"])