security-client-rs 0.1.1

Async Kibana client for security operations and Fleet management
Documentation
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! Enrolled agents and the actions submitted to them.
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};

use super::{Fleet, FleetPage, Item};
use crate::{Scope, http::Method, pagination::paginated, request::endpoint};

#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Agent {
    pub id: String,
    #[serde(default)]
    pub policy_id: Option<String>,
    /// The revision of `policy_id` the agent last acknowledged. Absent until it
    /// reports one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub policy_revision: Option<u64>,
    #[serde(default)]
    pub active: Option<bool>,
    #[serde(default)]
    pub status: Option<String>,
    #[serde(default)]
    pub local_metadata: Value,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_checkin: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<String>>,
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

/// An explicit ID set or a Fleet KQL query. A query can select many agents.
#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum AgentSelection {
    Ids(Vec<String>),
    Query(String),
}

/// Submission does not prove completion. Follow an action ID through
/// [`Fleet::agent_action_status`] or [`Fleet::wait_for_action`].
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum BulkActionResult {
    Action {
        #[serde(rename = "actionId")]
        action_id: String,
    },
    /// The number of selected agents. Eligibility is not checked for every agent.
    DryRun { count: u64 },
}

#[derive(Clone, Copy, Debug, Serialize)]
#[non_exhaustive]
pub enum DiagnosticMetric {
    #[serde(rename = "CPU")]
    Cpu,
}

impl<'a> Fleet<'a> {
    pub fn find_agents(&self) -> FindAgents<'a> {
        FindAgents(
            self.0
                .request(Method::GET, Scope::Space, &["api", "fleet", "agents"]),
        )
    }

    pub fn get_agent(&self, id: &str) -> GetAgent<'a> {
        GetAgent(
            self.0
                .request(Method::GET, Scope::Space, &["api", "fleet", "agents", id]),
        )
    }

    /// Agent counts by status.
    pub fn agent_status(&self) -> AgentStatus<'a> {
        AgentStatus(
            self.0
                .request(Method::GET, Scope::Space, &["api", "fleet", "agent_status"]),
        )
    }

    pub fn reassign_agent(&self, id: &str, policy_id: &str) -> ReassignAgent<'a> {
        ReassignAgent(
            self.0
                .request(
                    Method::POST,
                    Scope::Space,
                    &["api", "fleet", "agents", id, "reassign"],
                )
                .nonempty("policy_id", policy_id)
                .field("policy_id", policy_id),
        )
    }

    pub fn unenroll_agent(&self, id: &str) -> UnenrollAgent<'a> {
        UnenrollAgent(
            self.0
                .request(
                    Method::POST,
                    Scope::Space,
                    &["api", "fleet", "agents", id, "unenroll"],
                )
                .json(&json!({})),
        )
    }

    /// Schedules an upgrade; the acknowledgment is not proof of completion.
    /// Container agents cannot perform Fleet-managed binary upgrades.
    pub fn upgrade_agent(&self, id: &str, version: &str) -> UpgradeAgent<'a> {
        UpgradeAgent(
            self.0
                .request(
                    Method::POST,
                    Scope::Space,
                    &["api", "fleet", "agents", id, "upgrade"],
                )
                .nonempty("version", version)
                .field("version", version),
        )
    }

    /// Correlate the returned action ID with [`list_agent_uploads`](Self::list_agent_uploads)
    /// or [`wait_for_upload`](Self::wait_for_upload).
    pub fn request_agent_diagnostics(&self, id: &str) -> RequestAgentDiagnostics<'a> {
        RequestAgentDiagnostics(
            self.0
                .request(
                    Method::POST,
                    Scope::Space,
                    &["api", "fleet", "agents", id, "request_diagnostics"],
                )
                .json(&json!({})),
        )
    }

    pub fn bulk_reassign_agents(
        &self,
        agents: AgentSelection,
        policy_id: &str,
    ) -> BulkReassignAgents<'a> {
        BulkReassignAgents(
            self.0
                .request(
                    Method::POST,
                    Scope::Space,
                    &["api", "fleet", "agents", "bulk_reassign"],
                )
                .field("agents", agents)
                .nonempty("policy_id", policy_id)
                .field("policy_id", policy_id),
        )
    }

    pub fn bulk_unenroll_agents(&self, agents: AgentSelection) -> BulkUnenrollAgents<'a> {
        BulkUnenrollAgents(
            self.0
                .request(
                    Method::POST,
                    Scope::Space,
                    &["api", "fleet", "agents", "bulk_unenroll"],
                )
                .field("agents", agents),
        )
    }

    pub fn bulk_update_agent_tags(&self, agents: AgentSelection) -> BulkUpdateAgentTags<'a> {
        BulkUpdateAgentTags(
            self.0
                .request(
                    Method::POST,
                    Scope::Space,
                    &["api", "fleet", "agents", "bulk_update_agent_tags"],
                )
                .field("agents", agents),
        )
    }

    pub fn bulk_upgrade_agents(
        &self,
        agents: AgentSelection,
        version: &str,
    ) -> BulkUpgradeAgents<'a> {
        BulkUpgradeAgents(
            self.0
                .request(
                    Method::POST,
                    Scope::Space,
                    &["api", "fleet", "agents", "bulk_upgrade"],
                )
                .field("agents", agents)
                .nonempty("version", version)
                .field("version", version),
        )
    }

    pub fn bulk_request_agent_diagnostics(
        &self,
        agents: AgentSelection,
    ) -> BulkRequestAgentDiagnostics<'a> {
        BulkRequestAgentDiagnostics(
            self.0
                .request(
                    Method::POST,
                    Scope::Space,
                    &["api", "fleet", "agents", "bulk_request_diagnostics"],
                )
                .field("agents", agents),
        )
    }
}

