>

Accessing Cloud SCC programmatically

This page walks you through accessing Cloud Security Command Center (Cloud SCC) using the Cloud SCC Python library to review your organization's Google Cloud Platform (GCP) resources.

Before you begin

To access Cloud SCC programmatically, you must have one of the following Cloud Identity and Access Management (Cloud IAM) roles:

  • securitycenter.viewer
  • securitycenter.editor

For more information about these Cloud IAM roles, see Access control.

To complete this guide, you'll need the following:

  • An existing directory path in which a service account private key can be stored. This path is in the context of your Cloud Shell environment, such as /home/myuser/mykeys/.

Accessing Cloud SCC

To access Cloud SCC programmatically, you'll use Cloud Shell to get the client library and authenticate a service account. To get the Cloud SCC client library, follow the steps below:

Setting up environment variables

  1. Go to the Google Cloud Platform Console.
    Go to the Google Cloud Platform Console
  2. Click Activate Google Cloud Shell.
  3. Run the following commands to set environment variables:
    1. Set your organization name:

      ORG_ID=YOUR_ORGANIZATION_ID

    2. Set the project ID for which Cloud SCC is enabled:

      PROJECT_ID=Cloud SCC-ENABLED_PROJECT_ID

    3. Set the custom ID you want to use for a new service account, such as SCC-SA:

      SERVICE_ACCOUNT=CUSTOM_ID

    4. Set the path in which the service account key should be stored, such as /home/myuser/mykeys/SERVICE_ACCOUNT.json:

      KEY_LOCATION=FULL_PATH

Installing the Python library

  1. Download and untar the client library to the location you prefer:

    gsutil cp gs://cscc-client-libraries/v1alpha3.1.tar.gz .
    tar -zxvf v1alpha3.1.tar.gz
          

  2. [Optional] Before you install the Python library, we recommend using Virtualenv to create an isolated Python environment.

    virtualenv onboarding_example
    source onboarding_example/bin/activate
          

  3. Install pip to manage the Python library installation.
  4. Run the following commands to install the Python library:

    pip install v1alpha3/python/securitycenter-v1alpha3/
          

Setting up a service account

The Python library takes a private key from a service account to be used by the client. The service account must have the organization level role securitycenter.editor.

  1. Create a service account that's associated with your project ID:

    gcloud iam service-accounts create SERVICE_ACCOUNT  --display-name
     "Service Account for USER"  --project PROJECT_ID
          
  2. Create a key to associate with the service account. The key will be used for the life of the service and persistently stored at the KEY_LOCATION you specify.

    gcloud iam service-accounts keys create KEY_LOCATION  --iam-account
     SERVICE_ACCOUNT@PROJECT_ID.iam.gserviceaccount.com
          
  3. Grant the service account the securitycenter.editor role for the organization.

    gcloud beta organizations add-iam-policy-binding $ORG_ID
      --member="serviceAccount:SERVICE_ACCOUNT@PROJECT_ID.iam.gserviceaccount.com"
      --role='roles/securitycenter.editor'
        

Starting the service

Start the interactive Python shell and set up imports, variables, and helper function.

$ python

from google.cloud import securitycenter
from google.oauth2 import service_account
from google.protobuf import timestamp_pb2, duration_pb2, struct_pb2
from datetime import datetime, timedelta

ORGANIZATION = "organizations/TO_BE_UPDATED"
credentials = service_account.Credentials.from_service_account_file(
  'FULL_PATH_TO_SERVICE_ACCOUNT_CREDENTIALS')
scoped_credentials = credentials.with_scopes(
  ['https://www.googleapis.com/auth/cloud-platform'])
client = securitycenter.SecurityCenterClient(credentials = scoped_credentials)
def print_iterator( resource_iterator ):
  for asset in resource_iterator:
    print(asset)
def unix_time_millis(dt):
  return int((dt - datetime.utcfromtimestamp(0)).total_seconds())

Example calls

Python

Most of the following example calls use the print_iterator defined above to print results. In the following examples, the : operator performs partial and substring matching.

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Security Command Center