Connect a GCP Project

In this guide, you will connect a single GCP project to Turbot Guardrails using the Create Connection wizard. This process allows Guardrails to discover and manage resources across the project.

For most organizations, connecting via a GCP Organization with Workload Identity Federation is the recommended approach. It provides keyless authentication and automatic project discovery. Use this guide when you need to connect a standalone project outside of an organization.

Prerequisites

What Permissions to Grant

Which permissions you grant to Guardrails depends on your organization's requirements.

Full Remediation

To take advantage of every GCP integration offered by Guardrails (recommended), assign the Owner role to the service account.

Mixed Remediation + Event Handlers

You can build a custom role or collection of standard roles that meet your requirements. Guardrails uses whatever permissions are granted. If Guardrails is asked to do something without sufficient permissions, Access Denied errors will appear in the Guardrails console. Resolve these errors by granting additional permissions or adjusting your Guardrails policies to match.

Readonly + Event Handlers

To enable read-only access for Guardrails with Event Handler support, assign the following roles:

Or, to strictly limit Guardrails write access to only event handling resources, build a role with the following permissions. Convenient Terraform templates can be found in the custom roles for event handling section.

Readonly + Event Pollers

To enable read-only access for Guardrails with Event Poller support, assign the following roles:

Step 1: Create a Service Account

Turbot Support recommends creating a service account for each project. For a discussion of using a single service account across multiple projects, refer to the service accounts documentation.

Via the GCP Console

  1. In the Google Cloud console, go to the Create service account page.
  2. Select your GCP project.
  3. Enter a service account name to display in the Google Cloud console.
  4. The Google Cloud console generates a service account ID based on this name. Edit the ID if necessary -- you cannot change the ID later.
  5. Click Create and continue to set access controls.
  6. Choose one or more IAM roles to grant to the service account on the project. If necessary, create a custom role with the required permissions.
  7. Click Done to finish creating the service account.
  8. Create a service account key. After key creation, download the JSON key type file.

For additional help, refer to Google's documentation on creating a service account.

Via the gcloud CLI

If you don't have access to create the service account via the console, use the CLI.

  1. Create a service account:
$ gcloud iam service-accounts create --project=your-project-id  guardrails-service-owner
Created service account [guardrails-service-owner].
  1. Assign the owner role:
$ gcloud projects add-iam-policy-binding your-project-id --member=serviceAccount:[email protected] --role=roles/owner
Updated IAM policy for project [your-project-id].
bindings:
- members:
  - serviceAccount:[email protected]
etag: BwWK_UiAW44=
version: 1
  1. Generate an access key for the service account:
$ gcloud iam service-accounts keys create --iam-account=guardrails-service-owner@your-project-id.iam.gserviceaccount.com key.json
created key [1402f86298e8a44e4d0a32cf5ec3a7241a4008cc] of type [json] as [key.json] for [[email protected]]
  1. The private_key in the key.json file is a required secret for the import. Guardrails needs the text in multi-line format (not a single long string). For example:
$ cat key.json | jq -r .private_key
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQClvph2e9f6Dl/H
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<truncated>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Pwd0PmmSB1U3h3+Ued/eDhw=
-----END PRIVATE KEY-----

Step 2: Enable Required APIs

The Cloud Resource Manager, Service Management, and Service Usage APIs must be enabled in the project. Without the Cloud Resource Manager API, the GCP > Project > CMDB control goes to Error as soon as the project is connected. See Required GCP APIs for what each API is used for.

Guardrails uses the Service Usage API to read whether each service's API is enabled in the project. This read happens during discovery, so Service Usage is required even if you never ask Guardrails to enable an API on your behalf. Without it, Guardrails cannot determine the API state of any service. It is also the API Guardrails calls to enable services when a GCP > {Service} > API Enabled policy is set to Enforce: Enabled. The Service Management API is only used by the API Enabled controls of the BigQuery, Monitoring, Service Networking, and Cloud Scheduler mods.

Via the Console

  1. Go to the APIs & Services service in the GCP console.
  2. Search for the Cloud Resource Manager API and select it.
  3. If it is not already enabled, click Enable.
  4. Repeat steps 2 and 3 for the Service Management API and the Service Usage API.

Via the CLI

  1. Enable Cloud Resource Manager:
$ gcloud services enable cloudresourcemanager.googleapis.com --project=your-project-id
Operation "operations/acf.d2983634-c21e-480d-880f-16d060069925" finished successfully.
  1. Enable Service Management:
gcloud services enable servicemanagement.googleapis.com --project=your-project-id
Operation "operations/acf.cecc205f-3e9f-4707-86b5-9b22ded3f8db" finished successfully.
  1. Enable Service Usage:
gcloud services enable serviceusage.googleapis.com --project=your-project-id
Operation "operations/acf.7c1f0a3e-51b2-4f0c-9c7a-3f8b1d2e4a60" finished successfully.

Step 3: Enable Billing

