How to do it...

In this recipe, we take an input data.csv which has some sensitive data. A Python program parses the file and generates a request JSON file for the API. The second part of the same Python program validates the response and gives a custom output to the user:

  1. Navigate to the Chapter04/using_dlp_api folder.
  2. The Python program check-with-dlp.py reads the data from the data.csv file and generates a request. The request is sent to the DLP API and the response is parsed to find if there is sensitive information in the data sent. The first part of the code is the initial setup for the program:
# -*- coding: utf-8 -*-
"""
@author: Legorie
# The code identifies sensitive information and logs it on the console
"""
import json
import requests

# Enter the API Key which has access to the DLP API
api_key = "<Enter API Key>"
  1. Then, we have a function to verify the likelihood of the response return from the DLP API. The API returns six types of match likelihood namely LIKELIHOOD_UNSPECIFIED, VERY_UNLIKELY, UNLIKELY, POSSIBLE, LIKELY, and VERY_LIKELY.
# Functions returns true if the likelihood is acceptable
def verify_likelihood(match):
if match in ['POSSIBLE','LIKELY','VERY_LIKELY']:
return 1
else:
return 0
  1. The main() function can be divided into two parts. The first part reads the data from the input file, line by line. The JSON request is built using the JSON module. We are building the request for phone number and email address verification in the given text:
    with open('data.csv') as txtfile:
for line in txtfile:
#print(line)
line_num+=1
data['item'] = {}
data['item']['type'] = 'text/plain'
data['item']['value'] = line

data['inspectConfig'] = {}
data['inspectConfig']['infoTypes'] = []

pNum = {}
pNum['name'] = 'PHONE_NUMBER'
data['inspectConfig']['infoTypes'].append(pNum)

eMail = {}
eMail['name'] = 'EMAIL_ADDRESS'
data['inspectConfig']['infoTypes'].append(eMail)

json_data = json.dumps(data)
Print the variable json_data to have a look at the request JSON sent to the API.
  1. The built JSON data is sent to the DLP API using appropriate authentication and the response is collected:
r = requests.post('https://dlp.googleapis.com/v2beta2/projects/
<Project ID>/content:inspect?key=' + api_key, data=json_data)
#print(r.status_code)
response = r.json()
  1. The response is then validated to check if an acceptable likelihood is achieved for the requested line item:
if 'findings' in response['result']:
if len(response['result']['findings']) == 2 and
verify_likelihood(response['result']['findings'][0]
['likelihood']) and
verify_likelihood(response['result']['findings'][1]
['likelihood']):
#if response['result']['findings']['infoType']['name'] ==
'PHONE_NUMBER':
print("Phone number and Email address are present in line #"
+ str(line_num))
elif response['result']['findings'][0]['infoType']['name'] ==
'PHONE_NUMBER' and
verify_likelihood(response['result']['findings'][0]
['likelihood']):
print("Phone number is present in line #" + str(line_num))
elif response['result']['findings'][0]['infoType']['name'] ==
'EMAIL_ADDRESS' and
verify_likelihood(response['result']['findings'][0]
['likelihood']):
print("Email address is present in line #" + str(line_num))

The results are then logged on the Console. The following is example test data:

First Name, Last Name, Address, Mobile, email, Credit Card
Robert, Bernista, '232, 95th Street, Wisconsin', +001-142-232-2387, [email protected], 9374 7363 8273 3943
John, Shuman, '555 W Madison, Chicago', +001-312-223-3212, [email protected], 7363 3637 2983 3234
John, Shuman, '555 W Madison, Chicago', +001-312-223-3212, , 7363 3637 2983 3234
Robert, Bernista, '232, 95th Street, Wisconsin', , testo@example, 9374 7363 8273 3943
Bernie, Sanman, '232, 95th Street, Wisconsin', , [email protected], 9374 7363 8273 3943

The following is the output of the preceding data:

Phone number and Email address are present in line #2
Phone number and Email address are present in line #3
Phone number is present in line #4
Email address is present in line #6
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.123.184