esdiag 0.16.4

Elastic Stack diagnostic collector and processor
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.

use crate::data::Product;
use eyre::{Result, eyre};
use semver::{Version, VersionReq};
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::{BTreeMap, HashMap};
use std::sync::OnceLock;
use tokio::sync::mpsc::Sender;

#[derive(Debug)]
pub enum DataSourceError {
    UnsupportedVersion(Version),
    MissingSource(String, String),
}

impl std::fmt::Display for DataSourceError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UnsupportedVersion(v) => write!(f, "API not supported on target version {}", v),
            Self::MissingSource(product, name) => {
                write!(
                    f,
                    "Source configuration missing for product: {}, name: {}",
                    product, name
                )
            }
        }
    }
}

impl std::error::Error for DataSourceError {}

#[derive(Clone, Debug, Default)]
pub struct SourceContext {
    pub product: &'static str,
    pub version: Option<Version>,
}

impl SourceContext {
    pub fn new(product: &'static str, version: Option<Version>) -> Self {
        Self { product, version }
    }
}

pub trait DataSource {
    fn name() -> String;
    fn aliases() -> Vec<&'static str> {
        Vec::new()
    }

    fn resolve_source_request_path(ctx: &SourceContext) -> Result<String> {
        let version = ctx
            .version
            .as_ref()
            .ok_or_else(|| eyre!("Version required for request path"))?;
        let name = Self::name();
        let aliases = Self::aliases();
        let (_, source_conf) = get_source(ctx.product, &name, &aliases)?;
        source_conf.get_url(version)
    }

    fn resolve_source_file_path(ctx: &SourceContext) -> Result<String> {
        let name = Self::name();
        let aliases = Self::aliases();
        let (matched_name, source_conf) = get_source(ctx.product, &name, &aliases)?;
        Ok(source_conf.get_file_path(matched_name))
    }

    fn resolve_source_extension(ctx: &SourceContext) -> Result<String> {
        let name = Self::name();
        let aliases = Self::aliases();
        let (_, source_conf) = get_source(ctx.product, &name, &aliases)?;
        Ok(source_conf.extension.as_deref().unwrap_or(".json").to_string())
    }

    fn candidate_source_file_paths(ctx: &SourceContext) -> Result<Vec<String>> {
        let name = Self::name();
        let aliases = Self::aliases();
        let mut paths = Vec::new();

        let (matched_name, source_conf) = get_source(ctx.product, &name, &aliases)?;
        paths.push(source_conf.get_file_path(matched_name));

        for alias in aliases {
            if let Ok((matched_name, source_conf)) = get_source(ctx.product, alias, &[]) {
                let path = source_conf.get_file_path(matched_name);
                if !paths.contains(&path) {
                    paths.push(path);
                }
            }
        }

        Ok(paths)
    }
}

pub fn source_product_key(product: &Product) -> Result<&'static str> {
    match product {
        Product::Elasticsearch => Ok("elasticsearch"),
        Product::Kibana => Ok("kibana"),
        Product::Logstash => Ok("logstash"),
        _ => Err(eyre!("sources.yml overrides are not supported for product {}", product)),
    }
}

pub trait StreamingDataSource: DataSource {
    type Item: Send + 'static;
    fn deserialize_stream<'de, D>(
        deserializer: D,
        sender: Sender<Result<Self::Item>>,
    ) -> std::result::Result<(), D::Error>
    where
        D: Deserializer<'de>;
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Eq)]
#[serde(untagged)]
pub enum VersionSource {
    Url(String),
    Structured(VersionSourceDetails),
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Eq)]
pub struct VersionSourceDetails {
    pub url: String,
    #[serde(default)]
    pub spaceaware: bool,
    pub paginate: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ResolvedVersionSource {
    pub url: String,
    pub spaceaware: bool,
    pub paginate: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Eq)]
pub struct Source {
    pub extension: Option<String>,
    pub subdir: Option<String>,
    pub tags: Option<String>,
    pub versions: BTreeMap<String, VersionSource>,
}

impl Default for Source {
    fn default() -> Self {
        Self {
            extension: Some(String::from(".json")),
            subdir: None,
            tags: None,
            versions: BTreeMap::new(),
        }
    }
}

