-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.py
More file actions
190 lines (156 loc) · 6.89 KB
/
client.py
File metadata and controls
190 lines (156 loc) · 6.89 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from itertools import count
from typing import Iterator
from typing import List
from typing import Optional
from typing import Union
import requests
from ..enums import SortDirection
from .models import Agent
from .models import AgentsPage
from .models import AgentType
from .models import AgentUpdateRequest
from .models import QueryAgentsRequest
from .models import SortKeys
class AgentsV1:
"""
Client for `/v1/agents` endpoints.
Usage example:
>>> import incydr
>>> client = incydr.Client(**kwargs)
>>> client.agents.v1.get_page()
"""
def __init__(self, parent):
self._parent = parent
def get_page(
self,
active: bool = None,
agent_type: Optional[AgentType] = None,
sort_dir: SortDirection = SortDirection.ASC,
sort_key: SortKeys = SortKeys.NAME,
page_num: int = 1,
page_size: int = 500,
agent_healthy: bool = None,
agent_health_issue_types: Union[List[str], str] = None,
agent_health_modified_in_last_days: Optional[int] = None,
user_id: str = None,
) -> AgentsPage:
"""
Get a page of agents.
Filter results by passing the appropriate parameters:
**Parameters**:
* **active**: `bool | None` - When `True`, return only active agents. When `False`, return only deactivated agents. Defaults to `None` (returning both).
* **agent_type**: `AgentType | str | None` - Return only agents of given type.
* **page_num**: `int` - Page number for results, starting at 1.
* **page_size**: `int` - Max number of results to return per page.
* **sort_dir**: `SortDirection` - `asc` or `desc`. The direction in which to sort the response based on the corresponding key. Defaults to `asc`.
* **sort_key**: [`SortKeys`][agents-sort-keys] - Values on which the response will be sorted. Defaults to agent name.
* **agent_healthy**: `bool | None` - Optionally retrieve agents with this health status. Agents that have no health issue types are considered healthy.
* **agent_health_issue_types**: `List[str] | str` - Optionally retrieve agents that have (at least) any of the given issue type(s). Health issue types include the following: `NOT_CONNECTING`, `NOT_SENDING_SECURITY_EVENTS`, `SECURITY_INGEST_REJECTED`, `MISSING_MACOS_PERMISSION_FULL_DISK_ACCESS`, `MISSING_MACOS_PERMISSION_ACCESSIBILITY`.
* **agent_health_modified_in_last_days**: `int | None` - Optionally retrieve agents that have had their agent health modified in the last N days.
* **user_id**: `str` - Optionally retrieve only agents associated with this user ID.
**Returns**: An [`AgentsPage`][agentspage-model] object.
"""
data = QueryAgentsRequest(
active=active,
agentType=agent_type,
agentHealthy=agent_healthy,
anyOfAgentHealthIssueTypes=[agent_health_issue_types]
if isinstance(agent_health_issue_types, str)
else agent_health_issue_types,
agentHealthModifiedInLastDays=agent_health_modified_in_last_days,
srtDir=sort_dir,
srtKey=sort_key,
pageSize=page_size,
page=page_num,
userId=user_id,
)
response = self._parent.session.get("/v1/agents", params=data.dict())
return AgentsPage.parse_response(response)
def iter_all(
self,
active: bool = None,
agent_type: Optional[AgentType] = None,
sort_dir: SortDirection = SortDirection.ASC,
sort_key: SortKeys = SortKeys.NAME,
page_size: int = 500,
agent_healthy: bool = None,
agent_health_issue_types: List[str] = None,
agent_health_modified_in_last_days: Optional[int] = None,
user_id: str = None,
) -> Iterator[Agent]:
"""
Iterate over all agents.
Accepts the same parameters as `.get_page()` excepting `page_num`.
**Returns**: A generator yielding individual [`Agent`][agent-model] objects.
"""
for page_num in count(1):
page = self.get_page(
active=active,
agent_type=agent_type,
agent_healthy=agent_healthy,
agent_health_issue_types=agent_health_issue_types,
agent_health_modified_in_last_days=agent_health_modified_in_last_days,
sort_dir=sort_dir,
sort_key=sort_key,
page_num=page_num,
page_size=page_size,
user_id=user_id,
)
yield from page.agents
if len(page.agents) < page_size:
break
def get_agent(self, agent_id: str) -> Agent:
"""
Get a single agent.
**Parameters**:
* **agent_id**: `str` (required) - The unique ID for the agent.
**Returns**: An [`Agent`][agent-model] object representing the agent.
"""
response = self._parent.session.get(f"/v1/agents/{agent_id}")
return Agent.parse_response(response)
def update(
self, agent_id: str, name: str = None, external_reference: str = None
) -> requests.Response:
"""
Update an agent.
**Parameters:**
* **agent_id**: `str` (required) - The unique ID for the agent.
* **name**: `str | None` - The updated name for the agent.
* **external_reference**: `str | None` - The updated external reference info for the agent.
**Returns**: A `requests.Response` indicating success.
"""
data = AgentUpdateRequest(name=name, externalReference=external_reference)
return self._parent.session.put(f"/v1/agents/{agent_id}", json=data.dict())
def activate(self, agent_ids: Union[str, List[str]]) -> requests.Response:
"""
Activate a set of agents.
**Parameters**:
* **agent_ids**: `str | List[str]` - An agent ID or list of agent IDs to activate.
**Returns**: A `requests.Response` indicating success.
"""
if isinstance(agent_ids, str):
agent_ids = [agent_ids]
return self._parent.session.post(
"/v1/agents/activate", json={"agentIds": agent_ids}
)
def deactivate(self, agent_ids: Union[str, List[str]]) -> requests.Response:
"""
Deactivate a set of agents.
**Parameters**:
* **agent_ids**: `str | List[str]` - An agent ID or list of agent IDs to deactivate.
**Returns**: A `requests.Response` indicating success.
"""
if isinstance(agent_ids, str):
agent_ids = [agent_ids]
return self._parent.session.post(
"/v1/agents/deactivate", json={"agentIds": agent_ids}
)
class AgentsClient:
def __init__(self, parent):
self._parent = parent
self._v1 = None
@property
def v1(self):
if self._v1 is None:
self._v1 = AgentsV1(self._parent)
return self._v1