<?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 Bastien Cadiot on Medium]]></title>
        <description><![CDATA[Stories by Bastien Cadiot on Medium]]></description>
        <link>https://medium.com/@bastien.cadiot?source=rss-f487b4fa978------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lWzb-kvuwf-2qd0J-MfNFQ.jpeg</url>
            <title>Stories by Bastien Cadiot on Medium</title>
            <link>https://medium.com/@bastien.cadiot?source=rss-f487b4fa978------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 16:15:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bastien.cadiot/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[How to create a DMZ with VPC Service Controls on Google Cloud Platform]]></title>
            <link>https://medium.com/@bastien.cadiot/how-to-create-a-dmz-with-vpc-service-controls-on-google-cloud-platform-985c06d9472e?source=rss-f487b4fa978------2</link>
            <guid isPermaLink="false">https://medium.com/p/985c06d9472e</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[cloud-storage]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[data-security]]></category>
            <category><![CDATA[gcp]]></category>
            <dc:creator><![CDATA[Bastien Cadiot]]></dc:creator>
            <pubDate>Sun, 14 Jun 2020 21:27:49 GMT</pubDate>
            <atom:updated>2020-06-14T21:27:49.944Z</atom:updated>
            <content:encoded><![CDATA[<p>When you deal with data on GCP, you usually protect them by using a combination of IAM policies, VPC firewall rules, and checks over audit logs. But few people knows that a product called <a href="https://cloud.google.com/vpc-service-controls">VPC Service Control</a> (VPC SC) can help you to prevent unauthorized access and data exfiltration using GCP public APIs.</p><p>VPC SC is built with 2 components:</p><ul><li>Service Perimeters: where you define protected projects, and APIs</li><li>Access Levels: managed by Access Context Manager, where you define who can access from outside and how</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*8JOhOcEc_fkzEHqFhig94w.png" /></figure><h3>The problem</h3><p>This mechanism is very powerful and allow you to create a virtual firewall around your projects and APIs. But sometimes this system is too rigid and you can be tempted to deactivate it, or locate your data outside, especially when you are in one of these situations:</p><ul><li>You want to use a native capability of a Google backend which is impossible with a service perimeter. For example, if you want to make a GCS copy between a bucket outside and one inside. With a service perimeter you would need to read the data from the source and write it into the destination bucket. Any use of a GCS copy is impossible because Google don’t use a service account in the backend to make this operation. Moreover it is impossible to create an access level for this particular use case.</li><li>You want to push data from inside to outside. For example you want to export the data from a bucket protected to an unprotected one to allow external people to read it. It’s impossible by design because the Service Perimeter is here to prevent data exfiltration. You cannot create an access level to allow it because this mechanism can only be used to authorize incoming request.</li><li>Inside the perimeter you want to read data located outside. For example you want to read an unprotected BigQuery dataset shared with you by a project outside the perimeter. The request will be blocked because you cannot access unprotected data.</li></ul><p>For all of these reasons (and others), most people find it painful to use service control and prefer to get rid of it. <strong>Please don’t</strong>, it’s possible to create an intermediate zone allowing you to safely exchange data with the outside world.</p><h3>Use virtual bridges</h3><p>This method is not documented but try to take advantage of the service control and its capabilities.</p><p>One option when you need to share data between two perimeters is to create a bridge between them. Then you can exchange data within the targeted projects.</p><p>This really interesting, but how this can help us to share data with the outside world? Because the same problem will occur on this other perimeter. You’re right… except that nothing force us to protect the same APIs on this new perimeter, or to protect any APIs!</p><p>Here is the trick, let’s take an example with GCS. The problem is:</p><blockquote><em>You cannot share data using a bucket in the project X because the API is protected (storage.googleapis.com) by the perimeter A</em></blockquote><p>Now the solution is:</p><ul><li>You create a Perimeter B protecting a project Y but without protecting any APIs</li><li>You create a bridge between projects X and Y</li><li>You can then exchange data between X and Y. Others member of the perimeter cannot share their data.</li><li>Because the api storage.googleapis.com isn’t protected in the perimeter B you can exchange the data with the outside world from project Y</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/511/1*9HQJPZ0XVq_FC9fzSlaX2g.png" /></figure><p>Let’s write some code to make it more clear. The code is written using <a href="https://terraform.io/">Terraform</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b136244d9afd0406ce5d85e5e5a49b1/href">https://medium.com/media/7b136244d9afd0406ce5d85e5e5a49b1/href</a></iframe><p>That’s it! With this setup you are able to share data and solve all the cases seen at the beginning of this article.</p><h3>Last (security) notes</h3><p>As you understand creating such a mechanism isn’t innocent, you can create a hole inside the security provided by the Service Control. Because this system allow to export data, keep it in mind when you allow people to use this airlock:</p><ul><li>Consider the two projects involved as highly sensitive, they become a new gatekeeper to your data</li><li>Ideally grant only automated system to push/pull into these projects (for example a prepared Dataflow job or a Composer DAG)</li><li>Data exchange <strong>need</strong> to be automated between the two projects (for example a GCS notification triggering a function performing the copy)</li><li>No humans access in these projects to prevent arbitrary data read</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=985c06d9472e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Connect GCP and AWS with Cloud VPN HA]]></title>
            <link>https://medium.com/wescale/connect-gcp-and-aws-with-cloud-vpn-ha-12e9128b9db4?source=rss-f487b4fa978------2</link>
            <guid isPermaLink="false">https://medium.com/p/12e9128b9db4</guid>
            <category><![CDATA[high-availability]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[vpn]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Bastien Cadiot]]></dc:creator>
            <pubDate>Thu, 05 Sep 2019 12:40:02 GMT</pubDate>
            <atom:updated>2019-09-05T12:40:02.019Z</atom:updated>
            <content:encoded><![CDATA[<p>The last Cloud Next was rich in announcements, many new features and products. We will focus on one specific thing in this article: <a href="https://cloud.google.com/blog/topics/inside-google-cloud/100-plus-announcements-from-google-cloud-next19">Cloud VPN is available in HA with a 99,99% SLA coverage</a>. This is the right moment to update my previous blogpost on “<a href="https://blog.wescale.fr/2017/07/31/saga-de-lete-e01-construction-dune-infrastructure-multi-cloud/">How to build a multi-cloud Infrastructure</a>” by converting it to Terraform 0.12+ version and implementing VPN in HA!</p><h3>Prepare the environment</h3><p>We need to prepare the environment, be sure to have:</p><ul><li>A way to authenticate to GCP (usually with GCloud CLI with a<a href="https://www.terraform.io/docs/providers/google/provider_reference.html"> keyfile</a> or<a href="https://medium.com/wescale/how-to-generate-and-use-temporary-credentials-on-google-cloud-platform-b425ef95a00d"> impersonation</a>)</li><li>A way to authenticate to AWS (with AWS CLI and a pair of<a href="https://www.terraform.io/docs/providers/aws/index.html"> Access Key and Secret Key</a>)</li><li><a href="https://www.terraform.io/downloads.html">Terraform 0.12+</a></li></ul><h3>The Goal</h3><p>The overall objective is to deploy two VPCs on AWS and GCP, and connect them through a<a href="https://cloud.google.com/vpn/docs/concepts/topologies"> managed highly available VPN</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/957/1*yBFVzW6LyClH8cJizEIahw.png" /></figure><p>As you can see in the above diagram, deploying two tunnels is not sufficient, because HA on AWS is done through the redundancy of the Customer Gateway, and each GW provides two tunnels. So, to accomplish HA we need to deploy 4 tunnels to connect AWS and GCP.</p><h3>Deploy Networks and VPCs</h3><p>First of all, we need to create a VPC on each cloud provider, you can skip this step if you want to reuse your existing VPC.</p><p>This part isn’t complicated on its own, so I suggest you to get inspiration from the official Terraform documentation. You will have to create a<a href="https://www.terraform.io/docs/providers/google/r/compute_subnetwork.html"> GCP Network with at least one subnetwork</a> and an<a href="https://www.terraform.io/docs/providers/aws/r/vpc.html"> AWS VPC</a> with their<a href="https://www.terraform.io/docs/providers/aws/r/subnet.html"> subnetworks</a>. Both must be deployed in the same region as your VPN connections.</p><p>Additionally, we have to create a Cloud Router on the GCP side. This router will be connected to your global GCP route table and will manage BGP sessions for all of your VPN tunnels.</p><pre>resource &quot;google_compute_router&quot; &quot;main&quot; {<br>  name    = &quot;router-main&quot;<br>  network = google_compute_network.main.self_link</pre><pre>bgp {<br>    asn = 65273 # you can choose any number in the private range<br>  }<br>}</pre><p>For the ASN (Autonomous System Number) you can choose any number in the private range 64512 to 65534 and 4200000000 to 4294967294.</p><p>On AWS there isn’t a Cloud Router, but you have to ensure that your route table will propagate the routing informations received from BGP sessions.</p><pre>resource &quot;aws_route_table&quot; &quot;main&quot; {<br>  vpc_id = aws_vpc.main.id</pre><pre>propagating_vgws = [aws_vpn_gateway.vpn_gateway.id]<br>}</pre><pre>resource &quot;aws_route_table_association&quot; &quot;main&quot; {<br>  count = 3</pre><pre>subnet_id = element(aws_subnet.main.*.id, count.index)<br>  route_table_id = aws_route_table.main.id<br>}</pre><p>With that code, we have activated the propagation from the VPN gateway, and have associated the route table with our subnets.</p><h3>Deployment of the VPN</h3><p>This is the most interesting and complicated part, so I decided to split it in two parts: AWS and GCP.</p><h4>VPN on AWS</h4><p>I decided to begin with AWS because this the easiest part. We do not connect to anything, we just expose the VPN entrypoints and wait until the other side connects.</p><p>We start by creating three resources: the VPN gateway itself (remember this component will be connected to your VPC), and two customer gateways for the high availability.</p><pre>resource &quot;aws_vpn_gateway&quot; &quot;vpn_gateway&quot; {<br>  vpc_id = aws_vpc.main.id<br>}</pre><pre>resource &quot;aws_customer_gateway&quot; &quot;customer_gateway_1&quot; {<br>  bgp_asn    = google_compute_router.main.bgp[0].asn<br>  ip_address = google_compute_ha_vpn_gateway.target_gateway.vpn_interfaces[0].ip_address<br>  type       = &quot;ipsec.1&quot;<br>}</pre><pre>resource &quot;aws_customer_gateway&quot; &quot;customer_gateway_2&quot; {<br>  bgp_asn    = google_compute_router.main.bgp[0].asn<br>  ip_address = google_compute_ha_vpn_gateway.target_gateway.vpn_interfaces[1].ip_address<br>  type       = &quot;ipsec.1&quot;<br>}</pre><p>You can note that we are referencing a resource not yet declared. Do not worry, Terraform graph will handle this perfectly and will avoid the cyclic dependency.</p><p>The BGP number on Google side is read from the Cloud Router resource. It would be possible to set the BGP for the AWS side on the VPN gateway resource, but we decided to let the default value (unlike Google’s side which do not provide a default).</p><p>Then we create a VPN connection attached to each customer gateway. This setup will provide 4 tunnels because each VPN connection comes with two.</p><pre>resource &quot;aws_vpn_connection&quot; &quot;cx_1&quot; {<br>  vpn_gateway_id      = aws_vpn_gateway.vpn_gateway.id<br>  customer_gateway_id = aws_customer_gateway.customer_gateway_1.id<br>  type                = &quot;ipsec.1&quot;<br>}</pre><pre>resource &quot;aws_vpn_connection&quot; &quot;cx_2&quot; {<br>  vpn_gateway_id      = aws_vpn_gateway.vpn_gateway.id<br>  customer_gateway_id = aws_customer_gateway.customer_gateway_2.id<br>  type                = &quot;ipsec.1&quot;<br>}</pre><h4>VPN on GCP</h4><p>This part is slightly more complicated, we have to create the following resources:</p><ul><li>1 HA VPN Gateway</li><li>1 External VPN Gateway</li><li>4 VPN Tunnels</li><li>4 VPN Interface on Cloud Router</li><li>4 Peers on Cloud Router</li></ul><p>The gateways are a new kind of resources on GCP and are still in Beta (for the moment).</p><pre>resource &quot;google_compute_ha_vpn_gateway&quot; &quot;target_gateway&quot; {<br>  provider = &quot;google-beta&quot;<br>  name     = &quot;vpn-aws&quot;<br>  network  = google_compute_network.main.self_link<br>}</pre><pre>resource &quot;google_compute_external_vpn_gateway&quot; &quot;aws_gateway&quot; {<br>  provider        = &quot;google-beta&quot;<br>  name            = &quot;aws-gateway&quot;<br>  redundancy_type = &quot;FOUR_IPS_REDUNDANCY&quot;<br>  description     = &quot;VPN gateway on AWS side&quot;</pre><pre>interface {<br>    id         = 0<br>    ip_address = aws_vpn_connection.cx_1.tunnel1_address<br>  }</pre><pre>interface {<br>    id         = 1<br>    ip_address = aws_vpn_connection.cx_1.tunnel2_address<br>  }</pre><pre>interface {<br>    id         = 2<br>    ip_address = aws_vpn_connection.cx_2.tunnel1_address<br>  }</pre><pre>interface {<br>    id         = 3<br>    ip_address = aws_vpn_connection.cx_2.tunnel2_address<br>  }<br>}</pre><p>As we said, we chose to create 4 interfaces to connect to AWS. You can see that the first 2 are connected to the first AWS VPN connection, and the others 2 are connected to the second VPN connection. Remember this order, otherwise the link will not work and/or the BGP sessions will never establish.</p><p>And now the redundant part, for more simplicity I will paste here only the first and the last resource, you will have to adapt (or simply paste the complete code from my Github repo linked at the end of this post).</p><pre>resource &quot;google_compute_vpn_tunnel&quot; &quot;main-1&quot; {<br>  provider                        = &quot;google-beta&quot;<br>  name                            = &quot;vpn-tunnel-1&quot;<br>  vpn_gateway                     = google_compute_ha_vpn_gateway.target_gateway.self_link<br>  shared_secret                   = aws_vpn_connection.cx_1.tunnel1_preshared_key<br>  peer_external_gateway           = google_compute_external_vpn_gateway.aws_gateway.self_link<br>  peer_external_gateway_interface = 0<br>  router                          = google_compute_router.main.name<br>  ike_version                     = 2<br>  vpn_gateway_interface           = 0<br>}</pre><pre>[...]</pre><pre>resource &quot;google_compute_vpn_tunnel&quot; &quot;main-4&quot; {<br>  provider                        = &quot;google-beta&quot;<br>  name                            = &quot;vpn-tunnel-4&quot;<br>  vpn_gateway                     = google_compute_ha_vpn_gateway.target_gateway.self_link<br>  shared_secret                   = aws_vpn_connection.cx_2.tunnel2_preshared_key<br>  peer_external_gateway           = google_compute_external_vpn_gateway.aws_gateway.self_link<br>  peer_external_gateway_interface = 3<br>  router                          = google_compute_router.main.name<br>  ike_version                     = 2<br>  vpn_gateway_interface           = 1<br>}</pre><p>You can note that I have only changed:</p><ul><li>the resource name (obviously)</li><li>the shared secret</li><li>the peer external gateway (from 0 to 3, remember the order on the GCP external gateway)</li><li>the VPN Gateway interface (from 0 to 1, corresponding on the AWS VPN Connection resource)</li></ul><p>Then let’s create the router interface for all of these tunnels.</p><pre>resource &quot;google_compute_router_interface&quot; &quot;main-1&quot; {<br>  name       = &quot;interface-1&quot;<br>  router     = google_compute_router.main.name<br>  ip_range   = &quot;${aws_vpn_connection.cx_1.tunnel1_cgw_inside_address}/30&quot;<br>  vpn_tunnel = google_compute_vpn_tunnel.main-1.name<br>}</pre><pre>[...]</pre><pre>resource &quot;google_compute_router_interface&quot; &quot;main-4&quot; {<br>  name       = &quot;interface-4&quot;<br>  router     = google_compute_router.main.name<br>  ip_range   = &quot;${aws_vpn_connection.cx_2.tunnel2_cgw_inside_address}/30&quot;<br>  vpn_tunnel = google_compute_vpn_tunnel.main-4.name<br>}</pre><p>Here again, I have only pasted two interfaces. The things that will vary are :</p><ul><li>the name (what a surprise)</li><li>the IP range used (given by the AWS tunnel)</li><li>the tunnel itself</li></ul><p>And to finish, do not forget to configure BGP for each interface with the peer configuration.</p><pre>resource &quot;google_compute_router_peer&quot; &quot;main-1&quot; {<br>  name                      = &quot;peer-1&quot;<br>  router                    = google_compute_router.main.name<br>  peer_ip_address           = aws_vpn_connection.cx_1.tunnel1_vgw_inside_address<br>  peer_asn                  = aws_vpn_connection.cx_1.tunnel1_bgp_asn<br>  advertised_route_priority = 100<br>  interface                 = google_compute_router_interface.main-1.name<br>}</pre><pre>[...]</pre><pre>resource &quot;google_compute_router_peer&quot; &quot;main-4&quot; {<br>  name                      = &quot;peer-4&quot;<br>  router                    = google_compute_router.main.name<br>  peer_ip_address           = aws_vpn_connection.cx_2.tunnel2_vgw_inside_address<br>  peer_asn                  = aws_vpn_connection.cx_2.tunnel2_bgp_asn<br>  advertised_route_priority = 100<br>  interface                 = google_compute_router_interface.main-4.name<br>}</pre><p>Some parts have changed also:</p><ul><li>the name</li><li>the peer IP address (AWS side)</li><li>the peer ASN</li><li>the interface on Cloud Router</li></ul><p>We do not need to change the priority because we want to share traffic equally between all tunnels.</p><p>And that’s it, with this you can securely connect AWS and GCP with an HA VPN connection.</p><pre>terraform init<br>terraform apply<br>---</pre><pre>An execution plan has been generated and is shown below.<br>Resource actions are indicated with the following symbols:<br>  + create</pre><pre>Terraform will perform the following actions:<br>[...]<br>Plan: 30 to add, 0 to change, 0 to destroy.</pre><h3>Review our setup</h3><p>After a couple of minutes from applying, everything should be up and looks like this:</p><p>Let’s start by checking our tunnels states on GCP and AWS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1QlPAQcGCIHtTCFalpoebQ.png" /><figcaption>GCP VPN Tunnels</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aUZ9m6OsXTizFvfJ3U_biA.png" /><figcaption>AWS VPN Tunnels</figcaption></figure><p>Well, everything seems ok, all the tunnels are up (I did not take a screenshot of the second customer gateway on AWS, but it works too).</p><p>Now let’s take a look on the GCP Cloud Router.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oAeoiwLaqiAtnV5rQVb93Q.png" /><figcaption>GCP Cloud Router</figcaption></figure><p>All BGP sessions are connected, so the routes exchange should be fine. We cannot see this part on AWS.</p><p>The last part is to ensure that routes are propagated on the routes tables directly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xgq-boOMIf4c4xQfDSANkA.png" /><figcaption>GCP Route Table</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HmQeCoISZiOt7Hj2ZGWpbg.png" /><figcaption>AWS Route Table</figcaption></figure><p>Everything looks good! The AWS subnet is propagated to the GCP route table, and the GCP network is propagated to AWS.</p><h3>To finish</h3><p>If you want to test this setup on your own, and do not want to copy/paste the above code, take a look at my GitHub repo implementing this:<a href="https://github.com/bcadiot/multi-cloud"> https://github.com/bcadiot/multi-cloud</a>.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=12e9128b9db4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wescale/connect-gcp-and-aws-with-cloud-vpn-ha-12e9128b9db4">Connect GCP and AWS with Cloud VPN HA</a> was originally published in <a href="https://medium.com/wescale">WeScale</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to generate and use temporary credentials on Google Cloud Platform]]></title>
            <link>https://medium.com/wescale/how-to-generate-and-use-temporary-credentials-on-google-cloud-platform-b425ef95a00d?source=rss-f487b4fa978------2</link>
            <guid isPermaLink="false">https://medium.com/p/b425ef95a00d</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[cloud]]></category>
            <dc:creator><![CDATA[Bastien Cadiot]]></dc:creator>
            <pubDate>Fri, 26 Apr 2019 08:58:03 GMT</pubDate>
            <atom:updated>2019-04-26T09:26:58.604Z</atom:updated>
            <content:encoded><![CDATA[<p>With the 2.4 version of the GCP Terraform provider, a new feature is shipped allowing to generate short lived credentials. These credentials are based on the <a href="https://tools.ietf.org/html/draft-ietf-oauth-token-exchange-16">Oauth2 token exchange</a> mechanism to implement impersonation between accounts.<br>You can also use this feature with the —-impersonate-service-account flag in the CLI gcloud.</p><p>This feature is very similar of the AWS AssumeRole allowing users to obtain temporary new permissions onto specific resources.</p><p>Let’s take a look at this feature!</p><h3>Why using such mechanism?</h3><p>This is great news because authenticating Terraform against GCP in a complex environment can be painful, and even more so when you want to manage the lifecycle of keys and users.</p><p>The two main ways to authenticate with gcloud are using personal access with oauth2 login or using a service account with its own key. The first may not be ideal because you have to grant individual (or group) permissions to the resources, which can be hard to track along time. Furthermore, not all APIs are working with individual credentials. The second way is more suitable to be used in automation but the credentials are long lived, and you have to share them with the members of your team who need to use automation tools.</p><p>To mitigate this issue, some people use a central tool like <a href="https://www.hashicorp.com/products/terraform/enterprise">Terraform Enterprise</a> or <a href="https://www.runatlantis.io/">Atlantis</a>. These tools handle your service account key and create another level of authorization for your users.</p><p>With this new mechanism, we can imagine new security models:</p><ul><li>We can limit write access to a service account and grant read only access to users</li><li>We can grant impersonate access from users and/or group to a central service account</li><li>We can create many service accounts in projects and decide to allow different impersonation on each</li></ul><p>For example you can see below an example of integration with a central service account.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*4OzFM68MAc5hyG3IO1PjSQ.png" /></figure><h3>How does this work?</h3><p>The feature implements the token exchange described in the <a href="https://cloud.google.com/iam/docs/creating-short-lived-service-account-credentials">GCP Documentation for short lived credentials</a>.</p><p>As a prerequisite, we need to create the service account to be impersonated:</p><ul><li>Create the service account</li><li>Give permissions to the service account on desired resources</li><li>Allow users (or other SA) to issue access tokens on behalf of this service account</li></ul><p>Then, you can emit impersonated credentials by following these steps:</p><ul><li>Authenticate yourself with your own account or with another service account</li><li>Request a token for the service account</li><li>Use this token to authenticate on GCP</li></ul><p>This new token will be linked to the service account and can use all of its permissions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*S6HoM4d4PJylJOHLoP-d3g.png" /></figure><h3>Granting permissions to a service account</h3><p>As described before, we need some prerequisites to enable impersonation, the first is standard: we need to create a service account and grant it some permissions.</p><p>You can do it either in the gcloud console, or via Terraform. This is a sample using Terraform code:</p><pre>resource &quot;google_service_account&quot; &quot;admin_sa&quot; {<br> account_id = &quot;admin-impersonated&quot;<br> display_name = &quot;admin impersonated&quot;<br>}</pre><pre>resource &quot;google_project_iam_member&quot; &quot;editor&quot; {<br> role = &quot;roles/editor&quot;<br> member = &quot;serviceAccount:${google_service_account.admin_sa.email}&quot;<br>}</pre><p>Then, the second step is to grant permissions to your members to create access token:</p><pre>resource &quot;google_service_account_iam_member&quot; &quot;admin-account-iam&quot; {<br> service_account_id = &quot;${google_service_account.admin_sa.name}&quot;<br> role = &quot;roles/iam.serviceAccountTokenCreator&quot;<br> member = &quot;user:${var.user_email}&quot;<br>}</pre><p>Now the user targeted can issue a token on behalf of the service account.</p><p>On the other hand, you may want to use gcloud commands to set up this base permissions. Here are the commands to do this task:</p><pre>PROJECT=my-project-id<br><a href="/service/mailto:USER=my-user@email.com">USER=my-user@email.com</a></pre><pre>gcloud iam service-accounts create admin-impersonated<br>gcloud projects add-iam-policy-binding ${PROJECT} --member serviceAccount:admin-impersonated@${PROJECT}.iam.gserviceaccount.com --role roles/editor<br>gcloud iam service-accounts add-iam-policy-binding admin-impersonated@${PROJECT}.iam.gserviceaccount.com --member user:${USER} --role roles/iam.serviceAccountTokenCreator</pre><h3>Impersonating the service account to create a temporary access token</h3><p>From now on, we can issue a token and use it to interact with GCP. To do that, we just have to call the API endpoint to generate tokens <a href="https://cloud.google.com/iam/credentials/reference/rest/v1/projects.serviceAccounts/generateAccessToken%60">https://cloud.google.com/iam/credentials/reference/rest/v1/projects.serviceAccounts/generateAccessToken</a>. This endpoint supports some args: we can limit the scopes, or restrict the lifetime of the token. After the call, the response will contain the access token that we are looking for.</p><p>Here is a sample of code using Terraform. You will notice that we use two providers, one using the default authentication, and the other using the newly impersonated access token.</p><pre>provider &quot;google&quot; {<br> scopes = [<br> &quot;<a href="https://www.googleapis.com/auth/cloud-platform">https://www.googleapis.com/auth/cloud-platform</a>&quot;,<br> &quot;<a href="https://www.googleapis.com/auth/userinfo.email">https://www.googleapis.com/auth/userinfo.email</a>&quot;,<br> ]<br>}</pre><pre>data &quot;google_service_account_access_token&quot; &quot;default&quot; {<br> target_service_account = &quot;${google_service_account.admin_sa.email}&quot;</pre><pre>scopes = [<br> &quot;userinfo-email&quot;,<br> &quot;cloud-platform&quot;,<br> ]</pre><pre>lifetime = &quot;300s&quot;<br>}</pre><p>In the code above we are setting the default provider, and calling the API endpoint to request an access token.</p><pre>provider &quot;google&quot; {<br> version = &quot;~&gt; 2.4&quot;<br> alias = &quot;impersonated&quot;<br> access_token = &quot;${data.google_service_account_access_token.default.access_token}&quot;</pre><pre>scopes = [<br> &quot;<a href="https://www.googleapis.com/auth/cloud-platform">https://www.googleapis.com/auth/cloud-platform</a>&quot;,<br> &quot;<a href="https://www.googleapis.com/auth/userinfo.email">https://www.googleapis.com/auth/userinfo.email</a>&quot;,<br> ]<br>}</pre><pre>resource &quot;google_compute_network&quot; &quot;vpc_network&quot; {<br> provider = &quot;google.impersonated&quot;<br> name = &quot;test-net&quot;<br>}</pre><p>That’s it, the mechanism is very simple, you can easily implement it in your workflows. Note that the Terraform datasource will generate a new access token at each run, even if the previous is not expired yet.</p><p>Then, in this second block, we are defining another provider using the access token, and a dummy resource (a VPC) created with this new provider.</p><p>You can also use the gcloud command to do the same thing. Since version 240.0.0 (2019–03–26), the global flag —-impersonate-service-account is added into gcloud.</p><pre>gcloud --<a href="/service/mailto:impersonate-service-account=admin-impersonated@my-project-id.iam.gserviceaccount.com">impersonate-service-account=admin-impersonated@my-project-id.iam.gserviceaccount.com</a> compute networks create test-net</pre><h3>Be careful</h3><p>Even if this method is much more secure than having the same key used by everyone, you have to know that <strong>these generated credentials are non revocable</strong> and last for 3600 seconds by default (unless you change their lifetime). It means that if someone steals your temporary token, you won’t be able to mitigate the leak without deleting the service account itself!</p><h3>To finish</h3><p>I hope you enjoy this new technique and you will use it in your GCP deployments!</p><p>If you are looking for other nice contents and you are not afraid of French, feel free to check out our French blog: <a href="https://blog.wescale.fr/">https://blog.wescale.fr</a> (Come on, google translate is your friend!)</p><p>Thanks to my friends from <a href="https://www.wescale.fr/wetribu.html">The WeTribu</a> who have patiently reviewed this article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b425ef95a00d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wescale/how-to-generate-and-use-temporary-credentials-on-google-cloud-platform-b425ef95a00d">How to generate and use temporary credentials on Google Cloud Platform</a> was originally published in <a href="https://medium.com/wescale">WeScale</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>