impl std::fmt::Display for Source {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.subdir {
            Some(subdir) => write!(fmt, "{}", subdir),
            None => Ok(()),
        }
    }
}

static SOURCES: OnceLock<HashMap<&'static str, HashMap<String, Source>>> = OnceLock::new();

fn embedded_sources_str(product: &str) -> Result<&'static str> {
    match product {
        "elasticsearch" => Ok(include_str!("../../../assets/elasticsearch/sources.yml")),
        "kibana" => Ok(include_str!("../../../assets/kibana/sources.yml")),
        "logstash" => Ok(include_str!("../../../assets/logstash/sources.yml")),
        other => Err(eyre!("Unsupported sources product: {}", other)),
    }
}

fn required_source_keys(product: &str) -> &'static [&'static str] {
    match product {
        "elasticsearch" => &["version"],
        "kibana" => &["kibana_status", "kibana_spaces"],
        "logstash" => &["logstash_node", "logstash_version"],
        _ => &[],
    }
}

fn parse_sources_content(label: &str, content: &str) -> Result<HashMap<String, Source>> {
    serde_yaml::from_str(content).map_err(|e| eyre!("Failed to parse {}: {}", label, e))
}

fn validate_sources_product(product: &str, sources: &HashMap<String, Source>, label: &str) -> Result<()> {
    let required = required_source_keys(product);
    let missing: Vec<&str> = required
        .iter()
        .copied()
        .filter(|key| !sources.contains_key(*key))
        .collect();
    if missing.is_empty() {
        Ok(())
    } else {
        Err(eyre!(
            "{} does not look like a valid {} sources.yml; missing required keys: {}",
            label,
            product,
            missing.join(", ")
        ))
    }
}

fn load_embedded_sources(
    override_product: Option<&str>,
    override_path: Option<&str>,
) -> Result<HashMap<&'static str, HashMap<String, Source>>> {
    let mut products = HashMap::new();

    for product in ["elasticsearch", "kibana", "logstash"] {
        let (label, content) = if override_product == Some(product) {
            let path = override_path.ok_or_else(|| eyre!("Override path missing for {}", product))?;
            (
                format!("override sources file at {}", path),
                std::fs::read_to_string(path)
                    .map_err(|e| eyre!("Failed to read override sources file at {}: {}", path, e))?,
            )
        } else {
            (
                format!("embedded {} sources.yml", product),
                embedded_sources_str(product)?.to_string(),
            )
        };

        let sources = parse_sources_content(&label, &content)?;
        validate_sources_product(product, &sources, &label)?;
        products.insert(product, sources);
    }

    Ok(products)
}

pub fn get_sources() -> &'static HashMap<&'static str, HashMap<String, Source>> {
    SOURCES.get_or_init(|| load_embedded_sources(None, None).expect("Valid embedded sources.yml files"))
}

pub fn init_sources(product: &str, override_path: String) -> Result<()> {
    let products = load_embedded_sources(Some(product), Some(&override_path))?;
    SOURCES
        .set(products)
        .map_err(|_| eyre!("Sources already initialized"))?;
    Ok(())
}

pub fn get_source<'a>(
    product: &str,
    name: &'a str,
    aliases: &[&'a str],
) -> std::result::Result<(&'a str, &'static Source), DataSourceError> {
    let sources = get_sources();
    let product_sources = sources
        .get(product)
        .ok_or_else(|| DataSourceError::MissingSource(product.to_string(), name.to_string()))?;
    if let Some(source) = product_sources.get(name) {
        return Ok((name, source));
    }
    for alias in aliases {
        if let Some(source) = product_sources.get(*alias) {
            return Ok((*alias, source));
        }
    }
    Err(DataSourceError::MissingSource(product.to_string(), name.to_string()))
}

pub fn get_product_sources(product: &str) -> Option<&'static HashMap<String, Source>> {
    get_sources().get(product)
}

pub fn get_source_keys(product: &str) -> Vec<String> {
    get_product_sources(product)
        .map(|sources| sources.keys().cloned().collect())
        .unwrap_or_default()
}

pub fn get_source_keys_with_tag(product: &str, tag: &str) -> Vec<String> {
    get_product_sources(product)
        .map(|sources| {
            sources
                .iter()
                .filter_map(|(name, source)| source.has_tag(tag).then_some(name.clone()))
                .collect()
        })
        .unwrap_or_default()
}

