<?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 Aleksandr Averbukh on Medium]]></title>
        <description><![CDATA[Stories by Aleksandr Averbukh on Medium]]></description>
        <link>https://medium.com/@averbuks?source=rss-439726e92d3c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lBBTh0eRMZ_SVjIWiHfXGQ.jpeg</url>
            <title>Stories by Aleksandr Averbukh on Medium</title>
            <link>https://medium.com/@averbuks?source=rss-439726e92d3c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 23:17:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@averbuks/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[Centralised management of VPCSC with Terraform and Cloud Asset Inventory]]></title>
            <link>https://medium.com/google-cloud/centralised-management-of-vpcsc-with-terraform-and-cloud-asset-inventory-272efeeb2598?source=rss-439726e92d3c------2</link>
            <guid isPermaLink="false">https://medium.com/p/272efeeb2598</guid>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[gcpsecurity]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[gcp-security-operations]]></category>
            <category><![CDATA[vpc-service-control]]></category>
            <dc:creator><![CDATA[Aleksandr Averbukh]]></dc:creator>
            <pubDate>Tue, 07 Nov 2023 15:39:47 GMT</pubDate>
            <atom:updated>2023-11-07T20:07:46.557Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/894/1*YhAdL75GQq4hr-ByxEX4gQ.png" /></figure><p>If you want to learn more about <a href="https://cloud.google.com/vpc-service-controls"><strong>VPC Service Controls</strong></a>, there is a list of articles written by my colleagues as well as detailed <a href="https://cloud.google.com/vpc-service-controls/docs/overview">official documentation</a>.</p><ul><li><a href="https://medium.com/google-cloud/google-cloud-vpc-service-controls-lessons-learned-670619c3d82c">Google Cloud VPC-Service Controls: Lessons Learned</a></li><li><a href="https://medium.com/google-cloud/gcp-vpc-sc-with-shared-vpc-network-526f85377cdd">GCP VPC SC with Shared VPC Network</a></li></ul><p>In this article, I’ll explore a specific approach to centrally managing <a href="https://cloud.google.com/vpc-service-controls">VPC Service Controls</a> (VPCSC) using <a href="https://www.terraform.io/">Terraform</a> and <a href="https://cloud.google.com/asset-inventory/docs/overview">Cloud Asset Inventory</a>. This method is particularly useful in scenarios where you lack control over project management, have a large number of projects (hundreds or even thousands) within your GCP organization, and have a strict requirement to add every project to a service perimeter. Additionally, you prefer to manage VPCSC with Terraform.</p><p>A crucial aspect of VPC Service Controls is grouping projects into service perimeters to control access and communication within those boundaries. The key question is how to efficiently add projects to these designated service perimeters.</p><h4>Decentralised Management</h4><p>The initial and currently recommended approach for adding projects to a service perimeter with terraform involves the <a href="https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/access_context_manager_service_perimeter_resource"><strong>google_access_context_manager_service_perimeter_resource</strong></a> resource. This resource effectively adds a single project to an existing VPC Service Controls perimeter. By leveraging the project management pipeline, each project can be seamlessly incorporated into the designated perimeter.</p><p>While this approach seems straightforward, it presents certain limitations:</p><ol><li><strong>Security Implications:</strong> The project creation service account requires VPCSC Admin permissions at the organization level, raising potential security concerns.</li><li><strong>Collision Conflicts:</strong> With multiple project creation pipelines, concurrent execution becomes problematic as they all modify the same VPCSC resource, potentially overwriting each other’s changes, leading to collisions. This issue becomes particularly challenging for large organizations managing hundreds or thousands of projects.</li><li><strong>Control over Project Creation:</strong> Effective implementation of this approach necessitates centralised control over project creation. If users or business units have independent project creation capabilities, there’s no guarantee that these projects will be automatically added to the perimeter.</li></ol><p>Despite these limitations, the decentralised approach remains viable for organizations with centralised project creation and a limited number of concurrent project creation pipelines.</p><p>Note: The term “<em>decentralised</em>” might seem misleading. In this context, it refers to the possibility of adding projects to the perimeter from multiple sources, while other aspects of the perimeter, such as access levels, ingress or egress rules, remain centrally managed in a separate terraform state.</p><h4>Centralised Management</h4><p>Now we dive into the core of the discussion: centralised management of VPC Service Controls.</p><p>With centralised management, you define the entire VPC Service Controls configuration, including the service perimeter, the projects to be included, access levels, and ingress and egress rules, within a single Terraform configuration. This approach offers several advantages:</p><ol><li><strong>Simplified Management</strong>: Centralised management streamlines the administration of VPC Service Controls by consolidating all configurations into a single location.</li><li><strong>Improved Security</strong>: Centralised management eliminates the need to grant VPCSC permissions to every service account which is behind the project creation pipeline.</li><li><strong>Eliminates Conflicts:</strong> Centralised management eliminates the risk of collision conflicts that arise when multiple project creation pipelines attempt to modify the same VPCSC resource simultaneously.</li></ol><p>Simple, isn’t it?</p><pre>resource &quot;google_access_context_manager_service_perimeter&quot; &quot;service-perimeter&quot; {<br>  parent = &quot;accessPolicies/${google_access_context_manager_access_policy.access-policy.name}&quot; <br>  name = &quot;accessPolicies/${google_access_context_manager_access_policy.access-policy.name}/servicePerimeters/restrict_storage&quot; <br>  title = &quot;restrict_storage&quot; <br>  status { <br>    restricted_services = [&quot;storage.googleapis.com&quot;] <br>    resources = [<br>      &quot;projects/1234567890&quot;,<br>      &quot;projects/1234567891&quot;,<br>      &quot;projects/1234567892&quot;,<br>      &quot;projects/1234567893&quot;,<br>      # Put a new project here;)<br>    ]<br>  }<br>}</pre><p>In the <em>status.resources</em> we put all our project IDs, that’s where we add all our projects to the perimeter.</p><p>At that point you are probably concerned:</p><p><em>“</em><strong><em>Do you expect me to do a code change, pull request, code promotion and running pipeline whenever someone creates a new project? How do I know that someone created a new project? How do I know if they use </em></strong><a href="https://cloud.google.com/functions/docs/securing/using-vpc-service-controls#grant-build-access"><strong><em>CloudBuild, CloudFunctions or CloudRun</em></strong></a><strong><em>, so I need to put their CloudBuild SA into an access level? How do I know if someone creates an Org or Folder level Logging Sink, so I need to put the log sink SA into the access level? It’s not a scalable solution!”</em></strong> Please, hold on for a sec, all the answers below.</p><p><em>Note: CloudBuild SA and org/folder level logging sink SA should be added to an access level or ingress rule, since they operate from outside of the perimeter while target resources are inside.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*0c_1zaPVmeJDC0LO" /></figure><p><strong>Cloud Asset Inventory search with terraform</strong></p><p>I share your concerns about the manual effort involved in managing project attachment tasks. To address this challenge, we’ve developed an addition to the Google Terraform provider — the <a href="https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/cloud_asset_resources_search_all"><strong><em>google_cloud_asset_resources_search_all</em></strong></a> data source. This powerful tool enables you to filter and retrieve essential information about your cloud resources in <a href="https://cloud.google.com/asset-inventory/docs/reference/rest/v1p1beta1/resources/searchAll">one API call</a>, including:</p><ul><li>Google project IDs and numbers within an organization or folder</li><li>List projects utilising specific APIs</li><li>Subnets, VPCs, GCS buckets, VM instances, firewall rules, and eventually any resource supported by <a href="https://cloud.google.com/asset-inventory/docs/supported-asset-types">Cloud Asset Inventory</a></li></ul><p>Finally, let’s put it all together into a code example:</p><p><em>Note: User or service account would require Cloud Asset Viewer IAM role on the corresponding level in order to fetch the data. Cloud Asset Inventory API should be enabled on the project level where SA lives, or on the billing project when terraform run with a user identity.</em></p><p><strong>Getting all projects</strong></p><pre>// getting all active projects in an org or folder<br>data &quot;google_cloud_asset_resources_search_all&quot; &quot;projects&quot; {<br>  provider = google-beta<br>  scope    = &quot;organizations/0123456789&quot;<br>  // scope = &quot;folders/0123456789&quot;<br>  asset_types = [<br>    &quot;cloudresourcemanager.googleapis.com/Project&quot;<br>  ]<br>  query = &quot;state:ACTIVE&quot;<br>}<br>locals {<br>  projects = [<br>    for asset in data.google_cloud_asset_resources_search_all.projects.results : asset.project<br>  ]<br>}</pre><p><strong>Getting all Cloud Build service accounts</strong></p><pre>data &quot;google_cloud_asset_resources_search_all&quot; &quot;cb_api_enabled&quot; {<br>  provider = google-beta<br>  scope    = &quot;organizations/0123456789&quot;<br>  // scope = &quot;folders/0123456789&quot;<br>  asset_types = [<br>    &quot;serviceusage.googleapis.com/Service&quot;<br>  ]<br>  query = &quot;displayName=cloudbuild.googleapis.com AND state=ENABLED&quot;<br>}<br><br>locals {<br>  cb_project_numbers = [<br>    for asset in data.google_cloud_asset_resources_search_all.cb_api_enabled.results : trimprefix(asset.project, &quot;projects/&quot;)<br>  ]<br>  cb_sas = [<br>    for project_number in local.cb_project_numbers :  &quot;serviceAccount:${project_number}@cloudbuild.gserviceaccount.com&quot;<br>  ]<br>}</pre><p><strong>Getting all organization and folder level Logging Sink service accounts</strong></p><pre>data google_cloud_asset_resources_search_all logging_sinks {<br>  provider = google-beta<br>  scope    = &quot;organizations/0123456789&quot;<br>  asset_types = [<br>    &quot;logging.googleapis.com/LogSink&quot;<br>  ]<br>  query = &quot;NOT displayName:(_Default OR _Required) AND parentAssetType:(organization OR folder)&quot;<br>}<br><br>locals {<br>  log_sink_ids = toset([<br>     for item in data.google_cloud_asset_resources_search_all.logging_sinks.results:<br>         trimprefix(item.name, &quot;//logging.googleapis.com/&quot;)<br>  ])<br><br>  log_sink_sas = [for item in data.google_logging_sink.log-sinks: item.writer_identity]<br>}<br><br>data google_logging_sink &quot;log-sinks&quot; {<br>    for_each = local.log_sink_ids<br>    id = each.value<br>}</pre><p><strong>Define the perimeter and the access levels</strong></p><pre>resource &quot;google_access_context_manager_service_perimeter&quot; &quot;service-perimeter&quot; {<br>  parent = &quot;accessPolicies/${google_access_context_manager_access_policy.access-policy.name}&quot; <br>  name = &quot;accessPolicies/${google_access_context_manager_access_policy.access-policy.name}/servicePerimeters/restrict_storage&quot; <br>  title = &quot;restrict_storage&quot; <br>  status { <br>    restricted_services = [&quot;storage.googleapis.com&quot;] <br>    access_levels = [<br>      resource.google_access_context_manager_access_level.cb-sas-access-level.name,<br>      resource.google_access_context_manager_access_level.logsink-sas-access-level.name<br>    ]<br>    // All projects we fetched from Cloud Asset Inventory<br>    resources = local.projects<br>  }<br>  // .... any other aspects of the perimeter here ...<br>}<br><br>resource &quot;google_access_context_manager_access_level&quot; &quot;cb-sas-access-level&quot; {<br>  parent   = &quot;accessPolicies/${google_access_context_manager_access_policy.access-policy.name}&quot;<br>  name     = &quot;accessPolicies/${google_access_context_manager_access_policy.access-policy.name}/accessLevels/cb-sas-access-level&quot;<br>  title    = &quot;cb-sas-access-level&quot;<br>  basic {<br>    conditions {<br>      // All CB Service accounts we fetched from Cloud Asset Inventory<br>      members = locals.cb_sas<br>    }<br>  }<br>}<br><br>resource &quot;google_access_context_manager_access_level&quot; &quot;logsink-sas-access-level&quot; {<br>  parent   = &quot;accessPolicies/${google_access_context_manager_access_policy.access-policy.name}&quot;<br>  name     = &quot;accessPolicies/${google_access_context_manager_access_policy.access-policy.name}/accessLevels/logsink-sas-access-level&quot;<br>  title    = &quot;logsink-sas-access-level&quot;<br>  basic {<br>    conditions {<br>      // All LogSink (org and folder) writer identities we fetched from Cloud Asset Inventory<br>      members = locals.log_sink_sas<br>    }<br>  }<br>}</pre><p>The main drawback of the centralised approach is the fact that you have to run your central VPCSC terraform execution in order to get new projects included. Ideally such a pipeline should be set on schedule to run every 1h or even 10 minutes to keep adding new projects, service accounts to the perimeter.</p><p><strong>Choosing the Right Approach</strong></p><p>The best approach for your organization depends on your specific needs and requirements. If you have a large number of projects and lack control over project management, centralised VPCSC management is a good option. If you own project creation and have a limited number of concurrent project creation pipelines, decentralised VPCSC management may be a better choice.</p><p>Ultimately, the goal is to automate VPCSC management to reduce toil and human errors.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=272efeeb2598" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/centralised-management-of-vpcsc-with-terraform-and-cloud-asset-inventory-272efeeb2598">Centralised management of VPCSC with Terraform and Cloud Asset Inventory</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[Terraform Cloud/Enterprise and GCP Workload Identity Federation]]></title>
            <link>https://medium.com/google-cloud/terraform-cloud-enterprise-and-gcp-workload-identity-federation-fbb84a3dfbeb?source=rss-439726e92d3c------2</link>
            <guid isPermaLink="false">https://medium.com/p/fbb84a3dfbeb</guid>
            <category><![CDATA[gcp-security-operations]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[workload-identity]]></category>
            <category><![CDATA[oidc]]></category>
            <category><![CDATA[terraform-cloud]]></category>
            <dc:creator><![CDATA[Aleksandr Averbukh]]></dc:creator>
            <pubDate>Wed, 26 Oct 2022 13:41:32 GMT</pubDate>
            <atom:updated>2023-02-19T14:58:12.664Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Note: this article has been updated, and the initial article was based primarily on integration of GCP Workload Identity and </em><a href="https://developer.hashicorp.com/terraform/enterprise/releases/2022/v202208-1"><em>Terraform Cloud OpenID Connect (OIDC)</em></a><em> integration. Today there is a cleaner way called </em><a href="https://www.hashicorp.com/blog/terraform-cloud-adds-dynamic-provider-credentials-vault-official-cloud-providers"><em>Terraform Cloud Dynamic Provider Credentials</em></a><em>, which is the same under the hood, but more automated on TFC runner side.</em></p><p>If you come across this article you probably make use of <a href="https://cloud.hashicorp.com/products/terraform">Terraform Cloud</a> or <a href="https://developer.hashicorp.com/terraform/enterprise">Terraform Enterprise</a> self hosted instance. For a very long time the only way to leverage Terraform Cloud for Google Cloud deployments was to <a href="https://support.hashicorp.com/hc/en-us/articles/4406586874387-How-to-set-up-Google-Cloud-GCP-credentials-in-Terraform-Cloud">upload a Service Account Json Key</a>. There were more options for self hosted Terraform Enterprise instance while it is hosted on GCP itself, it could make use of a Service Account attached to the hosting Compute Engine instance and impersonate workflow specific Service Accounts. But even that is not ideal since there is no a proper control preventing workflows from impersonation of other service accounts intended to be used by other workflows.</p><p>So, what has changed? As a part of <a href="https://developer.hashicorp.com/terraform/enterprise/releases/2022/v202208-1">TFE Release v202208–1 (647)</a> OpenID Connect (OIDC) was introduced for Terraform Enterprise/Cloud. I got wondering if that would work with <a href="https://cloud.google.com/iam/docs/workload-identity-federation">GCP Workload Identity Federation</a> which natively supports OIDC as an external identity provider. To make a long story short, it does work!</p><p>To add more on that, Terraform Cloud has introduced <a href="https://developer.hashicorp.com/terraform/cloud-docs/workspaces/dynamic-provider-credentials?product_intent=terraform">Dynamic Provider Credentials</a>, which is a simplification layer for setting up OIDC integration with major cloud providers.</p><figure><img alt="GCP Workload Identity Federation for Terraform CLoud/Enterprise" src="https://cdn-images-1.medium.com/max/765/1*1YQypo8QcO2ybcBfdcjNuQ.jpeg" /></figure><h3>Configuration of GCP Workload Identity Federation for Terraform Cloud/Enterprise workflows</h3><p>To run through the example, start by cloning the <a href="https://github.com/GoogleCloudPlatform/cloud-foundation-fabric">repository</a> and changing to the example folder.</p><pre>git clone <a href="https://github.com/GoogleCloudPlatform/cloud-foundation-fabric.git">https://github.com/GoogleCloudPlatform/cloud-foundation-fabric.git</a><br>cd cloud-foundation-fabric/blueprints/cloud-operations/terraform-cloud-dynamic-credentials</pre><h4>Create Terraform Cloud Workflow</h4><p>If you don’t have an existing Terraform Cloud organisation you can sign up for a <a href="https://app.terraform.io/public/signup/account">free trial</a> account.</p><p>Create a new Workspace for a <a href="https://developer.hashicorp.com/terraform/cloud-docs/run/cli">CLI-driven</a> workflow. Workload Identity Federation works for any workflow type, but for simplicity of the blueprint we use CLI driven workflow). Note workspace name and id, we will use them on a later stage (your values will be different).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*tRgUbEsOnLkOCotlVy8G3w.png" /></figure><p>Go to the organisation settings and note the org name and id (your values will be different).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*I_Uj7TMhCW8BAc8OTcDeRA.png" /></figure><h4>Deploy GCP Workload Identity Pool Provider for Terraform Cloud integration</h4><p><em>NOTE:</em> <em>This is a preparation part and should be executed on behalf of a user with enough permissions.</em></p><p>Required permissions when new project is created:</p><ul><li>Project Creator on the parent GCP folder/organisation</li></ul><p>Required permissions when an existing project is used:</p><ul><li>Workload Identity Admin on the GCP project level</li><li>Project IAM Admin on the GCP project level</li></ul><p>Fill out required variables, use TFE Org and Workspace IDs from the previous steps (IDs are not the as names):</p><pre>cd gcp-workload-identity-provider<br>mv terraform.auto.tfvars.template terraform.auto.tfvars<br>vi terraform.auto.tfvars</pre><p>Authenticate using application default credentials, execute terraform code and deploy resources</p><pre>gcloud auth application-default login<br>terraform init<br>terraform apply</pre><p>As a result a set of outputs will be generated (your values will be different), note the output, we will use it on the next steps.</p><pre>project_id = &quot;tfc-dynamic-creds-gcp&quot;<br>tfc_workspace_wariables = {<br>  &quot;TFC_GCP_PROJECT_NUMBER&quot; = &quot;200635100209&quot;<br>  &quot;TFC_GCP_PROVIDER_AUTH&quot; = &quot;true&quot;<br>  &quot;TFC_GCP_RUN_SERVICE_ACCOUNT_EMAIL&quot; = &quot;sa-tfc@tfc-dynamic-creds-gcp.iam.gserviceaccount.com&quot;<br>  &quot;TFC_GCP_WORKLOAD_POOL_ID&quot; = &quot;tfc-pool&quot;<br>  &quot;TFC_GCP_WORKLOAD_PROVIDER_ID&quot; = &quot;tfc-provider&quot;<br>}</pre><p>As part of the deployment we have the following:</p><ul><li>GCP Workload Identity Pool and Provider.</li><li>Provider has an attribute condition to make sure only OIDC token generated in a specific TFC Organisation can be used.</li><li>Service Account for impersonation, where we only allow impersonation by a specific TFC workflow based the token attribute.</li><li>We grant the Service Account with Storage Admin permissions for the testing step.</li></ul><h4>Configure Dynamic Provider Credentials for your TFC Workflow</h4><p>To configure <a href="https://developer.hashicorp.com/terraform/cloud-docs/workspaces/dynamic-provider-credentials/gcp-configuration">GCP Dynamic Provider Credentials</a> for a TFC workflow, you need to set a set of environment variables:</p><ul><li>TFC_GCP_PROVIDER_AUTH</li><li>TFC_GCP_PROJECT_NUMBER</li><li>TFC_GCP_RUN_SERVICE_ACCOUNT_EMAIL</li><li>TFC_GCP_WORKLOAD_POOL_ID</li><li>TFC_GCP_WORKLOAD_PROVIDER_ID</li></ul><p>Go to the Workflow -&gt; Variables page and click the + Add variable button. For variable type select Environment variable. The variable names listed above are the names of the variables that you need to set. The values provided in the terraform output in the previous step are the values that you need to provide for each variable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pbQwtWflZ-GwqnRK_5Lnlg.png" /></figure><p>At that point we set up GCP Identity Federation to trust TFC generated OIDC tokens, workflow should be able to use Dynamic Provider Credentials to impersonate a GCP Service Account.</p><h4>Testing the result</h4><p>To test the setup, we will deploy a GCS bucket from the TFC Workflow created in the previous step. This will allow us to verify that the workflow can successfully interact with GCP services using the TFC Dynamic Provider Credentials.</p><p>Let’s jump into <em>tfc-workflow-using-wif </em>directory and configure terraform backend to point to our workspace, use TFE organisation name and workspace name (names are not the same as ids):</p><pre>cd ../tfc-workflow-using-wif<br>mv backend.tf.template backend.tf<br>vi backend.tf</pre><p>Fill out variables based on the output from the preparation steps:</p><pre>mv terraform.auto.tfvars.template terraform.auto.tfvars<br>vi terraform.auto.tfvars</pre><h4>Authenticate terraform for triggering CLI-driven workflow</h4><p>Follow this <a href="https://learn.hashicorp.com/tutorials/terraform/cloud-login">documentation</a> to login ti terraform cloud from the CLI and trigger the workflow execution:</p><pre>terraform login<br>terraform init<br>terraform apply</pre><p>As a result, we have successfully deployed a GCS bucket from Terraform Cloud workflow using Workload Identity Federation.</p><p>Once done testing, you can clean up resources by running <em>terraform destroy</em> first in the <em>tfc-workflow-using-wif</em> and then <em>gcp-workload-identity-provider</em> folders.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fbb84a3dfbeb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/terraform-cloud-enterprise-and-gcp-workload-identity-federation-fbb84a3dfbeb">Terraform Cloud/Enterprise and GCP Workload Identity Federation</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[Upload public keys for GCP service accounts with terraform]]></title>
            <link>https://medium.com/google-cloud/upload-public-keys-for-gcp-service-accounts-with-terraform-e1c87fca816e?source=rss-439726e92d3c------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1c87fca816e</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[service-account-key]]></category>
            <category><![CDATA[gcp-app-dev]]></category>
            <category><![CDATA[terraform]]></category>
            <dc:creator><![CDATA[Aleksandr Averbukh]]></dc:creator>
            <pubDate>Tue, 28 Jun 2022 08:15:13 GMT</pubDate>
            <atom:updated>2022-06-28T10:36:25.196Z</atom:updated>
            <cc:license>https://creativecommons.org/publicdomain/mark/1.0/</cc:license>
            <content:encoded><![CDATA[<p>Hope you already came across a great article by <a href="https://medium.com/u/5a967609ab69">Ryan Canty</a> <a href="https://jryancanty.medium.com/stop-downloading-google-cloud-service-account-keys-1811d44a97d9">Stop Downloading Google Cloud Service Account Keys</a>, today there are a plenty of <a href="https://cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys#alternatives">alternatives to creating service account keys</a>.</p><p>But imagine you are at an early cloud adoption stage, you are not yet ready to involve OIDC providers and <a href="https://cloud.google.com/iam/docs/workload-identity-federation#pools">Workload Identity Pools</a>. The same time on-prem applications/servers require a way to authenticate against Google Cloud APIs. The most insecure way to go about it is to allow engineers to generate service account keys which may lead to these keys flying around from one user to another via internal messengers or mailbox. A slightly better way is to centralize key generation and allow key provisioning only via GitOps + Terraform, so you have a clean history of “integration” service account management. But this approach still has a few caveats:</p><ul><li>key has no expiration time</li><li>key material is written in the terraform state</li><li>key needs to be transferred to the end application/server</li><li>everyone with access to the terraform pipeline has access to the key</li></ul><p>There is a better way which can handle all the above!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rmnwrE8VNXgrdi3_qV8U0Q.png" /></figure><h4>Managing service account keys by uploading public RSA keys via terraform</h4><p>This example shows how to manage IAM Service Account Keys by manually generating a key pair with <a href="https://www.openssl.org/">OpenSSL</a> and uploading the public part of the key to GCP. It has the following benefits:</p><ul><li>no <a href="https://cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys#pass-between-users">passing keys between users</a> or systems</li><li>no private keys stored in the terraform state (only public part of the key is in the state)</li><li>let keys <a href="https://cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys#key-expiryhaving">expire automatically</a></li></ul><h4>Deployment</h4><p>To run the example, start by cloning the <a href="https://github.com/terraform-google-modules/cloud-foundation-fabric">repository</a> and changing to the example folder. If you prefer using Cloud Shell, <a href="https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fcloud-foundation-fabric&amp;cloudshell_print=cloud-shell-readme.txt&amp;cloudshell_working_dir=examples%2Fcloud-operations%2Fonprem-sa-key-management&amp;cloudshell_open_in_editor=cloudshell_open%2Fcloud-foundation-fabric%2Fexamples%2Fcloud-operations%2Fonprem-sa-key-management%2Fvariables.tf">this link</a> will run the Git clone for you, and switch to the correct folder so that you can skip the initial step below.</p><pre>git clone <a href="https://github.com/GoogleCloudPlatform/cloud-foundation-fabric.git">https://github.com/GoogleCloudPlatform/cloud-foundation-fabric.git</a><br>cd <a href="https://github.com/GoogleCloudPlatform/cloud-foundation-fabric.git">cloud-foundation-fabric</a>/examples/cloud-operations/<a href="https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/tree/master/examples/cloud-operations/onprem-sa-key-management">onprem-sa-key-management</a></pre><p>Cleaning up example keys.</p><pre>rm -f /public-keys/data-uploader/<br>rm -f /public-keys/prisma-security/</pre><p>Generate new keys for the service accounts.</p><pre>mkdir keys &amp;&amp; cd keys<br>openssl req -x509 -nodes -newkey rsa:2048 -days 30 \<br>    -keyout data_uploader_private_key.pem \<br>    -out ../public-keys/data-uploader/public_key.pem \<br>    -subj &quot;/CN=unused&quot;<br>openssl req -x509 -nodes -newkey rsa:2048 -days 30 \<br>    -keyout prisma_security_private_key.pem \<br>    -out ../public-keys/prisma-security/public_key.pem \<br>    -subj &quot;/CN=unused&quot;</pre><p>Deploy service accounts and keys.</p><p><em>Note: We only use the public part of the keys for deployment, ideally private part of the key should never leave the server where it’s generated and going to be consumed.</em></p><pre>cd ..<br>terraform init<br>terraform apply -var project_id=$GOOGLE_CLOUD_PROJECT</pre><p>Extract JSON credentials templates from terraform output and put the private part of the keys into the templates.</p><pre>terraform show -json | jq &#39;.values.outputs.&quot;sa-credentials&quot;.value.&quot;data-uploader&quot;.&quot;public_key.pem&quot; | fromjson&#39; &gt; data-uploader.json<br>terraform show -json | jq &#39;.values.outputs.&quot;sa-credentials&quot;.value.&quot;prisma-security&quot;.&quot;public_key.pem&quot; | fromjson&#39; &gt; prisma-security.json<br><br>contents=$(jq --arg key &quot;$(cat keys/data_uploader_private_key.pem)&quot; &#39;.private_key=$key&#39; data-uploader.json) &amp;&amp; echo &quot;$contents&quot; &gt; data-uploader.json<br>contents=$(jq --arg key &quot;$(cat keys/prisma_security_private_key.pem)&quot; &#39;.private_key=$key&#39; prisma-security.json) &amp;&amp; echo &quot;$contents&quot; &gt; prisma-security.json</pre><h4>Testing</h4><p>Validate that service accounts json credentials are valid.</p><pre>gcloud auth activate-service-account --key-file prisma-security.json<br>gcloud auth activate-service-account --key-file data-uploader.json</pre><p>Cleaning up.</p><pre>terraform destroy -var project_id=$GOOGLE_CLOUD_PROJECT</pre><p>It’s always better to avoid service account keys, but in real life scenarios we are still forced to rely on them. With the approach shown in the article we mitigate a big part of security issues related to service account key management, such as:</p><ul><li>keys are generated and stored on the consumer server and never leave it</li><li>keys have expiration time set by user</li><li>private part of the key is not stored in the terraform state or code</li><li>GitOps and terraform provides a central plane of glass on the “integration” key management</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1c87fca816e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/upload-public-keys-for-gcp-service-accounts-with-terraform-e1c87fca816e">Upload public keys for GCP service accounts with terraform</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>