Enable billing for the project, if not already enabled. This may already be enabled at the Organization level.

  1. Go to the API Console.
  2. From the projects list, select the project you're connecting.
  3. Open the console left side menu and select Billing.
  4. Click Enable billing (if billing is already enabled, this option won't appear).
  5. If you don't have a billing account, create one.
  6. Select your location, fill out the form, and click Submit and enable billing.

Refer to GCP documentation for more information about the billing API.

Step 4: Connect the Project in Guardrails

  1. In Guardrails, open Workspace from the sidebar, then select the Accounts tab.
  2. Click Import in the top right to open the Create Connection wizard.
  3. On the Select a Provider step, choose GCP Project.
  4. Select the Import location (the imported project will be a child of this resource).
  5. Select the Access mode as Upload a credential file.
  6. Drop the JSON key file you saved in Step 1 into the wizard.
  7. Select Connect to start Guardrails discovery.

You'll be redirected to a splash page that shows resources discovered in real time. If you see a large number of errors, refer to the troubleshooting section below.

Step 5: Enable GCP Service APIs

If not already complete, set the appropriate GCP > {Service} > API Enabled policies to Enforce: Enabled. Guardrails cannot complete the discovery process for services with disabled APIs. See the Enabling GCP Services guide for details.

Custom Roles for Event Handling

The Guardrails GCP Event Handlers depend on the Pub/Sub and Logging services. The custom role definitions and conditionals described below are sufficient to manage the event handler infrastructure in the project.

Logging

The custom role below enumerates the required write permissions for GCP Logging. These permissions assume that the Guardrails service role also has read-only access to GCP Logging.

Condition: resource.name.endsWith("turbot_gcp_event_sink")

resource "google_project_iam_custom_role" "guardrails_logging_role" {
  role_id     = "guardrails_logging"
  title       = "Guardrails Logging Maintainer"
  description = "A role for Guardrails to deploy, manage and destroy Logging resources for Guardrails Event Handling."
  permissions = [
    "logging.sinks.create",
    "logging.sinks.delete",
    "logging.sinks.update"
  ]
}

If the GCP > Turbot > Event Handlers > Logging > Sink > Name Prefix policy is set to something other than the default value, you'll need to update the condition above to match the new prefix.

Pub/Sub

The custom role below enumerates the required write permissions for GCP Pub/Sub. These permissions assume that the Guardrails service role also has read-only access to GCP Pub/Sub.

Condition: resource.name.startsWith("projects/_/topics/turbot_gcp_event_handler")

resource "google_project_iam_custom_role" "guardrails_event_handler_role" {
  role_id     = "guardrails_event_handlers"
  title       = "Guardrails Event Handler Maintainer"
  description = "A role for Guardrails to deploy, manage and destroy for Pub/Sub resources for Guardrails Event Handler infrastructure."
  permissions = [
    "pubsub.subscriptions.create",
    "pubsub.subscriptions.delete",
    "pubsub.subscriptions.setIamPolicy",
    "pubsub.subscriptions.update",

    "pubsub.topics.attachSubscription",
    "pubsub.topics.detachSubscription",
    "pubsub.topics.create",
    "pubsub.topics.delete",
    "pubsub.topics.setIamPolicy",
    "pubsub.topics.update",
    "pubsub.topics.updateTag"
  ]
}

If the GCP > Turbot > Event Handlers > Pub/Sub > Topic > Name Prefix policy is set to something other than the default value, you'll need to update the condition above to match the new prefix.

Troubleshooting

IssueDescriptionGuide
Access Denied: Missing Token Creator RoleIf using Service Account Impersonation, the impersonating user or workload must have roles/iam.serviceAccountTokenCreator on the service account.Refer to the Service Account Token Creator Role Documentation.
Access Denied: Malformed Secret KeyGuardrails requires the multi-line format of the Secret Key. Ensure it includes the -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY----- headers.
Access Denied: Improper Client EmailGuardrails cannot use a non-service account email to access the project. Ensure the Client Email is in the form of {identifier}@{your-project-id}.iam.gserviceaccount.com.Check GCP Service Account Documentation.
Access Denied: Missing or Insufficient PermissionsIf Guardrails is asked to discover, track, or remediate resources without the necessary permissions, access denied errors will appear in the Discovery and CMDB controls in the Guardrails console. Resolve by granting the required permissions.
Lots of Controls in Error StateIf there were issues with credentials during project import, many Discovery controls may show an error state. You can either delete and reimport the project or rerun the controls in error using scripts provided in the Guardrails Samples Repo.Use the Python, Node, or Shell scripts.
Project CMDB in Error: Cloud Resource Manager API has not been usedThe Cloud Resource Manager API is disabled in the project. Enable it, wait a few minutes, and rerun the GCP > Project > CMDB control.Required GCP APIs
Service Discovery and CMDB Controls SkippedIf a service's API is not enabled in the project, its Discovery and CMDB controls are skipped with the reason {Service} API is disabled. Enable the API in GCP, or set GCP > {Service} > API Enabled to Enforce: Enabled (the service account then needs serviceusage.services.enable).Enabling GCP Services
Further AssistanceIf you continue to encounter issues, please open a ticket with the relevant information to assist you more efficiently.Open Support Ticket.

Next Steps