Skip to content

Commit 3574697

Browse files
committed
Updating sensitive data suppression logic.
Signed-off-by: Eric Evans <[email protected]>
1 parent ae0c6d1 commit 3574697

File tree

4 files changed

+76
-14
lines changed

4 files changed

+76
-14
lines changed

src/aiq/front_ends/fastapi/fastapi_front_end_plugin_worker.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import typing
2121
from abc import ABC
2222
from abc import abstractmethod
23+
from collections.abc import Awaitable
24+
from collections.abc import Callable
2325
from contextlib import asynccontextmanager
2426
from functools import partial
2527
from pathlib import Path
@@ -123,18 +125,8 @@ async def lifespan(starting_app: FastAPI):
123125
self.set_cors_config(aiq_app)
124126

125127
@aiq_app.middleware("http")
126-
async def _suppress_authentication_logs(request: Request, call_next):
127-
"""
128-
Intercepts authentication request and supreses logs that contain sensitive data.
129-
"""
130-
default_log_level = logging.getLogger("uvicorn.access").level
131-
if request.url.path in (AuthenticationEndpoint.REDIRECT_URI.value
132-
or AuthenticationEndpoint.PROMPT_REDIRECT_URI.value):
133-
logging.getLogger("uvicorn.access").setLevel(logging.WARNING)
134-
response = await call_next(request)
135-
logging.getLogger("uvicorn.access").setLevel(default_log_level)
136-
137-
return response
128+
async def authentication_log_filter(request: Request, call_next: Callable[[Request], Awaitable[Response]]):
129+
return await self._suppress_authentication_logs(request, call_next)
138130

139131
return aiq_app
140132

@@ -170,6 +162,25 @@ def set_cors_config(self, aiq_app: FastAPI) -> None:
170162
**cors_kwargs,
171163
)
172164

165+
async def _suppress_authentication_logs(self, request: Request,
166+
call_next: Callable[[Request], Awaitable[Response]]) -> Response:
167+
"""
168+
Intercepts authentication request and supreses logs that contain sensitive data.
169+
"""
170+
from aiq.utils.log_utils import LogFilter
171+
172+
logs_to_suppress: list[str] = [
173+
AuthenticationEndpoint.REDIRECT_URI.value, AuthenticationEndpoint.PROMPT_REDIRECT_URI.value
174+
]
175+
176+
logging.getLogger("uvicorn.access").addFilter(LogFilter(logs_to_suppress))
177+
try:
178+
response = await call_next(request)
179+
finally:
180+
logging.getLogger("uvicorn.access").removeFilter(LogFilter(logs_to_suppress))
181+
182+
return response
183+
173184
@abstractmethod
174185
async def configure(self, app: FastAPI, builder: WorkflowBuilder):
175186
pass

src/aiq/runtime/session.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
import httpx
1716
import asyncio
1817
import contextvars
1918
import typing
20-
2119
from collections.abc import Awaitable
2220
from collections.abc import Callable
2321
from contextlib import asynccontextmanager
2422
from contextlib import nullcontext
2523

24+
import httpx
2625
from fastapi import Request
2726

2827
from aiq.builder.context import AIQContext

src/aiq/utils/log_utils.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import logging
17+
18+
19+
class LogFilter(logging.Filter):
20+
"""
21+
This class is used to filter log records based on a defined set of criteria.
22+
"""
23+
24+
def __init__(self, filter_criteria: list[str]):
25+
self._filter_criteria = filter_criteria
26+
super().__init__()
27+
28+
def filter(self, record: logging.LogRecord):
29+
"""
30+
Evaluates whether a log record should be emitted based on the message content.
31+
32+
Returns:
33+
False if the message content contains any of the filter criteria, True otherwise.
34+
"""
35+
if any(match in record.getMessage() for match in self._filter_criteria):
36+
return False
37+
return True

tests/aiq/authentication/test_authentication.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
116
import webbrowser
217
from datetime import datetime
318
from datetime import timedelta

0 commit comments

Comments
 (0)