Secret Manager for ADK¶
Supported in ADKPython v1.29.0
The Secret Manager integration provides a standard interface for ADK agents to retrieve sensitive credentials (such as API keys, database passwords, and private keys) at runtime. This approach ensures that sensitive information isn't hardcoded in the source code or exposed in the LLM's context window, conversation history, or observability logs.
Use cases¶
- Just-in-time tool authorization: Storing static API keys in agent initialization code is insecure. With this integration, the ADK agent dynamically retrieves credentials from Secret Manager at runtime, ensuring keys are loaded into memory on demand.
- Secure multi-tenant workflows: To avoid passing raw user tokens from frontend, agents can map user IDs to specific Secret Manager resources. A
before_agent_callbackhook dynamically retrieves the user's secret to securely rehydrate thesession.stateOAuth token. - Encrypted system tasks: Background system tasks, such as database polling, retrieve credentials directly from Secret Manager inside the tool logic. This prevents passwords from entering the LLM's conversation history and exposes only execution summary to the model.
Prerequisites¶
- Required Software Versions: ADK Python version v1.29.0 or higher
- Required Accounts / APIs: A Google Cloud Project with the Secret Manager API and Agent Development Kit API enabled.
Complete the following setup steps:
- Set up an agent with ADK.
- Create a secret (such as an API key) in Secret Manager.
- Grant the
Secret Manager Secret AccessorIAM role to your agent identity.
Installation¶
Use with agent¶
import os
from google.adk import Agent
from google.adk.integrations.secret_manager.secret_client import SecretManagerClient
# Fetch secret from global Secret Manager
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")
secret_id = os.environ.get("ADK_TEST_SECRET_ID")
secret_version = os.environ.get("ADK_TEST_SECRET_VERSION", "latest")
if not project_id or not secret_id:
raise ValueError("GOOGLE_CLOUD_PROJECT and ADK_TEST_SECRET_ID environment variables must be set.")
resource_name = f"projects/{project_id}/secrets/{secret_id}/versions/{secret_version}"
print("Fetching secret from global Secret Manager...")
# Initialize Secret Manager Client (Global)
client = SecretManagerClient()
# Fetch secret
try:
secret_payload = client.get_secret(resource_name)
print("Successfully fetched secret.")
# The secret_payload can now be used by the agent or its tools as required.
except Exception as e:
print(f"Error fetching secret: {e}")
raise e
# Initialize Agent
root_agent = Agent(
model='gemini-2.5-flash',
name='root_agent',
description='A helpful assistant for user questions.',
instruction='Answer user questions to the best of your knowledge',
)
print("Agent initialized successfully.")