Skip to main content

security_client_rs/fleet/
mod.rs

1//! Agent policies, integration packages, package policies, and enrolled agents.
2//!
3//! Installing a package changes assets; assigning a package policy changes agent
4//! configuration. Agent actions are asynchronous: a submitted action ID is not
5//! proof of completion, so follow it through [`Fleet::agent_action_status`] or
6//! [`Fleet::wait_for_action`].
7//!
8//! Package policies have two formats. [`NewPackagePolicy`] builds Fleet's
9//! simplified format, keyed by input and stream name. A retrieved
10//! [`PackagePolicy`] holds the full format; change it through
11//! [`PackagePolicy::edit`] to keep its existing configuration.
12//! Agent-policy edits use [`AgentPolicy::edit`] and [`Fleet::edit_agent_policy`]
13//! to retain the inactivity timeout. Optional settings are changed only when
14//! requested; [`AgentPolicyEdit::clear_data_output_id`] restores the default
15//! output with JSON null.
16//!
17//! Policy variables can hold credentials, so `Debug` output of package policies
18//! and their inputs omits variable values.
19use serde::{Deserialize, Serialize};
20use serde_json::Value;
21
22use crate::{Kibana, Scope, http::Method, request::endpoint};
23
24/// Pagination and KQL filtering for Fleet collections, whose pages start at one.
25macro_rules! page_setters {
26    ($($name:ident),*) => {$(
27        impl $name<'_> {
28            /// One-based page number.
29            pub fn page(self, page: u32) -> Self {
30                Self(self.0.positive_param("page", page))
31            }
32
33            pub fn per_page(self, per_page: u32) -> Self {
34                Self(self.0.positive_param("perPage", per_page))
35            }
36
37            /// A KQL filter over the collection's fields.
38            pub fn kuery(self, kuery: &str) -> Self {
39                Self(self.0.param("kuery", kuery))
40            }
41        }
42    )*};
43}
44
45macro_rules! sort_setters {
46    ($($name:ident),*) => {$(
47        impl $name<'_> {
48            pub fn sort_field(self, field: &str) -> Self {
49                Self(self.0.param("sortField", field))
50            }
51
52            pub fn sort_order(self, order: $crate::SortOrder) -> Self {
53                Self(self.0.param("sortOrder", order.as_str()))
54            }
55        }
56    )*};
57}
58
59mod actions;
60mod agent_policies;
61mod agents;
62mod enrollment;
63mod package_policies;
64mod packages;
65mod wait;
66
67pub use actions::{
68    ActionStatus, AgentActionStatus, AgentActionStatuses, AgentUpload, CancelAgentAction,
69    DownloadAgentFile, ListAgentUploads, UploadStatus,
70};
71pub use agent_policies::{
72    AgentPolicy, AgentPolicyEdit, CopyAgentPolicy, CreateAgentPolicy, DeleteAgentPolicy,
73    DownloadAgentPolicy, FindAgentPolicies, GetAgentPolicy, NewAgentPolicy, UpdateAgentPolicy,
74};
75pub use agents::{
76    Agent, AgentSelection, AgentStatus, BulkActionResult, BulkReassignAgents,
77    BulkRequestAgentDiagnostics, BulkUnenrollAgents, BulkUpdateAgentTags, BulkUpgradeAgents,
78    DiagnosticMetric, FindAgents, GetAgent, ReassignAgent, RequestAgentDiagnostics, UnenrollAgent,
79    UpgradeAgent,
80};
81pub use enrollment::{
82    CreateEnrollmentKey, EnrollmentKey, FindEnrollmentKeys, GetEnrollmentKey, RevokeEnrollmentKey,
83};
84pub use package_policies::{
85    CreatePackagePolicy, DeletePackagePolicy, FindPackagePolicies, GetPackagePolicy,
86    NewPackagePolicy, PackagePolicy, PackagePolicyEdit, PackagePolicyUpdate, PackageRef,
87    PolicyInput, PolicyStream, UpdatePackagePolicy,
88};
89pub use packages::{GetPackage, InstallPackage, ListPackages, Package, UninstallPackage};
90
91/// Fleet wraps single resources in `{"item": ...}`.
92#[derive(Clone, Debug, Deserialize, Serialize)]
93#[non_exhaustive]
94pub struct Item<T> {
95    pub item: T,
96}
97
98#[derive(Clone, Debug, Deserialize, Serialize)]
99#[non_exhaustive]
100pub struct Items<T> {
101    pub items: Vec<T>,
102}
103
104#[derive(Clone, Debug, Deserialize, Serialize)]
105#[non_exhaustive]
106pub struct FleetPage<T> {
107    pub items: Vec<T>,
108    pub page: u32,
109    #[serde(rename = "perPage")]
110    pub per_page: u32,
111    pub total: u64,
112}
113
114/// The Fleet API. Endpoints are grouped by resource in private modules and
115/// re-exported here.
116#[derive(Clone, Copy, Debug)]
117pub struct Fleet<'a>(pub(crate) &'a Kibana);
118
119impl<'a> Fleet<'a> {
120    /// Initializes Fleet in the current space. Safe to repeat.
121    pub fn setup(&self) -> Setup<'a> {
122        Setup(
123            self.0
124                .request(Method::POST, Scope::Space, &["api", "fleet", "setup"]),
125        )
126    }
127
128    pub fn list_outputs(&self) -> ListOutputs<'a> {
129        ListOutputs(
130            self.0
131                .request(Method::GET, Scope::Space, &["api", "fleet", "outputs"]),
132        )
133    }
134}
135
136endpoint! {
137    /// `POST /api/fleet/setup`
138    Setup => Value
139}
140
141endpoint! {
142    /// `GET /api/fleet/outputs`
143    ListOutputs => Value
144}