Skip to content

Commit d9cfbb4

Browse files
committed
Add policy pack - Validate GCP > Project User for any unapproved role association. Closes #889
1 parent 266a97a commit d9cfbb4

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
categories: ["access management", "security"]
3+
primary_category: "access management"
4+
type: "featured"
5+
---
6+
7+
# Enforce GCP IAM Project Users Do Not Have Restricted Roles Assigned
8+
9+
Enforcing that GCP IAM Project Users do not have restricted roles assigned is essential for maintaining the principle of least privilege. This minimizes the risk of unauthorized access and potential misuse of administrative capabilities, enhancing security by ensuring that service accounts only have the permissions necessary to perform their specific tasks.
10+
11+
This [policy pack](https://turbot.com/guardrails/docs/concepts/policy-packs) can help you configure the following settings for IAM Project Users:
12+
13+
- Delete Project Users if new that have the Restricted Roles Assigned
14+
15+
**[Review policy settings →](https://hub.guardrails.turbot.com/policy-packs/gcp_iam_enforce_project_users_do_not_have_restricted_roles_assigned/settings)**
16+
17+
## Getting Started
18+
19+
### Requirements
20+
21+
- [Terraform](https://developer.hashicorp.com/terraform/install)
22+
- Guardrails mods:
23+
- [@turbot/gcp-iam](https://hub.guardrails.turbot.com/mods/gcp/mods/gcp-iam)
24+
25+
### Credentials
26+
27+
To create a policy pack through Terraform:
28+
29+
- Ensure you have `Turbot/Admin` permissions (or higher) in Guardrails
30+
- [Create access keys](https://turbot.com/guardrails/docs/guides/iam/access-keys#generate-a-new-guardrails-api-access-key) in Guardrails
31+
32+
And then set your credentials:
33+
34+
```sh
35+
export TURBOT_WORKSPACE=myworkspace.acme.com
36+
export TURBOT_ACCESS_KEY=acce6ac5-access-key-here
37+
export TURBOT_SECRET_KEY=a8af61ec-secret-key-here
38+
```
39+
40+
Please see [Turbot Guardrails Provider authentication](https://registry.terraform.io/providers/turbot/turbot/latest/docs#authentication) for additional authentication methods.
41+
42+
## Usage
43+
44+
### Install Policy Pack
45+
46+
> [!NOTE]
47+
> By default, installed policy packs are not attached to any resources.
48+
>
49+
> Policy packs must be attached to resources in order for their policy settings to take effect.
50+
51+
Clone:
52+
53+
```sh
54+
git clone https://github.com/turbot/guardrails-samples.git
55+
cd guardrails-samples/policy_packs/gcp/iam/enforce_project_users_do_not_have_restricted_roles_assigned
56+
```
57+
58+
Run the Terraform to create the policy pack in your workspace:
59+
60+
```sh
61+
terraform init
62+
terraform plan
63+
```
64+
65+
Then apply the changes:
66+
67+
```sh
68+
terraform apply
69+
```
70+
71+
### Apply Policy Pack
72+
73+
Log into your Guardrails workspace and [attach the policy pack to a resource](https://turbot.com/guardrails/docs/guides/policy-packs#attach-a-policy-pack-to-a-resource).
74+
75+
If this policy pack is attached to a Guardrails folder, its policies will be applied to all accounts and resources in that folder. The policy pack can also be attached to multiple resources.
76+
77+
For more information, please see [Policy Packs](https://turbot.com/guardrails/docs/concepts/policy-packs).
78+
79+
### Enable Enforcement
80+
81+
> [!TIP]
82+
> You can also update the policy settings in this policy pack directly in the Guardrails console.
83+
>
84+
> Please note your Terraform state file will then become out of sync and the policy settings should then only be managed in the console.
85+
86+
By default, the policies are set to `Check` in the pack's policy settings. To enable automated enforcements, you can switch these policies settings by adding a comment to the `Check` setting and removing the comment from one of the listed enforcement options:
87+
88+
```hcl
89+
resource "turbot_policy_setting" "gcp_iam_project_user_approved" {
90+
resource = turbot_policy_pack.main.id
91+
type = "tmod:@turbot/gcp-iam#/policy/types/projectUserApproved"
92+
# value = "Check: Approved"
93+
value = "Enforce: Delete unapproved if new"
94+
}
95+
```
96+
97+
Then re-apply the changes:
98+
99+
```sh
100+
terraform plan
101+
terraform apply
102+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "turbot_policy_pack" "main" {
2+
title = "Enforce GCP IAM Project Users do not have Restricted Roles assigned"
3+
description = "Ensures that project users do not have Restricted roles assigned. This reduces the risk of over-privileged access and enhances security by enforcing least privilege principles for project users."
4+
akas = ["gcp_iam_enforce_project_users_do_not_have_restricted_roles_assigned"]
5+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# GCP > IAM > Project User > Approved
2+
resource "turbot_policy_setting" "gcp_iam_project_user_approved" {
3+
resource = turbot_policy_pack.main.id
4+
type = "tmod:@turbot/gcp-iam#/policy/types/projectUserApproved"
5+
value = "Check: Approved"
6+
# value = "Enforce: Delete unapproved if new"
7+
}
8+
9+
# GCP > IAM > Project User > Approved > Custom
10+
resource "turbot_policy_setting" "gcp_iam_project_user_approved_custom" {
11+
resource = turbot_policy_pack.main.id
12+
type = "tmod:@turbot/gcp-iam#/policy/types/projectUserApprovedCustom"
13+
template_input = <<-EOT
14+
{
15+
resource {
16+
data
17+
}
18+
}
19+
EOT
20+
template = <<-EOT
21+
{%- set restrictedRoles = [
22+
"roles/editor",
23+
"roles/owner",
24+
"roles/viewer",
25+
"roles/resourcemanager.tagUser",
26+
"roles/resourcemanager.tagAdmin",
27+
"roles/iam.serviceAccountTokenCreator",
28+
"roles/iam.serviceAccountUser"
29+
] -%}
30+
31+
{%- set email = $.resource.data.userId -%}
32+
{%- set assignedRestrictedRoles = [] -%}
33+
34+
{%- for role in $.resource.data.roles -%}
35+
{%- if role in restrictedRoles -%}
36+
{%- set assignedRestrictedRoles = assignedRestrictedRoles.concat([role]) -%}
37+
{%- endif -%}
38+
{%- endfor -%}
39+
40+
{%- if assignedRestrictedRoles | length > 0 -%}
41+
- "title": "Role Assignments"
42+
"result": "Not approved"
43+
"message": "The user {{ email }} has restricted role(s): {{ assignedRestrictedRoles | join(", ") }} assigned."
44+
{%- else -%}
45+
- "title": "Role Assignments"
46+
"result": "Approved"
47+
"message": "The user {{ email }} does not have any restricted roles assigned."
48+
{%- endif -%}
49+
EOT
50+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
terraform {
2+
required_providers {
3+
turbot = {
4+
source = "turbot/turbot"
5+
version = ">= 1.11.0"
6+
}
7+
}
8+
}
9+
10+
provider "turbot" {
11+
}

0 commit comments

Comments
 (0)