Skip to main content

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:

  1. Encrypting the street and zip fields.
  2. Creating a zip_prefix field containing the first 3 digits of the zip for safe regional analysis.
  3. Deleting the original street and zip fields.

Implementation

  1. Start with the Previous Pipeline: Copy the encrypt-pii.yaml file from Step 2 to a new file named encrypt-location.yaml.

    cp encrypt-pii.yaml encrypt-location.yaml
  2. Enhance the Encryption Processor: Open encrypt-location.yaml and add the new location logic to the bottom of the existing mapping processor.

    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.

  3. 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
  4. Verify: Check your logs. You will see that the billing_address object now contains street_encrypted, zip_encrypted, and the safe zip_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.