Skip to content

Commit a243194

Browse files
feat: add 12-factor compliant configuration with environment variable support
1 parent 0549d07 commit a243194

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -774,21 +774,39 @@ python manage.py anon_fix_permissions [--role name | --all] # Fix role permissi
774774

775775
### Configuration Settings
776776

777-
```python
778-
POSTGRES_ANON = {
779-
# Core settings
780-
'DEFAULT_MASKED_ROLE': 'masked_reader', # Default role for anonymization
781-
'ANONYMIZED_DATA_ROLE': 'masked_reader', # Role for anonymized_data()
782-
'MASKED_GROUP': 'masked_users', # Django group for middleware
783-
784-
# Behavior settings
785-
'ENABLED': True, # Enable anonymization features
786-
'AUTO_APPLY_RULES': False, # Auto-apply when enabled
787-
'VALIDATE_FUNCTIONS': True, # Validate function syntax
788-
'ALLOW_CUSTOM_FUNCTIONS': False, # Allow non-anon functions
789-
'ENABLE_LOGGING': True, # Enable audit logging
790-
}
791-
```
777+
Configuration follows [12-factor app principles](https://12factor.net/config). Settings can be configured via:
778+
779+
1. **Environment variables (recommended for production)**:
780+
781+
```bash
782+
export POSTGRES_ANON_ENABLED=true
783+
export POSTGRES_ANON_DEFAULT_MASKED_ROLE=masked_reader
784+
export POSTGRES_ANON_MASKED_GROUP=view_masked_data
785+
export POSTGRES_ANON_AUTO_APPLY_RULES=false
786+
export POSTGRES_ANON_VALIDATE_FUNCTIONS=true
787+
export POSTGRES_ANON_ALLOW_CUSTOM_FUNCTIONS=false
788+
export POSTGRES_ANON_ENABLE_LOGGING=true
789+
```
790+
791+
2. **Django settings (for development)**:
792+
793+
```python
794+
POSTGRES_ANON = {
795+
# Core settings
796+
'DEFAULT_MASKED_ROLE': 'masked_reader', # Default role for anonymization
797+
'ANONYMIZED_DATA_ROLE': 'masked_reader', # Role for anonymized_data()
798+
'MASKED_GROUP': 'masked_users', # Django group for middleware
799+
800+
# Behavior settings
801+
'ENABLED': True, # Enable anonymization features
802+
'AUTO_APPLY_RULES': False, # Auto-apply when enabled
803+
'VALIDATE_FUNCTIONS': True, # Validate function syntax
804+
'ALLOW_CUSTOM_FUNCTIONS': False, # Allow non-anon functions
805+
'ENABLE_LOGGING': True, # Enable audit logging
806+
}
807+
```
808+
809+
**Priority**: Environment variables override Django settings, which override defaults.
792810

793811
## Documentation
794812

django_postgres_anon/config.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Simple configuration helpers for django-postgres-anonymizer"""
22

3+
import os
4+
35
from django.conf import settings
46

57

@@ -20,7 +22,39 @@ def get_setting(key: str, default=None):
2022
"ENABLE_LOGGING": True,
2123
}
2224

25+
# Environment variable mappings (12-factor compliant)
26+
ENV_VAR_MAPPING = {
27+
"DEFAULT_MASKED_ROLE": "POSTGRES_ANON_DEFAULT_MASKED_ROLE",
28+
"MASKED_GROUP": "POSTGRES_ANON_MASKED_GROUP",
29+
"ANONYMIZED_DATA_ROLE": "POSTGRES_ANON_ANONYMIZED_DATA_ROLE",
30+
"ENABLED": "POSTGRES_ANON_ENABLED",
31+
"AUTO_APPLY_RULES": "POSTGRES_ANON_AUTO_APPLY_RULES",
32+
"VALIDATE_FUNCTIONS": "POSTGRES_ANON_VALIDATE_FUNCTIONS",
33+
"ALLOW_CUSTOM_FUNCTIONS": "POSTGRES_ANON_ALLOW_CUSTOM_FUNCTIONS",
34+
"ENABLE_LOGGING": "POSTGRES_ANON_ENABLE_LOGGING",
35+
}
36+
37+
38+
def _parse_env_bool(value: str) -> bool:
39+
"""Parse environment variable as boolean"""
40+
return value.lower() in ("true", "1", "yes", "on")
41+
2342

2443
def get_anon_setting(key: str):
25-
"""Get anonymization setting with built-in default"""
26-
return get_setting(key, DEFAULTS.get(key))
44+
"""Get anonymization setting with built-in default (12-factor compliant)"""
45+
# First check environment variables (12-factor principle)
46+
env_var = ENV_VAR_MAPPING.get(key)
47+
if env_var and env_var in os.environ:
48+
env_value = os.environ[env_var]
49+
# Handle boolean conversion for known boolean settings
50+
if key in ["ENABLED", "AUTO_APPLY_RULES", "VALIDATE_FUNCTIONS", "ALLOW_CUSTOM_FUNCTIONS", "ENABLE_LOGGING"]:
51+
return _parse_env_bool(env_value)
52+
return env_value
53+
54+
# Fall back to Django settings
55+
django_setting = get_setting(key)
56+
if django_setting is not None:
57+
return django_setting
58+
59+
# Finally use default
60+
return DEFAULTS.get(key)

0 commit comments

Comments
 (0)