<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Trinquier Vannick on Medium]]></title>
        <description><![CDATA[Stories by Trinquier Vannick on Medium]]></description>
        <link>https://medium.com/@trinquier.vannick?source=rss-3d0d3b6f6eb1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Ovzc4gic9mb47IdE9AhOAA.jpeg</url>
            <title>Stories by Trinquier Vannick on Medium</title>
            <link>https://medium.com/@trinquier.vannick?source=rss-3d0d3b6f6eb1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 07:20:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@trinquier.vannick/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Create tailored guardrails with custom organization policy constraints in GCP]]></title>
            <link>https://medium.com/google-cloud/create-tailored-guardrails-with-custom-organization-policy-constraints-in-gcp-0d7b12eb001f?source=rss-3d0d3b6f6eb1------2</link>
            <guid isPermaLink="false">https://medium.com/p/0d7b12eb001f</guid>
            <category><![CDATA[gcp-security-operations]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[organization-policies]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[compliance]]></category>
            <dc:creator><![CDATA[Trinquier Vannick]]></dc:creator>
            <pubDate>Mon, 13 Nov 2023 09:09:35 GMT</pubDate>
            <atom:updated>2023-11-14T14:33:28.888Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to easily create custom controls that can be enforced when provisioning resources in GCP?</h4><h4>Use custom organization policy constraint to perform your own defined preventive controls</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MErC8qqTjmDKqJ6ZqxWCqw.png" /><figcaption>Preventive controls with custom organization policy constraints</figcaption></figure><p>In today’s world, businesses are moving their operations to the cloud to get more flexibility, scale up or down as needed, and save money. But there are challenges with this change, especially when it comes to making sure your cloud setup follows all the rules.</p><p>Compliance controls are essential for organizations that want to use Google Cloud Platform in a secure, responsible manner. By implementing effective compliance controls, organizations can reduce their risk, protect their data, and meet their regulatory obligations.</p><p>There are different ways to make sure your cloud setup is compliant, but organization policies are a central and effective approach. Google Cloud Platform (GCP) has a wide range of organization policies that let businesses set and enforce compliance rules across their cloud infrastructure. But sometimes, the standard organization policies might not be enough to meet your specific compliance needs.</p><p>That’s where custom organization policy constraints come in. This is a new feature (still in pre-GA as of October 2023) that gives you more flexibility and control over how you define your compliance requirements. Custom constraints let you tailor your compliance policies to your unique needs and the specific controls that apply to your business.</p><p>In this article, we’ll talk about organization policies and custom constraints and how they can help you achieve compliance. We’ll also show you some real-world examples of how to use custom organization policies to implement essential compliance controls that are important for many businesses.</p><h3>Organization Policy</h3><p>Organization policies allow you to define constraints that govern the creation and modification of resources. These constraints can be tailored to meet the specific needs of your organization and ensure that they align with the organization’s overall goals and policies.</p><p>Imagine a scenario where you want to prevent engineers from creating Compute Engine VM instances with external IP addresses. This can be easily achieved using an existing organization policy, constraints/compute.vmExternalIpAccess. This policy provides a basic level of customization with allowed and denied lists, allowing specific VMs to be granted external IP access if necessary.</p><p>However, what if your organization has more granular requirements, such as enforcing a specific naming convention for some type of resources ?</p><p>Standard organization policies might not suffice in such cases. Custom organization policy constraints provide the flexibility to address these specific needs.</p><h3>Custom Organization Policy Constraint</h3><p>Organization Policy provides predefined constraints for many existing Google Cloud services. However, if you need better control and need to adapt the compliance rule to your organization, you can now create custom constraints and use them in your organization policies.</p><p>Custom Constraint has the notion of conditions where you are able to define your own logic using Common Expression Language (CEL).</p><p>CEL is a powerful tool for evaluating logical expressions and conditions in various contexts, including policy enforcement and data validation. It provides a concise and expressive way to define complex rules and constraints, making it well-suited for managing access control, resource allocation, and data integrity requirements.</p><p>Here are some example of CEL expressions that can be used:</p><ul><li>resource.machineType.contains(‘/machineTypes/n2d’)</li><li>resource.sizeGb &lt;= 250</li></ul><p>Feel free to have a look <a href="https://cloud.google.com/resource-manager/docs/organization-policy/creating-managing-custom-constraints#common_expression_language">here</a> for more information on CEL and see all the possibility of the language.</p><h3>Enforcing naming convention with custom constraint</h3><p>Based on the discussion above, we will create a custom constraint to ensure that every firewall rule created is following a specific naming convention.</p><p>Let’s follow that sample pattern for our firewall rule name (allow|deny)-(ingress|egress)-&lt;description&gt;-(dev|uat|prd)</p><p>Examples:</p><ul><li>allow-ingress-web-to-db-prd Allowed</li><li>allow-ingress-web-to-db Denied</li><li>allow-ssh-to-db Denied</li><li>allow-egress-web-to-internet-uat Allowed</li></ul><p>The nice thing is now with custom organization policy, we can enforce naming of VPC Firewall rules using a regular expression as expressed above.</p><h3>Testing Custom Organization Policy</h3><p>Let’s have a look and check how we can create a custom organization policy to enforce the firewall rule naming convention above.</p><p>0. Setting environment variables for this test</p><pre>ORGANIZATION_ID=11111111<br>PROJECT_ID=my-project</pre><ol><li>Enabling organization policy API</li></ol><pre>gcloud services enable orgpolicy.googleapis.com --project $PROJECT_ID</pre><p>2. Create a custom constraint organization policy to enforce firewall rule naming convention. In this example, we are using following regular expression for validation ^(allow|deny)-(ingress|egress)-[a-zA-Z0–9-]*-(dev|uat|prd)$</p><p>3. Create the custom constraint at the organization level</p><p><em>Creation of the file enforceFirewallRuleNaming.yaml</em></p><pre>cat &lt;&lt; EOL &gt; enforceFirewallRuleNaming.yaml <br>name: organizations/$ORGANIZATION_ID/customConstraints/custom.enforceFirewalLRuleNamingConvention<br>resource_types: compute.googleapis.com/Firewall<br>condition: resource.name.matches(&quot;^(allow|deny)-(ingress|egress)-[a-zA-Z0-9-]*-(dev|uat|prd)$&quot;) == false<br>action_type: DENY<br>method_types: CREATE<br>display_name: Enforce Firewall Rule naming Convention<br>description:  &quot;Enforce that every Firewall Rule created are named according to regular expression pattern: ^(allow|deny)-(ingress|egress)-[a-zA-Z0-9-]*-(dev|uat|prd)$&quot;<br>EOL</pre><p><em>Creation of the custom constraint</em></p><pre>gcloud org-policies set-custom-constraint enforceFirewallRuleNaming.yaml</pre><p><em>Output</em></p><pre>Created custom constraint [organizations/11111111/customConstraints/custom.enforceFirewalLRuleNamingConvention].<br>actionType: DENY<br>condition: resource.name.matches(&quot;^(allow|deny)-(ingress|egress)-[a-zA-Z0-9-]*-(dev|uat|prd)$&quot;)<br>  == false<br>description: &#39;Enforce that every Firewall Rule created are named according to regular<br>  expression pattern: ^(allow|deny)-(ingress|egress)-[a-zA-Z0-9-]*-(dev|uat|prd)$&#39;<br>displayName: Enforce Firewall Rule naming Convention<br>methodTypes:<br>- CREATE<br>name: organizations/11111111/customConstraints/custom.enforceFirewalLRuleNamingConvention<br>resourceTypes:<br>- compute.googleapis.com/Firewall<br>updateTime: &#39;2023-11-13T06:23:24.248497Z&#39; </pre><p>4. Enforce this custom organization policy to a specific project</p><p><em>Creation of the file enforceFirewallRuleNaming-policy.yaml</em></p><pre>cat &lt;&lt; EOL &gt; enforceFirewallRuleNaming-policy.yaml<br>name: projects/$PROJECT_ID/policies/custom.enforceFirewalLRuleNamingConvention<br>spec:<br>  rules:<br>  - enforce: true<br>EOL</pre><p><em>Enforcing the policy</em></p><pre>gcloud org-policies set-policy enforceFirewallRuleNaming-policy.yaml</pre><p><em>Output</em></p><pre>Created policy [projects/my-project/policies/custom.enforceFirewalLRuleNamingConvention].<br>name: projects/my-project/policies/custom.enforceFirewalLRuleNamingConvention<br>spec:<br>  etag: CNKCx6oGEKDauu4B<br>  rules:<br>  - enforce: true<br>  updateTime: &#39;2023-11-13T06:25:22.500084Z&#39;</pre><p>5. Create a VPC firewall rule and checking whether the custom organization policy is applied</p><p>Let’s first try to create a firewall rule not following naming convention. As expected in the custom organization policy, the creation should be denied.</p><pre>gcloud compute firewall-rules create allow-ssh \<br> --action=ALLOW --direction=INGRESS --network=default \<br> --priority=1000 --rules=tcp:22 --source-ranges=0.0.0.0/0 \<br> --project $PROJECT_ID</pre><p><em>Output</em></p><pre>Creating firewall…failed. <br>ERROR: (gcloud.compute.firewall-rules.create) Could not fetch resource:<br> - Operation denied by custom org policy: [customConstraints/custom.enforceFirewalLRuleNamingConvention] : Enforce that every Firewall Rule created are named according to regular expression pattern: ^(allow|deny)-(ingress|egress)-[a-zA-Z0–9-]*-(dev|uat|prd)$</pre><p>Indeed, the firewall rule cannot be created. We can see that the operation is denied and the description message is providing more information about the cause of the failure.</p><p>6. Create a second VPC firewall rule following expected naming convention</p><p>To do so, we are now using that specific firewall rule name allow-ingress-internet-to-ssh-prd which should be compliant with our policy.</p><pre>gcloud compute firewall-rules create allow-ingress-internet-to-ssh-prd \<br> - action=ALLOW - direction=INGRESS - network=default \<br> - priority=1000 - rules=tcp:22 - source-ranges=0.0.0.0/0 - project $PROJECT_ID</pre><p><em>Output</em></p><pre>Creating firewall… Created [https://www.googleapis.com/compute/v1/projects/my-project/global/firewalls/allow-ingress-internet-to-ssh-prd]. <br>Creating firewall…done.</pre><p>As expected the firewall rule can be created successfully !</p><h3>Conclusion</h3><p>As you have seen, now with custom organization policy, we are able to easily create security and compliance controls when provisioning resources on our GCP infrastructure.</p><p>As per now, only limited services have been made available. And only few supported resources for each of these API are supported. However, it should continue to grow in the future and will certainly simplify a lot the work of security team in every organization to ensure resources provisioning keeps secure and compliant</p><h3>References</h3><p>Services available with custom organization policy constraint:</p><ul><li><a href="https://cloud.google.com/compute/docs/access/custom-constraints">Compute Engine API</a></li><li><a href="https://cloud.google.com/firewall/docs/custom-constraints">Cloud Firewall API</a></li><li><a href="https://cloud.google.com/kubernetes-engine/docs/how-to/custom-org-policies">Google Kubernetes Engine</a></li><li><a href="https://cloud.google.com/vpc/docs/custom-constraints">Cloud VPC</a></li><li><a href="https://cloud.google.com/dataproc/docs/concepts/dataproc-custom-constraints">Dataproc</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0d7b12eb001f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/create-tailored-guardrails-with-custom-organization-policy-constraints-in-gcp-0d7b12eb001f">Create tailored guardrails with custom organization policy constraints in GCP</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Evaluating your existing GCP resources]]></title>
            <link>https://medium.com/google-cloud/evaluating-your-existing-gcp-resources-810ef901ed40?source=rss-3d0d3b6f6eb1------2</link>
            <guid isPermaLink="false">https://medium.com/p/810ef901ed40</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[cloud-compliance]]></category>
            <category><![CDATA[policy-library]]></category>
            <category><![CDATA[infrastructure]]></category>
            <category><![CDATA[cloud-asset-inventory]]></category>
            <dc:creator><![CDATA[Trinquier Vannick]]></dc:creator>
            <pubDate>Wed, 21 Jun 2023 04:12:36 GMT</pubDate>
            <atom:updated>2023-06-21T04:42:05.073Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to validate compliance of existing GCP resources from a CAI export using Cloud Scheduler and Cloud Batch?</h4><h4>Use Cloud Batch and Cloud Scheduler to perform one-shot or periodic compliance checks</h4><figure><img alt="Detective control on GCP" src="https://cdn-images-1.medium.com/max/1024/0*jpW99Yv9ToYTS7WO" /><figcaption>Detective control on GCP</figcaption></figure><p>Compliance control is important for organizations to ensure that they are following security standards, regulations, and organizational policies. They can help to identify and investigate potential compliance violations. By using compliance controls, organizations can quickly identify and address any compliance issues that may arise.</p><p>It exists multiples on ways to enforce compliance controls:</p><ul><li><strong>Preventive Control</strong> targets Infrastructure as Code (IaC) CI/CD pipeline before the changes are applied to the infrastructure.<a href="https://github.com/GoogleCloudPlatform/terraform-validator"> Terraform Vet</a> is tool that can be used for organization using Terraform</li><li><strong>Real Time Control</strong> is another way that can be used to detect whether changes that have been recently done are violating existing compliance controls. This<a href="https://medium.com/google-cloud/evaluating-your-gcp-resource-realtime-57a0f25d4587"> article</a> explained very well how it can be done using Cloud Asset Inventory feed and<a href="https://github.com/GoogleCloudPlatform/config-validator"> Config Validator</a></li></ul><p>Preventive control enforces that resources that are going to be deployed using IaC are compliant. Realtime control ensures that resources that have just been created are also compliant. However, to have a complete vision of the current state of an existing organization, it is also needed to assess existing infrastructure.</p><p>This article will focus on executing <strong>Detective Control</strong> using Cloud Batch jobs. Our goal will be to ensure that buckets created in a project are created in a specific zone.</p><p>The different steps in this can article can be accessed also from this Git repository <a href="https://github.com/vannicktrinquier/gcp-detective-control">https://github.com/vannicktrinquier/gcp-detective-control</a>.</p><p><em>The code present in the repository and in this article is for demonstration only, not for production use.</em></p><h3>Design Overview</h3><p>Overall design is based on Google Cloud Batch executing a command line application named CFT Scorecard to perform detective control. The checks are based on Cloud Asset Inventory export to a Google Cloud Storage bucket. Finally, Cloud Scheduler is used to periodically create an execution and ensure the infrastructure stays compliant.</p><p>The following diagram presents the high level flow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g921N7PC-MlJSLOKd5xlew.png" /><figcaption>High level flow</figcaption></figure><h3>Components</h3><p>Various components are used to process detective control. Here is a brief list and explanation of all the tools, libraries and services used.</p><h4>Cloud Batch</h4><p><a href="https://cloud.google.com/batch/docs/get-started">Google Cloud Batch</a> is a managed service that makes it easy to run large-scale batch jobs on Google Cloud Platform. Cloud Batch abstracts away the complexities of managing and scheduling batch jobs, so you can focus on your application code. CAI Export and execution of CFT Scorecard command-line is a long running task that is a great use case for Cloud Batch.</p><h4>CFT Scorecard</h4><p><a href="https://github.com/GoogleCloudPlatform/cloud-foundation-toolkit/blob/master/cli/docs/scorecard.md">CFT Scorecard</a> is a command-line tool that can be used to scan Google Cloud Platform (GCP) environments for resources and IAM policies. It uses data from Cloud Asset Inventory (CAI) exports to test policies based on constraints and constraint templates from the Config Validator policy library.</p><h4>Config Validator</h4><p><a href="https://github.com/GoogleCloudPlatform/config-validator">Config Validator</a> is a tool that checks the syntax and correctness of Google Cloud Platform (GCP) resources against Rego based policies. It can be used to check for compliance with GCP’s security policies, as well as to enforce specific requirements for your environment.</p><h4>Config Validator Policy Library</h4><p><a href="https://github.com/GoogleCloudPlatform/policy-library">Config Validator Policy library</a> is a repository with a collection of policies that can be used to scan Google Cloud Platform (GCP) resources for compliance with security standards. The policies are written in Rego, a policy language that is supported by the Open Policy Agent (OPA) framework.</p><p>Policy library can be used to scan GCP resources for a variety of compliance standards, including:</p><ul><li>CIS Benchmarks</li><li>Google Cloud Security Best Practices</li><li>And even your own internal security policies</li></ul><h4>Cloud Asset Inventory Export</h4><p><a href="https://cloud.google.com/asset-inventory/docs/export-asset-metadata">Cloud Asset Inventory Export</a> to Google Cloud Storage bucket allows you to export all the asset metadata to files in a Cloud Storage bucket. This can be used to check GCP resources against policies. CAI is able to export various content types such as resources, iam policies, organizational policies and access policies.</p><h3>Performing validation with CFT Scorecard</h3><p>We will use <em>gcloud</em> to create a Cloud Batch job and a script is executed to perform detective control. The script is quite simple and is doing the following things:</p><ol><li>Download CFT Scorecard command line tool</li><li>Clone the Config Validator policy library from Github. <br>In a typical customer case, the clone might be more complex as the Git repository will not be public and Git authentication will have to be handled.</li><li>Copy a constraint from the <em>samples</em> folder. The constraint chosen <em>storage_location.yaml</em> is ensuring that every Cloud Storage buckets are created are in <em>asia-southeast1</em></li><li>Execute CFT Scorecard using list of constraint and policies downloaded previously for the organization SCAN_ORGANIZATION_ID</li><li>Copy the result to the bucket for further analysis</li></ol><pre>#!/bin/sh<br><br>echo &#39;Downloading CFT Scorecard&#39;<br>curl -o cft https://storage.googleapis.com/cft-cli/latest/cft-linux-amd64<br>chmod +x cft<br><br>echo &#39;Downloading sample policy library&#39;<br>git clone https://github.com/GoogleCloudPlatform/policy-library.git<br>cp policy-library/samples/storage_location.yaml policy-library/policies/constraints/<br>mkdir -p ./reports<br><br>./cft scorecard --policy-path ./policy-library --bucket ${CAI_BUCKET} --target-organization ${SCAN_ORGANIZATION_ID} --output-format json --output-path ./reports --refresh<br>gsutil cp ./reports/scorecard.json gs://${CAI_BUCKET}</pre><p>Following constraint storage_location.yaml is used as a proof of concept in this article. This constraint is based on the following <a href="https://github.com/GoogleCloudPlatform/policy-library/blob/main/policies/templates/gcp_storage_location_v1.yaml">policy template</a>. This constraint ensures that only Google Cloud Storage bucket created in <em>asia-southeast1 </em>are allowed. In this article, we will create a bucket in a different region and we will see that the CFT Scorecard tool is reporting it as a violation.</p><p>File<em> storage_location.yaml</em></p><pre>apiVersion: constraints.gatekeeper.sh/v1alpha1<br>kind: GCPStorageLocationConstraintV1<br>metadata:<br>  name: allow_some_storage_location<br>  annotations:<br>    description: Checks Cloud Storage bucket locations against allowed or disallowed<br>      locations.<br>    bundles.validator.forsetisecurity.org/healthcare-baseline-v1: security<br>spec:<br>  severity: high<br>  match:<br>    ancestries:<br>    - &quot;organizations/**&quot;<br>  parameters:<br>    mode: &quot;allowlist&quot;<br>    locations:<br>    - asia-southeast1<br>    exemptions: []</pre><h3>Configuring the Cloud Batch job</h3><p>The <em>gcloud</em> command is quite limited and we need to use a JSON file to provide the configuration of the batch to create. The following JSON file contains:</p><ul><li>A bash script automatically injected inside a container during the execution</li><li>A service account associated with the job used to queries GCP APIs.</li></ul><p><em>File batch.json</em></p><pre><br>{<br>  &quot;taskGroups&quot;: [<br>    {<br>      &quot;taskSpec&quot;: {<br>        &quot;runnables&quot;: [<br>          {<br>            &quot;script&quot;: {<br>              &quot;text&quot;: &quot;#!/bin/sh\n\necho &#39;Downloading CFT Scorecard&#39;\ncurl -o cft https://storage.googleapis.com/cft-cli/latest/cft-linux-amd64\nchmod +x cft\n\necho &#39;Downloading sample policy library&#39;\ngit clone https://github.com/GoogleCloudPlatform/policy-library.git\ncp policy-library/samples/storage_location.yaml policy-library/policies/constraints/\nmkdir -p ./reports\n\n./cft scorecard --policy-path ./policy-library --bucket ${CAI_BUCKET} --target-organization ${SCAN_ORGANIZATION_ID} --output-format json --output-path ./reports --refresh\ngsutil cp ./reports/scorecard.json gs://${CAI_BUCKET}&quot;<br>            },<br>            &quot;environment&quot;: {<br>              &quot;variables&quot;: {<br>                &quot;SCAN_ORGANIZATION_ID&quot;: &quot;&lt;ORGANIZATION_ID_TO_REPLACE&gt;&quot;,<br>                &quot;CAI_BUCKET&quot;: &quot;&lt;CAI_BUCKET_TO_REPLACE&gt;&quot;<br>              }<br>            }<br>          }<br>        ],<br>        &quot;computeResource&quot;: {<br>          &quot;cpuMilli&quot;: 8000,<br>          &quot;memoryMib&quot;: 32768<br>        },<br>        &quot;maxRetryCount&quot;: 1,<br>        &quot;maxRunDuration&quot;: &quot;36000s&quot;<br>      },<br>      &quot;taskCount&quot;: 1,<br>      &quot;parallelism&quot;: 1<br>    }<br>  ],<br>  &quot;logsPolicy&quot;: {<br>    &quot;destination&quot;: &quot;CLOUD_LOGGING&quot;<br>  },<br>  &quot;allocationPolicy&quot;: {<br>    &quot;serviceAccount&quot;: {<br>      &quot;email&quot;: &quot;batch-detective-sa@&lt;PROJECT_ID_TO_REPLACE&gt;.iam.gserviceaccount.com&quot;<br>    }<br>  }<br>}</pre><p>Before execution and creation of the Cloud Batch job, some specific information have to be updated such as:</p><ul><li>the project where compliance checks are performed &lt;<em>ORGANIZATION_ID_TO_REPLACE</em>&gt;</li><li>the Cloud Storage bucket used to store CAI export and that is used by Config Validator to performance checks &lt;<em>CAI_BUCKET_TO_REPLACE</em>&gt;</li><li>the Cloud Batch service account used to perform actions to Google APIs. This service account required various permissions assigned to be able to execute compliance checks. This service account is assigned to VM created to perform the execution</li></ul><h4>Service account permissions</h4><p>To get the permissions needed to execute a job, following IAM roles have to be granted to the job service account:</p><ul><li>Batch Agent Reporter (<em>roles/batch.agentReporter)</em> to allow the job to report its status to the control plane.</li><li>Logs Writer <em>(roles/logging.logWriter)</em> to log Cloud Batch job output and ensure everything is happening as expected/</li><li>Storage Admin <em>(roles/storage.admin) </em>to store files on Cloud Storage bucket.</li><li>Cloud Asset Viewer<em> (roles/cloudasset.viewer)</em> at the organization level to export CAI assets.</li></ul><p>Additionally, the Cloud Asset Service Agent needs to be granted the following role to be export CAI assets to a bucket:</p><ul><li>Storage Admin <em>(roles/storage.admin)</em></li></ul><h3>Performing detective control step by step</h3><p>Set environment variables and initialize the environment</p><pre>export SCAN_ORGANIZATION_ID=&lt;ORGANIZATION_ID_TO_REPLACE&gt;<br>export PROJECT_ID=&lt;PROJECT_ID_TO_REPLACE&gt;<br>export CAI_BUCKET=&lt;CAI_BUCKET_TO_REPLACE&gt;<br>export PROJECT_NUMBER=`gcloud projects describe $PROJECT_ID \<br>    --format=&#39;value(projectNumber)&#39;`<br><br>gcloud config set project $PROJECT_ID</pre><p>Activate the CAI API on your project</p><pre>gcloud services enable cloudasset.googleapis.com batch.googleapis.com logging.googleapis.com storage.googleapis.com compute.googleapis.com iam.googleapis.com<br>gcloud beta services identity create --service=cloudasset.googleapis.com</pre><p>Assign permission to service account used by Cloud Inventory to store exported files to a GCS bucket</p><pre>gcloud projects add-iam-policy-binding $PROJECT_ID \<br>    --role roles/storage.admin \<br>    --member \<br>    serviceAccount:service-$PROJECT_NUMBER@gcp-sa-cloudasset.iam.gserviceaccount.com</pre><p>Create a GCS bucket to store CAI export. This bucket will store the CAI export but also the results of CFT Scorecard execution</p><pre>gcloud storage buckets create gs://$CAI_BUCKET</pre><p>Create the service account that will be used to run the Cloud Batch job</p><pre>BATCH_DETECTIVE_SA=batch-detective-sa<br><br>gcloud iam service-accounts create ${BATCH_DETECTIVE_SA} \<br>    --description=&quot;Service Account used by Detective Cloud Batch Job&quot;</pre><p>Assign the good permissions to the Cloud Batch Job</p><pre>gcloud projects add-iam-policy-binding $PROJECT_ID \<br>    --role roles/batch.agentReporter \<br>    --member \<br>    serviceAccount:$BATCH_DETECTIVE_SA@$PROJECT_ID.iam.gserviceaccount.com<br><br>gcloud projects add-iam-policy-binding $PROJECT_ID \<br>    --role roles/logging.logWriter \<br>    --member \<br>    serviceAccount:$BATCH_DETECTIVE_SA@$PROJECT_ID.iam.gserviceaccount.com <br><br>gcloud projects add-iam-policy-binding $PROJECT_ID \<br>    --role roles/storage.admin \<br>    --member \<br>    serviceAccount:$BATCH_DETECTIVE_SA@$PROJECT_ID.iam.gserviceaccount.com <br><br>gcloud organizations add-iam-policy-binding $SCAN_ORGANIZATION_ID \<br>    --role roles/cloudasset.viewer \<br>    --member \<br>    serviceAccount:$BATCH_DETECTIVE_SA@$PROJECT_ID.iam.gserviceaccount.com</pre><p>Create a Cloud Batch job and monitor execution of the job. Execution of the job will take a little time as it is needed to wait CAI export for the project id is done and completed. Additionally, execution of CFT Scorecard is needed.</p><pre>gcloud batch jobs submit detective-job-orga \<br>    --location asia-southeast1 \<br>    --config batch-organization.json</pre><p>As soon the Cloud Batch job execution is done, the generated report will be present in the bucket folder with the name <em>scorecard.json</em></p><h3>Analyzing the results</h3><p>Cloud Batch job status can be found directly from the console. Few minutes after the creation of the job, the execution will be over and the status of the job will be <em>Succeeded.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jVOMrtvWYRBgjRqKziVuNg.png" /><figcaption>One shot Batch job execution</figcaption></figure><p>At the end of our job execution, the CFT Scorecard report is copied to the bucket for further analysis. During the example, we can see that only a unique violation have been detected.</p><pre>$ gsutil cp gs://${CAI_BUCKET}/scorecard.json . <br>$ cat scorecard.json <br>[<br>  {<br>    &quot;Category&quot;: &quot;Other&quot;,<br>    &quot;Resource&quot;: &quot;//storage.googleapis.com/my-bucket&quot;,<br>    &quot;Message&quot;: &quot;//storage.googleapis.com/my-bucket is in a disallowed location.&quot;,<br>    &quot;metadata&quot;: {<br>      &quot;ancestry_path&quot;: &quot;organizations/1111/folders/2222/folders/3333/folders/4444/projects/5555&quot;,<br>      &quot;constraint&quot;: {<br>        &quot;annotations&quot;: {<br>          &quot;bundles.validator.forsetisecurity.org/healthcare-baseline-v1&quot;: &quot;security&quot;,<br>          &quot;description&quot;: &quot;Checks Cloud Storage bucket locations against allowed or disallowed locations.&quot;,<br>          &quot;validation.gcp.forsetisecurity.org/originalName&quot;: &quot;allow_some_storage_location&quot;,<br>          &quot;validation.gcp.forsetisecurity.org/yamlpath&quot;: &quot;policy-library/policies/constraints/storage_location.yaml&quot;<br>        },<br>        &quot;labels&quot;: {},<br>        &quot;parameters&quot;: {<br>          &quot;exemptions&quot;: [],<br>          &quot;locations&quot;: [<br>            &quot;asia-southeast1&quot;<br>          ],<br>          &quot;mode&quot;: &quot;allowlist&quot;<br>        }<br>      },<br>      &quot;details&quot;: {<br>        &quot;location&quot;: &quot;US&quot;,<br>        &quot;resource&quot;: &quot;//storage.googleapis.com/my-bucket&quot;<br>      }<br>    }<br>  }<br>]</pre><p>Here, we found a violation for the resource <em>//storage.googleapis.com/my-bucket </em>because of the bucket location.</p><h3>Scheduling the job for periodical execution</h3><p>Above, we have seen how to perform a one time execution. However, we might want to periodically execute detective control on the organization or on a specific critical project. All of that can be done easily and without any coding using the Cloud Scheduler managed services.</p><p>A dedicated service account for the Cloud Scheduler cron job:</p><ul><li>Batch Job Editor (<em>roles/batch.jobsEditor</em>) on the project to ensure that job can be created by Cloud Scheduler</li><li>Service Account User (<em>roles/iam.serviceAccountUser</em>) on the service account used by the batch job to ensure that Cloud Scheduler is able to create a job and assigned the Batch service account</li></ul><pre>REGION=asia-southeast1<br>SCHEDULER_SA=scheduler-detective-sa<br><br>gcloud iam service-accounts create ${SCHEDULER_SA} \<br>    --description=&quot;Service Account used by Detective Cloud Scheduler&quot;<br><br>gcloud projects add-iam-policy-binding $PROJECT_ID \<br>    --role roles/batch.jobsEditor \<br>    --member \<br>    serviceAccount:$SCHEDULER_SA@$PROJECT_ID.iam.gserviceaccount.com<br><br>gcloud iam service-accounts add-iam-policy-binding $BATCH_DETECTIVE_SA@$PROJECT_ID.iam.gserviceaccount.com \<br>    --role roles/iam.serviceAccountUser \<br>    --member \<br>    serviceAccount:$SCHEDULER_SA@$PROJECT_ID.iam.gserviceaccount.com</pre><p>Creation of the Cloud Batch job is done using Cloud Scheduler with HTTP targeting the Google APIs. Authentication is done using <em>oauth</em> with the associated service account. Here, a cron schedule is created to perform detective control every 3 hours.</p><pre>gcloud services enable cloudscheduler.googleapis.com<br><br>gcloud scheduler jobs create http detective-job --schedule=&quot;0 */3 * * *&quot; \<br>    --uri=&quot;https://batch.googleapis.com/v1/projects/$PROJECT_NUMBER/locations/$REGION/jobs&quot; \<br>    --http-method=POST \<br>    --location=$REGION \<br>    --headers=User-Agent=Google-Cloud-Scheduler,Content-Type=application/json \<br>    --oauth-service-account-email=$SCHEDULER_SA@$PROJECT_ID.iam.gserviceaccount.com \<br>    --oauth-token-scope=&quot;https://www.googleapis.com/auth/cloud-platform&quot; \<br>    --message-body-from-file=batch-organization.json</pre><p>After a while, it can be seen that regular Cloud Batch jobs are created:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ff81Lpiz63iqEacY8Jmjzw.png" /><figcaption>Multiples Batch job executions</figcaption></figure><h3>Conclusion</h3><p>That ‘s it, with a few <em>gcloud</em> commands, we have set up a periodic detective controls ready to be used !</p><p>Other enhancements can be done such as integrating those findings with Security Command Center or even Big Query. It might involve some coding to be able to integrate with those Google services.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=810ef901ed40" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/evaluating-your-existing-gcp-resources-810ef901ed40">Evaluating your existing GCP resources</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>