Skip to content

Commit 701c175

Browse files
committed
feat(add $VAR expansion from config):
1 parent ba36dcd commit 701c175

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

agentic_security/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ def generate_default_cfg(self, host: str = "0.0.0.0", port: int = 8718):
105105
medium = 0.3
106106
high = 0.5
107107
108+
[secrets]
109+
# Secrets for the security scan from environment variables
110+
OPENAI_API_KEY = "$OPENAI_API_KEY"
111+
DEEPSEEK_API_KEY = "$DEEPSEEK_API_KEY"
108112
109113
""".replace(
110114
"$HOST", host

agentic_security/core/app.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import os
12
from asyncio import Event, Queue
23

34
from fastapi import FastAPI
45

56
tools_inbox: Queue = Queue()
67
stop_event: Event = Event()
78
current_run: str = {"spec": "", "id": ""}
9+
_secrets = {}
810

911

1012
def create_app() -> FastAPI:
@@ -33,3 +35,20 @@ def set_current_run(spec):
3335
current_run["id"] = hash(id(spec))
3436
current_run["spec"] = spec
3537
return current_run
38+
39+
40+
def get_secrets():
41+
return _secrets
42+
43+
44+
def set_secrets(secrets):
45+
_secrets.update(secrets)
46+
expand_secrets(_secrets)
47+
return _secrets
48+
49+
50+
def expand_secrets(secrets):
51+
for key in secrets:
52+
val = secrets[key]
53+
if val.startswith("$"):
54+
secrets[key] = os.getenv(val.strip("$"))

agentic_security/core/test_app.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
import pytest
3+
from agentic_security.core.app import expand_secrets
4+
5+
6+
@pytest.fixture(autouse=True)
7+
def setup_env_vars():
8+
# Set up environment variables for testing
9+
os.environ["TEST_ENV_VAR"] = "test_value"
10+
11+
12+
def test_expand_secrets_with_env_var():
13+
secrets = {"secret_key": "$TEST_ENV_VAR"}
14+
expand_secrets(secrets)
15+
assert secrets["secret_key"] == "test_value"
16+
17+
18+
def test_expand_secrets_without_env_var():
19+
secrets = {"secret_key": "$NON_EXISTENT_VAR"}
20+
expand_secrets(secrets)
21+
assert secrets["secret_key"] is None
22+
23+
24+
def test_expand_secrets_without_dollar_sign():
25+
secrets = {"secret_key": "plain_value"}
26+
expand_secrets(secrets)
27+
assert secrets["secret_key"] == "plain_value"

agentic_security/dependencies.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from agentic_security.config import CfgMixin
2+
from agentic_security.core.app import set_secrets
23

34

45
class InMemorySecrets:
@@ -7,6 +8,7 @@ def __init__(self):
78
self.config = CfgMixin()
89
self.config.get_or_create_config()
910
self.secrets = self.config.config.get("secrets", {})
11+
set_secrets(self.secrets)
1012

1113
def set_secret(self, key: str, value: str):
1214
self.secrets[key] = value

agentic_security/http_spec.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ def parse_http_spec(http_spec: str) -> LLMSpec:
138138
Returns:
139139
LLMSpec: An object representing the parsed HTTP specification, with attributes for the method, URL, headers, and body.
140140
"""
141+
from agentic_security.core.app import get_secrets
142+
143+
secrets = get_secrets()
141144

142145
# Split the spec by lines
143146
lines = http_spec.strip().split("\n")
@@ -164,6 +167,11 @@ def parse_http_spec(http_spec: str) -> LLMSpec:
164167
has_files = "multipart/form-data" in headers.get("Content-Type", "")
165168
has_image = "<<BASE64_IMAGE>>" in body
166169
has_audio = "<<BASE64_AUDIO>>" in body
170+
171+
for key, value in secrets.items():
172+
key = key.strip("$")
173+
body = body.replace(f"${key}", value)
174+
167175
return LLMSpec(
168176
method=method,
169177
url=url,

agentic_security/routes/telemetry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import sentry_sdk
2+
from loguru import logger
23
from sentry_sdk.integrations.logging import ignore_logger
34

45
from ..models.schemas import Settings
5-
from loguru import logger
66

77

88
def setup(app):

0 commit comments

Comments
 (0)