-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconf-cli.py
executable file
·2084 lines (1796 loc) · 80.3 KB
/
conf-cli.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
#!/usr/bin/env python
# Copyright 2017 Check Point Software Technologies LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import collections
import copy
import json
import os
import re
import subprocess
import shutil
import sys
AZURE_ENVIRONMENTS = [
'AzureCloud', 'AzureChinaCloud', 'AzureGermanCloud', 'AzureUSGovernment'
]
AVAILABLE_VERSIONS = ['R77.30', 'R80.10', 'R80.20']
DEPLOYMENT_TYPES = ['TGW']
AWS_REGIONS = collections.OrderedDict([
('US East (N. Virginia)', 'us-east-1'),
('US East (Ohio)', 'us-east-2'),
('US West (N. California)', 'us-west-1'),
('US West (Oregon)', 'us-west-2'),
('Asia Pacific (Mumbai)', 'ap-south-1'),
('Asia Pacific (Seoul)', 'ap-northeast-2'),
('Asia Pacific (Singapore)', 'ap-southeast-1'),
('Asia Pacific (Sydney)', 'ap-southeast-2'),
('Asia Pacific (Tokyo)', 'ap-northeast-1'),
('Canada (Central)', 'ca-central-1'),
('EU (Frankfurt)', 'eu-central-1'),
('EU (Ireland)', 'eu-west-1'),
('EU (London)', 'eu-west-2'),
('EU (Paris)', 'eu-west-3'),
('EU (Stockholm)', 'eu-north-1'),
('South America (Sao Paulo)', 'sa-east-1'),
('China (Beijing)', 'cn-north-1'),
('AWS GovCloud (US)', 'us-gov-west-1')])
MIN_SIC_LENGTH = 8
"""
Usage examples to be displayed in the help output and in the error message.
"""
USAGE_EXAMPLES = {
'init_aws': [
'init AWS -mn <MANAGEMENT-NAME> -tn <TEMPLATE-NAME> -otp <SIC-KEY> '
'-ver {' + ','.join(AVAILABLE_VERSIONS) + '} -po <POLICY-NAME> -cn '
'<CONTROLLER-NAME> -r eu-west-1,us-east-1,eu-central-1 -fi '
'<FILE-PATH>',
'init AWS -mn <MANAGEMENT-NAME> -tn <TEMPLATE-NAME> -otp <SIC-KEY> '
'-ver {' + ','.join(AVAILABLE_VERSIONS) + '} -po <POLICY-NAME> -cn '
'<CONTROLLER-NAME> -r eu-west-1,us-east-1,eu-central-1 -ak '
'<ACCESS-KEY> -sk <SECRET-KEY> -sr <STS-ROLE>',
'init AWS -mn <MANAGEMENT-NAME> -tn <TEMPLATE-NAME> -otp <SIC-KEY> '
'-ver {' + ','.join(AVAILABLE_VERSIONS) + '} -po <POLICY-NAME> -cn '
'<CONTROLLER-NAME> -r eu-west-1,us-east-1,eu-central-1 -iam'
],
'init_azure': [
'init Azure -mn <MANAGEMENT-NAME> -tn <TEMPLATE-NAME> -otp '
'<SIC-KEY> -ver {' + ','.join(AVAILABLE_VERSIONS) + '} -po '
'<POLICY-NAME> -cn <CONTROLLER-NAME> -sb <SUBSCRIPTION> -at '
'<TENANT> -aci <CLIENT-ID> -acs <CLIENT-SECRET>',
'init Azure -mn <MANAGEMENT-NAME> -tn <TEMPLATE-NAME> -otp '
'<SIC-KEY> -ver {' + ','.join(AVAILABLE_VERSIONS) + '} -po '
'<POLICY-NAME> -cn <CONTROLLER-NAME> -sb <SUBSCRIPTION> -au '
'<USERNAME> -ap <PASSWORD>'
],
'init_GCP': [],
'show': ['show all',
'show management',
'show templates',
'show controllers'],
'add_template': [
'add template -tn <TEMPLATE-NAME> -otp <SIC-KEY> -ver {' +
','.join(AVAILABLE_VERSIONS) + '} -po <POLICY-NAME>',
'add template -tn <TEMPLATE-NAME> -otp <SIC-KEY> -ver {' +
','.join(AVAILABLE_VERSIONS) + '} -po <POLICY-NAME> [-hi] [-ia] '
'[-appi]'
],
'add_controller_AWS': [
'add controller AWS -cn <NAME> -r eu-west-1,us-east-1,eu-central-1 '
'-fi <FILE-PATH>',
'add controller AWS -cn <NAME> -r eu-west-1,eu-central-1 -ak '
'<ACCESS-KEY> -sk <SECRET-KEY>',
'add controller AWS -cn <NAME> -r eu-west-1 -iam -sn '
'<SUB-ACCOUNT-NAME> -sak <SUB-ACCOUNT-ACCESS-KEY> -ssk '
'<SUB-ACCOUNT-SECRET-KEY>'
],
'add_controller_Azure': [
'add controller Azure -cn <NAME> -sb <SUBSCRIPTION> [-en {'
'AzureCloud,AzureChinaCloud,AzureGermanCloud,AzureUSGovernment}] '
'-at <TENANT> -aci <CLIENT-ID> -acs <CLIENT-SECRET>',
'add controller Azure -cn <NAME> -sb <SUBSCRIPTION> -au '
'<USERNAME> -ap <PASSWORD>'
],
'add_controller_GCP': [
'add controller GCP -cn <NAME> -proj <PROJECT> -cr <FILE-PATH>'
],
'set_delay': ['set delay 60'],
'set_management': [
'set management [-mn <NEW-NAME>] [-mh <NEW-HOST> [-d <DOMAIN>] [-fp '
'<FINGERPRINT>] [-u <USER>] [-pass <PASSWORD>] [-pr <PROXY>] [-cs '
'<CUSTOM-SCRIPT-PATH>]'
],
'set_template': [
'set template -tn <NAME> [-otp <SIC-KEY>] [-ver {' +
','.join(AVAILABLE_VERSIONS) + '}]',
'[-po <POLICY>]', 'set template -tn <NAME> [-hi] [-ia] [-appi]'
],
'set_controller_AWS': [
'set controller AWS -cn <NAME> '
'[-r <COMMA-SEPARATED-LIST-OF-AWS-REGIONS>]',
'set controller AWS -cn <NAME> [-fi <FILE-PATH> | -iam]'
],
'set_controller_Azure': [
'set controller Azure -cn <NAME> [-au <USERNAME>] [-ap <PASSWORD>]',
'set controller Azure -cn <NAME> [-cd <DOMAIN>]'
],
'set_controller_GCP': [
'set controller GCP -cn <NAME> [-cr <FILE-PATH> | "IAM"]'
],
'delete_management': ['delete management',
'delete management -pr'],
'delete_template': [
'delete template -tn <NAME>',
'delete template -tn <NAME> [-pr] [-cp]'
],
'delete_controller_AWS': [
'delete controller AWS -cn <NAME> ',
'delete controller AWS -cn <NAME> [-cd] [-ct]'
],
'delete_controller_Azure': [
'delete controller Azure -cn <NAME> ',
'delete controller Azure -cn <NAME> [-d] [-ap]'
],
'delete_controller_GCP': [
'delete controller GCP -cn <NAME> ',
'delete controller GCP -cn <NAME> [-ct] [-cr]'
]
}
filename = os.path.basename(__file__)
for k, v in USAGE_EXAMPLES.iteritems():
USAGE_EXAMPLES[k] = [filename + ' ' + example for example in v]
CONFPATH = os.environ.get(
'AUTOPROVISION_CONFIG_FILE',
os.environ.get('MDSDIR',
os.environ['FWDIR']) + '/conf/autoprovision.json')
VERSIONPATH = os.environ.get(
'AUTOPROVISION_VERSION_FILE',
os.path.join(os.path.dirname(os.path.realpath(__file__)), 'version'))
with open(VERSIONPATH) as f:
version = f.read()
PROTECTED = '__protected__autoprovision'
PROTECTED_FIELDS = ['password', 'b64password', 'client_secret', 'secret-key',
'one-time-password']
SAVED_WORDS = ['controllers', 'credentials', 'sub-creds', 'management']
def my_check_value(self, action, value):
"""Custom value check for the argument parser.
Choices are str instead of repr.
Modified error message for empty choices list.
"""
if action.choices is not None and value not in action.choices:
tup = value, ', '.join(map(str, action.choices))
if not action.choices:
msg = (
'invalid choice: no values to set or delete, please add first')
else:
msg = ('invalid choice: %r (choose from %s)') % tup
raise argparse.ArgumentError(action, msg)
def my_error(self, message):
"""Custom error handling for the argument parser.
Adds the epilog (in this case, usage examples), if such exists,
to the end of the error output.
"""
self.print_usage(sys.stderr)
if self.epilog:
args = {'prog': self.prog, 'message': message, 'epilog': self.epilog}
self.exit(2, ('%(prog)s: error: %(message)s\n\n%(epilog)s\n') % args)
else:
args = {'prog': self.prog, 'message': message}
self.exit(2, ('%(prog)s: error: %(message)s\n') % args)
argparse.ArgumentParser._check_value = my_check_value
argparse.ArgumentParser.error = my_error
REQUIRED_GROUP, OPTIONAL_GROUP = 'required arguments', 'optional group'
SHOW, INIT, ADD, SET, DELETE = 'show', 'init', 'add', 'set', 'delete'
DELAY = 'delay'
MANAGEMENT = 'management'
TEMPLATE = 'template'
CONTROLLER = 'controller'
TEMPLATES = 'templates'
CONTROLLERS = 'controllers'
AWS, AZURE, GCP = 'AWS', 'Azure', 'GCP'
TEMPLATE_NAME = 'template name'
CONTROLLER_NAME = 'controller name'
SUBCREDENTIALS_NAME = 'sub-credentials name'
NEW_KEY = 'new key'
SUBCREDS = 'sub-creds'
SYNC = 'sync'
KEYS_TO_UPDATE_WITH_USER_INPUT = (TEMPLATE_NAME, CONTROLLER_NAME,
SUBCREDENTIALS_NAME, NEW_KEY)
NON_CONFIG_KEYS = (TEMPLATE_NAME, CONTROLLER_NAME, SUBCREDENTIALS_NAME,
'force', 'mode', 'branch')
MANDATORY_KEYS = {
MANAGEMENT: ['name', 'host'],
AWS: ['class', 'regions'],
AZURE: ['class', 'subscription'],
GCP: ['class', 'project', 'credentials']
}
AWS_SUBACCOUNT_ARGS = (SUBCREDENTIALS_NAME,
'AWS sub-credentials access key',
'AWS sub-credentials secret key',
'AWS sub-credentials file path',
'AWS sub-credentials IAM',
'AWS sub-credentials STS role',
'AWS sub-credentials STS external id')
LIST_PARAMETERS = ('regions', 'proxy ports', 'controller templates',
'communities')
def get_templates(conf):
"""Return an array of names of existing templates."""
try:
return conf['templates'].keys()
except KeyError:
return []
def get_controllers(conf, clazz):
"""Return an array of names of existing 'clazz' controllers."""
try:
lst = [c for c in conf[CONTROLLERS]
if conf[CONTROLLERS][c]['class'] == clazz]
return lst
except KeyError:
return []
def create_parser_dict(conf):
"""Create the parsers dictionary.
Structure of dictionary:
{parser_name: [positional argument, mandatory arguments, optional
arguments, help, epilog, defaults]
Override default argument's kwargs (that are specified in
the ARGUMENTS array) by specifying a tuple (argument name, {key: value})
instead of just the name when a parser requires a custom behavior.
"""
parsers = {
SHOW: [SHOW, [], ['branch'],
'show all or specific configuration settings',
'usage examples: \n' + '\n'.join(USAGE_EXAMPLES['show']), None],
INIT: [INIT, [], [],
'initialize Auto-Provision with Management, a template and '
'a controller configuration for either AWS or Azure', None,
None],
ADD: [ADD, [], [], 'add a template or a controller to an existing '
'configuration', None, None],
SET: [SET, [], [],
'set values in an existing configuration of Management '
'or of existing templates or controllers',
None, None],
DELETE: [DELETE, [], [],
'delete configurations of Management, or of existing '
'templates or controllers', None, None
],
'init_aws': [
AWS,
['Management name', TEMPLATE_NAME, 'one time password', 'version',
'policy', CONTROLLER_NAME, 'regions'],
['AWS access key', 'AWS secret key', 'AWS IAM',
'AWS credentials file path', 'STS role', 'STS external id',
'deployment type', 'vpn', 'community name', 'vpn-domain'],
'initialize autoprovision settings for AWS',
'usage examples: \n' + '\n'.join(USAGE_EXAMPLES['init_aws']),
{'delay': 30, 'class': 'AWS', 'host': 'localhost'}],
'init_azure': [
AZURE, ['Management name', TEMPLATE_NAME, 'one time password',
'version', 'policy', CONTROLLER_NAME, 'subscription'],
['Service Principal credentials tenant',
'Service Principal credentials client id',
'Service Principal credentials client secret', 'Azure username',
'Azure password'],
'initialize autoprovision settings for Azure',
'usage examples: \n' + '\n'.join(USAGE_EXAMPLES['init_azure']),
{'delay': 30, 'class': 'Azure', 'host': 'localhost'}
],
'init_gcp': [GCP, [], [],
'support for GCP will be added in the future',
'usage examples: \n' + '\n'.join(USAGE_EXAMPLES[
'init_GCP']), None],
'add_template': [
TEMPLATE, [TEMPLATE_NAME],
['one time password', 'version', 'deployment type', 'policy',
'custom parameters', 'custom gateway script',
'prototype', 'specific network',
'generation', 'proxy ports', 'HTTPS Inspection',
'Identity Awareness', 'Application Control',
'Intrusion Prevention', 'IPS Profile', 'URL Filtering',
'Anti-Bot', 'Anti-Virus', 'restrictive policy',
'vpn', 'community name', 'vpn-domain', 'section name',
'send logs to server', 'send alerts to server', NEW_KEY],
'add a gateway configuration template. When a new gateway '
'instance is detected, the template\'s name is used to '
'determines the eventual gateway configuration',
'usage examples: \n' + '\n'.join(USAGE_EXAMPLES['add_template']),
None
],
'add_controller': [
CONTROLLER, [], [],
'add a controller configuration. These settings will be used to '
'connect to cloud environments such as AWS, Azure or GCP', None,
None
],
'add_controller_aws': [
AWS, [CONTROLLER_NAME, 'regions'],
['controller templates', 'controller domain', 'AWS access key',
'AWS secret key', 'AWS IAM', 'AWS credentials file path',
'STS role', 'STS external id', SUBCREDENTIALS_NAME,
'AWS sub-credentials access key',
'AWS sub-credentials secret key',
'AWS sub-credentials file path', 'AWS sub-credentials IAM',
'AWS sub-credentials STS role',
'AWS sub-credentials STS external id', 'communities',
'sync gateway', 'sync vpn', 'sync load balancers',
'deletion-tolerance'],
'add AWS Controller',
'usage examples: \n' + '\n'.join(
USAGE_EXAMPLES['add_controller_AWS']),
{'class': 'AWS'}
],
'add_controller_azure': [
AZURE, [CONTROLLER_NAME, 'subscription'],
['controller templates', 'controller domain', 'environment',
'Service Principal credentials tenant',
'Service Principal credentials client id',
'Service Principal credentials client secret',
'Azure username', 'Azure password', 'deletion-tolerance'],
'add Azure controller',
'usage examples: \n' + '\n'.join(USAGE_EXAMPLES[
'add_controller_Azure']),
{'class': 'Azure'}
],
'add_controller_gcp': [
GCP, [CONTROLLER_NAME, 'GCP project', 'GCP credentials'],
['controller templates', 'controller domain',
'deletion-tolerance'],
'add GCP Controller',
'usage examples: \n' + '\n'.join(
USAGE_EXAMPLES['add_controller_GCP']),
{'class': 'GCP'}
],
'set_delay': [
DELAY, [], [DELAY], 'set delay',
'usage examples: \n' + '\n'.join(USAGE_EXAMPLES['set_delay']), None
],
'set_management': [
MANAGEMENT, [],
['Management name', 'host', 'domain', 'fingerprint', 'user',
'Management password', 'Management password 64bit', 'proxy',
'custom script'], 'set management arguments',
'usage examples: \n' + '\n'.join(USAGE_EXAMPLES['set_management']),
None
],
'set_template': [
TEMPLATE, [(TEMPLATE_NAME, {'choices': get_templates(conf),
'dest': TEMPLATE_NAME})],
['one time password', 'version', 'deployment type', 'policy',
'custom parameters', 'custom gateway script', 'prototype',
'specific network', 'generation', 'proxy ports',
'HTTPS Inspection', 'Identity Awareness', 'Application Control',
'Intrusion Prevention', 'IPS Profile', 'URL Filtering',
'Anti-Bot', 'Anti-Virus', 'restrictive policy',
'vpn', 'community name', 'vpn-domain', 'section name',
'send logs to server', 'send alerts to server', NEW_KEY],
'set template arguments', 'usage examples: \n' + '\n'.join(
USAGE_EXAMPLES['set_template']), None
],
'set_controller': [
CONTROLLER, [], [],
'set an existing controller configuration. These settings will be '
'used to connect to cloud environments such as AWS, Azure or GCP',
None, None
],
'set_controller_aws': [
AWS, [(CONTROLLER_NAME, {'choices': get_controllers(conf, AWS),
'dest': CONTROLLER_NAME})],
['controller templates', 'controller domain',
'regions', 'AWS access key', 'AWS secret key', 'AWS IAM',
'AWS credentials file path', 'STS role', 'STS external id',
'deletion-tolerance',
SUBCREDENTIALS_NAME, 'AWS sub-credentials access key',
'AWS sub-credentials secret key',
'AWS sub-credentials file path', 'AWS sub-credentials IAM',
'AWS sub-credentials STS role',
'AWS sub-credentials STS external id', 'communities',
'sync gateway', 'sync vpn', 'sync load balancers'],
'set AWS controller values',
'usage examples: \n' + '\n'.join(
USAGE_EXAMPLES['set_controller_AWS']),
None
],
'set_controller_azure': [
AZURE,
[(CONTROLLER_NAME, {'choices': get_controllers(conf, AZURE),
'dest': CONTROLLER_NAME})],
['controller templates', 'controller domain', 'subscription',
'environment', 'Service Principal credentials tenant',
'Service Principal credentials client id',
'Service Principal credentials client secret',
'Azure username', 'Azure password', 'deletion-tolerance'],
'set Azure controller values',
'usage examples: \n' + '\n'.join(USAGE_EXAMPLES[
'set_controller_Azure']),
None
],
'set_controller_gcp': [
GCP, [(CONTROLLER_NAME, {'choices': get_controllers(conf, GCP),
'dest': CONTROLLER_NAME})],
['controller templates', 'controller domain', 'GCP project',
'GCP credentials', 'deletion-tolerance'],
'set GCP controller values',
'usage examples: \n' + '\n'.join(
USAGE_EXAMPLES['set_controller_GCP']),
None
],
'delete_management': [
MANAGEMENT, [],
[('Management name', {'action': 'store_true'}),
('host', {'action': 'store_true'}),
('domain', {'action': 'store_true'}),
('fingerprint', {'action': 'store_true'}),
('user', {'action': 'store_true'}),
('Management password', {'action': 'store_true'}),
('Management password 64bit', {'action': 'store_true'}),
('proxy', {'action': 'store_true'}),
('custom script', {'action': 'store_true'})],
'delete management arguments',
'usage examples: \n' + '\n'.join(
USAGE_EXAMPLES['delete_management']),
None
],
'delete_template': [
TEMPLATE, [(TEMPLATE_NAME, {'choices': get_templates(conf)})],
[('one time password', {'action': 'store_true'}),
('version', {'action': 'store_true'}),
('deployment type', {'action': 'store_true'}),
('policy', {'action': 'store_true'}),
('custom parameters', {'action': 'store_true'}),
('custom gateway script', {'action': 'store_true'}),
('prototype', {'action': 'store_true'}),
('specific network', {'action': 'store_true'}),
('generation', {'action': 'store_true'}),
('proxy ports', {'action': 'store_true'}),
('HTTPS Inspection', {'action': 'store_true'}),
('Identity Awareness', {'action': 'store_true'}),
('Application Control', {'action': 'store_true'}),
('Intrusion Prevention', {'action': 'store_true'}),
('IPS Profile', {'action': 'store_true'}),
('URL Filtering', {'action': 'store_true'}),
('Anti-Bot', {'action': 'store_true'}),
('Anti-Virus', {'action': 'store_true'}),
('restrictive policy', {'action': 'store_true'}),
('section name', {'action': 'store_true'}),
('send logs to server', {'action': 'store_true'}),
('send alerts to server', {'action': 'store_true'}),
(NEW_KEY, {'nargs': 1,
'help': 'optional attributes of a gateway. Usage '
'-nk [KEY]'}),
('vpn', {'action': 'store_true'}),
('community name', {'action': 'store_true'}),
('vpn-domain', {'action': 'store_true'})],
'delete a template or its values',
'usage examples: \n' + '\n'.join(
USAGE_EXAMPLES['delete_template']),
None
],
'delete_controller': [
CONTROLLER, [], [],
'delete a controller or existing controller values. These '
'settings are used to connect to cloud environments such as AWS, '
'Azure or GCP', None, None
],
'delete_controller_aws': [
AWS, [(CONTROLLER_NAME, {'choices': get_controllers(conf, AWS),
'dest': CONTROLLER_NAME})],
[('controller templates', {'action': 'store_true'}),
('controller domain', {'action': 'store_true'}),
('regions', {'action': 'store_true'}),
('AWS access key', {'action': 'store_true'}),
('AWS secret key', {'action': 'store_true'}),
('AWS IAM', {'action': 'store_true'}),
('AWS credentials file path', {'action': 'store_true'}),
('STS role', {'action': 'store_true'}),
('STS external id', {'action': 'store_true'}),
('deletion-tolerance', {'action': 'store_true'}),
SUBCREDENTIALS_NAME,
('AWS sub-credentials access key', {'action': 'store_true'}),
('AWS sub-credentials secret key', {'action': 'store_true'}),
('AWS sub-credentials file path', {'action': 'store_true'}),
('AWS sub-credentials IAM', {'action': 'store_true'}),
('AWS sub-credentials STS role', {'action': 'store_true'}),
('AWS sub-credentials STS external id',
{'action': 'store_true'}),
('communities', {'action': 'store_true'}),
('sync gateway', {'action': 'store_true'}),
('sync vpn', {'action': 'store_true'}),
('sync load balancers', {'action': 'store_true'})],
'delete an AWS controller or its values',
'usage examples: \n' + '\n'.join(USAGE_EXAMPLES[
'delete_controller_AWS']),
None
],
'delete_controller_azure': [
AZURE, [(CONTROLLER_NAME,
{'choices': get_controllers(conf, AZURE)})],
[('controller templates', {'action': 'store_true'}),
('controller domain', {'action': 'store_true'}),
('subscription', {'action': 'store_true'}),
('environment', {'action': 'store_true'}),
('Service Principal credentials tenant',
{'action': 'store_true'}),
('Service Principal credentials client id',
{'action': 'store_true'}),
('Service Principal credentials client secret',
{'action': 'store_true'}),
('deletion-tolerance', {'action': 'store_true'}),
('Azure username', {'action': 'store_true'}),
('Azure password', {'action': 'store_true'})],
'delete an Azure controller or its values',
'usage examples: \n' + '\n'.join(USAGE_EXAMPLES[
'delete_controller_Azure']),
None
],
'delete_controller_gcp': [
GCP, [(CONTROLLER_NAME, {'choices': get_controllers(conf, GCP)})],
[('controller templates', {'action': 'store_true'}),
('controller domain', {'action': 'store_true'}),
('GCP project', {'action': 'store_true'}),
('deletion-tolerance', {'action': 'store_true'}),
('GCP credentials', {'action': 'store_true'})],
'delete a GCP controller or its values',
'usage examples: \n' + '\n'.join(USAGE_EXAMPLES[
'delete_controller_GCP']),
None
]
}
return parsers
def validate_SIC(value):
"""Validates length and char restrictions of the SIC value."""
if len(value) < MIN_SIC_LENGTH:
raise argparse.ArgumentTypeError(
'one time password should consist of at least %s characters'
% repr(MIN_SIC_LENGTH))
if not value.isalnum():
raise argparse.ArgumentTypeError(
'one time password should contain only alphanumeric characters')
return value
def validate_guid_uuid(value):
"""Validate that a value is a GUID OR UUID. """
pattern = re.compile(
'^[0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12}$',
re.IGNORECASE)
if not pattern.match(value):
raise argparse.ArgumentTypeError('value %s is not a GUID.\n' % value)
return value
def validate_ports(value):
"""Validate that a value is a list of digits. """
ports = value.split(',')
for port in ports:
if not port.isdigit():
raise argparse.ArgumentTypeError('port %s is invalid.\n' % port)
return ports
def validate_bool(value):
"""Validate that an inputted string indicates a boolean. """
if value.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif value.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('boolean value expected (yes, no, '
'true, false, t, y, f, n).\n')
def validate_filepath(value):
"""Validate that a string is a valid path to an existing file. """
if os.path.exists(value):
return value
raise argparse.ArgumentTypeError('file %s does not exist.\n' % value)
def validate_iam_or_filepath(value):
"""Validate either 'IAM' or a path to an existing file. """
if value == 'IAM':
return value
return validate_filepath(value)
def validate_hex(value):
"""Validate that a value is hexadecimal. """
try:
int(value, 16)
except ValueError:
raise argparse.ArgumentTypeError('value %s is not hexadecimal.\n' %
value)
return value
def validate_comma_seperated_list(input):
"""Split the input string into an array. """
return input.split(',')
"""
Structure of ARGUMENTS dictionary:
{argument_name(unique): [flag (must be unique within each parser), path in
the configuration file, help, constraints (dict containing 'type:', 'choices:'
or 'action:')
"""
ARGUMENTS = {
'branch': ['branch', [], 'the branch of the configuration to show',
{'choices': ['all', MANAGEMENT, TEMPLATES, CONTROLLERS]}],
DELAY: [DELAY, [DELAY],
'time to wait in seconds after each poll cycle',
{'type': int}],
'Management name': [
'-mn',
[MANAGEMENT, 'name'],
'the name of the management server', None
],
'host': [
'-mh', [MANAGEMENT, 'host'],
'"IP-ADDRESS-OR-HOST-NAME[:PORT]" - of the management server', None
],
'domain': [
'-d', [MANAGEMENT, 'domain'],
'the name or UID of the management domain if applicable', None
],
'fingerprint': [
'-fp', [MANAGEMENT, 'fingerprint'],
'"sha256:FINGERPRINT-IN-HEX" - the SHA256 fingerprint '
'of the management certificate. '
'disable fingerprint checking by providing an empty string "" '
'(insecure but reasonable if running locally '
'on the management server). '
'To retrieve the fingerprint, '
'run the following command on the management server (in bash): '
'cpopenssl s_client -connect 127.0.0.1:443 2>/dev/null '
'</dev/null | cpopenssl x509 -outform DER '
r'| sha256sum | awk "{printf "sha256:%%s\n", $1}"',
{'type': validate_hex}
],
'user': [
'-u', [MANAGEMENT, 'user'], 'a SmartConsole administrator username',
None
],
'Management password': [
'-pass', [MANAGEMENT, 'password'],
'the password associated with the user', None
],
'Management password 64bit': [
'-pass64', [MANAGEMENT, 'b64password'],
'the base64 encoded password associated with the user', None
],
'proxy': [
'-pr', [MANAGEMENT, 'proxy'],
'"http://PROXY-HOST-NAME-OR-ADDRESS:PROXY-PORT" '
'- an optional value for the https_proxy environment variable',
None
],
'custom script': [
'-cs', [MANAGEMENT, 'custom-script'],
'"PATH-TO-CUSTOMIZATION-SCRIPT" - '
'an optional script to run on the management server just after the '
'policy is installed when a gateway is provisioned, and at the '
'beginning of the deprovisioning process. '
'When a gateway is added the script will be run with '
'the keyword "add", '
'with the gateway name and the custom-parameters '
'attribute in the template. '
'When a gateway is deleted the script will run with the keyword '
'"delete" and the gateway name. '
'In the case of a configuration update '
'(for example, a load balancing configuration change '
'or a template/generation change), '
'the custom script will be run with "delete" '
'and later again with "add" and the custom parameters', None
],
TEMPLATE_NAME: [
'-tn', [TEMPLATES],
'the name of the template. The name must be unique', None
],
'one time password': [
'-otp', [TEMPLATES, TEMPLATE_NAME, 'one-time-password'],
'a random string consisting of at least %s alphanumeric characters'
% repr(MIN_SIC_LENGTH), {'type': validate_SIC}
],
'version': [
'-ver', [TEMPLATES, TEMPLATE_NAME, 'version'],
'the gateway version (e.g. R77.30)',
{'choices': AVAILABLE_VERSIONS}
],
'deployment type': [
'-dt', [TEMPLATES, TEMPLATE_NAME, 'deployment-type'],
'the type of the deployment of the CloudGuard Security Gateways',
{'choices': DEPLOYMENT_TYPES}
],
'policy': [
'-po', [TEMPLATES, TEMPLATE_NAME, 'policy'],
'the name of an existing security policy intended to be installed on '
'the gateways', None
],
'custom parameters': [
'-cp', [TEMPLATES, TEMPLATE_NAME, 'custom-parameters'],
'an optional string with space separated parameters to specify when a '
'gateway is added and a custom script is specified in the management '
'section', None
],
'custom gateway script': [
'-cg', [TEMPLATES, TEMPLATE_NAME, 'custom-gateway-script'],
'an optional string with a path on the management server to a script '
'to run on the gateways, prior to policy installation with its '
'parameters, separated by space, '
'e.g. "PATH-TO-SCRIPT param1 param2 ..."', None
],
'prototype': ['-pr', [TEMPLATES, TEMPLATE_NAME, 'proto'],
'a prototype for this template', None
],
'specific network': [
'-sn', [TEMPLATES, TEMPLATE_NAME, 'specific-network'],
'an optional name of a pre-existing network object group '
'that defines the topology settings for the interfaces marked '
'with "specific" topology. This attribute is mandatory '
'if any of the scanned instances has an interface '
'with a topology set to "specific". '
'Typically this should point to the name of a '
'"Group with Exclusions" object, '
'which contains a network group holding the VPC '
'address range and excludes a network group which contains '
'the "external" networks of the VPC, that is,'
'networks that are connected to the internet', None
],
'generation': [
'-g', [TEMPLATES, TEMPLATE_NAME, 'generation'],
'an optional string or number that can be used to force '
're-applying a template to an already existing gateway. '
'If generation is specified and its value is different '
'than the previous value, then the template settings '
'will be reapplied to the gateway', None
],
'proxy ports': [
'-pp', [TEMPLATES, TEMPLATE_NAME, 'proxy-ports'],
'an optional comma-separated list of list of TCP ports '
'on which to enable the proxy on gateway feature. e.g. "8080,8443"',
{'type': validate_ports}
],
'HTTPS Inspection': [
'-hi', [TEMPLATES, TEMPLATE_NAME, 'https-inspection'],
'use this flag to specify whether to enable the HTTPS Inspection '
'blade on the gateway',
{'action': 'store_true'}
],
'Identity Awareness': [
'-ia', [TEMPLATES, TEMPLATE_NAME, 'identity-awareness'],
'use this flag to specify whether to enable the Identity Awareness '
'blade on the gateway',
{'action': 'store_true'}
],
'Application Control': [
'-appi', [TEMPLATES, TEMPLATE_NAME, 'application-control'],
'use this flag to specify whether to enable the Application Control '
'blade on the gateway', {'action': 'store_true'}
],
'Intrusion Prevention': [
'-ips', [TEMPLATES, TEMPLATE_NAME, 'ips'],
'use this flag to specify whether to enable the Intrusion Prevention '
'System blade on the gateway',
{'action': 'store_true'}
],
'IPS Profile': [
'-ipf', [TEMPLATES, TEMPLATE_NAME, 'ips-profile'],
'an optional IPS profile name to associate with a pre-R80 gateway',
None
],
'URL Filtering': [
'-uf', [TEMPLATES, TEMPLATE_NAME, 'url-filtering'],
'use this flag to specify whether to enable the URL Filtering '
'Awareness blade on the gateway', {'action': 'store_true'}
],
'Anti-Bot': [
'-ab', [TEMPLATES, TEMPLATE_NAME, 'anti-bot'],
'use this flag to specify whether to enable the Anti-Bot blade on '
'the gateway', {'action': 'store_true'}
],
'Anti-Virus': [
'-av', [TEMPLATES, TEMPLATE_NAME, 'anti-virus'],
'use this flag to specify whether to enable the Anti-Virus blade on '
'the gateway', {'action': 'store_true'}
],
'vpn': [
'-vpn', [TEMPLATES, TEMPLATE_NAME, 'vpn'],
'use this flag to specify whether to enable the VPN blade on the '
'gateway', {'action': 'store_true'}
],
'restrictive policy': [
'-rp', [TEMPLATES, TEMPLATE_NAME, 'restrictive-policy'],
'an optional name of a pre-existing policy package to be '
'installed as the first policy on a new provisioned gateway. '
'(Created to avoid a limitation in which Access Policy and '
'Threat Prevention Policy cannot be installed at the first '
'time together). In the case where no attribute is provided, '
'a default policy will be used (the default policy has only '
'the implied rules and a drop-all cleanup rule). '
'The value "none" can be used to explicitly avoid any such policy.'
'Note: the name "none" cannot be used as a policy name',
None
],
'community name': [
'-con', [TEMPLATES, TEMPLATE_NAME, 'vpn-community-star-as-center'],
'a star community in which to place the VPN gateway '
'(with "vpn": true) as center (optional)', None
],
'vpn-domain': [
'-vd', [TEMPLATES, TEMPLATE_NAME, 'vpn-domain'],
'the group object to be set as the VPN domain for the VPN gateway '
'(with "vpn": true). An empty string will automatically set an empty '
'group as the encryption domain. No value or null will set the '
'encryption domain to addresses behind the gateways', None
],
'section name': [
'-secn', [TEMPLATES, TEMPLATE_NAME, 'section-name'],
'a name of a rule section in the access and NAT layers in the '
'policy, where to insert the automatically generated rules', None
],
'send logs to server': [
'-sl', [TEMPLATES, TEMPLATE_NAME, 'send-logs-to-server'],
'the name of a log server object in SmartConsole, to send logs to',
None
],
'send alerts to server': [
'-sa', [TEMPLATES, TEMPLATE_NAME, 'send-alerts-to-server'],
'the name of a log server object in SmartConsole, to send alerts to',
None
],
NEW_KEY: [
'-nk', [TEMPLATES, TEMPLATE_NAME, NEW_KEY],
'any other attribute that can be set with the set-simple-gateway '
'Management API. Usage -nk [KEY] [VALUE]',
{'nargs': 2, 'metavar': ('KEY', 'VALUE')}
],
CONTROLLER_NAME: [
'-cn', [CONTROLLERS],
'the name of the cloud environment controller. The name must be '
'unique', None
],
'class': [
'-cc', [CONTROLLERS, CONTROLLER_NAME, 'class'],
'either "AWS", "Azure", "GCP"', None
],
'controller domain': [
'-cd', [CONTROLLERS, CONTROLLER_NAME, 'domain'],
'the name or UID of the management domain if applicable (optional). '
'In MDS, instances that are discovered by this controller, '
'will be defined in this domain. If not specified, '
'the domain specified in the management object '
'(in the configuration), will be used. This attribute should not be '
'specified if the management server is not an MDS', None
],
'controller templates': [
'-ct', [CONTROLLERS, CONTROLLER_NAME, 'templates'],
'an optional list of of templates, which are allowed for instances '
'that are discovered by this controller. If this attribute is '
'missing or its value is an empty list, the meaning is that any '
'template may be used by gateways that belong to this controller. '
'This is useful in MDS environments, where controllers work with '
'different domains and it is necessary to restrict a gateway to only '
'use templates that were intended for its domain. e.g. '
'TEMPLATE1-NAME TEMPLATE2-NAME', {'nargs': '+'}
],
'regions': ['-r', [CONTROLLERS, CONTROLLER_NAME, 'regions'],
'a comma-separated list of AWS regions, in which the '
'gateways are being deployed. For example: eu-west-1,'
'us-east-1,eu-central-1', None
],
'AWS access key': [
'-ak', [CONTROLLERS, CONTROLLER_NAME, 'access-key'],
'AWS access key', None
],
'AWS secret key': ['-sk', [CONTROLLERS, CONTROLLER_NAME, 'secret-key'],
'AWS secret key', None
],
'AWS credentials file path': [
'-fi', [CONTROLLERS, CONTROLLER_NAME, 'cred-file'],
'the path to a text file containing AWS credentials',
{'type': validate_filepath}
],
'AWS IAM': ['-iam', [CONTROLLERS, CONTROLLER_NAME, 'cred-file'],
'use this flag to specify whether to use an IAM role profile',
{'action': 'store_const', 'const': 'IAM'}],
'STS role': ['-sr', [CONTROLLERS, CONTROLLER_NAME, 'sts-role'],
'the STS RoleARN of the role to assume', None],
'STS external id': [
'-se', [CONTROLLERS, CONTROLLER_NAME, 'sts-external-id'],
'an optional STS ExternalId to use when assuming the role', None
],
SUBCREDENTIALS_NAME: [
'-sn', [CONTROLLERS, CONTROLLER_NAME, SUBCREDS],
'the name of the sub credentials object. The name must be '
'unique', None
],
'AWS sub-credentials access key': [
'-sak', [CONTROLLERS, CONTROLLER_NAME, SUBCREDS,
SUBCREDENTIALS_NAME, 'access-key'],
'AWS access key for the sub-account', None
],
'AWS sub-credentials secret key': [
'-ssk', [CONTROLLERS, CONTROLLER_NAME, SUBCREDS,
SUBCREDENTIALS_NAME, 'secret-key'],
'AWS secret key for the sub-account', None
],
'AWS sub-credentials file path': [
'-sfi', [CONTROLLERS, CONTROLLER_NAME, SUBCREDS,
SUBCREDENTIALS_NAME, 'cred-file'],
'the path to a text file containing the AWS credentials for the '
'sub-account', {'type': validate_filepath}
],
'AWS sub-credentials IAM': [
'-siam', [CONTROLLERS, CONTROLLER_NAME, SUBCREDS,
SUBCREDENTIALS_NAME, 'cred-file'],
'use this flag to specify whether to use an IAM role profile for '
'the sub-account', {'action': 'store_const', 'const': 'IAM'}
],