Skip to main content

Data Protection Framework

Data Protection Framework is a python library/command line application for identification, anonymization and de-anonymization of Personally Identifiable Information data.

The framework aims to work on a two-fold principle for detecting PII:

  1. Using RegularExpressions using a pattern
  2. Using NLP for detecting NER (Named Entity Recognitions)

Common Usage

  1. pip install pii-anonymizer
  2. Specify configs in pii-anonymizer.json
  3. Choose whether to run in standalone or spark mode with python -m pii_anonymizer.standalone or python -m pii_anonymizer.spark

Features and Current Status

Completed

  • Following Global detectors have been completed:

    • EMAIL_ADDRESS : An email address identifies the mailbox that emails are sent to or from. The maximum length of the domain name is 255 characters, and the maximum length of the local-part is 64 characters.
    • CREDIT_CARD_NUMBER : A credit card number is 12 to 19 digits long. They are used for payment transactions globally.
  • Following detectors specific to Singapore have been completed:

    • PHONE_NUMBER : A telephone number.
    • FIN/NRIC : A unique set of nine alpha-numeric characters on the Singapore National Registration Identity Card.
    • THAI_ID : 13 numeric digits of Thai Citizen ID
  • Following anonymizers have been added

    • Replacement ('replace'): Replaces a detected sensitive value with a specified surrogate value. Leave the value empty to simply delete detected sensitive value.
    • Hash ('hash'): Hash detected sensitive value with sha256.
    • Encryption: Encrypts the original sensitive data value using a Fernet (AES based).

Currently supported file formats: csv, parquet

Encryption

To use encryption as anonymize mode, a compatible encryption key needs to be created and assigned to PII_SECRET environment variables. Compatible key can be generated with

python -m pii_anonymizer.key

This will generate output similar to

Keep this encrypt key safe
81AOjk7NV66O62QpnFsvCXH8BDB26KM9TIH7pBfZ6PQ=

To set this key as an environment variable run

export PII_SECRET=81AOjk7NV66O62QpnFsvCXH8BDB26KM9TIH7pBfZ6PQ=

TO-DO

Following features are part of the backlog with more features coming soon

  • Detectors:
    • NAME
    • ADDRESS
  • Anonymizers:
    • Masking: Replaces a number of characters of a sensitive value with a specified surrogate character, such as a hash (#) or asterisk (*).
    • Bucketing: "Generalizes" a sensitive value by replacing it with a range of values. (For example, replacing a specific age with an age range, or temperatures with ranges corresponding to "Hot," "Medium," and "Cold.")

You can have a detailed at upcoming features and backlog in this Github Board

Development setup

  1. Install Poetry
  2. Setup hooks and install packages with make install

Config JSON

Limitation: when reading multiple files, all files that matches the file_path must have same headers. Additionally, when file format is not given anonymizer will assume that the file format is the first matched filename. Thus, when the file_path ends with /* and the folder contains mixed file format, the operation will fail.

An example for the config JSON is located at <PROJECT_ROOT>/pii-anonymizer.json

{
  "acquire": {
    "file_path": <FILE PATH TO YOUR INPUT CSV>, -> ./input_data/file.csv or ./input_data/*.csv to read all files that matches
    "delimiter": <YOUR CSV DELIMITER>
  },
  "analyze": {
    "exclude": ['Exception']
  },
  "anonymize": {
    "mode": <replace|hash|encrypt>,
    "value": "string to replace",
    "output_file_path" : <PATH TO YOUR CSV OUTPUT FOLDER>,
    "output_file_format": <csv|parquet>,
    "output_file_name": "anonymized" -> optionally, specify the output filename.
  },
  "report" : {
    "location" : <PATH TO YOUR REPORT OUTPUT FOLDER>,
    "level" : <LOG LEVEL>
  }
}

Running Tests

You can run the tests by running make test or triggering shell script located at <PROJECT_ROOT>/bin/run_tests.sh

Trying out on local

Anonymizing a delimited csv file
  1. Set up a JSON config file similar to the one seen at the project root. In the 'acquire' section of the json, populate the input file path and the delimiter. In the 'report' section, provide the output path, where you want the PII detection report to be generated. A 'high' level report just calls out which columns have PII attributes. A 'medium' level report calls out the percentage of PII in each column and the associated PII (email, credit card, etc)type for the same.
  2. Run the main class - python -m pii_anonymizer.standalone --config <optionally, path of the config file or leave blank to defaults to pii-anonymizer.json> You should see the report being appended to the file named 'report_<date>.log' in the output path specified in the config file.

Packaging

Run poetry build and the .whl file will be created in the dist folder.

Licensing

Distributed under the MIT license. See LICENSE for more information.

Contributing

You want to help out? Awesome!

Metadata

Release files for pii-anonymizer 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pii-anonymizer 0.2.5
File Size Uploaded
pii_anonymizer-0.2.5.tar.gz 32.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pii-anonymizer 0.2.5
File Interpreter ABI Platform
pii_anonymizer-0.2.5-py3-none-any.whl Python 3 none any Details

Total release size: 86.8 kB

Release files / pii_anonymizer-0.2.5.tar.gz

Download URL pii_anonymizer-0.2.5.tar.gz
Size 32.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9bf19a07634ccbe4ff96b8f0305f21b62bc42c936a14cdbe46e7b11a2ba1f688
BLAKE2b-256 checksum
How to use checksums
b95ac9f7a981bfccd16c0ad573099a0f31df993d6bf68f0728aaba8545b3c605
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.2 Linux/5.15.0-1024-azure

Release files / pii_anonymizer-0.2.5-py3-none-any.whl

Download URL pii_anonymizer-0.2.5-py3-none-any.whl
Size 54.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f6c7ebec67e5bc985afe2485dd486eb708b2b9fa565f9f745468eaa4f7c2f64
BLAKE2b-256 checksum
How to use checksums
215ef65569a1a3a1c7669be81076ab469ad53f652df79ffdeeada79689a49953
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.2 Linux/5.15.0-1024-azure

Release history Release notifications | RSS feed

This release

0.2.5 This release

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page