Skip to content

Commit 3c0c98b

Browse files
authored
Merge pull request #502 from omertuc/private
LCORE-395: config endpoint should not leak secrets
2 parents e42062c + 626137b commit 3c0c98b

File tree

9 files changed

+43
-19
lines changed

9 files changed

+43
-19
lines changed

src/app/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _create_postgres_engine(
5959
) -> Engine:
6060
"""Create PostgreSQL database engine."""
6161
postgres_url = (
62-
f"postgresql://{config.user}:{config.password}@"
62+
f"postgresql://{config.user}:{config.password.get_secret_value()}@"
6363
f"{config.host}:{config.port}/{config.db}"
6464
f"?sslmode={config.ssl_mode}&gssencmode={config.gss_encmode}"
6565
)

src/app/endpoints/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"llama_stack": {
3838
"url": "http://localhost:8321",
39-
"api_key": "xyzzy",
39+
"api_key": "*****",
4040
"use_as_library_client": False,
4141
"library_client_config_path": None,
4242
},

src/app/endpoints/query.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,8 @@ async def query_endpoint_handler(
176176
# Enforce RBAC: optionally disallow overriding model/provider in requests
177177
validate_model_provider_override(query_request, request.state.authorized_actions)
178178

179-
# log Llama Stack configuration, but without sensitive information
180-
llama_stack_config = configuration.llama_stack_configuration.model_copy()
181-
llama_stack_config.api_key = "********"
182-
logger.info("Llama stack config: %s", llama_stack_config)
179+
# log Llama Stack configuration
180+
logger.info("Llama stack config: %s", configuration.llama_stack_configuration)
183181

184182
user_id, _, token = auth
185183

src/app/endpoints/streaming_query.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,8 @@ async def streaming_query_endpoint_handler( # pylint: disable=too-many-locals
552552
# Enforce RBAC: optionally disallow overriding model/provider in requests
553553
validate_model_provider_override(query_request, request.state.authorized_actions)
554554

555-
# log Llama Stack configuration, but without sensitive information
556-
llama_stack_config = configuration.llama_stack_configuration.model_copy()
557-
llama_stack_config.api_key = "********"
558-
logger.info("Llama stack config: %s", llama_stack_config)
555+
# log Llama Stack configuration
556+
logger.info("Llama stack config: %s", configuration.llama_stack_configuration)
559557

560558
user_id, _user_name, token = auth
561559

src/client.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ async def load(self, llama_stack_config: LlamaStackConfiguration) -> None:
3838
else:
3939
logger.info("Using Llama stack running as a service")
4040
self._lsc = AsyncLlamaStackClient(
41-
base_url=llama_stack_config.url, api_key=llama_stack_config.api_key
41+
base_url=llama_stack_config.url,
42+
api_key=(
43+
llama_stack_config.api_key.get_secret_value()
44+
if llama_stack_config.api_key is not None
45+
else None
46+
),
4247
)
4348

4449
def get_client(self) -> AsyncLlamaStackClient:

src/models/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
FilePath,
1717
AnyHttpUrl,
1818
PositiveInt,
19+
SecretStr,
1920
)
2021
from typing_extensions import Self, Literal
2122

@@ -80,7 +81,7 @@ class PostgreSQLDatabaseConfiguration(ConfigurationBase):
8081
port: PositiveInt = 5432
8182
db: str
8283
user: str
83-
password: str
84+
password: SecretStr
8485
namespace: Optional[str] = "lightspeed-stack"
8586
ssl_mode: str = constants.POSTGRES_DEFAULT_SSL_MODE
8687
gss_encmode: str = constants.POSTGRES_DEFAULT_GSS_ENCMODE
@@ -167,7 +168,7 @@ class LlamaStackConfiguration(ConfigurationBase):
167168
"""Llama stack configuration."""
168169

169170
url: Optional[str] = None
170-
api_key: Optional[str] = None
171+
api_key: Optional[SecretStr] = None
171172
use_as_library_client: Optional[bool] = None
172173
library_client_config_path: Optional[str] = None
173174

tests/integration/test_configuration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_loading_proper_configuration(configuration_filename: str) -> None:
5858
ls_config = cfg.llama_stack_configuration
5959
assert ls_config.use_as_library_client is False
6060
assert ls_config.url == "http://localhost:8321"
61-
assert ls_config.api_key == "xyzzy"
61+
assert ls_config.api_key.get_secret_value() == "xyzzy"
6262

6363
# check 'user_data_collection' section
6464
udc_config = cfg.user_data_collection_configuration

tests/unit/models/test_config.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,18 @@ def test_dump_configuration(tmp_path) -> None:
537537
user_data_collection=UserDataCollection(
538538
feedback_enabled=False, feedback_storage=None
539539
),
540+
database=DatabaseConfiguration(
541+
sqlite=None,
542+
postgres=PostgreSQLDatabaseConfiguration(
543+
db="lightspeed_stack",
544+
user="ls_user",
545+
password="ls_password",
546+
port=5432,
547+
ca_cert_path=None,
548+
ssl_mode="require",
549+
gss_encmode="disable",
550+
),
551+
),
540552
mcp_servers=[],
541553
customization=None,
542554
inference=InferenceConfiguration(
@@ -601,8 +613,8 @@ def test_dump_configuration(tmp_path) -> None:
601613
},
602614
"llama_stack": {
603615
"url": None,
604-
"api_key": "whatever",
605616
"use_as_library_client": True,
617+
"api_key": "**********",
606618
"library_client_config_path": "tests/configuration/run.yaml",
607619
},
608620
"user_data_collection": {
@@ -625,8 +637,18 @@ def test_dump_configuration(tmp_path) -> None:
625637
"default_model": "default_model",
626638
},
627639
"database": {
628-
"sqlite": {"db_path": "/tmp/lightspeed-stack.db"},
629-
"postgres": None,
640+
"sqlite": None,
641+
"postgres": {
642+
"host": "localhost",
643+
"port": 5432,
644+
"db": "lightspeed_stack",
645+
"user": "ls_user",
646+
"password": "**********",
647+
"ssl_mode": "require",
648+
"gss_encmode": "disable",
649+
"namespace": "lightspeed-stack",
650+
"ca_cert_path": None,
651+
},
630652
},
631653
"authorization": None,
632654
}
@@ -980,7 +1002,7 @@ def test_postgresql_database_configuration() -> None:
9801002
assert c.port == 5432
9811003
assert c.db == "db"
9821004
assert c.user == "user"
983-
assert c.password == "password"
1005+
assert c.password.get_secret_value() == "password"
9841006
assert c.ssl_mode == POSTGRES_DEFAULT_SSL_MODE
9851007
assert c.gss_encmode == POSTGRES_DEFAULT_GSS_ENCMODE
9861008
assert c.namespace == "lightspeed-stack"

tests/unit/test_configuration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_init_from_dict() -> None:
8383
assert cfg.configuration.name == "foo"
8484

8585
# check for llama_stack_configuration subsection
86-
assert cfg.llama_stack_configuration.api_key == "xyzzy"
86+
assert cfg.llama_stack_configuration.api_key.get_secret_value() == "xyzzy"
8787
assert cfg.llama_stack_configuration.url == "http://x.y.com:1234"
8888
assert cfg.llama_stack_configuration.use_as_library_client is False
8989

0 commit comments

Comments
 (0)