{"id":1660,"date":"2022-02-03T10:33:50","date_gmt":"2022-02-03T09:33:50","guid":{"rendered":"https:\/\/nine30.info\/?p=1660"},"modified":"2022-02-03T10:33:50","modified_gmt":"2022-02-03T09:33:50","slug":"salt-sddc-modules-manage-vmc-security-groups-and-rules","status":"publish","type":"post","link":"https:\/\/nine30.nxt70.com\/index.php\/2022\/02\/03\/salt-sddc-modules-manage-vmc-security-groups-and-rules\/","title":{"rendered":"Salt SDDC Modules \u2013 Manage VMC Security Groups and Rules"},"content":{"rendered":"\n<p>After two SDDC Extension Modules introductory posts about <a rel=\"noreferrer noopener\" href=\"https:\/\/nine30.info\/salt-sddc-modules-getting-started\/\" target=\"_blank\">getting started<\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/nine30.info\/salt-sddc-modules-state-files\/\" target=\"_blank\">state files<\/a>, in this post I cover something more juicy from my point of view: managing VMware Cloud on AWS (VMC) Security Groups and Security Rules. Of course as a pre-requisite, in addition to having Salt Master with SDDC extensions modules, you need to have access to a VMC SDDC. For this lab activity I was supported by <a rel=\"noreferrer noopener\" href=\"https:\/\/www.linkedin.com\/in\/matteoconcilio\" target=\"_blank\">Matteo Concilio<\/a> who is a true VMC and VCF expert and a super nice person!<\/p>\n\n\n\n<p>In order to manage VMC SDDC Networking &amp; Security configuration we will leverage modules named <code>vmc_security_group<\/code> and <code>vmc_security_rules<\/code>, but to collect all the required info we will also use module named <code>vmc_sddc<\/code>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"required-information-and-how-to-collect-it\">Required information and how to collect it<\/h2>\n\n\n\n<p>These are the information you need to collect before creating your state files.<\/p>\n\n\n\n<p><code>refresh_key<\/code><\/p>\n\n\n\n<p>This is your VMware CSP API Token, <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.vmware.com\/en\/VMware-Cloud-services\/services\/Using-VMware-Cloud-Services\/GUID-E2A3B1C1-E9AD-4B00-A6B6-88D31FCDDF7C.html\" target=\"_blank\">here<\/a> is the doc page &#8220;How Do I Generate API Tokens&#8221; for VMware Cloud Console. The grants I used for this use case are showed in the picture below, they might be a bit beyond the minimum required. <\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1704\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2022\/02\/11.vmc_api_token-1024x470.png\" alt=\"\" class=\"wp-image-1704\"\/><\/figure>\n<\/figure>\n\n\n\n<p><code>authorization_host<\/code><\/p>\n\n\n\n<p>This is a constant value <code>console.cloud.vmware.com<\/code><\/p>\n\n\n\n<p><code>org_id<\/code><\/p>\n\n\n\n<p>This is the Organization ID of the SDDC instance to be managed. On the Cloud Services Console toolbar, click your username and copy the <meta charset=\"utf-8\">Organization ID. There is little icon button that helps you in this.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1705\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2022\/02\/12.vmc_org_id-1024x469.png\" alt=\"\" class=\"wp-image-1705\"\/><\/figure>\n<\/figure>\n\n\n\n<p><code>hostname<\/code><\/p>\n\n\n\n<p>This is the  hostname of NSX-T manager of your SDDC. In order to obtain this info you can use the <code>get<\/code> function from the <code>vmc_sddc<\/code> module as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>salt-call vmc_sddc.get hostname=vmc.vmware.com refresh_key=&lt;Your_CSP_API_Token&gt; authorization_host=console.cloud.vmware.com org_id=&lt;Your_Org_ID&gt; verify_ssl=False cert=None &gt; ~\/sddcs.yaml<\/code><\/pre>\n\n\n\n<p>In the <code>~\/sddcs.yaml<\/code> file look for the parameter named <code>nsx_reverse_proxy_url<\/code> and this would be something like <code>https:\/\/nsx-11-22-33-44.rp.vmwarevmc.com\/vmc\/reverse-proxy\/api\/<\/code> here you just need to save the FQDN of that URL, <meta charset=\"utf-8\"><code>nsx-11-22-33-44.rp.vmwarevmc.com<\/code> in the example.<\/p>\n\n\n\n<p>It is a good practice to store this sensitive data in a Pillar, so you can add a new Pillar file <code>~\/salt\/srv\/pillar\/smea_vmc_conf.sls<\/code> with the following structure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nsx_hostname: &lt;Your_NSX_Hostname&gt;\nrefresh_key: <meta charset=\"utf-8\">&lt;Your_Refresh_Key&gt;\nauthorization_host: console.cloud.vmware.com\norg_id: &lt;Your_Org_ID&gt;\nsddc_id: &lt;Your_SDDC_ID&gt;<\/code><\/pre>\n\n\n\n<p>Then you need to modify the Pillar Top file by adding the newly created pillar file as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>base:\n  master_minion:\n    - my_vsphere_conf\n    - semea_vmc_conf<\/code><\/pre>\n\n\n\n<p>To make sure the new Pillar data is assigned to your minion you can run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>salt-call pillar.items<\/code><\/pre>\n\n\n\n<p>The output should include both <code>my_vpshere_conf <\/code>and <code>semea_vmc_conf<\/code> contents.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-3 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1684\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2022\/01\/15.salt_pillar-1024x607.png\" alt=\"\" class=\"wp-image-1684\"\/><\/figure>\n<\/figure>\n\n\n\n<p>Now we have the basic info required to authenticate to our SDDC instance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"state-files\">State Files<\/h2>\n\n\n\n<p><meta charset=\"utf-8\">The first state file will be used to create a new Security Group, please note that we could have all the configuration in a single state file. Add <code>~\/salt\/srv\/salt\/group_create.sls<\/code> State file with the content reported in the snippet below. You may need to use a different path if you didn&#8217;t configure your Master as per instructions from my <a rel=\"noreferrer noopener\" href=\"https:\/\/nine30.info\/salt-sddc-modules-getting-started\/\" target=\"_blank\">Salt SDDC Modules \u2013 Getting Started<\/a> post.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Create a new Security Group\ngroup_present:\n  module.run:\n    - name: vmc_security_groups.create\n    - hostname: {{ pillar&#91;'nsx_hostname'] }} \n    - refresh_key: {{ pillar&#91;'refresh_key'] }} \n    - authorization_host: {{ pillar&#91;'authorization_host'] }} \n    - org_id: {{ pillar&#91;'org_id'] }} \n    - sddc_id: {{ pillar&#91;'sddc_id'] }} \n    - domain_id: cgw \n    - verify_ssl: False \n    - cert: None \n    - security_group_id: paolo<\/code><\/pre>\n\n\n\n<p>Relevant information in the state above:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>domain_id<\/code>: this allows you to choose whether create the new group under the Management Gateway or the Compute Gateway. Possible values: <code>mgw<\/code>, <code>cgw<\/code>. In this case we are using <code>cgw<\/code> that clearly stands for the Compute Gateway<\/li><li><code>security_group_id<\/code>: this lets you set the name and the id (to be the same) of your new Security Group<\/li><\/ul>\n\n\n\n<p>Please, note that a good number of other optional parameters are available for this function.<meta charset=\"utf-8\"><\/p>\n\n\n\n<p>The second State file creates a Security Rule. Add <code>~\/salt\/srv\/salt\/rule_create.sls<\/code> State file with the content reported below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Create a new Security Rule\nensure_security_rule:\n  vmc_security_rules.present:\n    - hostname: {{ pillar&#91;'nsx_hostname'] }} \n    - refresh_key: {{ pillar&#91;'refresh_key'] }} \n    - authorization_host: {{ pillar&#91;'authorization_host'] }} \n    - org_id: {{ pillar&#91;'org_id'] }} \n    - sddc_id: {{ pillar&#91;'sddc_id'] }} \n    - verify_ssl: False \n    - cert: None\n    - rule_id: paolo_rule \n    - domain_id: cgw \n    - source_groups: &#91;\"\/infra\/domains\/cgw\/groups\/paolo\"] \n    - destination_groups: &#91;\"ANY\"] \n    - services: &#91;\"\/infra\/services\/SSH\"] \n    - action: ALLOW \n    - logged: true <\/code><\/pre>\n\n\n\n<p><meta charset=\"utf-8\">Relevant information in the state above:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>rule_id<\/code>: this lets you set the name and the id (to be the same) of your new Security Rule<\/li><li><code>source_groups<\/code>: this lets you provide a list of Security Groups as source in the rule. IMPORTANT: we need to provide Security Group as a path as duplicated Security Group names may exist for groups under different domains. Along with paths we support IPv4 and IPv6 Address. IP Address can be in one of the format: CIDR, IP Address, and Range of IP Address. In order to specify all groups, use the constant \u201cANY\u201d <\/li><li><code>destination_groups<\/code>: this lets you provide a list of Security Groups as destination in the rule. See above <code>source_group<\/code> for format and options<\/li><li><code>services<\/code>: this lets you provide a list of Services in the rule. IMPORTANT: we need to provide Services as a path as we did for groups in source and destination. In order to specify all Services, use the constant \u201cANY\u201d. This is case insensitive. If \u201cANY\u201d is used, it should be the ONLY element in the services array<\/li><li><code>action<\/code>: this lets you specify the action for the rule. Possible values for domain_id=cgw are: ALLOW, DROP, REJECT. While, the only possible value for domain_id=mgw is ALLOW;<\/li><li><code>logged<\/code>: this is a flag to let you enable packet logging. Default is <code>false<\/code> (disabled)<\/li><\/ul>\n\n\n\n<p><meta charset=\"utf-8\">Please, note that also for this function many other optional parameters are available.<meta charset=\"utf-8\"><\/p>\n\n\n\n<p>Note: you have two options to collect the correct path for a Security Group:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>List all the groups in a domain using the <code>get<\/code> function from the <code>vmc_security_groups<\/code> Execution Module<\/li><li>From the VMC UI select your SDDC, go to the &#8220;Networking &amp; Security&#8221; tab, click &#8220;Inventory>Groups&#8221; in the left tree and then click on the three dots icon close to your group and select &#8220;Copy Path to Clipboard&#8221;<\/li><\/ol>\n\n\n\n<p>To my knowledge, the only way to collect the path of a Service is: from the VMC UI <span style=\"font-size: revert;\">select your SDDC, go to the &#8220;Networking &amp; Security&#8221; tab and in the left tree<\/span> <span style=\"font-size: revert;\">click &#8220;Inventory>Services&#8221; and then click on the three dots icon close to your service and select &#8220;Copy Path to Clipboard&#8221;<\/span>.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-4 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1703\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2022\/02\/16.vmc_service_path-1024x469.png\" alt=\"\" class=\"wp-image-1703\"\/><\/figure>\n<\/figure>\n\n\n\n<p>Let&#8217;s use these State files to create group and rule respectively:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>salt-call state.apply group_create<\/code><\/pre>\n\n\n\n<p>You may want to first run this State file in test mode as I did in the screenshot below. To do that you just need to add the <code>test=True<\/code> option in your command.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-5 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1672\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2022\/01\/02.salt_groups_create_01-1024x587.png\" alt=\"\" class=\"wp-image-1672\"\/><\/figure>\n<\/figure>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-6 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1673\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2022\/01\/03.salt_groups_create_02-1024x587.png\" alt=\"\" class=\"wp-image-1673\"\/><\/figure>\n<\/figure>\n\n\n\n<p>The new Security Group is created under Compute Gateway, there is now a silly group named &#8220;paolo&#8221; in my console.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-7 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1706\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2022\/02\/04.vmc_groups_post-1024x469.png\" alt=\"\" class=\"wp-image-1706\"\/><\/figure>\n<\/figure>\n\n\n\n<p>Let&#8217;s create a rule that consumes the newly created group:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>salt-call state.apply rule_create<\/code><\/pre>\n\n\n\n<p>When I run this state I got an error message that however did not prevented the rule to be created. Honestly I didn&#8217;t bother to further troubleshoot it.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-8 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1675\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2022\/01\/06.salt_rule_create_01-1024x600.png\" alt=\"\" class=\"wp-image-1675\"\/><\/figure>\n<\/figure>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-9 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1676\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2022\/01\/07.salt_rule_create_02-1024x589.png\" alt=\"\" class=\"wp-image-1676\"\/><\/figure>\n<\/figure>\n\n\n\n<p><meta charset=\"utf-8\">The new Security Rule is created under Compute Gateway, there is now a very <em>creative<\/em> rule named &#8220;paolo_rule&#8221; in my console.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-10 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-id=\"1707\" src=\"https:\/\/nine30.info\/wp-content\/uploads\/2022\/02\/08.vmc_rule_post-1024x469.png\" alt=\"\" class=\"wp-image-1707\"\/><\/figure>\n<\/figure>\n\n\n\n<p>Here after I am also sharing  a State file to delete the newly created objects. I think it is now obvious how to use this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Delete the Security Rule named paolo_rule\nsecurity_rule_delete:\n  vmc_security_rules.absent:\n    - hostname: {{ pillar&#91;'nsx_hostname'] }} \n    - refresh_key: {{ pillar&#91;'refresh_key'] }} \n    - authorization_host: {{ pillar&#91;'authorization_host'] }} \n    - org_id: {{ pillar&#91;'org_id'] }} \n    - sddc_id: {{ pillar&#91;'sddc_id'] }} \n    - verify_ssl: False \n    - cert: None\n    - rule_id: paolo_rule \n    - domain_id: cgw\n\n#Delete the Security Group named paolo\ngroup_delete:\n  module.run:\n    - name: vmc_security_groups.delete\n    - hostname: {{ pillar&#91;'nsx_hostname'] }} \n    - refresh_key: {{ pillar&#91;'refresh_key'] }} \n    - authorization_host: {{ pillar&#91;'authorization_host'] }} \n    - org_id: {{ pillar&#91;'org_id'] }} \n    - sddc_id: {{ pillar&#91;'sddc_id'] }} \n    - domain_id: cgw \n    - verify_ssl: False \n    - cert: None \n    - security_group_id: paolo\n    - requires:\n      - security_rule_delete<\/code><\/pre>\n\n\n\n<p>You can use this as a starting point for automating your VMC SDDC&#8217;s security rules management activities. I hop it helps!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After two SDDC Extension Modules introductory posts about getting started and state files, in this post I cover something more juicy from my point of view: managing VMware Cloud on AWS (VMC) Security Groups and Security Rules. Of course as a pre-requisite, in addition to having Salt Master with SDDC extensions modules, you need to&hellip; <a class=\"more-link\" href=\"https:\/\/nine30.nxt70.com\/index.php\/2022\/02\/03\/salt-sddc-modules-manage-vmc-security-groups-and-rules\/\">Continue reading <span class=\"screen-reader-text\">Salt SDDC Modules \u2013 Manage VMC Security Groups and Rules<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1678,"comment_status":"open","ping_status":"open","sticky":false,"template":"templates\/template-cover.php","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[63,88,122,126,143,150,152,177,178],"class_list":["post-1660","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech","tag-firewall","tag-iac","tag-opensource","tag-oss","tag-salt","tag-sddc-2","tag-security","tag-vmc","tag-vmware","entry"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/posts\/1660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/comments?post=1660"}],"version-history":[{"count":0,"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/posts\/1660\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/media?parent=1660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/categories?post=1660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nine30.nxt70.com\/index.php\/wp-json\/wp\/v2\/tags?post=1660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}