-
Notifications
You must be signed in to change notification settings - Fork 55
feat: add agent authorization in agent callback #303
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
efb7dfa
feat: add agent authorization in agent callback
BhAem 0ab0f32
fix callback addition
BhAem ba66c4a
Merge branch 'main' into feat/authz_agent
BhAem a642866
fix: change workload id and using assumeRole
BhAem c370908
fix: fix docstrings
BhAem 8f674d1
Merge branch 'main' into feat/authz_agent
BhAem bca1468
fix: fix comment issues and add getting role_trn from VEFAAS_IAM_CRID…
BhAem 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,99 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| from typing import Optional | ||
|
|
||
| from google.genai import types | ||
| from google.adk.agents.callback_context import CallbackContext | ||
|
|
||
| from veadk.auth.veauth.utils import get_credential_from_vefaas_iam | ||
| from veadk.utils.logger import get_logger | ||
| from veadk.utils.volcengine_sign import ve_request | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| def check_agent_authorization( | ||
| callback_context: CallbackContext, | ||
| ) -> Optional[types.Content]: | ||
| """Callback to check agent authorization before execution. | ||
|
|
||
| Args: | ||
| callback_context (CallbackContext): The callback context containing agent info. | ||
|
|
||
| Returns: | ||
| Optional[types.Content]: If authorization fails, return a Content object with | ||
| an error message; otherwise, return None to proceed. | ||
| """ | ||
| ak = callback_context.state.get("VOLCENGINE_ACCESS_KEY") | ||
| sk = callback_context.state.get("VOLCENGINE_SECRET_KEY") | ||
| session_token = "" | ||
|
|
||
| if not (ak and sk): | ||
| logger.debug("Get AK/SK from tool context failed.") | ||
| ak = os.getenv("VOLCENGINE_ACCESS_KEY") | ||
| sk = os.getenv("VOLCENGINE_SECRET_KEY") | ||
| if not (ak and sk): | ||
| logger.debug("Get AK/SK from environment variables failed.") | ||
| credential = get_credential_from_vefaas_iam() | ||
| ak = credential.access_key_id | ||
| sk = credential.secret_access_key | ||
| session_token = credential.session_token | ||
| else: | ||
| logger.debug("Successfully get AK/SK from environment variables.") | ||
| else: | ||
| logger.debug("Successfully get AK/SK from tool context.") | ||
|
|
||
| agent_name = callback_context.agent_name | ||
| user_id = callback_context._invocation_context.user_id | ||
|
|
||
| namespace = "default" | ||
| user_id = user_id | ||
| action = "invoke" | ||
| workload_id = agent_name | ||
|
||
|
|
||
| response = ve_request( | ||
| request_body={ | ||
| "NamespaceName": namespace, | ||
| "Principal": {"Type": "User", "Id": user_id}, | ||
| "Operation": {"Type": "Action", "Id": action}, | ||
| "Resource": {"Type": "Agent", "Id": workload_id}, | ||
| }, | ||
| action="CheckPermission", | ||
| ak=ak, | ||
| sk=sk, | ||
| service="id", | ||
| version="2025-10-30", | ||
| region="cn-beijing", | ||
| host="open.volcengineapi.com", | ||
| header={"X-Security-Token": session_token}, | ||
| ) | ||
|
|
||
| try: | ||
| allowed = response["Result"]["Allowed"] | ||
| if allowed: | ||
| logger.debug("Agent is authorized to run.") | ||
| return None | ||
| else: | ||
| logger.warning("Agent is not authorized to run.") | ||
| return types.Content( | ||
| parts=[ | ||
| types.Part(text=f"Agent {agent_name} is not authorized to run.") | ||
| ], | ||
| role="model", | ||
| ) | ||
| except Exception as e: | ||
| logger.error(f"Authorization check failed: {e}") | ||
| return None | ||
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.
和方法的默认参数重复