Quickstart: Using the command-line tool

This page shows you how to perform basic tasks in the Cloud Data Loss Prevention API using a command-line interface.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the GCP Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  3. Make sure that billing is enabled for your project.

    Learn how to enable billing

  4. Enable the DLP API.

    Enable the API

  5. Set up authentication:
    1. In the GCP Console, go to the Create service account key page.

      Go to the Create Service Account Key page
    2. From the Service account drop-down list, select New service account.
    3. In the Service account name field, enter a name .
    4. From the Role drop-down list, select Project > Owner.

      Note: The Role field authorizes your service account to access resources. You can view and change this field later by using GCP Console. If you are developing a production app, specify more granular permissions than Project > Owner. For more information, see granting roles to service accounts.
    5. Click Create. A JSON file that contains your key downloads to your computer.
  6. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

  7. Download and install Node.js and NPM.

Permissions

Inspecting content requires the serviceusage.services.use permission for the project that's specified in parent. The roles/editor, roles/owner, and roles.dlp.user roles contain the required permission or you can define your own custom role.

To give your user the dlp.admin role at the project level:

Web UI

  1. Open the Cloud Identity and Access Management page in the Google Cloud Platform Console.

    Open the IAM page

  2. If a project hasn't already been selected, click Select a project.

  3. Select your project and click Open.

  4. On the Cloud Identity and Access Management page, click Add.

  5. In the Add members dialog:

    • For Members type the user email: test@example.com.
    • For Roles, click Select a role and choose Cloud DLP > DLP User.

  6. Click Add.

Command-line

  1. To add a single binding to the project's Cloud IAM policy, type the following command. Replace project-id with your project ID.
    gcloud projects add-iam-policy-binding project-id --member group:test@example.com --role roles/dlp.user
  2. The command writes the updated policy to the console window:
    bindings:
    - members:
      - group:test@example.com
        role: roles/dlp.user

Set up the DLP API CLI app

  1. Clone or download a ZIP file of the Node.js samples repository.

  2. Open a command-line tool and navigate to the repo's root folder.

  3. Install the app dependencies by running npm install.

  4. If you haven't done so already, create the GCLOUD_PROJECT environment variable and set it to the project ID of the Google Cloud Platform project you set up to use with the DLP API.

Inspect a string for sensitive information

This section shows you how to ask the service to scan sample text using the inspect string command.

  1. Open a command-line tool and navigate to the samples folder of the Node.js samples repository.

  2. Run the following command:

    node inspect.js string "My email address is joe@example.com."

You should receive the following output:

{
  "results": [
    {
      "findings": [
        {
          "quote": "joe@example.com",
          "infoType": "EMAIL_ADDRESS",
          "likelihood": "VERY_LIKELY",
          "location": {
            "byteRange": {
              "start": "20",
              "end": "35"
            },
            "codepointRange": {
              "start": "20",
              "end": "35"
          }
          }
        }
      ]
    }
  ]
}

Congratulations! You've sent your first request to the Cloud Data Loss Prevention API!

What's next?

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

Send feedback about...

Data Loss Prevention API Documentation