|
| 1 | +import uuid |
| 2 | +from typing import Any, Dict, List, Optional, Tuple |
| 3 | + |
| 4 | +import structlog |
| 5 | +from presidio_analyzer import AnalyzerEngine |
| 6 | +from presidio_anonymizer import AnonymizerEngine |
| 7 | + |
| 8 | +from codegate.db.models import AlertSeverity |
| 9 | +from codegate.pipeline.base import PipelineContext |
| 10 | + |
| 11 | +logger = structlog.get_logger("codegate.pii.analyzer") |
| 12 | + |
| 13 | + |
| 14 | +class PiiSessionStore: |
| 15 | + """ |
| 16 | + A class to manage PII (Personally Identifiable Information) session storage. |
| 17 | +
|
| 18 | + Attributes: |
| 19 | + session_id (str): The unique identifier for the session. If not provided, a new UUID |
| 20 | + is generated. mappings (Dict[str, str]): A dictionary to store mappings between UUID |
| 21 | + placeholders and PII. |
| 22 | +
|
| 23 | + Methods: |
| 24 | + add_mapping(pii: str) -> str: |
| 25 | + Adds a PII string to the session store and returns a UUID placeholder for it. |
| 26 | +
|
| 27 | + get_pii(uuid_placeholder: str) -> str: |
| 28 | + Retrieves the PII string associated with the given UUID placeholder. If the placeholder |
| 29 | + is not found, returns the placeholder itself. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__(self, session_id: str = None): |
| 33 | + self.session_id = session_id or str(uuid.uuid4()) |
| 34 | + self.mappings: Dict[str, str] = {} |
| 35 | + |
| 36 | + def add_mapping(self, pii: str) -> str: |
| 37 | + uuid_placeholder = f"<{str(uuid.uuid4())}>" |
| 38 | + self.mappings[uuid_placeholder] = pii |
| 39 | + return uuid_placeholder |
| 40 | + |
| 41 | + def get_pii(self, uuid_placeholder: str) -> str: |
| 42 | + return self.mappings.get(uuid_placeholder, uuid_placeholder) |
| 43 | + |
| 44 | + |
| 45 | +class PiiAnalyzer: |
| 46 | + """ |
| 47 | + PiiAnalyzer class for analyzing and anonymizing text containing PII. |
| 48 | + This is a singleton class - use PiiAnalyzer.get_instance() to get the instance. |
| 49 | +
|
| 50 | + Methods: |
| 51 | + get_instance(): |
| 52 | + Get or create the singleton instance of PiiAnalyzer. |
| 53 | + analyze: |
| 54 | + text (str): The text to analyze for PII. |
| 55 | + Tuple[str, List[Dict[str, Any]], PiiSessionStore]: The anonymized text, a list of |
| 56 | + found PII details, and the session store. |
| 57 | + entities (List[str]): The PII entities to analyze for. |
| 58 | + restore_pii: |
| 59 | + anonymized_text (str): The text with anonymized PII. |
| 60 | + session_store (PiiSessionStore): The PiiSessionStore used for anonymization. |
| 61 | + str: The text with original PII restored. |
| 62 | + """ |
| 63 | + |
| 64 | + _instance: Optional["PiiAnalyzer"] = None |
| 65 | + _name = "codegate-pii" |
| 66 | + |
| 67 | + @classmethod |
| 68 | + def get_instance(cls) -> "PiiAnalyzer": |
| 69 | + """Get or create the singleton instance of PiiAnalyzer""" |
| 70 | + if cls._instance is None: |
| 71 | + logger.debug("Creating new PiiAnalyzer instance") |
| 72 | + cls._instance = cls() |
| 73 | + return cls._instance |
| 74 | + |
| 75 | + def __init__(self): |
| 76 | + """ |
| 77 | + Initialize the PiiAnalyzer. |
| 78 | + Note: Use get_instance() instead of creating a new instance directly. |
| 79 | + """ |
| 80 | + if PiiAnalyzer._instance is not None: |
| 81 | + raise RuntimeError("Use PiiAnalyzer.get_instance() instead") |
| 82 | + |
| 83 | + import os |
| 84 | + |
| 85 | + from presidio_analyzer.nlp_engine import NlpEngineProvider |
| 86 | + |
| 87 | + # Get the path to our custom spacy config |
| 88 | + current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 89 | + config_path = os.path.join(current_dir, "spacy_config.yaml") |
| 90 | + |
| 91 | + # Initialize the NLP engine with our custom configuration |
| 92 | + provider = NlpEngineProvider(conf_file=config_path) |
| 93 | + nlp_engine = provider.create_engine() |
| 94 | + |
| 95 | + # Create analyzer with custom NLP engine |
| 96 | + self.analyzer = AnalyzerEngine(nlp_engine=nlp_engine) |
| 97 | + self.anonymizer = AnonymizerEngine() |
| 98 | + self.session_store = PiiSessionStore() |
| 99 | + |
| 100 | + PiiAnalyzer._instance = self |
| 101 | + |
| 102 | + def analyze( |
| 103 | + self, text: str, context: Optional["PipelineContext"] = None |
| 104 | + ) -> Tuple[str, List[Dict[str, Any]], PiiSessionStore]: |
| 105 | + # Prioritize credit card detection first |
| 106 | + entities = [ |
| 107 | + "PHONE_NUMBER", |
| 108 | + "EMAIL_ADDRESS", |
| 109 | + "CRYPTO", |
| 110 | + "CREDIT_CARD", |
| 111 | + "IBAN_CODE", |
| 112 | + "MEDICAL_LICENSE", |
| 113 | + "US_BANK_NUMBER", |
| 114 | + "US_ITIN", |
| 115 | + "US_PASSPORT", |
| 116 | + "US_SSN", |
| 117 | + "UK_NHS", |
| 118 | + "UK_NINO", |
| 119 | + ] |
| 120 | + |
| 121 | + # Analyze the text for PII with adjusted threshold for credit cards |
| 122 | + analyzer_results = self.analyzer.analyze( |
| 123 | + text=text, |
| 124 | + entities=entities, |
| 125 | + language="en", |
| 126 | + score_threshold=0.3, # Lower threshold to catch more potential matches |
| 127 | + ) |
| 128 | + |
| 129 | + # Track found PII |
| 130 | + found_pii = [] |
| 131 | + |
| 132 | + # Only anonymize if PII was found |
| 133 | + if analyzer_results: |
| 134 | + # Log each found PII instance and anonymize |
| 135 | + anonymized_text = text |
| 136 | + for result in analyzer_results: |
| 137 | + pii_value = text[result.start : result.end] |
| 138 | + uuid_placeholder = self.session_store.add_mapping(pii_value) |
| 139 | + pii_info = { |
| 140 | + "type": result.entity_type, |
| 141 | + "value": pii_value, |
| 142 | + "score": result.score, |
| 143 | + "start": result.start, |
| 144 | + "end": result.end, |
| 145 | + "uuid_placeholder": uuid_placeholder, |
| 146 | + } |
| 147 | + found_pii.append(pii_info) |
| 148 | + anonymized_text = anonymized_text.replace(pii_value, uuid_placeholder) |
| 149 | + |
| 150 | + # Log each PII detection with its UUID mapping |
| 151 | + logger.info( |
| 152 | + "PII detected and mapped", |
| 153 | + pii_type=result.entity_type, |
| 154 | + score=f"{result.score:.2f}", |
| 155 | + uuid=uuid_placeholder, |
| 156 | + # Don't log the actual PII value for security |
| 157 | + value_length=len(pii_value), |
| 158 | + session_id=self.session_store.session_id, |
| 159 | + ) |
| 160 | + |
| 161 | + # Log summary of all PII found in this analysis |
| 162 | + if found_pii and context: |
| 163 | + # Create notification string for alert |
| 164 | + notify_string = ( |
| 165 | + f"**PII Detected** 🔒\n" |
| 166 | + f"- Total PII Found: {len(found_pii)}\n" |
| 167 | + f"- Types Found: {', '.join(set(p['type'] for p in found_pii))}\n" |
| 168 | + ) |
| 169 | + context.add_alert( |
| 170 | + self._name, |
| 171 | + trigger_string=notify_string, |
| 172 | + severity_category=AlertSeverity.CRITICAL, |
| 173 | + ) |
| 174 | + |
| 175 | + logger.info( |
| 176 | + "PII analysis complete", |
| 177 | + total_pii_found=len(found_pii), |
| 178 | + pii_types=[p["type"] for p in found_pii], |
| 179 | + session_id=self.session_store.session_id, |
| 180 | + ) |
| 181 | + |
| 182 | + # Return the anonymized text, PII details, and session store |
| 183 | + return anonymized_text, found_pii, self.session_store |
| 184 | + |
| 185 | + # If no PII found, return original text, empty list, and session store |
| 186 | + return text, [], self.session_store |
| 187 | + |
| 188 | + def restore_pii(self, anonymized_text: str, session_store: PiiSessionStore) -> str: |
| 189 | + """ |
| 190 | + Restore the original PII (Personally Identifiable Information) in the given anonymized text. |
| 191 | +
|
| 192 | + This method replaces placeholders in the anonymized text with their corresponding original |
| 193 | + PII values using the mappings stored in the provided PiiSessionStore. |
| 194 | +
|
| 195 | + Args: |
| 196 | + anonymized_text (str): The text containing placeholders for PII. |
| 197 | + session_store (PiiSessionStore): The session store containing mappings of placeholders |
| 198 | + to original PII. |
| 199 | +
|
| 200 | + Returns: |
| 201 | + str: The text with the original PII restored. |
| 202 | + """ |
| 203 | + for uuid_placeholder, original_pii in session_store.mappings.items(): |
| 204 | + anonymized_text = anonymized_text.replace(uuid_placeholder, original_pii) |
| 205 | + return anonymized_text |
0 commit comments