File size: 3,473 Bytes
d77abf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
Security and compliance utilities.

This module provides a unified interface for security features
including audit logging, GDPR compliance, and encryption.

Usage:
    from security import (
        audit_logger, AuditAction,
        gdpr_service, ConsentType,
        field_encryption
    )
"""
from .audit import (
    AuditLogger,
    AuditAction,
    AuditEntry,
    audit_logger,
    log_login_success,
    log_login_failure,
    log_data_access,
    log_data_modification,
    log_security_event,
    SENSITIVE_FIELDS
)

from .gdpr import (
    GDPRService,
    ConsentType,
    DataCategory,
    DeletionStatus,
    ExportStatus,
    Consent,
    DeletionRequest,
    ExportRequest,
    gdpr_service,
    consent_required
)

from .encryption import (
    FieldEncryption,
    EncryptedValue,
    EncryptionError,
    DecryptionError,
    EncryptionNotConfigured,
    field_encryption,
    encrypt_sensitive_fields,
    decrypt_sensitive_fields,
    generate_encryption_key,
    mask_sensitive_value,
    ENCRYPTED_FIELDS
)


__all__ = [
    # Audit
    "AuditLogger",
    "AuditAction",
    "AuditEntry",
    "audit_logger",
    "log_login_success",
    "log_login_failure",
    "log_data_access",
    "log_data_modification",
    "log_security_event",
    "SENSITIVE_FIELDS",
    
    # GDPR
    "GDPRService",
    "ConsentType",
    "DataCategory",
    "DeletionStatus",
    "ExportStatus",
    "Consent",
    "DeletionRequest",
    "ExportRequest",
    "gdpr_service",
    "consent_required",
    
    # Encryption
    "FieldEncryption",
    "EncryptedValue",
    "EncryptionError",
    "DecryptionError",
    "EncryptionNotConfigured",
    "field_encryption",
    "encrypt_sensitive_fields",
    "decrypt_sensitive_fields",
    "generate_encryption_key",
    "mask_sensitive_value",
    "ENCRYPTED_FIELDS"
]


def get_security_stats() -> dict:
    """Get combined security statistics"""
    return {
        "audit": audit_logger.stats,
        "gdpr": gdpr_service.stats,
        "encryption": {
            "configured": field_encryption.is_configured,
            "encrypted_tables": list(ENCRYPTED_FIELDS.keys())
        }
    }


def initialize_security(db=None) -> None:
    """
    Initialize all security services.
    
    Args:
        db: Database instance for persistent storage
    """
    if db:
        audit_logger.set_database(db)
        gdpr_service.set_database(db)


# Security checklist for compliance
SECURITY_CHECKLIST = {
    "authentication": {
        "password_hashing": "bcrypt with salt",
        "session_management": "JWT with short expiry",
        "2fa": "TOTP-based",
        "brute_force_protection": "Rate limiting"
    },
    "authorization": {
        "model": "RBAC",
        "admin_separation": True,
        "principle_of_least_privilege": True
    },
    "data_protection": {
        "encryption_at_rest": "Field-level AES-256",
        "encryption_in_transit": "TLS 1.3",
        "data_classification": True,
        "backup_encryption": True
    },
    "audit_compliance": {
        "audit_logging": True,
        "log_retention": "7 years",
        "tamper_protection": "Append-only logs"
    },
    "gdpr_compliance": {
        "consent_management": True,
        "data_export": True,
        "data_deletion": True,
        "privacy_policy": True
    },
    "security_headers": {
        "hsts": True,
        "csp": True,
        "x_frame_options": "DENY",
        "x_content_type_options": "nosniff"
    }
}