## Introduction

This guide walks you through the steps to create a service account and gather the values needed
to configure a Google Network Security integration.

## Prerequisites

Before you begin, ensure that you have:

- A Google Cloud project
- Logging turned on for the traffic you want to monitor. Both kinds are off by default, and the integration reads only the logs that are on:
  - **VPC flow logs**: a VPC Flow Logs configuration, or flow logs turned on in a subnet's own settings
  - **Cloud DNS logging**: logging turned on for a DNS server policy, or Cloud Logging turned on for a public zone
- Access to the [Google Cloud Console](https://console.cloud.google.com/) with permission to enable APIs, create service accounts and custom roles, and grant IAM roles


## Required Permissions

### On your Google Cloud project

The service account needs these roles on your Google Cloud project.

| Role | IAM identifier | Purpose |
|  --- | --- | --- |
| Network Management Viewer | `roles/networkmanagement.viewer` | Discover Network Management VPC Flow Logs configs |
| Compute Network Viewer | `roles/compute.networkViewer` | Discover Compute subnetworks with flow logging enabled, and read the firewall rules that apply to each VPC network |
| Network Security Firewall Policy Viewer (custom) | Custom role, see [Create the custom role](#2-create-the-custom-role) | Read the network firewall policies attached to each VPC network |
| Logs Viewer | `roles/logging.viewer` | Query VPC flow log and DNS query log entries from Cloud Logging, and check where the project's logs are routed |
| DNS Reader | `roles/dns.reader` | Discover Cloud DNS server policies and public zones with query logging enabled |


### Only if you set Log Resource Names

Some organizations store flow logs and DNS logs outside the project, for example in a central
log bucket in a separate logging project. The integration then reads the logs from there, and
needs roles in each of those places. To find out if this applies to you, see
[Choose where to read logs](#choose-where-to-read-logs).

Skip this table if you leave **Log Resource Names** empty. Otherwise, grant these roles for
**every** entry you list, or the integration can't read the logs there.

| Role | IAM identifier | Grant it on | When |
|  --- | --- | --- | --- |
| Logs Viewer | `roles/logging.viewer` | Each **other** project an entry is in | The entry is in a project other than your Google Cloud project |
| Logs View Accessor | `roles/logging.viewAccessor` | The project that **owns the bucket** | The entry is a log bucket, as a Destination or a view, in any project, including your Google Cloud project |


For example, for `logging.googleapis.com/projects/central-logs/locations/global/buckets/netsec`,
grant both Logs Viewer and Logs View Accessor on `central-logs`.

## Choose where to read logs

Three optional settings decide which logs the integration reads and where it reads them from.
Most integrations leave all three empty. The integration then reads every flow log and DNS log
in the project.

Log Resource Names
The integration always monitors your Google Cloud project. Its flow logs and DNS query logs
record that project's traffic, and the integration reads that project's firewall rules. Log
Resource Names changes only where the integration reads those logs from.

By default, the integration reads the logs from your Google Cloud project's own `_Default` log
bucket. Some organizations send these logs elsewhere, for example to a central log bucket in a
separate logging project. Log Resource Names tells the integration where to find them.

When you set Log Resource Names, the integration reads logs only from the places you list. If
some logs still stay in your Google Cloud project, also add `projects/{project_id}`. Use the
steps below to decide whether you need it and what to enter.

#### Step 1: See where the project's logs are sent

In the Google Cloud console, select your Google Cloud project, then go to
**Logging > [Log router](https://console.cloud.google.com/logs/router)**. Each row in
**Log Router Sinks** is a sink, a rule that sends logs to a destination:

- **Type** is the kind of destination. The integration can only read from sinks whose type is
**Logging bucket** or **Google Cloud project**. Logs sent only to Cloud Storage, BigQuery
or Pub/Sub can't be read.
- **Destination** is where the logs go. A `_Default` destination is the project's own log
bucket. Any other destination is a log bucket or a project, which may be outside this
project.
- Skip `_Required`. It holds only audit logs.


#### Step 2: See which logs each sink receives

For each sink with type **Logging bucket** or **Google Cloud project**, open
**⋮ > View sink details**. The **Inclusion filter** and **Exclusion filter(s)** decide which
logs the sink receives. These are the logs the integration reads:

- `dns.googleapis.com/dns_queries` (DNS query logs)
- `compute.googleapis.com/vpc_flows` (flow logs turned on for a subnet)
- `networkmanagement.googleapis.com/vpc_flows` (VPC Flow Logs configurations)


Read the two kinds of sinks differently:

- **`_Default`** receives everything its filter doesn't leave out. It keeps these logs
unless its inclusion filter has `NOT LOG_ID("…")` with one of these names, or one of its
exclusion filters matches them. The standard filter leaves out only the audit logs that
go to `_Required`, so by default `_Default` keeps them.
- **Other sinks** receive only what their filter names. A sink receives one of these logs
only if its inclusion filter includes it, for example
`log_id("dns.googleapis.com/dns_queries")`.


Ignore **Writer identity**. It's the account Google uses to deliver logs, not the service
account you create for the integration.

#### Step 3: Decide what to enter

- **`_Default` keeps all of these logs:** leave Log Resource Names empty. This is true even
if other sinks also send copies elsewhere.
- **Another sink receives all of these logs, and `_Default` doesn't:** enter that sink's
**Destination**.
- **Some of these logs go only to another sink, and the rest stay in `_Default`:** enter
that sink's **Destination** and `projects/{project_id}`, using the ID of your Google Cloud
project. For example, if a sink receives DNS query logs and `_Default` excludes them,
but flow logs still go to `_Default`, enter both. The integration then reads DNS logs from
the sink's destination and flow logs from the project.
- **These logs go only to Cloud Storage, BigQuery or Pub/Sub:** the integration can't read
them. Keep them in `_Default`, or also route them to a log bucket.


#### What you can enter

- **A sink's Destination**, copied exactly from **Log router**, for example
`logging.googleapis.com/projects/central-logs/locations/global/buckets/netsec` or
`logging.googleapis.com/projects/central-logs`. This is the usual choice. The integration
reads a log bucket through its `_AllLogs` view, which every bucket has.
- **`projects/{project_id}`**, using the ID of your Google Cloud project. Add it when some logs
still stay in your Google Cloud project, as in Step 3.
- **A specific log bucket view**,
`projects/{project}/locations/{location}/buckets/{bucket}/views/{view}`. Use this only
if you've been given access to a view other than `_AllLogs`. To find it, open
**Logging > [Logs storage](https://console.cloud.google.com/logs/storage)** in the project
that owns the bucket and open the bucket. Use its **Region** as the location, and a
view **Name** from the **Log views** table.


#### Permissions for Log Resource Names

Each entry you list needs extra roles. See
[Only if you set Log Resource Names](#only-if-you-set-log-resource-names).

#### Logs routed by your organization

Sinks set on your organization or a folder don't appear on the project's Log router. If
your organization routes logs centrally, ask whoever manages it which bucket receives
these logs, then enter it as described above.

Flow Log Configurations
This setting is a filter. It does not add logs. It limits the integration to the flow logs
you list. Leave it empty to read every flow log configuration in the project.

Google Cloud turns on flow logs in two ways, and each is found in a
different place.

- **A VPC Flow Logs configuration**, created on the VPC Flow Logs page for a network,
subnet, VLAN attachment or Cloud VPN tunnel. It has a configuration name, which is what
you enter. A configuration for a network covers all of that network's subnets.
- **Flow logs turned on in a subnet's own settings** (the older way). No configuration is
created, so you enter the subnet itself, as its region and name.


#### VPC Flow Logs configurations

1. Go to **VPC Network > [VPC Flow Logs](https://console.cloud.google.com/networking/vpc-flow-logs)**.
2. Under **Project-level configurations**, check the **VPC networks**, **Subnets**, **VLAN attachments** and **Cloud VPN tunnels** tabs.
3. For each configuration with status **On** that you want, enter its **Configuration name**, for example `prod-vpc-flow-logs`.


#### Flow logs turned on in a subnet's settings

1. Go to **VPC Network > [VPC Flow Logs](https://console.cloud.google.com/networking/vpc-flow-logs)** and open the **Subnets (Compute Engine API)** tab. This tab appears only when at least one subnet has flow logs turned on this way. If it isn't there, skip this section.
2. Enter each subnet as `{region}/{subnet name}`, for example `us-east1/web-subnet`. Include the region, because subnet names can repeat across regions.


#### Full identifiers

You can also enter a full identifier:
`projects/{project}/locations/global/vpcFlowLogsConfigs/{name}` or
`projects/{project}/regions/{region}/subnetworks/{name}`. Check each entry: one that does
not match a configuration makes flow log queries fail with an error.

DNS Server Policies and Public Zones
This setting is a filter. It does not add logs. It limits the integration to the DNS logging
you list. Leave it empty to read all DNS query logging in the project.

Two kinds of resource log DNS queries:

#### DNS server policies

A server policy with logging on logs every DNS lookup made from the VPC networks it's
attached to.

1. Go to **Network services > Cloud DNS > [DNS server policies](https://console.cloud.google.com/net-services/dns/policies/list)**.
2. Enter the **Name** of each policy with logging on that you want, for example
`prod-dns-policy`.


#### Public zones

A public zone with Cloud Logging on logs queries from the internet for that zone.

1. Go to **Network services > Cloud DNS > [Zones](https://console.cloud.google.com/net-services/dns/zones)**.
2. Open a zone with **Zone type** Public to check that Cloud Logging is on.
3. Enter its **Zone name**, not its DNS name. For example, enter `example-zone`, not
`example.com.`


#### Other zones and response policies

Private zones, forwarding zones, peering zones and response policies don't have their own
query logging. Their lookups are logged by the server policy of the VPC network that made
them, so enter that server policy instead.

#### Full identifiers

You can also enter a full identifier: `projects/{project}/policies/{name}` or
`projects/{project}/managedZones/{name}`. If a server policy and a public zone share a name,
enter the full identifier. An entry that matches nothing is skipped, and the others are still
read.

## Enable the required APIs

Enable these APIs in your Google Cloud project:

1. In the Google Cloud Console, go to **APIs & Services > Library**.
2. Search for and enable **Compute Engine API**.
3. Search for and enable **Cloud Logging API**.
4. Search for and enable **Network Management API**.
5. Search for and enable **Cloud DNS API**.


## Create a service account and assign permissions

### 1. Create the service account

Follow the Google documentation to [create a service account](https://cloud.google.com/iam/docs/service-accounts-create).

Give the service account a clear name such as `integration-google-network-security`.

### 2. Create the custom role

Compute Network Viewer doesn't include permission to list network firewall policies, so
create a custom role for it. Follow the Google documentation to
[create a custom role](https://cloud.google.com/iam/docs/creating-custom-roles#creating) in
your Google Cloud project. Give the role a title, such as
`Network Security Firewall Policy Viewer`, and an ID. Set its launch stage to
**General Availability**.

Add these permissions:

- `compute.firewallPolicies.list`
- `compute.regionFirewallPolicies.list`


### 3. Grant the required IAM roles

See [Grant a single role](https://cloud.google.com/iam/docs/grant-role-sa).

1. Grant every role under [On your Google Cloud project](#on-your-google-cloud-project).
2. If you set **Log Resource Names**, grant the roles under
[Only if you set Log Resource Names](#only-if-you-set-log-resource-names).


### 4. Create a JSON key for the service account

Create a JSON key for the service account by following the Google documentation to [create a service account key](https://cloud.google.com/iam/docs/keys-create-delete#iam-service-account-keys-create-console).

Download the JSON key and store it in a safe location. You need these values from the file:

- `client_email`
- `client_id`
- `private_key`


Proceed to the next section titled [Configuring the Integration](#configuring-the-integration).

## Configuring the Integration

To configure a new Google Network Security integration, provide each of the values as defined below:

| Integration Parameter | Description |
|  --- | --- |
| Client Email | The `client_email` value from the JSON key you downloaded in [Create a JSON key for the service account](#4-create-a-json-key-for-the-service-account). |
| Client ID | The `client_id` value from the same JSON key. |
| Client Secret | The `private_key` value from the same JSON key: the full PEM-encoded private key string. |
| Project ID | The ID of your Google Cloud project, for example `my-project`. Use the ID from the console's project picker, not the display name or project number. The integration monitors this project even when you set Log Resource Names. |
| Token URL *(optional)* | Leave empty to use the default Google token URL, `https://oauth2.googleapis.com/token`. |
| Log Resource Names *(optional)* | Where to read the project's logs from, if they are stored outside the project: sink Destinations, log bucket views, or `projects/{project_id}` (if some logs are still stored in the monitored project). Leave empty to read logs from the project. See [Choose where to read logs](#choose-where-to-read-logs). |
| Flow Log Configurations *(optional)* | VPC Flow Logs configuration names, subnets as `{region}/{subnet name}`, or full identifiers. Leave empty to read every flow log configuration. See [Choose where to read logs](#choose-where-to-read-logs). |
| DNS Server Policies and Public Zones *(optional)* | DNS server policy or public zone names, or full identifiers. Leave empty to read all DNS query logging. See [Choose where to read logs](#choose-where-to-read-logs). |