elasticctl-api 0.8.0

Typed detection-rule model and endpoint wrappers for Elastic Security.
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
//! Typed dashboard models and public Kibana route wrappers.

use crate::compose::require_path_segment;
use elasticctl_core::{Error, ErrorKind, Feature, Result, Transport, urlencode};
use serde::{Deserialize, Deserializer, Serialize, de::DeserializeOwned};
use serde_json::{Map, Value};
use std::collections::BTreeSet;

const BASE: &str = "/api/dashboards";

/// The portable, author-controlled dashboard representation.
///
/// `data` is an ordered, open JSON map so new public API fields survive a
/// portable artifact round trip.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct DashboardSpec {
    pub id: String,
    pub data: Map<String, Value>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawDashboardSpec {
    id: String,
    data: Map<String, Value>,
}

impl DashboardSpec {
    fn validate_shape(&self) -> Result<()> {
        if self.id.trim().is_empty() {
            return Err(Error::new(
                ErrorKind::Error,
                "dashboard id must not be empty",
            ));
        }
        match self.data.get("title") {
            Some(Value::String(title)) if !title.trim().is_empty() => Ok(()),
            _ => Err(Error::new(
                ErrorKind::Error,
                "dashboard data.title must be a non-empty string",
            )),
        }
    }
}

impl TryFrom<Value> for DashboardSpec {
    type Error = Error;

    fn try_from(value: Value) -> Result<Self> {
        serde_json::from_value(value)
            .map_err(|error| Error::new(ErrorKind::Error, format!("decoding dashboard: {error}")))
    }
}

impl<'de> Deserialize<'de> for DashboardSpec {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let raw = RawDashboardSpec::deserialize(deserializer)?;
        let spec = Self {
            id: raw.id,
            data: raw.data,
        };
        spec.validate_shape().map_err(serde::de::Error::custom)?;
        Ok(spec)
    }
}

/// A dashboard list row returned by Kibana.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DashboardSummary {
    pub id: String,
    pub title: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub tags: Option<Vec<String>>,
}

/// A warning Kibana returned while transforming a dashboard.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DashboardWarning {
    pub message: String,
}

/// A full dashboard returned by Kibana.
#[derive(Debug, Clone, PartialEq)]
pub struct Dashboard {
    pub id: String,
    pub data: Map<String, Value>,
    pub meta: Map<String, Value>,
    pub warnings: Vec<DashboardWarning>,
}

/// One page returned by Kibana's dashboard list route.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DashboardPage {
    pub data: Vec<DashboardSummary>,
    pub page: u64,
    pub per_page: u64,
    pub total: u64,
}

/// One submitted value that Kibana did not persist.
#[derive(Debug, Clone, PartialEq)]
pub struct DashboardLoss {
    pub path: String,
    pub expected: Value,
    pub actual: Option<Value>,
}

/// List one page of dashboards through the public Dashboards API.
pub async fn search(
    transport: &Transport,
    page: u64,
    query: Option<&str>,
    tags: &[String],
) -> Result<DashboardPage> {
    transport.require_feature(Feature::Dashboards).await?;
    let mut route = format!("{BASE}?page={page}&per_page=1000");
    if let Some(query) = query {
        route.push_str("&query=");
        route.push_str(&urlencode(query));
    }
    for tag in tags {
        route.push_str("&tags=");
        route.push_str(&urlencode(tag));
    }
    decode_search(&transport.get(&route).await?)
}

/// Get one dashboard by a stable id read from a response or a local file.
pub async fn get(transport: &Transport, id: &str) -> Result<Dashboard> {
    get_selector(transport, require_path_segment(id)?).await
}

/// Get one dashboard by the caller's own selector, which keeps the caller's
/// validation.
pub(crate) async fn get_selector(transport: &Transport, selector: &str) -> Result<Dashboard> {
    transport.require_feature(Feature::Dashboards).await?;
    decode_dashboard(
        &transport.get(&dashboard_path(selector)).await?,
        "dashboard get",
    )
}

/// Upsert one dashboard using only its portable body.
pub async fn put(transport: &Transport, spec: &DashboardSpec) -> Result<Dashboard> {
    validate_spec(spec)?;
    let id = require_path_segment(&spec.id)?;
    transport.require_feature(Feature::Dashboards).await?;
    decode_dashboard(
        &transport
            .put(&dashboard_path(id), &Value::Object(spec.data.clone()))
            .await?,
        "dashboard put",
    )
}

/// Delete one dashboard by id.
pub async fn delete(transport: &Transport, id: &str) -> Result<()> {
    let id = require_path_segment(id)?;
    transport.require_feature(Feature::Dashboards).await?;
    match transport.delete(&dashboard_path(id)).await? {
        Value::Null => Ok(()),
        _ => Err(Error::new(
            ErrorKind::Http,
            "decoding dashboard delete: expected an empty response body",
        )),
    }
}

/// Reject locally known dashboard values that Kibana accepts but drops.
pub fn validate_spec(spec: &DashboardSpec) -> Result<()> {
    spec.validate_shape()?;
    if let Some(path) = time_range_mode_path(&Value::Object(spec.data.clone()), "$") {
        return Err(Error::new(
            ErrorKind::Unsupported,
            format!("dashboard {path} is unsupported because Kibana does not persist it"),
        ));
    }
    Ok(())
}

/// Return sorted, unique data-view reference ids found anywhere in `value`.
pub fn collect_data_view_refs(value: &Value) -> Vec<String> {
    fn collect(value: &Value, ids: &mut BTreeSet<String>) {
        match value {
            Value::Object(map) => {
                if map.get("type").and_then(Value::as_str) == Some("data_view_reference")
                    && let Some(id) = map.get("ref_id").and_then(Value::as_str)
                {
                    ids.insert(id.to_string());
                }
                for value in map.values() {
                    collect(value, ids);
                }
            }
            Value::Array(values) => {
                for value in values {
                    collect(value, ids);
                }
            }
            _ => {}
        }
    }

    let mut ids = BTreeSet::new();
    collect(value, &mut ids);
    ids.into_iter().collect()
}