macro_rules! bulk_setters {
    ($($name:ident),*) => {$(
        impl $name<'_> {
            /// Returns the number of selected agents instead of creating an action.
            /// Kibana 9.3 and later; earlier versions reject the field.
            pub fn dry_run(self, dry_run: bool) -> Self {
                Self(self.0.field("dryRun", dry_run))
            }

            /// How many agents Kibana processes per batch for query selections.
            /// Kibana 9.0 and later; 8.19 accepts it only for tag updates and
            /// diagnostics requests.
            pub fn batch_size(self, size: u32) -> Self {
                Self(self.0.field("batchSize", size))
            }
        }
    )*};
}

macro_rules! include_inactive_setter {
    ($($name:ident),*) => {$(
        impl $name<'_> {
            /// Also selects inactive agents matched by a query.
            pub fn include_inactive(self, include: bool) -> Self {
                Self(self.0.field("includeInactive", include))
            }
        }
    )*};
}

macro_rules! upgrade_setters {
    ($($name:ident),*) => {$(
        impl $name<'_> {
            /// Upgrades even when Fleet considers the agent ineligible.
            pub fn force(self, force: bool) -> Self {
                Self(self.0.field("force", force))
            }

            /// Downloads agent binaries from this URI instead of the default source.
            pub fn source_uri(self, uri: &str) -> Self {
                Self(self.0.field("source_uri", uri))
            }

            pub fn skip_rate_limit_check(self, skip: bool) -> Self {
                Self(self.0.field("skipRateLimitCheck", skip))
            }
        }
    )*};
}

macro_rules! diagnostics_setters {
    ($($name:ident),*) => {$(
        impl $name<'_> {
            pub fn additional_metrics(self, metrics: Vec<DiagnosticMetric>) -> Self {
                Self(self.0.field("additional_metrics", metrics))
            }
        }
    )*};
}

endpoint! {
    /// `GET /api/fleet/agents`
    FindAgents => FleetPage<Agent>
}

impl FindAgents<'_> {
    pub fn show_inactive(self, show: bool) -> Self {
        Self(self.0.param("showInactive", show))
    }
}

endpoint! {
    /// `GET /api/fleet/agents/{agentId}`
    GetAgent => Item<Agent>
}

