Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit e794d57

Browse files
authored
Merge pull request #354 from stacklok/optimizations
simplify signature initialization for optimization
2 parents d2c6069 + d2a1315 commit e794d57

File tree

7 files changed

+18
-53
lines changed

7 files changed

+18
-53
lines changed

src/codegate/cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,16 +305,16 @@ def serve(
305305
ca = CertificateAuthority.get_instance()
306306
ca.ensure_certificates_exist()
307307

308-
# Set up event loop
309-
loop = asyncio.new_event_loop()
310-
asyncio.set_event_loop(loop)
311-
312308
# Initialize secrets manager and pipeline factory
313309
secrets_manager = SecretsManager()
314310
pipeline_factory = PipelineFactory(secrets_manager)
315311

316312
app = init_app(pipeline_factory)
317313

314+
# Set up event loop
315+
loop = asyncio.new_event_loop()
316+
asyncio.set_event_loop(loop)
317+
318318
# Run the server
319319
try:
320320
loop.run_until_complete(run_servers(cfg, app))

src/codegate/pipeline/secrets/secrets.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class CodegateSecrets(PipelineStep):
2626
def __init__(self):
2727
"""Initialize the CodegateSecrets pipeline step."""
2828
super().__init__()
29+
# Initialize and load signatures immediately
30+
CodegateSignatures.initialize("signatures.yaml")
2931

3032
@property
3133
def name(self) -> str:

src/codegate/pipeline/secrets/signatures.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class CodegateSignatures:
3939
"""Main class for detecting secrets in text using regex patterns."""
4040

4141
_instance_lock: ClassVar[Lock] = Lock()
42-
_signatures_loaded: ClassVar[bool] = False
4342
_signature_groups: ClassVar[List[SignatureGroup]] = []
4443
_compiled_regexes: ClassVar[Dict[str, re.Pattern]] = {}
4544
_yaml_path: ClassVar[Optional[str]] = None
@@ -48,22 +47,23 @@ class CodegateSignatures:
4847
def reset(cls) -> None:
4948
"""Reset the cached patterns."""
5049
with cls._instance_lock:
51-
cls._signatures_loaded = False
5250
cls._signature_groups = []
5351
cls._compiled_regexes = {}
5452
cls._yaml_path = None
5553
logger.debug("SecretFinder cache reset")
5654

5755
@classmethod
5856
def initialize(cls, yaml_path: str) -> None:
59-
"""Initialize the SecretFinder with a YAML file path."""
57+
"""Initialize the SecretFinder with a YAML file path and load signatures."""
6058
if not Path(yaml_path).exists():
6159
raise FileNotFoundError(f"Signatures file not found: {yaml_path}")
6260

6361
with cls._instance_lock:
64-
cls._yaml_path = yaml_path
65-
cls._signatures_loaded = False
66-
logger.debug(f"SecretFinder initialized with {yaml_path}")
62+
# Only initialize if not already initialized with this path
63+
if cls._yaml_path != yaml_path:
64+
cls._yaml_path = yaml_path
65+
cls._load_signatures()
66+
logger.debug(f"SecretFinder initialized with {yaml_path}")
6767

6868
@classmethod
6969
def _preprocess_yaml(cls, content: str) -> str:
@@ -172,6 +172,10 @@ def _add_signature_group(cls, name: str, patterns: Dict[str, str]) -> None:
172172
def _load_signatures(cls) -> None:
173173
"""Load signature patterns from the YAML file."""
174174
try:
175+
# Clear existing signatures before loading new ones
176+
cls._signature_groups = []
177+
cls._compiled_regexes = {}
178+
175179
yaml_data = cls._load_yaml(cls._yaml_path)
176180

177181
# Add custom GitHub token patterns
@@ -205,32 +209,14 @@ def _load_signatures(cls) -> None:
205209
logger.error(f"Error loading signatures: {e}")
206210
raise
207211

208-
@classmethod
209-
def _ensure_signatures_loaded(cls) -> None:
210-
"""Ensure signatures are loaded before use."""
211-
if not cls._signatures_loaded:
212-
with cls._instance_lock:
213-
if not cls._signatures_loaded:
214-
if not cls._yaml_path:
215-
raise RuntimeError("SecretFinder not initialized. Call initialize() first.")
216-
try:
217-
cls._load_signatures()
218-
cls._signatures_loaded = True
219-
except Exception as e:
220-
logger.error(f"Failed to load signatures: {e}")
221-
raise
222-
223212
@classmethod
224213
def find_in_string(cls, text: str) -> List[Match]:
225214
"""Search for secrets in the provided string."""
226215
if not text:
227216
return []
228217

229-
try:
230-
cls._ensure_signatures_loaded()
231-
except Exception as e:
232-
logger.error(f"Failed to load signatures: {e}")
233-
return []
218+
if not cls._yaml_path:
219+
raise RuntimeError("SecretFinder not initialized.")
234220

235221
matches = []
236222
lines = text.splitlines()

src/codegate/server.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from codegate import __description__, __version__
55
from codegate.dashboard.dashboard import dashboard_router
66
from codegate.pipeline.factory import PipelineFactory
7-
from codegate.pipeline.secrets.signatures import CodegateSignatures
87
from codegate.providers.anthropic.provider import AnthropicProvider
98
from codegate.providers.llamacpp.provider import LlamaCppProvider
109
from codegate.providers.ollama.provider import OllamaProvider
@@ -31,9 +30,6 @@ def init_app(pipeline_factory: PipelineFactory) -> FastAPI:
3130
# Create provider registry
3231
registry = ProviderRegistry(app)
3332

34-
# Initialize SignaturesFinder
35-
CodegateSignatures.initialize("signatures.yaml")
36-
3733
# Register all known providers
3834
registry.add_provider(
3935
"openai",

tests/pipeline/secrets/test_signatures.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,12 @@ def test_initialize_and_reset(temp_yaml_file):
6969

7070
CodegateSignatures.reset()
7171
assert CodegateSignatures._yaml_path is None
72-
assert not CodegateSignatures._signatures_loaded
7372
assert not CodegateSignatures._signature_groups
7473
assert not CodegateSignatures._compiled_regexes
7574

7675

7776
def test_find_in_string_with_aws_credentials(temp_yaml_file):
7877
CodegateSignatures.initialize(temp_yaml_file)
79-
CodegateSignatures._signatures_loaded = False # Force reload of signatures
8078

8179
test_string = """
8280
aws_access_key = 'AKIAIOSFODNN7EXAMPLE'
@@ -100,7 +98,6 @@ def test_find_in_string_with_github_token():
10098

10199
try:
102100
CodegateSignatures.initialize(f.name)
103-
CodegateSignatures._signatures_loaded = False # Force reload of signatures
104101

105102
test_string = "github_token = 'ghp_1234567890abcdef1234567890abcdef123456'"
106103
matches = CodegateSignatures.find_in_string(test_string)
@@ -121,7 +118,6 @@ def test_find_in_string_with_github_token():
121118

122119
def test_find_in_string_with_no_matches(temp_yaml_file):
123120
CodegateSignatures.initialize(temp_yaml_file)
124-
CodegateSignatures._signatures_loaded = False # Force reload of signatures
125121

126122
test_string = "No secrets here!"
127123
matches = CodegateSignatures.find_in_string(test_string)
@@ -158,7 +154,6 @@ def test_duplicate_patterns():
158154

159155
try:
160156
CodegateSignatures.initialize(f.name)
161-
CodegateSignatures._signatures_loaded = False # Force reload of signatures
162157

163158
test_string = "aws_key = 'AKIAIOSFODNN7EXAMPLE'"
164159
matches = CodegateSignatures.find_in_string(test_string)

tests/test_cli.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,6 @@ def test_provider_registration(mock_registry, mock_secrets_mgr, mock_pipeline_fa
105105
assert "ollama" in provider_names
106106

107107

108-
@patch("codegate.server.CodegateSignatures")
109-
def test_signatures_initialization(mock_signatures, mock_pipeline_factory) -> None:
110-
"""Test that signatures are initialized correctly."""
111-
init_app(mock_pipeline_factory)
112-
mock_signatures.initialize.assert_called_once_with("signatures.yaml")
113-
114-
115108
def test_pipeline_initialization(mock_pipeline_factory) -> None:
116109
"""Test that pipelines are initialized correctly."""
117110
app = init_app(mock_pipeline_factory)

tests/test_server.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,6 @@ def test_provider_registration(mock_registry, mock_secrets_mgr, mock_pipeline_fa
9999
assert "ollama" in provider_names
100100

101101

102-
@patch("codegate.server.CodegateSignatures")
103-
def test_signatures_initialization(mock_signatures, mock_pipeline_factory) -> None:
104-
"""Test that signatures are initialized correctly."""
105-
init_app(mock_pipeline_factory)
106-
mock_signatures.initialize.assert_called_once_with("signatures.yaml")
107-
108-
109102
def test_pipeline_initialization(mock_pipeline_factory) -> None:
110103
"""Test that pipelines are initialized correctly."""
111104
app = init_app(mock_pipeline_factory)

0 commit comments

Comments
 (0)