forked from EmpireProject/Empire
-
-
Notifications
You must be signed in to change notification settings - Fork 579
/
agent_api.py
167 lines (138 loc) · 4.51 KB
/
agent_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import math
from datetime import datetime
from fastapi import Depends, HTTPException, Query
from empire.server.api.api_router import APIRouter
from empire.server.api.jwt_auth import get_current_active_user
from empire.server.api.v2.agent.agent_dto import (
Agent,
AgentCheckIns,
AgentCheckInsAggregate,
Agents,
AgentUpdateRequest,
AggregateBucket,
domain_to_dto_agent,
domain_to_dto_agent_checkin,
domain_to_dto_agent_checkin_agg,
)
from empire.server.api.v2.shared_dependencies import CurrentSession
from empire.server.api.v2.shared_dto import (
BadRequestResponse,
NotFoundResponse,
OrderDirection,
)
from empire.server.api.v2.tag import tag_api
from empire.server.core.config import empire_config
from empire.server.core.db import models
from empire.server.server import main
agent_service = main.agentsv2
router = APIRouter(
prefix="/api/v2/agents",
tags=["agents"],
responses={
404: {"description": "Not found", "model": NotFoundResponse},
400: {"description": "Bad request", "model": BadRequestResponse},
},
dependencies=[Depends(get_current_active_user)],
)
async def get_agent(uid: str, db: CurrentSession):
agent = agent_service.get_by_id(db, uid)
if agent:
return agent
raise HTTPException(404, f"Agent not found for id {uid}")
tag_api.add_endpoints_to_taggable(router, "/{uid}/tags", get_agent)
@router.get("/checkins", response_model=AgentCheckIns)
def read_agent_checkins_all(
db: CurrentSession,
agents: list[str] = Query(None),
limit: int = 1000,
page: int = 1,
start_date: datetime | None = None,
end_date: datetime | None = None,
order_direction: OrderDirection = OrderDirection.desc,
):
checkins, total = agent_service.get_agent_checkins(
db, agents, limit, (page - 1) * limit, start_date, end_date, order_direction
)
checkins = [domain_to_dto_agent_checkin(x) for x in checkins]
return AgentCheckIns(
records=checkins,
page=page,
total_pages=math.ceil(total / limit),
limit=limit,
total=total,
)
@router.get("/checkins/aggregate", response_model=AgentCheckInsAggregate)
def read_agent_checkins_aggregate(
db: CurrentSession,
agents: list[str] = Query(None),
start_date: datetime | None = None,
end_date: datetime | None = None,
bucket_size: AggregateBucket | None = AggregateBucket.day,
):
if empire_config.database.use == "sqlite":
raise HTTPException(
400,
"Aggregate checkins not supported with sqlite. Please use MySQL.",
)
checkins = agent_service.get_agent_checkins_aggregate(
db, agents, start_date, end_date, bucket_size
)
checkins = [domain_to_dto_agent_checkin_agg(x) for x in checkins]
return AgentCheckInsAggregate(
records=checkins,
start_date=start_date,
end_date=end_date,
bucket_size=bucket_size,
)
@router.get("/{uid}", response_model=Agent)
async def read_agent(uid: str, db_agent: models.Agent = Depends(get_agent)):
return domain_to_dto_agent(db_agent)
@router.get("/", response_model=Agents)
async def read_agents(
db: CurrentSession,
include_archived: bool = False,
include_stale: bool = True,
):
agents = [
domain_to_dto_agent(x)
for x in agent_service.get_all(db, include_archived, include_stale)
]
return {"records": agents}
@router.put("/{uid}", response_model=Agent)
async def update_agent(
uid: str,
agent_req: AgentUpdateRequest,
db: CurrentSession,
db_agent: models.Agent = Depends(get_agent),
):
resp, err = agent_service.update_agent(db, db_agent, agent_req)
if err:
raise HTTPException(status_code=400, detail=err)
return domain_to_dto_agent(resp)
@router.get("/{uid}/checkins", response_model=AgentCheckIns)
def read_agent_checkins(
db: CurrentSession,
db_agent: models.Agent = Depends(get_agent),
limit: int = -1,
page: int = 1,
start_date: datetime | None = None,
end_date: datetime | None = None,
order_direction: OrderDirection = OrderDirection.desc,
):
checkins, total = agent_service.get_agent_checkins(
db,
[db_agent.session_id],
limit,
(page - 1) * limit,
start_date,
end_date,
order_direction,
)
checkins = [domain_to_dto_agent_checkin(x) for x in checkins]
return AgentCheckIns(
records=checkins,
page=page,
total_pages=math.ceil(total / limit),
limit=limit,
total=total,
)