endpoint! {
    /// `GET /api/fleet/agent_status`
    AgentStatus => Value
}

impl AgentStatus<'_> {
    pub fn policy_id(self, id: &str) -> Self {
        Self(self.0.param("policyId", id))
    }

    pub fn kuery(self, kuery: &str) -> Self {
        Self(self.0.param("kuery", kuery))
    }
}

endpoint! {
    /// `POST /api/fleet/agents/{agentId}/reassign`
    ReassignAgent => Value
}

endpoint! {
    /// `POST /api/fleet/agents/{agentId}/unenroll`
    UnenrollAgent => Value
}

impl UnenrollAgent<'_> {
    /// Removes the agent immediately instead of waiting for it to acknowledge.
    pub fn force(self, force: bool) -> Self {
        Self(self.0.field("force", force))
    }

    /// Revokes the agent's API keys.
    pub fn revoke(self, revoke: bool) -> Self {
        Self(self.0.field("revoke", revoke))
    }
}

endpoint! {
    /// `POST /api/fleet/agents/{agentId}/upgrade`
    UpgradeAgent => Value
}

endpoint! {
    /// `POST /api/fleet/agents/{agentId}/request_diagnostics`
    RequestAgentDiagnostics => BulkActionResult
}

endpoint! {
    /// `POST /api/fleet/agents/bulk_reassign`
    BulkReassignAgents => BulkActionResult
}

endpoint! {
    /// `POST /api/fleet/agents/bulk_unenroll`
    BulkUnenrollAgents => BulkActionResult
}

impl BulkUnenrollAgents<'_> {
    /// Removes agents immediately instead of waiting for them to acknowledge.
    pub fn force(self, force: bool) -> Self {
        Self(self.0.field("force", force))
    }

    /// Revokes the agents' API keys.
    pub fn revoke(self, revoke: bool) -> Self {
        Self(self.0.field("revoke", revoke))
    }
}

endpoint! {
    /// `POST /api/fleet/agents/bulk_update_agent_tags`
    BulkUpdateAgentTags => BulkActionResult
}

impl BulkUpdateAgentTags<'_> {
    pub fn add_tags<I: IntoIterator<Item = S>, S: Into<String>>(self, tags: I) -> Self {
        let tags: Vec<String> = tags.into_iter().map(Into::into).collect();
        Self(self.0.field("tagsToAdd", tags))
    }

    pub fn remove_tags<I: IntoIterator<Item = S>, S: Into<String>>(self, tags: I) -> Self {
        let tags: Vec<String> = tags.into_iter().map(Into::into).collect();
        Self(self.0.field("tagsToRemove", tags))
    }
}

endpoint! {
    /// `POST /api/fleet/agents/bulk_upgrade`
    BulkUpgradeAgents => BulkActionResult
}

impl BulkUpgradeAgents<'_> {
    /// An ISO 8601 time at which the rollout starts.
    pub fn start_time(self, time: &str) -> Self {
        Self(self.0.field("start_time", time))
    }

    /// Spreads upgrades over this many seconds.
    pub fn rollout_duration_seconds(self, seconds: u64) -> Self {
        Self(self.0.field("rollout_duration_seconds", seconds))
    }
}

endpoint! {
    /// `POST /api/fleet/agents/bulk_request_diagnostics`
    BulkRequestAgentDiagnostics => BulkActionResult
}

page_setters!(FindAgents);
sort_setters!(FindAgents);
bulk_setters!(
    BulkReassignAgents,
    BulkUnenrollAgents,
    BulkUpdateAgentTags,
    BulkUpgradeAgents,
    BulkRequestAgentDiagnostics
);
include_inactive_setter!(
    BulkReassignAgents,
    BulkUnenrollAgents,
    BulkUpdateAgentTags,
    BulkUpgradeAgents
);
upgrade_setters!(UpgradeAgent, BulkUpgradeAgents);
diagnostics_setters!(RequestAgentDiagnostics, BulkRequestAgentDiagnostics);
paginated!(FindAgents => FleetPage<Agent>);