Skip to main content

elasticctl_api/
alerts_ops.rs

1//! Alert orchestration: filter construction, list/get, and the triage
2//! mutation plans behind the CLI guard.
3
4mod recheck;
5pub use recheck::{AlertFacet, observe_alerts};
6mod observed;
7pub use observed::{plan_status_by_ids_observed, plan_tags_observed};
8
9use crate::alerts::{self, AlertHit, AlertStatus, Conflicts, SignalsOutcome};
10use crate::{profiles, selection};
11use elasticctl_core::{Error, ErrorKind, Result, Transport};
12use serde_json::{Value, json};
13
14/// The `alerts list` filter set. Every field composes into one boolean query
15/// over the measured `kibana.alert.*` fields (triage spec section 4).
16#[derive(Debug, Clone, Default)]
17pub struct AlertFilter {
18    pub status: Option<AlertStatus>,
19    pub severity: Option<String>,
20    /// A rule name or `rule_id`, resolved through the standard name-or-id
21    /// resolution before filtering on `kibana.alert.rule.rule_id`.
22    pub rule: Option<String>,
23    pub tag: Option<String>,
24    /// A username or `uid:<profile_uid>`, resolved to a profile uid.
25    pub assignee: Option<String>,
26    /// A duration (`90m`, `24h`, `7d`) or an ISO timestamp.
27    pub since: Option<String>,
28    /// Substring match on the rule name and reason text.
29    pub search: Option<String>,
30}
31
32/// `--since` as a range clause: `<digits><s|m|h|d|w>` becomes `now-<dur>`;
33/// anything else passes through verbatim for the server to validate as a
34/// timestamp or date-math expression.
35pub fn since_clause(since: &str) -> Value {
36    let bytes = since.as_bytes();
37    let is_duration = bytes.len() >= 2
38        && bytes[..bytes.len() - 1].iter().all(u8::is_ascii_digit)
39        && matches!(bytes[bytes.len() - 1], b's' | b'm' | b'h' | b'd' | b'w');
40    let gte = if is_duration {
41        format!("now-{since}")
42    } else {
43        since.to_string()
44    };
45    json!({"range": {"@timestamp": {"gte": gte}}})
46}
47
48/// Newest first, with `kibana.alert.uuid` as the total-order tiebreaker
49/// `search_after` needs.
50pub fn default_sort() -> Value {
51    json!([
52        {"@timestamp": {"order": "desc"}},
53        {"kibana.alert.uuid": {"order": "asc"}}
54    ])
55}
56
57/// Escape `\`, `*`, and `?` so `--search` text cannot inject its own
58/// Elasticsearch `wildcard` metacharacters into the substring match. Escape
59/// backslashes first, or an inserted `\*`/`\?` escape would itself be
60/// re-escaped. Mirrors `rules::kql_escape_wildcard`'s ordering, though this
61/// value sits in a plain Query DSL string, not a quoted KQL literal, so no
62/// quote-escaping applies here.
63fn escape_wildcard(value: &str) -> String {
64    value
65        .replace('\\', "\\\\")
66        .replace('*', "\\*")
67        .replace('?', "\\?")
68}
69
70/// Compose the filter into one boolean query, resolving `rule` and
71/// `assignee` first. An empty filter is an explicit `match_all`.
72pub async fn build_query(t: &Transport, f: &AlertFilter) -> Result<Value> {
73    let mut filter = Vec::new();
74    if let Some(status) = f.status {
75        filter.push(json!({"term": {"kibana.alert.workflow_status": status.as_str()}}));
76    }
77    if let Some(severity) = &f.severity {
78        filter.push(json!({"term": {"kibana.alert.severity": severity}}));
79    }
80    if let Some(rule) = &f.rule {
81        let rule_id = selection::to_rule_id(t, rule).await?;
82        filter.push(json!({"term": {"kibana.alert.rule.rule_id": rule_id}}));
83    }
84    if let Some(tag) = &f.tag {
85        filter.push(json!({"term": {"kibana.alert.workflow_tags": tag}}));
86    }
87    if let Some(assignee) = &f.assignee {
88        let uid = profiles::resolve_assignee(t, assignee).await?;
89        filter.push(json!({"term": {"kibana.alert.workflow_assignee_ids": uid}}));
90    }
91    if let Some(since) = &f.since {
92        filter.push(since_clause(since));
93    }
94    if let Some(text) = &f.search {
95        let pattern = format!("*{}*", escape_wildcard(text));
96        filter.push(json!({"bool": {"minimum_should_match": 1, "should": [
97            {"wildcard": {"kibana.alert.rule.name": {"value": pattern, "case_insensitive": true}}},
98            {"wildcard": {"kibana.alert.reason": {"value": pattern, "case_insensitive": true}}}
99        ]}}));
100    }
101    if filter.is_empty() {
102        Ok(json!({"match_all": {}}))
103    } else {
104        Ok(json!({"bool": {"filter": filter}}))
105    }
106}
107
108#[derive(Debug, Clone, PartialEq)]
109pub struct AlertList {
110    pub hits: Vec<AlertHit>,
111    pub total: Option<u64>,
112    pub truncated: bool,
113}
114
115/// One bounded peek: `limit + 1` rows so truncation is observable without a
116/// second request.
117pub async fn list(t: &Transport, f: &AlertFilter, limit: usize) -> Result<AlertList> {
118    let query = build_query(t, f).await?;
119    let body = json!({
120        "query": query,
121        "sort": default_sort(),
122        "size": limit.saturating_add(1),
123        "track_total_hits": true,
124    });
125    let mut page = alerts::search(t, &body).await?;
126    let truncated = page.hits.len() > limit;
127    page.hits.truncate(limit);
128    Ok(AlertList {
129        hits: page.hits,
130        total: page.total,
131        truncated,
132    })
133}
134
135/// The `--out` path: page the filtered set fully, or stop at `limit` rows
136/// when the caller passes one (matching `search dsl --out --limit`).
137pub async fn export(t: &Transport, f: &AlertFilter, limit: Option<usize>) -> Result<Vec<AlertHit>> {
138    let query = build_query(t, f).await?;
139    alerts::search_all(t, &query, &default_sort(), limit).await
140}
141
142/// `alerts get`: an `_id`-filtered search returning one document.
143pub async fn get_one(t: &Transport, alert_id: &str) -> Result<AlertHit> {
144    let body = json!({"query": {"ids": {"values": [alert_id]}}, "size": 1});
145    let page = alerts::search(t, &body).await?;
146    page.hits.into_iter().next().ok_or_else(|| {
147        Error::new(
148            ErrorKind::NotFound,
149            format!("No alert with id '{alert_id}'"),
150        )
151    })
152}
153
154/// One explicitly named alert, resolved before a preview.
155#[derive(Debug, Clone, PartialEq, Eq)]
156pub struct ResolvedAlert {
157    pub id: String,
158    pub rule_name: String,
159    pub status: String,
160}
161
162/// `_source` fields `resolve_ids` requests: only what a mutation preview
163/// renders (rule name and current workflow status), not the whole document.
164/// `pub` so the fixture recorder can send the identical production body
165/// instead of a hand-rolled approximation (triage spec section 10).
166pub const RESOLVE_SOURCE_FIELDS: &[&str] =
167    &["kibana.alert.rule.name", "kibana.alert.workflow_status"];
168
169/// Alert documents store dotted field names as flat `_source` keys; older
170/// pipelines may nest them. Read both shapes. `pub(crate)` so `cases_ops`
171/// shares this instead of carrying its own copy (finding 11).
172pub(crate) fn source_str<'a>(source: &'a Value, key: &str) -> Option<&'a str> {
173    if let Some(v) = source.get(key).and_then(Value::as_str) {
174        return Some(v);
175    }
176    let pointer = format!("/{}", key.replace('.', "/"));
177    source.pointer(&pointer).and_then(Value::as_str)
178}
179
180fn workflow_status(source: &Value) -> &str {
181    source_str(source, "kibana.alert.workflow_status").unwrap_or("unknown")
182}
183
184/// Build the search body for explicit alert IDs.
185fn resolve_body(unique: &[String]) -> Value {
186    json!({
187        "query": {"ids": {"values": unique}},
188        "size": unique.len(),
189        "_source": RESOLVE_SOURCE_FIELDS,
190    })
191}
192
193/// Resolve explicit ids to alerts. Fail-closed: every id must resolve or the
194/// command refuses to proceed on the partial set (main spec section 6.3).
195/// Duplicate ids are collapsed, preserving first-seen order.
196async fn resolve_ids(t: &Transport, ids: &[String]) -> Result<Vec<ResolvedAlert>> {
197    let mut unique: Vec<String> = Vec::with_capacity(ids.len());
198    for id in ids {
199        if !unique.contains(id) {
200            unique.push(id.clone());
201        }
202    }
203    let body = resolve_body(&unique);
204    let page = alerts::search(t, &body).await?;
205    let mut resolved = Vec::with_capacity(unique.len());
206    for id in &unique {
207        let hit = page.hits.iter().find(|h| &h.id == id).ok_or_else(|| {
208            let missing: Vec<&str> = unique
209                .iter()
210                .filter(|i| !page.hits.iter().any(|h| &h.id == *i))
211                .map(String::as_str)
212                .collect();
213            Error::new(
214                ErrorKind::NotFound,
215                format!("No alert with id: {}", missing.join(", ")),
216            )
217        })?;
218        resolved.push(ResolvedAlert {
219            id: id.clone(),
220            rule_name: source_str(&hit.source, "kibana.alert.rule.name")
221                .unwrap_or("(unnamed rule)")
222                .to_string(),
223            status: workflow_status(&hit.source).to_string(),
224        });
225    }
226    Ok(resolved)
227}
228
229fn alert_noun(n: usize) -> &'static str {
230    if n == 1 { "alert" } else { "alerts" }
231}
232
233/// `1214` → `1,214`, matching the preview format in triage spec section 6.
234fn thousands(n: u64) -> String {
235    let digits = n.to_string();
236    let mut out = String::with_capacity(digits.len() + digits.len() / 3);
237    for (i, c) in digits.chars().enumerate() {
238        if i > 0 && (digits.len() - i).is_multiple_of(3) {
239            out.push(',');
240        }
241        out.push(c);
242    }
243    out
244}
245
246#[derive(Debug, Clone, PartialEq)]
247pub struct StatusPlan {
248    pub status: AlertStatus,
249    pub reason: Option<String>,
250    pub targets: Vec<String>,
251    pub preview_action: String,
252    pub preview_details: Vec<String>,
253}
254
255pub async fn plan_status_by_ids(
256    t: &Transport,
257    ids: &[String],
258    status: AlertStatus,
259    reason: Option<String>,
260) -> Result<StatusPlan> {
261    Ok(plan_status_by_ids_observed(t, ids, status, reason).await?.0)
262}
263
264/// The mutation report the CLI renders. `failed` is elasticctl's judgment —
265/// route failures plus, under `--conflicts abort`, version conflicts — and
266/// drives the non-zero exit; the verbatim counters render beside it.
267#[derive(Debug, Clone, PartialEq, serde::Serialize)]
268pub struct StatusReport {
269    pub applied: bool,
270    pub status: String,
271    pub total: u64,
272    pub updated: u64,
273    pub version_conflicts: u64,
274    pub noops: u64,
275    pub failed: u64,
276    pub failures: Vec<Value>,
277}
278
279fn failed_count(outcome: &SignalsOutcome, conflicts: Conflicts) -> u64 {
280    outcome.failures.len() as u64
281        + match conflicts {
282            Conflicts::Abort => outcome.version_conflicts,
283            Conflicts::Proceed => 0,
284        }
285}
286
287fn status_report(
288    status: AlertStatus,
289    outcome: SignalsOutcome,
290    conflicts: Conflicts,
291) -> StatusReport {
292    StatusReport {
293        applied: true,
294        status: status.as_str().to_string(),
295        total: outcome.total,
296        updated: outcome.updated,
297        version_conflicts: outcome.version_conflicts,
298        noops: outcome.noops,
299        failed: failed_count(&outcome, conflicts),
300        failures: outcome.failures,
301    }
302}
303
304pub async fn apply_status_by_ids(t: &Transport, plan: &StatusPlan) -> Result<StatusReport> {
305    require_targets(&plan.targets)?;
306    let outcome =
307        alerts::status_by_ids(t, &plan.targets, plan.status, plan.reason.as_deref()).await?;
308    Ok(status_report(plan.status, outcome, Conflicts::Abort))
309}
310
311pub const QUERY_SAMPLE_SIZE: usize = 10;
312
313#[derive(Debug, Clone, PartialEq)]
314pub struct QueryStatusPlan {
315    pub status: AlertStatus,
316    pub reason: Option<String>,
317    pub conflicts: Conflicts,
318    pub query: Value,
319    pub matched: u64,
320    pub preview_action: String,
321    pub preview_details: Vec<String>,
322}
323
324/// Refuse an empty `{}` query: it would mutate every alert. Called from both
325/// `plan_status_by_query` and `apply_status_by_query` — `QueryStatusPlan`'s
326/// fields and `apply_status_by_query` are both public, so an `-api` consumer
327/// that builds a plan directly (skipping `plan_status_by_query`, as the
328/// planned MCP server might) must not be able to bypass the check.
329fn require_scoped_query(query: &Value) -> Result<()> {
330    let obj = query
331        .as_object()
332        .ok_or_else(|| Error::new(ErrorKind::Error, "--query must be a JSON object"))?;
333    if obj.is_empty() {
334        return Err(Error::new(
335            ErrorKind::Error,
336            "an empty --query would mutate every alert; say what it matches, \
337             e.g. an explicit {\"match_all\":{}}",
338        ));
339    }
340    Ok(())
341}
342
343/// Resolve the operator's query to a count and a sample so the implicit set
344/// is visible before it is mutated (triage spec section 6).
345pub async fn plan_status_by_query(
346    t: &Transport,
347    query: Value,
348    status: AlertStatus,
349    conflicts: Conflicts,
350    reason: Option<String>,
351) -> Result<QueryStatusPlan> {
352    require_scoped_query(&query)?;
353    let body = json!({
354        "query": &query,
355        "size": QUERY_SAMPLE_SIZE,
356        "track_total_hits": true,
357        "sort": default_sort(),
358        "_source": ["kibana.alert.rule.name", "kibana.alert.severity", "@timestamp"],
359    });
360    let page = alerts::search(t, &body).await?;
361    let matched = page.total.ok_or_else(|| {
362        Error::new(
363            ErrorKind::Http,
364            "decoding alerts response field `hits.total.value`",
365        )
366    })?;
367    let mut preview_details = vec![format!(
368        "matched now: {}   showing {} of {}",
369        thousands(matched),
370        page.hits.len(),
371        thousands(matched)
372    )];
373    for hit in &page.hits {
374        preview_details.push(format!(
375            "{}  {}  {}  {}",
376            hit.id,
377            source_str(&hit.source, "kibana.alert.rule.name").unwrap_or("(unnamed rule)"),
378            source_str(&hit.source, "kibana.alert.severity").unwrap_or("-"),
379            source_str(&hit.source, "@timestamp").unwrap_or("-"),
380        ));
381    }
382    preview_details.push("The set is resolved again at apply time; this count is advisory.".into());
383    Ok(QueryStatusPlan {
384        preview_action: format!("{} alerts matching query", status.verb()),
385        status,
386        reason,
387        conflicts,
388        query,
389        matched,
390        preview_details,
391    })
392}
393
394pub async fn apply_status_by_query(t: &Transport, plan: &QueryStatusPlan) -> Result<StatusReport> {
395    require_scoped_query(&plan.query)?;
396    let outcome = alerts::status_by_query(
397        t,
398        &plan.query,
399        plan.status,
400        plan.conflicts,
401        plan.reason.as_deref(),
402    )
403    .await?;
404    Ok(status_report(plan.status, outcome, plan.conflicts))
405}
406
407/// The tags/assignees report: the same counters without a target status.
408#[derive(Debug, Clone, PartialEq, serde::Serialize)]
409pub struct EditReport {
410    pub applied: bool,
411    pub total: u64,
412    pub updated: u64,
413    pub version_conflicts: u64,
414    pub noops: u64,
415    pub failed: u64,
416    pub failures: Vec<Value>,
417}
418
419fn edit_report(outcome: SignalsOutcome) -> EditReport {
420    EditReport {
421        applied: true,
422        total: outcome.total,
423        updated: outcome.updated,
424        version_conflicts: outcome.version_conflicts,
425        noops: outcome.noops,
426        failed: failed_count(&outcome, Conflicts::Abort),
427        failures: outcome.failures,
428    }
429}
430
431/// Refuse an empty target list before any resolution or request. The CLI's
432/// `required = true` covers the `alerts tag`/`assign` invocations, but
433/// `plan_tags` and `plan_assign` are public, so an `-api` consumer (the
434/// planned MCP server) calling them with `&[]` must not be able to reach a
435/// `{"ids": []}` POST that reports `applied: true, total: 0`.
436fn require_targets(ids: &[String]) -> Result<()> {
437    if ids.is_empty() {
438        return Err(Error::new(ErrorKind::Error, "pass at least one alert id"));
439    }
440    Ok(())
441}
442
443fn require_edit(add: &[String], remove: &[String]) -> Result<()> {
444    if add.is_empty() && remove.is_empty() {
445        return Err(Error::new(ErrorKind::Error, "pass --add and/or --remove"));
446    }
447    Ok(())
448}
449
450fn require_disjoint(add: &[String], remove: &[String], what: &str) -> Result<()> {
451    let overlap: Vec<&str> = add
452        .iter()
453        .filter(|a| remove.contains(a))
454        .map(String::as_str)
455        .collect();
456    if overlap.is_empty() {
457        Ok(())
458    } else {
459        Err(Error::new(
460            ErrorKind::Conflict,
461            format!("{what} both added and removed: {}", overlap.join(", ")),
462        ))
463    }
464}
465
466fn edit_summary(add: &[String], remove: &[String]) -> String {
467    let mut parts: Vec<String> = add.iter().map(|a| format!("+{a}")).collect();
468    parts.extend(remove.iter().map(|r| format!("-{r}")));
469    parts.join(" ")
470}
471
472#[derive(Debug, Clone, PartialEq)]
473pub struct TagsPlan {
474    pub targets: Vec<String>,
475    pub add: Vec<String>,
476    pub remove: Vec<String>,
477    pub preview_action: String,
478    pub preview_details: Vec<String>,
479}
480
481pub async fn plan_tags(
482    t: &Transport,
483    ids: &[String],
484    add: Vec<String>,
485    remove: Vec<String>,
486) -> Result<TagsPlan> {
487    Ok(plan_tags_observed(t, ids, add, remove).await?.0)
488}
489
490pub async fn apply_tags(t: &Transport, plan: &TagsPlan) -> Result<EditReport> {
491    require_targets(&plan.targets)?;
492    require_edit(&plan.add, &plan.remove)?;
493    require_disjoint(&plan.add, &plan.remove, "tags")?;
494    Ok(edit_report(
495        alerts::set_tags(t, &plan.targets, &plan.add, &plan.remove).await?,
496    ))
497}
498
499#[derive(Debug, Clone, PartialEq)]
500pub struct AssignPlan {
501    pub targets: Vec<String>,
502    /// Resolved profile uids.
503    pub add: Vec<String>,
504    pub remove: Vec<String>,
505    pub preview_action: String,
506    pub preview_details: Vec<String>,
507}
508
509pub async fn plan_assign(
510    t: &Transport,
511    ids: &[String],
512    add_users: &[String],
513    remove_users: &[String],
514) -> Result<AssignPlan> {
515    require_targets(ids)?;
516    require_edit(add_users, remove_users)?;
517    let mut add = Vec::with_capacity(add_users.len());
518    let mut remove = Vec::with_capacity(remove_users.len());
519    let mut mapping = Vec::new();
520    for user in add_users {
521        let uid = profiles::resolve_assignee(t, user).await?;
522        mapping.push(format!("add {user} -> {uid}"));
523        add.push(uid);
524    }
525    for user in remove_users {
526        let uid = profiles::resolve_assignee(t, user).await?;
527        mapping.push(format!("remove {user} -> {uid}"));
528        remove.push(uid);
529    }
530    require_disjoint(&add, &remove, "assignees")?;
531    let resolved = resolve_ids(t, ids).await?;
532    let summary = edit_summary(&add, &remove);
533    let mut preview_details = mapping;
534    preview_details.extend(
535        resolved
536            .iter()
537            .map(|r| format!("{}  {}  {}", r.id, r.rule_name, summary)),
538    );
539    Ok(AssignPlan {
540        preview_action: format!("Assign {} {}", resolved.len(), alert_noun(resolved.len())),
541        targets: resolved.into_iter().map(|r| r.id).collect(),
542        add,
543        remove,
544        preview_details,
545    })
546}
547
548pub async fn apply_assign(t: &Transport, plan: &AssignPlan) -> Result<EditReport> {
549    require_targets(&plan.targets)?;
550    require_edit(&plan.add, &plan.remove)?;
551    require_disjoint(&plan.add, &plan.remove, "assignees")?;
552    Ok(edit_report(
553        alerts::set_assignees(t, &plan.targets, &plan.add, &plan.remove).await?,
554    ))
555}