Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /api /routes /workflows.py
| """ | |
| Workflow API Routes | |
| =================== | |
| REST API endpoints for managing multi-agent pentest workflows. | |
| """ | |
| import logging | |
| from typing import Any, List, Optional | |
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from pydantic import BaseModel | |
| from agents.workflows import WorkflowOrchestrator | |
| logger = logging.getLogger("api.workflows") | |
| router = APIRouter(prefix="/api/v1/workflows", tags=["Workflows"]) | |
| def get_orchestrator(): | |
| """Get workflow orchestrator instance""" | |
| from agents.workflows.orchestrator import get_workflow_orchestrator | |
| return get_workflow_orchestrator() | |
| # Simple auth dependency | |
| def get_current_user(): | |
| return {"sub": "admin", "role": "admin"} | |
| async def list_workflow_types( | |
| orchestrator: WorkflowOrchestrator = Depends(get_orchestrator), | |
| user: Any = Depends(get_current_user), | |
| ): | |
| """ | |
| List available workflow types. | |
| Returns all predefined workflow templates. | |
| """ | |
| types = orchestrator.get_available_workflow_types() | |
| return { | |
| "types": [ | |
| { | |
| "id": key, | |
| "name": value["name"], | |
| "description": value["description"], | |
| "steps": value["steps"], | |
| } | |
| for key, value in types.items() | |
| ] | |
| } | |
| class CreateWorkflowRequest(BaseModel): | |
| """Create workflow request""" | |
| type: str | |
| target: str | |
| agents: List[str] = [] | |
| parameters: dict = {} | |
| async def create_workflow( | |
| request: CreateWorkflowRequest, | |
| orchestrator: WorkflowOrchestrator = Depends(get_orchestrator), | |
| user: Any = Depends(get_current_user), | |
| ): | |
| """ | |
| Start a new workflow. | |
| Request: | |
| { | |
| "type": "network_recon", | |
| "target": "192.168.1.0/24", | |
| "agents": ["agent-1", "agent-2"], | |
| "parameters": {"ports": "top1000"} | |
| } | |
| """ | |
| workflow_type = request.type | |
| target = request.target | |
| agents = request.agents | |
| parameters = request.parameters | |
| if not workflow_type or not target: | |
| raise HTTPException( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| detail="Workflow type and target are required", | |
| ) | |
| if not agents: | |
| raise HTTPException( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| detail="At least one agent is required", | |
| ) | |
| try: | |
| workflow_id = await orchestrator.start_workflow( | |
| workflow_type=workflow_type, | |
| target=target, | |
| agents=agents, | |
| parameters=parameters, | |
| ) | |
| return { | |
| "workflow_id": workflow_id, | |
| "status": "pending", | |
| "message": "Workflow started successfully", | |
| } | |
| except ValueError as e: | |
| raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) | |
| except Exception as e: | |
| logger.exception(f"Failed to start workflow: {e}") | |
| raise HTTPException( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| detail="Failed to start workflow", | |
| ) | |
| async def list_workflows( | |
| status: Optional[str] = None, | |
| orchestrator: WorkflowOrchestrator = Depends(get_orchestrator), | |
| user: Any = Depends(get_current_user), | |
| ): | |
| """List workflows.""" | |
| workflows = orchestrator.list_workflows(state=status) | |
| return {"workflows": workflows, "count": len(workflows)} | |
| async def get_workflow( | |
| workflow_id: str, | |
| orchestrator: WorkflowOrchestrator = Depends(get_orchestrator), | |
| user: Any = Depends(get_current_user), | |
| ): | |
| """Get workflow status and details.""" | |
| workflow_status = orchestrator.get_workflow_status(workflow_id) | |
| if not workflow_status: | |
| raise HTTPException( | |
| status_code=status.HTTP_404_NOT_FOUND, | |
| detail=f"Workflow {workflow_id} not found", | |
| ) | |
| return workflow_status | |
| async def cancel_workflow( | |
| workflow_id: str, | |
| orchestrator: WorkflowOrchestrator = Depends(get_orchestrator), | |
| user: Any = Depends(get_current_user), | |
| ): | |
| """Cancel a running workflow.""" | |
| success = await orchestrator.cancel_workflow(workflow_id) | |
| if not success: | |
| raise HTTPException( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| detail=f"Workflow {workflow_id} not found or not running", | |
| ) | |
| return { | |
| "workflow_id": workflow_id, | |
| "status": "cancelled", | |
| "message": "Workflow cancelled successfully", | |
| } | |
| async def get_workflow_findings( | |
| workflow_id: str, | |
| orchestrator: WorkflowOrchestrator = Depends(get_orchestrator), | |
| user: Any = Depends(get_current_user), | |
| ): | |
| """Get findings from a workflow.""" | |
| workflow_status = orchestrator.get_workflow_status(workflow_id) | |
| if not workflow_status: | |
| raise HTTPException( | |
| status_code=status.HTTP_404_NOT_FOUND, | |
| detail=f"Workflow {workflow_id} not found", | |
| ) | |
| # Get full workflow object to access findings | |
| orch = get_orchestrator() | |
| if workflow_id in orch.workflows: | |
| workflow = orch.workflows[workflow_id] | |
| return { | |
| "workflow_id": workflow_id, | |
| "findings": workflow.findings, | |
| "count": len(workflow.findings), | |
| } | |
| return {"workflow_id": workflow_id, "findings": [], "count": 0} | |
| async def submit_task_result( | |
| workflow_id: str, | |
| task_id: str, | |
| result: dict, | |
| orchestrator: WorkflowOrchestrator = Depends(get_orchestrator), | |
| user: Any = Depends(get_current_user), | |
| ): | |
| """Submit task result (for agent use).""" | |
| await orchestrator.submit_task_result(task_id, result) | |
| return { | |
| "task_id": task_id, | |
| "status": "received", | |
| "message": "Task result submitted", | |
| } | |
Xet Storage Details
- Size:
- 5.92 kB
- Xet hash:
- ca46a3a2325c4cacdc0258f7e8569fdaec586d7a97aa8a5e38d41a7d5d75cf8e
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.