fn convert_npm_semver_to_cargo(req: &str) -> String {
    let parts: Vec<&str> = req.split_whitespace().collect();
    let mut out = String::new();
    for i in 0..parts.len() {
        out.push_str(parts[i]);
        if i + 1 < parts.len() {
            // If current part starts with a digit and next starts with an operator, insert comma.
            if parts[i].chars().next().is_some_and(|c| c.is_ascii_digit())
                && parts[i + 1]
                    .chars()
                    .next()
                    .is_some_and(|c| c == '<' || c == '>' || c == '=' || c == '~' || c == '^')
            {
                out.push_str(", ");
            } else {
                out.push(' ');
            }
        }
    }
    out
}

impl Source {
    pub fn get_file_path(&self, name: &str) -> String {
        let extension = self.extension.as_deref().unwrap_or(".json");
        match &self.subdir {
            Some(subdir) => format!("{}/{}{}", subdir, name, extension),
            None => format!("{}{}", name, extension),
        }
    }

    pub fn has_tag(&self, tag: &str) -> bool {
        self.tags
            .as_deref()
            .map(|tags| tags.split(',').any(|value| value.trim() == tag))
            .unwrap_or(false)
    }

    pub fn is_spaceaware(&self) -> bool {
        self.versions.values().any(|version| match version {
            VersionSource::Url(_) => false,
            VersionSource::Structured(details) => details.spaceaware,
        })
    }

    pub fn resolve_version(&self, version: &Version) -> Result<ResolvedVersionSource> {
        // Strip pre-release tags (like -SNAPSHOT) to ensure our broad semver matching logic
        // in sources.yml (e.g. ">= 7.0.0") matches properly. Standard semver treats ">= 7.0.0"
        // as NOT matching "8.0.0-SNAPSHOT" by default unless specifically asked to.
        let mut clean_version = version.clone();
        clean_version.pre = semver::Prerelease::EMPTY;

        for (req_str, source) in &self.versions {
            let cargo_req_str = convert_npm_semver_to_cargo(req_str);
            let req = VersionReq::parse(&cargo_req_str)
                .map_err(|e| eyre!("Failed to parse version req '{}': {}", req_str, e))?;
            if req.matches(&clean_version) {
                return Ok(match source {
                    VersionSource::Url(url) => ResolvedVersionSource {
                        url: url.clone(),
                        spaceaware: false,
                        paginate: None,
                    },
                    VersionSource::Structured(details) => ResolvedVersionSource {
                        url: details.url.clone(),
                        spaceaware: details.spaceaware,
                        paginate: details.paginate.clone(),
                    },
                });
            }
        }
        Err(DataSourceError::UnsupportedVersion(version.clone()).into())
    }

    pub fn get_url(&self, version: &Version) -> Result<String> {
        Ok(self.resolve_version(version)?.url)
    }
}

#[cfg(test)]
mod tests {
    use super::get_sources;
    use semver::Version;

    #[test]
    fn test_semver_parsing_and_matching() {
        let sources = get_sources();
        let es_sources = sources.get("elasticsearch").unwrap();

        // Let's test a simple one, like aliases
        let alias = es_sources.get("cat_aliases").unwrap();

        let v_0_9 = Version::parse("0.9.0").unwrap();
        let v_5_0 = Version::parse("5.0.0").unwrap();
        let v_5_1_1 = Version::parse("5.1.1").unwrap();
        let v_6_0 = Version::parse("6.0.0").unwrap();

        assert_eq!(alias.get_url(&v_0_9).unwrap(), "/_cat/aliases?v");
        assert_eq!(alias.get_url(&v_5_0).unwrap(), "/_cat/aliases?v");
        assert_eq!(alias.get_url(&v_5_1_1).unwrap(), "/_cat/aliases?v&s=alias,index");
        assert_eq!(alias.get_url(&v_6_0).unwrap(), "/_cat/aliases?v&s=alias,index");
    }