/// Find submitted values that are absent or changed in an accepted response.
pub fn subset_losses(expected: &Value, actual: &Value) -> Vec<DashboardLoss> {
    fn collect(
        expected: &Value,
        actual: Option<&Value>,
        path: &str,
        losses: &mut Vec<DashboardLoss>,
    ) {
        let Some(actual) = actual else {
            losses.push(DashboardLoss {
                path: path.to_string(),
                expected: expected.clone(),
                actual: None,
            });
            return;
        };

        match (expected, actual) {
            (Value::Object(expected), Value::Object(actual)) => {
                for (key, expected) in expected {
                    let path = format!("{path}.{}", json_path_key(key));
                    collect(expected, actual.get(key), &path, losses);
                }
            }
            (Value::Array(expected), Value::Array(actual)) => {
                for (index, expected) in expected.iter().enumerate() {
                    let path = format!("{path}[{index}]");
                    collect(expected, actual.get(index), &path, losses);
                }
            }
            _ if expected == actual => {}
            _ => losses.push(DashboardLoss {
                path: path.to_string(),
                expected: expected.clone(),
                actual: Some(actual.clone()),
            }),
        }
    }

    let mut losses = Vec::new();
    collect(expected, Some(actual), "$", &mut losses);
    losses
}

fn dashboard_path(id: &str) -> String {
    format!("{BASE}/{}", urlencode(id))
}

fn time_range_mode_path(value: &Value, path: &str) -> Option<String> {
    match value {
        Value::Object(map) => {
            for (key, value) in map {
                let child_path = format!("{path}.{}", json_path_key(key));
                if key == "time_range" && value.get("mode").is_some() {
                    return Some(format!("{child_path}.mode"));
                }
                if let Some(path) = time_range_mode_path(value, &child_path) {
                    return Some(path);
                }
            }
            None
        }
        Value::Array(values) => values
            .iter()
            .enumerate()
            .find_map(|(index, value)| time_range_mode_path(value, &format!("{path}[{index}]"))),
        _ => None,
    }
}

fn json_path_key(key: &str) -> String {
    if key.chars().enumerate().all(|(index, character)| {
        character == '_'
            || character.is_ascii_alphanumeric() && (index > 0 || !character.is_ascii_digit())
    }) {
        key.to_string()
    } else {
        format!("['{}']", key.replace('\\', "\\\\").replace('\'', "\\'"))
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct SearchEnvelope {
    data: Vec<SearchDashboardRow>,
    meta: SearchMeta,
}

/// The wire row is intentionally separate from the flat rendering summary.
/// Kibana nests dashboard fields under `data`; new nested fields do not affect
/// list selection and are not rejected merely because this CLI does not render
/// them yet.
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct SearchDashboardRow {
    id: String,
    data: SearchDashboardData,
    // The measured row carries its own open metadata object. It is not part
    // of the flat rendering summary, but requiring its object shape keeps a
    // malformed successful response from being silently accepted.
    #[serde(rename = "meta")]
    _meta: Map<String, Value>,
}

#[derive(Deserialize)]
struct SearchDashboardData {
    title: String,
    #[serde(default)]
    description: Option<String>,
    #[serde(default)]
    tags: Option<Vec<String>>,
    #[serde(flatten)]
    _extra: Map<String, Value>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct SearchMeta {
    page: u64,
    per_page: u64,
    total: u64,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct DashboardEnvelope {
    id: String,
    data: Map<String, Value>,
    meta: Map<String, Value>,
    #[serde(default)]
    warnings: Vec<DashboardWarning>,
}

fn decode_search(body: &Value) -> Result<DashboardPage> {
    let response = decode_envelope::<SearchEnvelope>(body, "dashboard search")?;
    let data = response
        .data
        .into_iter()
        .map(|row| {
            if row.id.trim().is_empty() {
                return Err(Error::new(
                    ErrorKind::Http,
                    "decoding dashboard search: id must be a non-empty string",
                ));
            }
            if row.data.title.trim().is_empty() {
                return Err(Error::new(
                    ErrorKind::Http,
                    "decoding dashboard search: data.title must be a non-empty string",
                ));
            }
            Ok(DashboardSummary {
                id: row.id,
                title: row.data.title,
                description: row.data.description,
                tags: row.data.tags,
            })
        })
        .collect::<Result<Vec<_>>>()?;
    Ok(DashboardPage {
        data,
        page: response.meta.page,
        per_page: response.meta.per_page,
        total: response.meta.total,
    })
}

fn decode_dashboard(body: &Value, context: &str) -> Result<Dashboard> {
    let response = decode_envelope::<DashboardEnvelope>(body, context)?;
    if response.id.trim().is_empty() {
        return Err(Error::new(
            ErrorKind::Http,
            format!("decoding {context}: id must be a non-empty string"),
        ));
    }
    if !matches!(response.data.get("title"), Some(Value::String(title)) if !title.trim().is_empty())
    {
        return Err(Error::new(
            ErrorKind::Http,
            format!("decoding {context}: data.title must be a non-empty string"),
        ));
    }
    Ok(Dashboard {
        id: response.id,
        data: response.data,
        meta: response.meta,
        warnings: response.warnings,
    })
}

fn decode_envelope<T: DeserializeOwned>(body: &Value, context: &str) -> Result<T> {
    serde_json::from_value(body.clone())
        .map_err(|error| Error::new(ErrorKind::Http, format!("decoding {context}: {error}")))
}