An enterprise Python package for reversible text anonymization using Google Cloud services.
Reversible Anonymizer provides a powerful, scalable solution for detecting and replacing sensitive information in text with realistic-looking fake data, while maintaining the ability to reverse the process and recover the original information.
- Complete Google DLP InfoType Support: Over 200 pre-configured detectors for PII, financial data, healthcare information, and more
- Consistent Anonymization: Name parts and repeated information anonymized consistently
- Multiple Cache Strategies: In-memory LRU cache and Google Cloud Memcache support
- Asynchronous Storage: High throughput processing with asynchronous storage updates
- Realistic or Token-based Replacement: Choose between human-readable fake data or systematic tokens
- Batch Operations: Process multiple texts efficiently in parallel
- Production-ready Resilience: Comprehensive error handling, fallbacks, and operational modes
pip install reversible-anonymizer
# With Memcache support
pip install reversible-anonymizer[memcache]- Python 3.8+
- Google Cloud project with the following services enabled:
- Cloud DLP API (dlp.googleapis.com)
- Cloud Firestore API (firestore.googleapis.com)
- [Optional] Memorystore for Memcached (memcache.googleapis.com)
- Google Cloud credentials configured
from reversible_anonymizer import ReversibleAnonymizer
# Initialize the anonymizer
anonymizer = ReversibleAnonymizer(project="your-project-id")
# Anonymize text
original_text = "Hello, my name is John Smith. Please contact me at [email protected]."
anonymized_text = anonymizer.anonymize(original_text)
print(f"Anonymized: {anonymized_text}")
# Output: "Hello, my name is Michael Johnson. Please contact me at [email protected]."
# De-anonymize back to original
recovered_text = anonymizer.deanonymize(anonymized_text)
print(f"De-anonymized: {recovered_text}")
# Output:
"Hello, my name is John Smith. Please contact me at [email protected]."anonymizer = ReversibleAnonymizer(
project="your-project-id",
info_types=["PERSON_NAME", "EMAIL_ADDRESS", "PHONE_NUMBER"]
)# Realistic fake data (default)
anonymizer = ReversibleAnonymizer(
project="your-project-id",
use_realistic_fake_data=True
)
# Token-based anonymization
anonymizer = ReversibleAnonymizer(
project="your-project-id",
use_realistic_fake_data=False
)result = anonymizer.anonymize(text, detailed_result=True)
print(f"Anonymized: {result['anonymized_text']}")
print(f"Findings: {len(result['findings'])}")
print(f"Duration: {result['stats']['duration_ms']} ms")
print(f"Cache hits: {result['stats']['cache_hits']}")texts = [
"John Smith lives in New York.",
"Jane Doe is from Seattle.",
"Contact John Smith at [email protected]."
]
# Process in parallel
anonymized_texts = anonymizer.anonymize_batch(texts)# Strict mode (default) - raises exceptions on errors
anonymizer = ReversibleAnonymizer(
project="your-project-id",
mode="strict"
)
# Tolerant mode - continues despite errors
anonymizer = ReversibleAnonymizer(
project="your-project-id",
mode="tolerant"
)
# Audit mode - detects but doesn't replace sensitive information
anonymizer = ReversibleAnonymizer(
project="your-project-id",
mode="audit"
)anonymizer = ReversibleAnonymizer(
project="your-project-id",
cache_type="memory",
cache_config={
"capacity": 10000, # Maximum items in cache
"ttl": 3600 # Time-to-live in seconds
}
)# Connect to existing Memcache instance by host
anonymizer = ReversibleAnonymizer(
project="your-project-id",
cache_type="memcache",
cache_config={
"host": "10.0.0.1", # Memcache IP address
"port": 11211, # Memcache port
"ttl": 86400 # Cache TTL in seconds
}
)
# Or connect using instance name and let the adapter discover endpoints
anonymizer = ReversibleAnonymizer(
project="your-project-id",
cache_type="memcache",
cache_config={
"instance_id": "anonymizer-cache",
"region": "us-central1",
"create_if_missing": True # Auto-create if not exists
}
)# Enable async storage updates for better performance
anonymizer = ReversibleAnonymizer(
project="your-project-id",
async_storage_updates=True
)Reversible Anonymizer supports all Google DLP InfoTypes, organized into categories:
- PERSON_NAME
- FIRST_NAME, LAST_NAME
- EMAIL_ADDRESS
- PHONE_NUMBER
- AGE, DATE_OF_BIRTH, GENDER
- CREDIT_CARD_NUMBER
- BANK_ACCOUNT, IBAN_CODE
- SWIFT_CODE, CURRENCY
- US_SOCIAL_SECURITY_NUMBER
- PASSPORT_NUMBER
- DRIVER_LICENSE_NUMBER
- STREET_ADDRESS
- CITY, STATE, ZIPCODE
- GPS_COORDINATES And many more!
# Get all supported info types
info_types = anonymizer.get_supported_infotypes()
# Get info types by category
health_info_types = anonymizer.get_infotypes_by_category("Health Information")# Core configuration
ANONYMIZER_PROJECT=your-project-id
ANONYMIZER_INFO_TYPES=PERSON_NAME,EMAIL_ADDRESS,PHONE_NUMBER
ANONYMIZER_COLLECTION=custom_mappings
ANONYMIZER_MODE=tolerant
ANONYMIZER_USE_REALISTIC_FAKE_DATA=true
# Cache configuration
ANONYMIZER_CACHE_TYPE=memcache
ANONYMIZER_MEMCACHE_HOST=10.0.0.1
ANONYMIZER_MEMCACHE_PORT=11211
ANONYMIZER_CACHE_TTL=3600
# Load from file
anonymizer = ReversibleAnonymizer.from_config("config.json")
{
"project": "your-project-id",
"info_types": ["PERSON_NAME", "EMAIL_ADDRESS", "PHONE_NUMBER"],
"collection_name": "custom_mappings",
"mode": "strict",
"use_realistic_fake_data": true,
"cache_type": "memcache",
"cache_config": {
"host": "10.0.0.1",
"port": 11211,
"ttl": 3600
}
}
# List supported info types
anonymizer --project your-project-id list-info-types
# Anonymize a file
anonymizer --project your-project-id anonymize --input input.txt --output anonymized.txt
# De-anonymize a file
anonymizer --project your-project-id deanonymize --input anonymized.txt --output original.txt# Enable the API
python -m tools.memcache_setup --project your-project-id enable-api
# Create a new Memcache instance
python -m tools.memcache_setup --project your-project-id create \
--name anonymizer-cache \
--region us-central1 \
--node-count 1 \
--node-memory 1The package provides custom exceptions:
- ServiceNotEnabledError: Raised when required Google Cloud services are not enabled
- AnonymizationError: Raised when anonymization fails
- DeAnonymizationError: Raised when de-anonymization fails
- InfoTypeNotSupportedError: Raised when an unsupported info type is requested
- StorageError: Raised when there's an error with the storage adapter
- ConfigurationError: Raised when there's a configuration error
- Use secure storage: Enable encryption for stored mappings
- Limit access: Use IAM to restrict access to the Firestore collection
- Set appropriate TTLs: Configure cache TTLs to minimize data exposure
- Enable audit logging: Use detailed_result to log anonymization operations
- Use Memcache: For high-throughput applications
- Enable async storage: Reduce latency by updating storage asynchronously
- Batch processing: Use batch methods for multiple texts
- Optimize info types: Select only the info types you need
Contributions are welcome! Please feel free to submit a pull request.
- Fork the repository
- Create your feature branch (git checkout -b feature/amazing-feature)
- Commit your changes (git commit -m 'Add some amazing feature')
- Push to the branch (git push origin feature/amazing-feature)
- Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Google Sensitive Data Protection (formerly Cloud DLP) for the powerful detection capabilities
- Faker library for generating realistic fake data