<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged terraform</title>
    <link href="https://passingcuriosity.com/tags/terraform/terraform.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/terraform/terraform.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2024-01-26T00:00:00Z</updated>
    <entry>
    <title>Splitting predefined GCP roles</title>
    <link href="https://passingcuriosity.com/2024/splitting-gcp-predefined-roles/" />
    <id>https://passingcuriosity.com/2024/splitting-gcp-predefined-roles/</id>
    <published>2024-01-26T00:00:00Z</published>
    <updated>2024-01-26T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>Recently I’ve worked with a client who wanted to restrict access to any <em>Set IAM
Policy</em> permissions in their Google Cloud Platform environment. Currently this
is implemented by defining a small number of custom roles which fall into one of
two groups:</p>
<ul>
<li><p>Roles which include all the <em>Set IAM Policy</em> permissions for the necessary
services;</p></li>
<li><p>Role/s which include all the non-<em>Set IAM Policy</em> permissions for the
necessary services.</p></li>
</ul>
<p>Currently these are defined by explicitly listing all the permissions that
should be granted (as far as they are known at the time we edit the definition).
I’ve recently been thinking about an approach that might help move toward a more
manageable approach.</p>
<ol type="1">
<li><p>Use the Google pre-defined roles where possible. This will help make sure
that documentation, error message, examples, etc. is more directly applicable
to the client’s environment.</p></li>
<li><p>Some pre-defined roles can’t be used because they mix <em>Set IAM Policy</em> with
other permissions that the client wants to manage separately. In this case,
use Terraform to define custom roles, but do so based on the definition of
the pre-defined role.</p></li>
</ol>
<p>Here’s a sketch of what this might look like.</p>
<pre class="terraform"><code>variable &quot;target_role&quot; {
  type        = string
  description = &quot;ID of the target role.&quot;
}

# Fetch the existing role.
data &quot;google_iam_role&quot; &quot;role&quot; {
  name = var.target_role
}

locals {
  role_components = split(&quot;/&quot;, var.target_role)
  role_name       = element(local.role_components, length(local.role_components) - 1)

  # Every permission in the target role that **IS** a setIamPolicy permission.
  setiam_permissions = [
    for permission in data.google_iam_role.role.included_permissions:
    permission if length(regexall(&quot;^.*[.]setIamPolicy$&quot;, permission)) == 1
  ]

  # Every permission in the target role that **IS NOT** a setIamPolicy permission.
  normal_permissions = [
    for permission in data.google_iam_role.role.included_permissions:
    permission if length(regexall(&quot;^.*[.]setIamPolicy$&quot;, permission)) == 0
  ]
}

resource &quot;google_project_iam_custom_role&quot; &quot;nonpriv_role&quot; {
  role_id     = &quot;custom.${role_name}.nonpriv&quot;
  title       = &quot;${data.google_iam_role.role.title} - (Non-priv)&quot;
  description = &quot;(Custom non-privileged version) ${data.google_iam_role.role.description}.&quot;
  permissions = local.normal_permissions
}

resource &quot;google_project_iam_custom_role&quot; &quot;priv_role&quot; {
  role_id     = &quot;custom.${role_name}.priv&quot;
  title       = &quot;${data.google_iam_role.role.title} - (Priv)&quot;
  description = &quot;(Custom privileged version) ${data.google_iam_role.role.description}.&quot;
  permissions = local.setiam_permissions
}

output &quot;permissions_role&quot; {
    value  = resource.google_project_iam_custom_role.nonpriv_role
}

output &quot;set_iam_policy_role&quot; {
    value  = resource.google_project_iam_custom_role.priv_role
}</code></pre>]]></summary>
</entry>

</feed>
