-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathvmc_security_rules.py
More file actions
743 lines (609 loc) · 23.5 KB
/
Copy pathvmc_security_rules.py
File metadata and controls
743 lines (609 loc) · 23.5 KB
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
"""
Salt execution module for VMC Security Rules
Provides methods to Create, Read, Update and Delete security rules.
"""
import logging
from saltext.vmware.utils import vmc_constants
from saltext.vmware.utils import vmc_request
from saltext.vmware.utils import vmc_templates
log = logging.getLogger(__name__)
__virtualname__ = "vmc_security_rules"
def __virtual__():
return __virtualname__
def _create_payload_for_security_rule(rule_id, domain_id, user_input):
"""
This function creates the payload based on the template and user input passed
"""
template_data = getattr(
vmc_templates, "create_security_rules_" + domain_id, vmc_templates.create_security_rules_cgw
)
data = vmc_request.create_payload_for_request(template_data, user_input)
data["id"] = data["display_name"] = rule_id
return data
def get(
hostname,
refresh_key,
authorization_host,
org_id,
sddc_id,
domain_id,
verify_ssl=True,
cert=None,
sort_by=None,
sort_ascending=None,
page_size=None,
cursor=None,
):
"""
Retrieves security rules for Given SDDC
CLI Example:
.. code-block:: bash
salt vm_minion vmc_security_rules.get hostname=nsxt-manager.local domain_id=mgw ...
hostname
The host name of NSX-T manager
refresh_key
API Token of the user which is used to get the Access Token required for VMC operations
authorization_host
Hostname of the VMC cloud console
org_id
The Id of organization to which the SDDC belongs to
sddc_id
The Id of SDDC for which the security rules should be retrieved
domain_id
The domain_id for which the security rules should be retrieved. Possible values: mgw, cgw
verify_ssl
(Optional) Option to enable/disable SSL verification. Enabled by default.
If set to False, the certificate validation is skipped.
cert
(Optional) Path to the SSL client certificate file to connect to VMC Cloud Console.
The certificate can be retrieved from browser.
sort_by
(Optional) Field by which records are sorted
sort_ascending
(Optional) Boolean value to sort result in ascending order
page_size
(Optional) Maximum number of results to return in this page
cursor
(Optional) Opaque cursor to be used for getting next page of records (supplied by current result page)
"""
vmc_connection_dict = vmc_request._vmc_connection_details(None, __pillar__)
print(vmc_connection_dict)
hostname = vmc_connection_dict["nsxt_host"]
refresh_key = vmc_connection_dict["api_key"]
authorization_host = vmc_connection_dict["console_host"]
sddc_id = vmc_connection_dict["sddc_id"]
org_id = vmc_connection_dict["org_id"]
# verify_ssl = vmc_connection_dict["verify_ssl"]
verify_ssl = False
cert = vmc_connection_dict["verify_ssl"]
log.info("Retrieving %s Security rules for SDDC %s", domain_id, sddc_id)
api_url_base = vmc_request.set_base_url(hostname)
api_url = (
"{base_url}vmc/reverse-proxy/api/orgs/{org_id}/sddcs/{sddc_id}/"
"policy/api/v1/infra/domains/{domain_id}/gateway-policies/default/rules"
)
api_url = api_url.format(
base_url=api_url_base, org_id=org_id, sddc_id=sddc_id, domain_id=domain_id
)
params = vmc_request._filter_kwargs(
allowed_kwargs=["cursor", "page_size", "sort_ascending", "sort_by"],
cursor=cursor,
page_size=page_size,
sort_by=sort_by,
sort_ascending=sort_ascending,
)
return vmc_request.call_api(
method=vmc_constants.GET_REQUEST_METHOD,
url=api_url,
refresh_key=refresh_key,
authorization_host=authorization_host,
description="vmc_security_rules.get",
verify_ssl=verify_ssl,
cert=cert,
params=params,
)
def get_by_id(
hostname,
refresh_key,
authorization_host,
org_id,
sddc_id,
domain_id,
rule_id,
verify_ssl=True,
cert=None,
):
"""
Retrieves given security rule from the given SDDC
CLI Example:
.. code-block:: bash
salt vm_minion vmc_security_rules.get_by_id hostname=nsxt-manager.local domain_id=mgw ...
hostname
The host name of NSX-T manager
refresh_key
API Token of the user which is used to get the Access Token required for VMC operations
authorization_host
Hostname of the VMC cloud console
org_id
The Id of organization to which the SDDC belongs to
sddc_id
The Id of SDDC for which the security rule should be retrieved
domain_id
The domain_id for which the security rule should be retrieved. Possible values: mgw, cgw
rule_id
Id of the security_rule to be retrieved from SDDC
verify_ssl
(Optional) Option to enable/disable SSL verification. Enabled by default.
If set to False, the certificate validation is skipped.
cert
(Optional) Path to the SSL client certificate file to connect to VMC Cloud Console.
The certificate can be retrieved from browser.
"""
vmc_connection_dict = vmc_request._vmc_connection_details(None, __pillar__)
print(vmc_connection_dict)
hostname = vmc_connection_dict["nsxt_host"]
refresh_key = vmc_connection_dict["api_key"]
authorization_host = vmc_connection_dict["console_host"]
sddc_id = vmc_connection_dict["sddc_id"]
org_id = vmc_connection_dict["org_id"]
# verify_ssl = vmc_connection_dict["verify_ssl"]
verify_ssl = False
log.info("Retrieving Security rule %s for SDDC %s", rule_id, sddc_id)
api_url_base = vmc_request.set_base_url(hostname)
api_url = (
"{base_url}vmc/reverse-proxy/api/orgs/{org_id}/sddcs/{sddc_id}/"
"policy/api/v1/infra/domains/{domain_id}/gateway-policies/default/rules/{rule_id}"
)
api_url = api_url.format(
base_url=api_url_base, org_id=org_id, sddc_id=sddc_id, domain_id=domain_id, rule_id=rule_id
)
return vmc_request.call_api(
method=vmc_constants.GET_REQUEST_METHOD,
url=api_url,
refresh_key=refresh_key,
authorization_host=authorization_host,
description="vmc_security_rules.get_by_id",
verify_ssl=verify_ssl,
cert=cert,
)
def delete(
hostname,
refresh_key,
authorization_host,
org_id,
sddc_id,
domain_id,
rule_id,
verify_ssl=True,
cert=None,
):
"""
Deletes given security rule from the given SDDC
CLI Example:
.. code-block:: bash
salt vm_minion vmc_security_rules.delete hostname=nsxt-manager.local domain_id=mgw ...
hostname
The host name of NSX-T manager
refresh_key
API Token of the user which is used to get the Access Token required for VMC operations
authorization_host
Hostname of the VMC cloud console
org_id
The Id of organization to which the SDDC belongs to
sddc_id
The Id of SDDC from which the security rule should be deleted
domain_id
The domain_id for which the security rule should be deleted. Possible values: mgw, cgw
rule_id
Id of the security_rule to be deleted from SDDC
verify_ssl
(Optional) Option to enable/disable SSL verification. Enabled by default.
If set to False, the certificate validation is skipped.
cert
(Optional) Path to the SSL client certificate file to connect to VMC Cloud Console.
The certificate can be retrieved from browser.
"""
vmc_connection_dict = vmc_request._vmc_connection_details(None, __pillar__)
print(vmc_connection_dict)
hostname = vmc_connection_dict["nsxt_host"]
refresh_key = vmc_connection_dict["api_key"]
authorization_host = vmc_connection_dict["console_host"]
sddc_id = vmc_connection_dict["sddc_id"]
org_id = vmc_connection_dict["org_id"]
# verify_ssl = vmc_connection_dict["verify_ssl"]
verify_ssl = False
log.info("Deleting Security rule %s for SDDC %s", rule_id, sddc_id)
api_url_base = vmc_request.set_base_url(hostname)
api_url = (
"{base_url}vmc/reverse-proxy/api/orgs/{org_id}/sddcs/{sddc_id}/"
"policy/api/v1/infra/domains/{domain_id}/gateway-policies/default/rules/{rule_id}"
)
api_url = api_url.format(
base_url=api_url_base, org_id=org_id, sddc_id=sddc_id, domain_id=domain_id, rule_id=rule_id
)
return vmc_request.call_api(
method=vmc_constants.DELETE_REQUEST_METHOD,
url=api_url,
refresh_key=refresh_key,
authorization_host=authorization_host,
description="vmc_security_rules.delete",
responsebody_applicable=False,
verify_ssl=verify_ssl,
cert=cert,
)
def create(
hostname,
refresh_key,
authorization_host,
org_id,
sddc_id,
domain_id,
rule_id,
verify_ssl=True,
cert=None,
source_groups=None,
destination_groups=None,
services=None,
scope=None,
action=None,
tag=None,
logged=None,
disabled=None,
notes=None,
sequence_number=None,
tags=vmc_constants.VMC_NONE,
):
"""
Creates security rule for the given SDDC
CLI Example:
.. code-block:: bash
salt vm_minion vmc_security_rules.create hostname=nsxt-manager.local domain_id=mgw ...
hostname
The host name of NSX-T manager
refresh_key
API Token of the user which is used to get the Access Token required for VMC operations
authorization_host
Hostname of the VMC cloud console
org_id
The Id of organization to which the SDDC belongs to
sddc_id
The Id of SDDC for which the security rule should be added
domain_id
The domain_id for which the security rule should be added. Possible values: mgw, cgw
rule_id
Id of the security_rule to be added to given SDDC
verify_ssl
(Optional) Option to enable/disable SSL verification. Enabled by default.
If set to False, the certificate validation is skipped.
cert
(Optional) Path to the SSL client certificate file to connect to VMC Cloud Console.
The certificate can be retrieved from browser.
source_groups
(Optional) List of Source group paths.
We need paths as duplicate names may exist for groups under different domains.
Along with paths we support IP Address of type IPv4 and IPv6.
IP Address can be in one of the format(CIDR, IP Address, Range of IP Address).
In order to specify all groups, use the constant "ANY". This is case insensitive.
If "ANY" is used, it should be the ONLY element in the group array.
Error will be thrown if ANY is used in conjunction with other values.
If this value is not passed, then ["ANY"] will be used by default.
destination_groups
(Optional) List of Destination group paths.
We need paths as duplicate names may exist for groups under different domains.
Along with paths we support IP Address of type IPv4 and IPv6.
IP Address can be in one of the format(CIDR, IP Address, Range of IP Address).
In order to specify all groups, use the constant "ANY". This is case insensitive.
If "ANY" is used, it should be the ONLY element in the group array.
Error will be thrown if ANY is used in conjunction with other values.
If this value is not passed, then ["ANY"] will be used by default.
Note: Both source_groups and destination_groups can not be ["ANY"] when domain_id=mgw
services
(Optional) Names of services. In order to specify all services, use the constant "ANY".
This is case insensitive. If "ANY" is used, it should be the ONLY element in the services array.
Error will be thrown if ANY is used in conjunction with other values.
If this value is not passed, then ["ANY"] will be used by default.
scope
(Optional) The list of policy paths where the rule is applied LR/Edge/T0/T1/LRP etc.
Note that a given rule can be applied on multiple LRs/LRPs.
action
(Optional) The action to be applied to all the services.
Possible Values for domain_id=cgw are: ALLOW, DROP, REJECT
Possible Values for domain_id=mgw are: ALLOW
tag
(Optional) Tag applied on the rule. User level field which will be printed in CLI and packet logs.
logged
(Optional) Enable logging flag. Flag to enable packet logging. Default is disabled.
disabled
(Optional) Flag to disable the rule. Default is enabled.
notes
(Optional) Text for additional notes on changes.
sequence_number
(Optional) Sequence number of the Rule.
This field is used to resolve conflicts between multiple Rules under Security or Gateway Policy for a Domain.
If no sequence number is specified by the user, a value of 0 is assigned by default.
If there are multiple rules with the same sequence number then their order is not deterministic.
If a specific order of rules is desired, then one has to specify unique sequence numbers.
tags
(Optional) Opaque identifiers meaningful to the user.
.. code::
tags='[
{
"tag": "<tag-key-1>"
"scope": "<tag-value-1>"
},
{
"tag": "<tag-key-2>"
"scope": "<tag-value-2>"
}
]'
Example values:
.. code::
{
"sequence_number": 0,
"source_groups": [
"ANY"
],
"services": ["/infra/services/HTTPS"],
"logged": false,
"disabled": false,
"destination_groups": [
"/infra/domains/mgw/groups/VCENTER"
],
"scope": [
"/infra/tier-1s/mgw"
],
"action": "ALLOW",
"tag": "",
"notes": "",
"tags": [
{
"tag": "tag1",
"scope": "scope1"
}
]
}
"""
vmc_connection_dict = vmc_request._vmc_connection_details(None, __pillar__)
print(vmc_connection_dict)
hostname = vmc_connection_dict["nsxt_host"]
refresh_key = vmc_connection_dict["api_key"]
authorization_host = vmc_connection_dict["console_host"]
sddc_id = vmc_connection_dict["sddc_id"]
org_id = vmc_connection_dict["org_id"]
# verify_ssl = vmc_connection_dict["verify_ssl"]
verify_ssl = False
log.info("Creating Security rule %s for SDDC %s", rule_id, sddc_id)
api_url_base = vmc_request.set_base_url(hostname)
api_url = (
"{base_url}vmc/reverse-proxy/api/orgs/{org_id}/sddcs/{sddc_id}/"
"policy/api/v1/infra/domains/{domain_id}/gateway-policies/default/rules/{rule_id}"
)
api_url = api_url.format(
base_url=api_url_base, org_id=org_id, sddc_id=sddc_id, domain_id=domain_id, rule_id=rule_id
)
allowed_dict = {
"source_groups": source_groups,
"destination_groups": destination_groups,
"services": services,
"scope": scope,
"action": action,
"tag": tag,
"logged": logged,
"disabled": disabled,
"notes": notes,
"sequence_number": sequence_number,
"tags": tags,
}
req_data = vmc_request._filter_kwargs(
allowed_kwargs=allowed_dict.keys(), allow_none=["tags"], **allowed_dict
)
payload = _create_payload_for_security_rule(rule_id, domain_id, req_data)
return vmc_request.call_api(
method=vmc_constants.PUT_REQUEST_METHOD,
url=api_url,
refresh_key=refresh_key,
authorization_host=authorization_host,
description="vmc_security_rules.create",
data=payload,
verify_ssl=verify_ssl,
cert=cert,
)
def update(
hostname,
refresh_key,
authorization_host,
org_id,
sddc_id,
domain_id,
rule_id,
verify_ssl=True,
cert=None,
source_groups=None,
destination_groups=None,
services=None,
scope=None,
action=None,
tag=None,
logged=None,
disabled=None,
notes=None,
sequence_number=None,
tags=vmc_constants.VMC_NONE,
display_name=None,
):
"""
Updates security rule for the given SDDC
CLI Example:
.. code-block:: bash
salt vm_minion vmc_security_rules.update hostname=nsxt-manager.local domain_id=mgw ...
hostname
The host name of NSX-T manager
refresh_key
API Token of the user which is used to get the Access Token required for VMC operations
authorization_host
Hostname of the VMC cloud console
org_id
The Id of organization to which the SDDC belongs to
sddc_id
The Id of SDDC for which the security rule belongs to
domain_id
The domain_id for which the security rule belongs to. Possible values: mgw, cgw
rule_id
Id of the security_rule to be updated for given SDDC
verify_ssl
(Optional) Option to enable/disable SSL verification. Enabled by default.
If set to False, the certificate validation is skipped.
cert
(Optional) Path to the SSL client certificate file to connect to VMC Cloud Console.
The certificate can be retrieved from browser.
source_groups
(Optional) List of Source group paths.
We need paths as duplicate names may exist for groups under different domains.
Along with paths we support IP Address of type IPv4 and IPv6.
IP Address can be in one of the format(CIDR, IP Address, Range of IP Address).
In order to specify all groups, use the constant "ANY". This is case insensitive.
If "ANY" is used, it should be the ONLY element in the group array.
Error will be thrown if ANY is used in conjunction with other values.
If this value is not passed, then ["ANY"] will be used by default.
destination_groups
(Optional) List of Destination group paths.
We need paths as duplicate names may exist for groups under different domains.
Along with paths we support IP Address of type IPv4 and IPv6.
IP Address can be in one of the format(CIDR, IP Address, Range of IP Address).
In order to specify all groups, use the constant "ANY". This is case insensitive.
If "ANY" is used, it should be the ONLY element in the group array.
Error will be thrown if ANY is used in conjunction with other values.
If this value is not passed, then ["ANY"] will be used by default.
Note: Both source_groups and destination_groups can not be ["ANY"] when domain_id=mgw
services
(Optional) Names of services. In order to specify all services, use the constant "ANY".
This is case insensitive. If "ANY" is used, it should be the ONLY element in the services array.
Error will be thrown if ANY is used in conjunction with other values.
If this value is not passed, then ["ANY"] will be used by default.
scope
(Optional) The list of policy paths where the rule is applied LR/Edge/T0/T1/LRP etc.
Note that a given rule can be applied on multiple LRs/LRPs.
action
(Optional) The action to be applied to all the services.
Possible Values for domain_id=cgw are: ALLOW, DROP, REJECT
Possible Values for domain_id=mgw are: ALLOW
tag
(Optional) Tag applied on the rule. User level field which will be printed in CLI and packet logs.
logged
(Optional) Enable logging flag. Flag to enable packet logging. Default is disabled.
disabled
(Optional) Flag to disable the rule. Default is enabled.
notes
(Optional) Text for additional notes on changes.
sequence_number
(Optional) Sequence number of the Rule.
This field is used to resolve conflicts between multiple Rules under Security or Gateway Policy for a Domain.
If no sequence number is specified by the user, a value of 0 is assigned by default.
If there are multiple rules with the same sequence number then their order is not deterministic.
If a specific order of rules is desired, then one has to specify unique sequence numbers.
tags
(Optional) Opaque identifiers meaningful to the user.
.. code::
tags='[
{
"tag": "<tag-key-1>"
"scope": "<tag-value-1>"
},
{
"tag": "<tag-key-2>"
"scope": "<tag-value-2>"
}
]'
display_name
Identifier to use when displaying entity in logs or GUI
Example values:
.. code::
{
"display_name": "vCenter Inbound Rule"
"sequence_number": 0,
"source_groups": [
"ANY"
],
"services": ["/infra/services/HTTPS"],
"logged": false,
"disabled": false,
"destination_groups": [
"/infra/domains/mgw/groups/VCENTER"
],
"scope": [
"/infra/tier-1s/mgw"
],
"action": "ALLOW",
"tag": "",
"notes": "",
"tags": [
{
"tag": "tag1",
"scope": "scope1"
}
]
}
"""
vmc_connection_dict = vmc_request._vmc_connection_details(None, __pillar__)
print(vmc_connection_dict)
hostname = vmc_connection_dict["nsxt_host"]
refresh_key = vmc_connection_dict["api_key"]
authorization_host = vmc_connection_dict["console_host"]
sddc_id = vmc_connection_dict["sddc_id"]
org_id = vmc_connection_dict["org_id"]
# verify_ssl = vmc_connection_dict["verify_ssl"]
verify_ssl = False
log.info("Updating Security rule %s for SDDC %s", rule_id, sddc_id)
api_url_base = vmc_request.set_base_url(hostname)
api_url = (
"{base_url}vmc/reverse-proxy/api/orgs/{org_id}/sddcs/{sddc_id}/"
"policy/api/v1/infra/domains/{domain_id}/gateway-policies/default/rules/{rule_id}"
)
api_url = api_url.format(
base_url=api_url_base, org_id=org_id, sddc_id=sddc_id, domain_id=domain_id, rule_id=rule_id
)
# fetch the security rule for the given rule_id
existing_data = get_by_id(
hostname,
refresh_key,
authorization_host,
org_id,
sddc_id,
domain_id,
rule_id,
verify_ssl,
cert,
)
if vmc_constants.ERROR in existing_data:
return existing_data
allowed_dict = {
"source_groups": source_groups,
"destination_groups": destination_groups,
"services": services,
"scope": scope,
"action": action,
"tag": tag,
"logged": logged,
"disabled": disabled,
"notes": notes,
"sequence_number": sequence_number,
"display_name": display_name,
"tags": tags,
}
req_data = vmc_request._filter_kwargs(
allowed_kwargs=allowed_dict.keys(), allow_none=["tags"], **allowed_dict
)
payload = vmc_request.create_payload_for_request(
vmc_templates.update_security_rules, req_data, existing_data
)
return vmc_request.call_api(
method=vmc_constants.PATCH_REQUEST_METHOD,
url=api_url,
refresh_key=refresh_key,
authorization_host=authorization_host,
description="vmc_security_rules.update",
responsebody_applicable=False,
data=payload,
verify_ssl=verify_ssl,
cert=cert,
)