    #[test]
    fn test_semver_snapshots() {
        let sources = get_sources();
        let es_sources = sources.get("elasticsearch").unwrap();

        // snapshot should strip prerelease
        let ilm = es_sources.get("ilm_explain").unwrap();

        let v_8 = Version::parse("8.0.0-SNAPSHOT").unwrap();
        assert_eq!(ilm.get_url(&v_8).unwrap(), "/*/_ilm/explain?human&expand_wildcards=all");
    }

    #[test]
    fn test_file_path_generation() {
        let sources = get_sources();
        let es_sources = sources.get("elasticsearch").unwrap();

        let alias = es_sources.get("cat_aliases").unwrap();
        assert_eq!(alias.get_file_path("cat_aliases"), "cat/cat_aliases.txt");

        let tasks = es_sources.get("tasks").unwrap();
        assert_eq!(tasks.get_file_path("tasks"), "tasks.json"); // no subdir, default extension is json if missing from yaml
    }

    #[test]
    fn test_logstash_sources_are_loaded() {
        let sources = get_sources();
        let logstash_sources = sources.get("logstash").unwrap();
        assert!(logstash_sources.contains_key("logstash_node"));
        assert!(logstash_sources.contains_key("logstash_nodes_hot_threads_human"));
    }

    #[test]
    fn test_logstash_source_url_and_extension_resolution() {
        let sources = get_sources();
        let logstash_sources = sources.get("logstash").unwrap();

        let health = logstash_sources.get("logstash_health_report").unwrap();
        let v_8_15 = Version::parse("8.15.0").unwrap();
        let v_8_16 = Version::parse("8.16.0").unwrap();
        assert!(health.get_url(&v_8_15).is_err());
        assert_eq!(health.get_url(&v_8_16).unwrap(), "/_health_report");

        let hot_threads_human = logstash_sources.get("logstash_nodes_hot_threads_human").unwrap();
        assert_eq!(
            hot_threads_human.get_file_path("logstash_nodes_hot_threads_human"),
            "logstash_nodes_hot_threads_human.txt"
        );
    }

    #[test]
    fn test_product_specific_override_only_replaces_target_product() {
        let dir = tempfile::tempdir().expect("temp dir");
        let override_path = dir.path().join("sources.yml");
        std::fs::write(
            &override_path,
            r#"
logstash_node:
  versions:
    "> 5.0.0": "/custom_node"
logstash_version:
  versions:
    "> 5.0.0": "/custom_version"
"#,
        )
        .expect("write override");

        let products =
            super::load_embedded_sources(Some("logstash"), Some(override_path.to_str().expect("override path")))
                .expect("load sources");

        let es_sources = products.get("elasticsearch").unwrap();
        let logstash_sources = products.get("logstash").unwrap();
        assert!(es_sources.contains_key("version"));
        assert_eq!(
            logstash_sources
                .get("logstash_node")
                .unwrap()
                .get_url(&Version::parse("8.19.0").unwrap())
                .unwrap(),
            "/custom_node"
        );
    }

    #[test]
    fn test_product_specific_override_rejects_wrong_product_shape() {
        let dir = tempfile::tempdir().expect("temp dir");
        let override_path = dir.path().join("sources.yml");
        std::fs::write(
            &override_path,
            r#"
version:
  versions:
    "> 5.0.0": "/"
"#,
        )
        .expect("write override");

        let err = match super::load_embedded_sources(
            Some("logstash"),
            Some(override_path.to_str().expect("override path")),
        ) {
            Ok(_) => panic!("override should fail"),
            Err(err) => err,
        };
        assert!(err.to_string().contains("valid logstash sources.yml"));
    }

    #[test]
    fn test_kibana_structured_version_resolution() {
        let sources = get_sources();
        let kibana_sources = sources.get("kibana").unwrap();
        let alerts = kibana_sources.get("kibana_alerts").unwrap();

        let resolved = alerts.resolve_version(&Version::parse("8.19.0").unwrap()).unwrap();

        assert_eq!(resolved.url, "/api/alerts/_find");
        assert!(resolved.spaceaware);
        assert_eq!(resolved.paginate.as_deref(), Some("per_page"));
    }

    #[test]
    fn test_kibana_source_file_path_generation() {
        let sources = get_sources();
        let kibana_sources = sources.get("kibana").unwrap();
        let status = kibana_sources.get("kibana_status").unwrap();

        assert_eq!(status.get_file_path("kibana_status"), "kibana_status.json");
    }
}