Stay organized with collections Save and categorize content based on your preferences.

Inspect sensitive text by using Node.js

Learn how to scan a sample string for sensitive information by using the Cloud Data Loss Prevention API (DLP API), the Google Cloud CLI, and Node.js.


For step-by-step guidance for this task directly in the Google Cloud console, click Guide me:

Guide me


The following sections take you through the same steps as clicking Guide me.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install and initialize the Google Cloud CLI.
  3. Create or select a Google Cloud project.

    • Create a Cloud project:

      gcloud projects create PROJECT_ID
    • Select the Cloud project that you created:

      gcloud config set project PROJECT_ID
  4. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  5. Enable the DLP API:

    gcloud services enable dlp.googleapis.com
  6. Create authentication credentials for your Google Account:

    gcloud auth application-default login
  7. Grant roles to your Google Account. Run the following command once for each of the following IAM roles: roles/dlp.user

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:EMAIL_ADDRESS" --role=ROLE
    • Replace PROJECT_ID with your project ID.
    • Replace EMAIL_ADDRESS with your email address.
    • Replace ROLE with each individual role.
  8. Install and initialize the Google Cloud CLI.
  9. Create or select a Google Cloud project.

    • Create a Cloud project:

      gcloud projects create PROJECT_ID
    • Select the Cloud project that you created:

      gcloud config set project PROJECT_ID
  10. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  11. Enable the DLP API:

    gcloud services enable dlp.googleapis.com
  12. Create authentication credentials for your Google Account:

    gcloud auth application-default login
  13. Grant roles to your Google Account. Run the following command once for each of the following IAM roles: roles/dlp.user

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:EMAIL_ADDRESS" --role=ROLE
    • Replace PROJECT_ID with your project ID.
    • Replace EMAIL_ADDRESS with your email address.
    • Replace ROLE with each individual role.
  14. Install Node.js and NPM.

Set up a Cloud DLP CLI app

To set up a Cloud DLP CLI app using Node.js, do the following:

  1. Clone the Node.js DLP client library:

     git clone https://github.com/googleapis/nodejs-dlp
    
  2. Navigate to the samples directory.

     cd nodejs-dlp/samples
    
  3. Install the app dependencies:

     npm install
    

Inspect a string for sensitive information

To scan sample text by using the DLP API and the inspectString Node.js script, run the following command:

  node inspectString.js PROJECT_ID "My email address is joe@example.com."

The output is similar to the following:

  Findings:
      Info type: EMAIL_ADDRESS
      Likelihood: LIKELY

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, delete the Cloud project with the resources.

Delete the project

If you created a new project for this quickstart, the easiest way to prevent additional charges is to delete the project.

    Delete a Cloud project:

    gcloud projects delete PROJECT_ID

Revoke your credentials

  1. Optional: Revoke the authentication credentials that you created, and delete the local credential file.

    gcloud auth application-default revoke
  2. Optional: Revoke credentials from the gcloud CLI.

    gcloud auth revoke

What's next