-
Notifications
You must be signed in to change notification settings - Fork 33
feat(agw): add implicit auditlog usage on agw #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ricardosrib
wants to merge
12
commits into
main
Choose a base branch
from
feat/add-implicit-auditlog-on-agw
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bb614bd
feat: add implicit audit log usage on agent gateway
ricardosrib fa3c770
create audit client on initialization time of the agw client
ricardosrib bd60cd3
update unit tests
ricardosrib 74d50c6
Merge branch 'main' into feat/add-implicit-auditlog-on-agw
ricardosrib f99f6b4
version bump
ricardosrib 5515bea
resolve pre-commit issue
ricardosrib 8d5b08e
move import to the top
ricardosrib 02dc447
ruff format
ricardosrib 5550854
Merge branch 'main' into feat/add-implicit-auditlog-on-agw
ricardosrib 2c7eb75
move the auditlog helpers in agw to a separate .py file and update un…
ricardosrib f7d74b8
created AuditLogMode config to control audit logging failure behavior
ricardosrib c16ac44
keep auditlogging only on call_mcp_tool()
ricardosrib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """Audit log helpers for the Agent Gateway client.""" | ||
|
|
||
| import logging | ||
| from datetime import datetime, timezone | ||
| from typing import Callable | ||
|
|
||
| import sap_cloud_sdk.core.auditlog_ng as auditlog_ng | ||
| from sap_cloud_sdk.agentgateway.config import AuditLogMode | ||
| from sap_cloud_sdk.core.auditlog_ng import AuditClient | ||
| from sap_cloud_sdk.core.auditlog_ng.gen.sap.auditlog.auditevent.v2 import ( | ||
| auditevent_pb2 as pb, | ||
| ) | ||
| from sap_cloud_sdk.core.telemetry import Module, get_tenant_id | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def create_audit_client( | ||
| tenant_subdomain: str | Callable[[], str] | None, | ||
| module: Module, | ||
| mode: AuditLogMode = AuditLogMode.BEST_EFFORT, | ||
| ) -> AuditClient | None: | ||
| """Create an audit client from a LoB destination. Returns None on failure.""" | ||
| if mode is AuditLogMode.DISABLED: | ||
| return None | ||
| resolved = tenant_subdomain() if callable(tenant_subdomain) else tenant_subdomain | ||
| if not resolved: | ||
| return None | ||
| try: | ||
| return auditlog_ng.create_client( | ||
| tenant=resolved, | ||
| _telemetry_source=module, | ||
| ) | ||
| except Exception: | ||
| if mode is AuditLogMode.STRICT: | ||
| raise | ||
| logger.warning("Failed to create audit client — audit events will not be recorded", exc_info=True) | ||
| return None | ||
|
|
||
|
|
||
| def send_audit_event( | ||
| audit_client: AuditClient | None, | ||
| object_id: str, | ||
| user_id: str | None = None, | ||
| mode: AuditLogMode = AuditLogMode.BEST_EFFORT, | ||
| ) -> None: | ||
| """Send a DataAccess audit event.""" | ||
| if mode is AuditLogMode.DISABLED: | ||
| return | ||
| tenant_id = get_tenant_id() | ||
| if audit_client is None or not tenant_id: | ||
| return | ||
| try: | ||
| event = pb.DataAccess() | ||
| event.common.timestamp.FromDatetime(datetime.now(timezone.utc)) | ||
| event.common.tenant_id = tenant_id | ||
| if user_id: | ||
| event.common.user_initiator_id = user_id | ||
| event.channel_type = "MCP" | ||
| event.channel_id = "agent-gateway" | ||
| event.object_type = "mcp-tool" | ||
| event.object_id = object_id | ||
| audit_client.send(event) | ||
| except Exception: | ||
| if mode is AuditLogMode.STRICT: | ||
| raise | ||
| logger.warning("Failed to send audit event", exc_info=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed implicit auditlog from list_mcp_tool() and kept it only on call_mcp_tool()