diff --git a/policy_packs/gcp/iam/enforce_service_accounts_do_not_have_restricted_roles_assigned/README.md b/policy_packs/gcp/iam/enforce_service_accounts_do_not_have_restricted_roles_assigned/README.md new file mode 100644 index 00000000..1b32953b --- /dev/null +++ b/policy_packs/gcp/iam/enforce_service_accounts_do_not_have_restricted_roles_assigned/README.md @@ -0,0 +1,102 @@ +--- +categories: ["access management", "security"] +primary_category: "access management" +type: "featured" +--- + +# Enforce GCP IAM Service Accounts Do Not Have Restricted Roles Assigned + +Enforcing that GCP IAM service accounts 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. + +This [policy pack](https://turbot.com/guardrails/docs/concepts/policy-packs) can help you configure the following settings for IAM user-managed service accounts: + +- Disable/Delete service accounts that have the Restricted Roles Assigned + +**[Review policy settings →](https://hub.guardrails.turbot.com/policy-packs/gcp_iam_enforce_service_accounts_do_not_have_restricted_roles_assigned/settings)** + +## Getting Started + +### Requirements + +- [Terraform](https://developer.hashicorp.com/terraform/install) +- Guardrails mods: + - [@turbot/gcp-iam](https://hub.guardrails.turbot.com/mods/gcp/mods/gcp-iam) + +### Credentials + +To create a policy pack through Terraform: + +- Ensure you have `Turbot/Admin` permissions (or higher) in Guardrails +- [Create access keys](https://turbot.com/guardrails/docs/guides/iam/access-keys#generate-a-new-guardrails-api-access-key) in Guardrails + +And then set your credentials: + +```sh +export TURBOT_WORKSPACE=myworkspace.acme.com +export TURBOT_ACCESS_KEY=acce6ac5-access-key-here +export TURBOT_SECRET_KEY=a8af61ec-secret-key-here +``` + +Please see [Turbot Guardrails Provider authentication](https://registry.terraform.io/providers/turbot/turbot/latest/docs#authentication) for additional authentication methods. + +## Usage + +### Install Policy Pack + +> [!NOTE] +> By default, installed policy packs are not attached to any resources. +> +> Policy packs must be attached to resources in order for their policy settings to take effect. + +Clone: + +```sh +git clone https://github.com/turbot/guardrails-samples.git +cd guardrails-samples/policy_packs/gcp/iam/enforce_service_accounts_do_not_have_restricted_roles_assigned +``` + +Run the Terraform to create the policy pack in your workspace: + +```sh +terraform init +terraform plan +``` + +Then apply the changes: + +```sh +terraform apply +``` + +### Apply Policy Pack + +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). + +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. + +For more information, please see [Policy Packs](https://turbot.com/guardrails/docs/concepts/policy-packs). + +### Enable Enforcement + +> [!TIP] +> You can also update the policy settings in this policy pack directly in the Guardrails console. +> +> Please note your Terraform state file will then become out of sync and the policy settings should then only be managed in the console. + +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: + +```hcl +resource "turbot_policy_setting" "gcp_iam_service_account_approved" { + resource = turbot_policy_pack.main.id + type = "tmod:@turbot/gcp-iam#/policy/types/serviceAccountApproved" + # value = "Check: Approved" + value = "Enforce: Disable unapproved" +} +``` + +Then re-apply the changes: + +```sh +terraform plan +terraform apply +``` diff --git a/policy_packs/gcp/iam/enforce_service_accounts_do_not_have_restricted_roles_assigned/main.tf b/policy_packs/gcp/iam/enforce_service_accounts_do_not_have_restricted_roles_assigned/main.tf new file mode 100644 index 00000000..d9c430bb --- /dev/null +++ b/policy_packs/gcp/iam/enforce_service_accounts_do_not_have_restricted_roles_assigned/main.tf @@ -0,0 +1,5 @@ +resource "turbot_policy_pack" "main" { + title = "Enforce GCP IAM Service Accounts do not have Restricted Roles assigned" + description = "Ensures that service accounts do not have Restricted roles assigned. This reduces the risk of over-privileged access and enhances security by enforcing least privilege principles for service accounts." + akas = ["gcp_iam_enforce_service_accounts_do_not_have_restricted_roles_assigned"] +} diff --git a/policy_packs/gcp/iam/enforce_service_accounts_do_not_have_restricted_roles_assigned/policies.tf b/policy_packs/gcp/iam/enforce_service_accounts_do_not_have_restricted_roles_assigned/policies.tf new file mode 100644 index 00000000..6b5e8346 --- /dev/null +++ b/policy_packs/gcp/iam/enforce_service_accounts_do_not_have_restricted_roles_assigned/policies.tf @@ -0,0 +1,70 @@ +# GCP > IAM > Service Account > Approved +resource "turbot_policy_setting" "gcp_iam_service_account_approved" { + resource = turbot_policy_pack.main.id + type = "tmod:@turbot/gcp-iam#/policy/types/serviceAccountApproved" + value = "Check: Approved" + # value = "Enforce: Delete unapproved if new" + # value = "Enforce: Disable unapproved" +} + +# GCP > IAM > Service Account > Approved > Custom +resource "turbot_policy_setting" "gcp_iam_service_account_approved_custom" { + resource = turbot_policy_pack.main.id + type = "tmod:@turbot/gcp-iam#/policy/types/serviceAccountApprovedCustom" + template_input = <<-EOT + { + resources( + filter: "resourceTypeId:tmod:@turbot/gcp-iam#/resource/types/projectIamPolicy resourceId:{{ $.resource.parent.turbot.id }}" + ) { + items { + projectBindings: get(path: "bindings") + } + } + resource { + email: get(path:"email") + disabled: get(path:"disabled") + } + } + EOT + template = <<-EOT + {%- set restrictedRoles = [ + "roles/editor", + "roles/owner", + "roles/viewer", + "roles/resourcemanager.tagUser", + "roles/resourcemanager.tagAdmin", + "roles/iam.serviceAccountTokenCreator", + "roles/iam.serviceAccountUser" + ] -%} + + {%- set email = $.resource.email -%} + {%- set isDisabled = $.resource.disabled == true -%} + {%- set assignedRestrictedRoles = [] -%} + + {%- if not isDisabled -%} {# If the service account is not disabled, check for restricted roles #} + {%- for binding in $.resources.items[0].projectBindings -%} + {%- if binding.role in restrictedRoles -%} + {%- for member in binding.members -%} + {%- if member == "serviceAccount:" + email -%} + {%- set assignedRestrictedRoles = assignedRestrictedRoles.concat([binding.role]) -%} + {%- endif -%} + {%- endfor -%} + {%- endif -%} + {%- endfor -%} + {%- endif -%} + + {%- if isDisabled -%} + - "title": "Role Bindings" + "result": "Approved" + "message": "The service account is disabled." + {%- elif assignedRestrictedRoles | length > 0 -%} + - "title": "Role Bindings" + "result": "Not approved" + "message": "The service account has restricted role(s): {{ assignedRestrictedRoles | join(", ") }} assigned." + {%- else -%} + - "title": "Role Bindings" + "result": "Approved" + "message": "The service account does not have any restricted roles assigned." + {%- endif -%} + EOT +} diff --git a/policy_packs/gcp/iam/enforce_service_accounts_do_not_have_restricted_roles_assigned/providers.tf b/policy_packs/gcp/iam/enforce_service_accounts_do_not_have_restricted_roles_assigned/providers.tf new file mode 100644 index 00000000..3ede1821 --- /dev/null +++ b/policy_packs/gcp/iam/enforce_service_accounts_do_not_have_restricted_roles_assigned/providers.tf @@ -0,0 +1,11 @@ +terraform { + required_providers { + turbot = { + source = "turbot/turbot" + version = ">= 1.11.0" + } + } +} + +provider "turbot" { +}