Redacting Sensitive Data with Cloud Data Loss Prevention

Join Sign in

Redacting Sensitive Data with Cloud Data Loss Prevention

45 minutes 1 Credit


Google Cloud self-paced labs logo


Cloud Data Loss Prevention (Cloud DLP) is a fully managed service designed to help discover, classify, and protect sensitive information. In this lab, you will learn the basic capabilities of the Cloud DLP API and try out the various ways it can be used to protect data.


In this lab, you will:

  • Use Cloud DLP to inspect strings and files for matching info types

  • Learn about deidentification techniques and use Cloud DLP to de-identify data

  • Use Cloud DLP to redact info types from strings and images

Setup and Requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud Console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.

  4. Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  5. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Cloud Console opens in this tab.

Note: You can view the menu with a list of Google Cloud Products and Services by clicking the Navigation menu at the top-left. Navigation menu icon

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:

gcloud auth list
  1. Click Authorize.

  2. Your output should now look like this:


ACTIVE: * ACCOUNT: To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:

gcloud config list project


[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Clone the repo and enable APIs

  1. Open a new Cloud Shell window and run the following command to download the Cloud Data Loss Prevention: Node.js Client repository:

git clone
  1. Once the project code is downloaded, change into the samples directory and install the required Node.js packages:

cd nodejs-dlp/samples && npm install
  1. Make sure you're using the correct project by setting it with the following gcloud command:

export PROJECT_ID=$(gcloud config get-value project) gcloud config set project $PROJECT_ID

Enable APIs

Here are the APIs you'll need to enable on your project:

  • Cloud Data Loss Prevention API - Provides methods for detection, risk analysis, and de-identification of privacy-sensitive fragments in text, images, and Google Cloud Platform storage repositories

  • Cloud Key Management Service (KMS) API - Google Cloud KMS allows customers to manage encryption keys and perform cryptographic operations with those keys.

  1. Enable the required APIs with the following gcloud command:

gcloud services enable \ --project $PROJECT_ID

Click Check my progress to verify the objective. Enable the APIs

Inspect strings and files

The samples directory of the project downloaded in the preceding step contains several javascript files that make use of the different functionality of Cloud DLP. The file, inspectString.js will inspect a provided string for sensitive info types.

  1. To test this out, provide the string option and a sample string with some potentially sensitive information:

node inspectString.js $PROJECT_ID "My email address is and you can call me at 555-867-5309"

The output should tell us the findings for each matched info type, which includes:

  • InfoType: the information type detected for that part of the string. You'll find a full list of possible info types here. By default, inspectString.js will only inspect for info types CREDIT_CARD_NUMBER, PHONE_NUMBER, PERSON_NAME AND EMAIL_ADDRESS
  • Likelihood: the results are categorized based on how likely they each represent a match. Likelihood can range from VERY_UNLIKELY to VERY_LIKELY.

The findings for the command request above are:

Findings: Info type: PERSON_NAME Likelihood: POSSIBLE Info type: EMAIL_ADDRESS Likelihood: LIKELY Info type: PHONE_NUMBER Likelihood: VERY_LIKELY
  1. Similarly, you can inspect files for info types. Run the following command to check out the sample accounts.txt file:

cat resources/accounts.txt

The file includes the following text:

My credit card number is 1234 5678 9012 3456, and my CVV is 789.
  1. Use the inspectFile.js file to inspect the provided file for sensitive info types:

node inspectFile.js $PROJECT_ID resources/accounts.txt

The results:

Findings: Info type: CREDIT_CARD_NUMBER Likelihood: VERY_LIKELY

Below is the asynchronous function that uses the API to inspect the string input:

async function inspectString( ProjectId, string, minLikelihood, maxFindings, infoTypes, customInfoTypes, includeQuote ) { ... }

The arguments provided for the parameters above are used to construct a request object. That request is then provided to the inspectContent function to get a response that results in our output:

// Construct item to inspect const item = {value: string}; // Construct request const request = { parent: `projects/${projectId}/locations/global`, inspectConfig: { infoTypes: infoTypes, customInfoTypes: customInfoTypes, minLikelihood: minLikelihood, includeQuote: includeQuote, limits: { maxFindingsPerRequest: maxFindings, }, }, item: item, }; // Run request const [response] = await dlp.inspectContent(request);


Beyond inspecting and detecting sensitive data, Cloud DLP can perform deidentification. Deidentification is the process of removing identifying information from data. The API detects sensitive data as defined by info types, and then uses a de-identification transformation to mask, delete, or otherwise obscure the data.

  1. Run the following command to use deidentifyWithMask.js to demonstrate deidentification with a mask:

node deidentifyWithMask.js $PROJECT_ID "My order number is F12312399. Email me at"

With a mask the API will replace the characters of the matching info type with a different character, * by default. The output will be:

My order number is F12312399. Email me at *****************************

Notice that the email address in the string is obfuscated while the arbitrary order number is intact. (Custom info types are possible but out of scope of this lab).

Let's see the function that uses the DLP API to deidentify with a mask. Once again, these arguments are used to construct a request object. This time it's provided to the deidentifyContent function:

async function deidentifyWithMask() { const request = { parent: `projects/${projectId}/locations/global`, deidentifyConfig: { infoTypeTransformations: { transformations: [ { primitiveTransformation: { characterMaskConfig: { maskingCharacter: maskingCharacter, numberToMask: numberToMask, }, }, }, ], }, }, item: item, }; // Run deidentification request const [response] = await dlp.deidentifyContent(request);

Redact strings and images

Another method of obfuscating sensitive information is redaction. Redaction will replace a match with the info type it's identified to match with.

  1. Use redactText.js to redact text from a sample input:

node redactText.js $PROJECT_ID "Please refund the purchase to my credit card 4012888888881881" CREDIT_CARD_NUMBER

The output replaces the sample credit card number with the info type CREDIT_CARD_NUMBER:

Please refund the purchase on my credit card [CREDIT_CARD_NUMBER]

This is useful if you'd like to hide sensitive information but still identify the type of information that's being removed. The DLP API can similarly redact information from images that contain text. To demonstrate, take a look at a sample image (located in the samples/resources directory):

image with sensitive info

  1. To redact the phone number from the image above, run the following command:

node redactImage.js $PROJECT_ID resources/test.png "" PHONE_NUMBER ./redacted-phone.png

As specified, a new image named redacted-phone.png will be generated with the requested information blacked out. You can verify this by opening the Cloud Shell Editor and opening up the samples/redacted-phone.png file:

info redacted image

  1. Try it again to redact the email address from the image:

node redactImage.js $PROJECT_ID resources/test.png "" EMAIL_ADDRESS ./redacted-email.png

As specified, a new image named redacted-email.png will be generated with the requested information blacked out. You can verify this by opening the Cloud Shell Editor and opening up the samples/redacted-email.png file:

info redacted image

Here is the function that is used to redact from a string:

async function redactText( callingProjectId, string, minLikelihood, infoTypes ) { ...}

And here is the request that will be provided to the deidentifyContent function:

const request = { parent: `projects/${projectId}/locations/global`, item: { value: string, }, deidentifyConfig: { infoTypeTransformations: { transformations: [replaceWithInfoTypeTransformation], }, }, inspectConfig: { minLikelihood: minLikelihood, infoTypes: infoTypes, }, }; const [response] = await dlp.deidentifyContent(request);

Similarly, here is the function for redacting an image:

async function redactImage( callingProjectId, filepath, minLikelihood, infoTypes, outputPath ) { ...}

And here is the request that will be provided to the redactImage function:

// Construct image redaction request const request = { parent: `projects/${projectId}/locations/global`, byteItem: { type: fileTypeConstant, data: fileBytes, }, inspectConfig: { minLikelihood: minLikelihood, infoTypes: infoTypes, }, imageRedactionConfigs: imageRedactionConfigs, };


Cloud DLP is a powerful tool that provides access to a powerful sensitive data inspection, classification, and de-identification platform. In this lab, you saw how the Cloud DLP can be used to inspect strings and files for multiple info types and used the DLP API to redact data from a string as well as an image.

Next Steps / Learn More

Be sure to check out the following documentation for more practice with Cloud Dataloss Prevention

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated August 11, 2022

Lab Last Tested August 11, 2022

Copyright 2023 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.