Step 3: Encrypt Address and Location Data
In addition to customer and payment PII, location data like street addresses and detailed zip codes must also be protected. In this step, you will extend your pipeline to encrypt address information while preserving city, state, and a partial zip code for regional analytics.
The Goal
You will enhance your processor to handle the billing_address object by:
- Encrypting the
streetandzipfields. - Creating a
zip_prefixfield containing the first 3 digits of the zip for safe regional analysis. - Deleting the original
streetandzipfields.
Implementation
-
Start with the Previous Pipeline: Copy the
encrypt-pii.yamlfile from Step 2 to a new file namedencrypt-location.yaml.cp encrypt-pii.yaml encrypt-location.yaml -
Enhance the Encryption Processor: Open
encrypt-location.yamland add the new location logic to the bottom of the existingmappingprocessor.Add this to the 'mapping' processor in encrypt-location.yaml# --- Logic from Step 1 & 2 (Card & PII Encryption) ---
# (The existing logic for card numbers, ssn, and email remains here)
# --- START: New additions for Location Data ---
# 5. Encrypt Billing Address (if it exists)
if this.billing_address.exists() {
# Encrypt the street address
if this.billing_address.street.exists() {
root.billing_address.street_encrypted = this.billing_address.street.encrypt_aes("gcm", env("ADDRESS_ENCRYPTION_KEY"))
}
# Encrypt the zip code and extract the prefix
if this.billing_address.zip.exists() {
let clean_zip = this.billing_address.zip.re_replace_all("[^0-9]", "")
root.billing_address.zip_encrypted = clean_zip.encrypt_aes("gcm", env("ADDRESS_ENCRYPTION_KEY"))
if clean_zip.length() >= 3 {
root.billing_address.zip_prefix = clean_zip.slice(0, 3)
}
}
# Remove the original plaintext fields
root.billing_address = this.billing_address.without("street", "zip")
}
# --- END: New additions ---Note: This step uses a separate
ADDRESS_ENCRYPTION_KEY. Ensure you have set it as an environment variable. -
Deploy and Test:
# Send the sample payment data, which includes an address
curl -X POST http://localhost:8080/events/payment \
-H "Content-Type: application/json" \
-d @/tmp/encryption-test/sample-payment.json -
Verify: Check your logs. You will see that the
billing_addressobject now containsstreet_encrypted,zip_encrypted, and the safezip_prefix, while the original sensitive fields are gone. All the previous PII (card number, SSN, etc.) is still being encrypted correctly.
You have now built a comprehensive PII protection pipeline that covers payment, personal, and location data.