-
Notifications
You must be signed in to change notification settings - Fork 0
Use Agent API key for auth #34
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 all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0de1941
Update base_acp_server.py
smoreinis faed7c0
Merge branch 'anishxyz/authn' into stas/agent-auth
smoreinis 32aafd0
use agent api key
smoreinis b934090
Merge branch 'main' into stas/agent-auth
smoreinis 1fc6c59
.
smoreinis 66c3959
Merge branch 'main' into stas/agent-auth
smoreinis 0d939db
check against server hash
smoreinis 1ec4851
update
smoreinis 14fefa1
Update uv.lock
smoreinis 0bdab5d
.
smoreinis 4dddf20
Merge branch 'main' into stas/agent-auth
smoreinis e674c84
add agent registration to temporal worker
smoreinis 5d9d55a
Update acp.py
smoreinis c18171e
Update acp.py
smoreinis 5e24b79
masking api key for logging
smoreinis 90c02d2
Update base_acp_server.py
smoreinis ae3c988
x-agent-api-key
smoreinis 9b612a2
Update worker.py
smoreinis 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
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
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,101 @@ | ||
| import base64 | ||
| import json | ||
| import os | ||
| import httpx | ||
| import asyncio | ||
|
|
||
| from agentex.lib.environment_variables import EnvironmentVariables, refreshed_environment_variables | ||
| from agentex.lib.utils.logging import make_logger | ||
|
|
||
| logger = make_logger(__name__) | ||
|
|
||
| def get_auth_principal(env_vars: EnvironmentVariables): | ||
| if not env_vars.AUTH_PRINCIPAL_B64: | ||
| return None | ||
|
|
||
| try: | ||
| decoded_str = base64.b64decode(env_vars.AUTH_PRINCIPAL_B64).decode('utf-8') | ||
| return json.loads(decoded_str) | ||
| except Exception: | ||
| return None | ||
|
|
||
| async def register_agent(env_vars: EnvironmentVariables): | ||
| """Register this agent with the Agentex server""" | ||
| if not env_vars.AGENTEX_BASE_URL: | ||
| logger.warning("AGENTEX_BASE_URL is not set, skipping registration") | ||
| return | ||
| # Build the agent's own URL | ||
| full_acp_url = f"{env_vars.ACP_URL.rstrip('/')}:{env_vars.ACP_PORT}" | ||
|
|
||
| description = ( | ||
| env_vars.AGENT_DESCRIPTION | ||
| or f"Generic description for agent: {env_vars.AGENT_NAME}" | ||
| ) | ||
|
|
||
| # Prepare registration data | ||
| registration_data = { | ||
| "name": env_vars.AGENT_NAME, | ||
| "description": description, | ||
| "acp_url": full_acp_url, | ||
| "acp_type": env_vars.ACP_TYPE, | ||
| "principal_context": get_auth_principal(env_vars) | ||
| } | ||
|
|
||
| if env_vars.AGENT_ID: | ||
| registration_data["agent_id"] = env_vars.AGENT_ID | ||
|
|
||
| # Make the registration request | ||
| registration_url = f"{env_vars.AGENTEX_BASE_URL.rstrip('/')}/agents/register" | ||
| # Retry logic with configurable attempts and delay | ||
| max_retries = 3 | ||
| base_delay = 5 # seconds | ||
| last_exception = None | ||
|
|
||
| attempt = 0 | ||
| while attempt < max_retries: | ||
| try: | ||
| async with httpx.AsyncClient() as client: | ||
| response = await client.post( | ||
| registration_url, json=registration_data, timeout=30.0 | ||
| ) | ||
| if response.status_code == 200: | ||
| agent = response.json() | ||
| agent_id, agent_name = agent["id"], agent["name"] | ||
| agent_api_key = agent["agent_api_key"] | ||
|
|
||
| os.environ["AGENT_ID"] = agent_id | ||
| os.environ["AGENT_NAME"] = agent_name | ||
| os.environ["AGENT_API_KEY"] = agent_api_key | ||
| env_vars.AGENT_ID = agent_id | ||
| env_vars.AGENT_NAME = agent_name | ||
| env_vars.AGENT_API_KEY = agent_api_key | ||
| global refreshed_environment_variables | ||
| refreshed_environment_variables = env_vars | ||
| logger.info( | ||
| f"Successfully registered agent '{env_vars.AGENT_NAME}' with Agentex server with acp_url: {full_acp_url}. Registration data: {registration_data}" | ||
smoreinis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| return # Success, exit the retry loop | ||
smoreinis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else: | ||
| error_msg = f"Failed to register agent. Status: {response.status_code}, Response: {response.text}" | ||
| logger.error(error_msg) | ||
| last_exception = Exception( | ||
| f"Failed to startup agent: {response.text}" | ||
| ) | ||
|
|
||
| except Exception as e: | ||
| logger.error( | ||
| f"Exception during agent registration attempt {attempt + 1}: {e}" | ||
| ) | ||
| last_exception = e | ||
| attempt += 1 | ||
| if attempt < max_retries: | ||
| delay = (attempt) * base_delay # 5, 10, 15 seconds | ||
| logger.info( | ||
| f"Retrying in {delay} seconds... (attempt {attempt}/{max_retries})" | ||
| ) | ||
| await asyncio.sleep(delay) | ||
|
|
||
| # If we get here, all retries failed | ||
| raise last_exception or Exception( | ||
| f"Failed to register agent after {max_retries} attempts" | ||
| ) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.