Agent Key Authentication Guide
Production security and authentication guide for AnyTask CLI workers. This document covers the agent key authentication system for production deployments, including security best practices, key rotation, multi-environment configuration, and compliance considerations.Table of Contents
- Overview
- Quick Start
- Migration Guide
- Architecture
- Fixes Implemented
- Developer Reference
- Troubleshooting
Overview
Agent keys are the PRIMARY and EXCLUSIVE authentication method for all CLI commands and workers. JWT tokens are ONLY used during the login process.What is an Agent Key?
Agent keys are API keys with the prefixanyt_agent_ that provide persistent authentication for:
- CLI commands (
anyt task list, etc.) - Worker processes (
anyt worker run) - Workflow execution and attempt tracking
- Artifact creation
Why Agent Keys?
- Clarity: Clear separation between agent keys (CLI/workers) and JWT tokens (login only)
- Consistency: All operations use the same authentication method
- Security: Agent keys are explicitly named and prioritized
- Simplicity: No confusion about which auth method to use
Quick Start
For New Users
Environment Variables
Migration Guide
Configuration Priority
The system loads configuration in this priority order (highest to lowest):ANYT_AGENT_KEYenvironment variable (highest)- Workspace config (
.anyt/anyt.json) -agent_keyfield - Global config (
~/.config/anyt/config.json) -agent_keyfield ANYT_AUTH_TOKENenvironment variable (lowest, auto-migrates if starts withanyt_agent_)
Auto-Migration
Old configurations are automatically migrated: Before:Testing
Architecture
Authentication Flow
Configuration Files
- Workspace Config (
.anyt/anyt.json):
Fixes Implemented
This section documents the three major fixes implemented to support agent key authentication throughout the codebase.Fix 1: Attempt Workflow Start/Finish
Problem: Coordinator was calling generated API services without passing agent key, causing “Header value must be str or bytes, not<class 'NoneType'>” errors.
Solution: Updated src/cli/worker/coordinator.py:
- Added
_load_agent_key()method to load agent key from config - Added
_get_api_config()method to load API URL from config - Updated
_start_attempt()to pass both parameters: - Updated
_finish_attempt()similarly
src/cli/worker/coordinator.py
Fix 2: Authentication System Refactoring
Problem: Mixed authentication with unclear priority between agent keys and JWT tokens. Solution: Refactored entire authentication system:src/cli/config.py - Updated get_effective_config():
- Agent key now takes absolute priority over JWT tokens
- Auto-migrates agent keys stored in wrong field
- Clear documentation about auth_token being login-only
src/cli/client/adapter.py - Updated _build_headers_for_request():
- Changed order: Check agent_key FIRST, then auth_token as fallback
- Always uses X-API-Key header if agent key present
src/cli/commands/auth.py - Updated login command:
- Agent keys now stored in
agent_keyfield (notauth_token) - Auto-detects if provided token is agent key vs JWT
- Clears old auth_token when storing agent key
tests/cli/integration/conftest.py - Updated test fixtures:
- Checks
ANYT_TEST_AGENT_KEYfirst - Auto-detects and stores in correct field
- Backward compatible with
ANYT_TEST_TOKEN
Fix 3: Artifact Creation
Problem: Workflow executor failed to create artifacts with same “Header value must be str or bytes” error. Solution: Updatedsrc/cli/worker/executor.py:
- Added
agent_keyparameter toWorkflowExecutor.__init__() - Added
_get_api_config()method - Updated artifact creation calls to pass both parameters:
src/cli/worker/coordinator.py:
- Moved agent key loading before executor initialization
- Pass agent_key to executor constructor
src/cli/worker/executor.py, src/cli/worker/coordinator.py
Summary of Changes
| Component | File | Changes |
|---|---|---|
| Config | src/cli/config.py | Agent key priority, auto-migration |
| HTTP Client | src/cli/client/adapter.py | Header building priority |
| Login | src/cli/commands/auth.py | Agent key storage, auto-detection |
| Coordinator | src/cli/worker/coordinator.py | Attempt API calls, agent key loading |
| Executor | src/cli/worker/executor.py | Artifact creation API calls |
| Tests | tests/cli/integration/conftest.py | Test fixtures |
Developer Reference
Pattern for Generated API Calls
When using any generated API service function, ALWAYS pass:api_config_override=self._get_api_config()- for correct base URLX_API_Key=self.agent_key- for agent authentication
Helper Method Template
Add this method to any class that calls generated API services:Integration Test Examples
Seetests/cli/integration/test_attempt_workflow_agent_auth.py for comprehensive examples of:
- Testing with agent key authentication
- Testing JWT token authentication (legacy)
- Testing priority when both are present
- Testing graceful handling of missing auth
Troubleshooting
Debug Configuration
Use the debug script to diagnose configuration issues:- Global config status
- Workspace config status
- Environment variables
- Effective configuration
- Common issues and fixes
Common Issues
Issue 1: “Request URL is missing protocol”
Symptom:Issue 2: “Header value must be str or bytes, not <class 'NoneType'>”
Symptom:
-
Ensure agent key is set:
-
If still failing, check that the code passes
X_API_Key=self.agent_keyto generated API calls
Issue 3: “Missing authentication credentials”
Symptom:Verification Commands
Testing
Test Attempt Workflow
Use the test script to verify attempt workflow:Manual Testing
Production Security Best Practices
Key Management
1. Agent Key Storage
Production Environment:2. Key Rotation
Implement regular key rotation to minimize security risk: Rotation Procedure:- Production: Every 90 days
- Staging: Every 180 days
- Development: As needed
3. Key Scoping
Use different agent keys for different purposes:Multi-Environment Configuration
Environment Isolation
Directory Structure:/etc/systemd/system/anyt-worker-prod.service):
/etc/systemd/system/anyt-worker-staging.service):
Configuration Management
Using Environment Switcher:Security Hardening
File Permissions
Secrets File:Network Security
Firewall Rules:Audit Logging
Enable System Audit:Monitoring and Alerting
Authentication Monitoring
Failed Authentication Tracking:- Authentication success/failure rate
- API key expiration warnings
- Unusual API access patterns
- Failed attempt creation rate
- Artifact upload failures
Security Alerts
Alert Conditions:Compliance Considerations
SOC 2 / ISO 27001
Access Control:- ✅ Role-based access to agent keys
- ✅ Audit logging of key access
- ✅ Regular key rotation (90-day cycle)
- ✅ Separation of duties (dev/staging/prod)
- ✅ Secrets stored in system keyring (encrypted at rest)
- ✅ TLS for all API communications (encrypted in transit)
- ✅ No secrets in logs or workflow files
- ✅ Artifact encryption support
GDPR Compliance
Data Minimization:- Only store necessary task data in workflows
- Avoid logging personal information
- Use artifact retention policies
- Implement data deletion workflows
Secret Management Integration
HashiCorp Vault Integration
Example Integration:/usr/local/bin/vault-get-secrets.sh):
AWS Secrets Manager Integration
Incident Response
Compromised Key Response
Immediate Actions:Audit Checklist After Incident
- Identify all systems using compromised key
- Review all attempts created with compromised key
- Check all artifacts created during compromise window
- Verify no unauthorized code changes
- Review git commit history
- Update incident response documentation
- Schedule post-incident review meeting
Related Documentation
- Worker Quick Start: Worker Quick Start
- Worker System Overview: Worker Overview
- CLI Usage: CLI Usage Guide
Status
✅ PRODUCTION READY The authentication system is production-ready with comprehensive security features:- Agent key authentication with proper isolation
- Multi-environment configuration support
- Security hardening guidelines
- Monitoring and audit logging
- Compliance considerations (SOC 2, ISO 27001, GDPR)
- Incident response procedures