elasticctl 0.1.3

Operate Elastic Security as code with a safety-first CLI for security engineers.
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
use assert_cmd::Command;
use serde_json::json;
use std::fs;
use wiremock::matchers::{body_partial_json, method, path, query_param};
use wiremock::{Mock, MockServer, ResponseTemplate};

fn config_for(dir: &std::path::Path, uri: &str) -> std::path::PathBuf {
    let p = dir.join("config.toml");
    fs::write(
        &p,
        format!(
            "current = \"default\"\n\n[profiles.default]\nkibana_url = \"{uri}\"\napi_key = \"essu_t\"\nspace = \"default\"\nverify = true\ntimeout_secs = 5\n"
        ),
    )
    .unwrap();
    p
}

/// Serves an export with volatile fields and unsorted keys.
async fn exporting_server() -> MockServer {
    let server = MockServer::start().await;
    let body = concat!(
        r#"{"zeta":1,"rule_id":"b","name":"Beta","id":"srv-2","updated_at":"2026-01-01T00:00:00Z"}"#,
        "\n",
        r#"{"zeta":1,"rule_id":"a","name":"Alpha","id":"srv-1","updated_at":"2026-01-01T00:00:00Z"}"#,
        "\n",
        r#"{"exported_count":2,"exported_rules_count":2,"missing_rules_count":0}"#,
        "\n"
    );
    Mock::given(method("POST"))
        .and(path("/api/detection_engine/rules/_export"))
        .respond_with(ResponseTemplate::new(200).set_body_string(body))
        .mount(&server)
        .await;
    server
}

#[tokio::test]
async fn export_writes_ndjson_without_volatile_fields() {
    let server = exporting_server().await;
    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let out_file = dir.path().join("rules.ndjson");

    Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "export", "--config"])
        .arg(&cfg)
        .arg("--out")
        .arg(&out_file)
        .assert()
        .success();

    let body = fs::read_to_string(&out_file).unwrap();
    assert_eq!(
        body.lines().count(),
        2,
        "the trailer is not written back out"
    );
    assert!(
        !body.contains("\"id\":"),
        "volatile id must be stripped: {body}"
    );
    assert!(
        !body.contains("updated_at"),
        "volatile timestamps must be stripped: {body}"
    );
}

#[tokio::test]
async fn export_is_deterministic_across_runs() {
    let server = exporting_server().await;
    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());

    let run = |name: &str| {
        let f = dir.path().join(name);
        Command::cargo_bin("elasticctl")
            .unwrap()
            .args(["rules", "export", "--config"])
            .arg(&cfg)
            .arg("--out")
            .arg(&f)
            .assert()
            .success();
        fs::read_to_string(&f).unwrap()
    };

    assert_eq!(
        run("one.ndjson"),
        run("two.ndjson"),
        "two exports must be byte-identical"
    );
}

#[tokio::test]
async fn export_orders_rules_by_rule_id() {
    let server = exporting_server().await;
    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let f = dir.path().join("rules.ndjson");

    Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "export", "--config"])
        .arg(&cfg)
        .arg("--out")
        .arg(&f)
        .assert()
        .success();

    let body = fs::read_to_string(&f).unwrap();
    let first: serde_json::Value = serde_json::from_str(body.lines().next().unwrap()).unwrap();
    assert_eq!(
        first["rule_id"], "a",
        "rules must be sorted by rule_id, not server order"
    );
}

#[tokio::test]
async fn export_yaml_carries_the_same_rules() {
    let server = exporting_server().await;
    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let f = dir.path().join("rules.yaml");

    Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "export", "--format-file", "yaml", "--config"])
        .arg(&cfg)
        .arg("--out")
        .arg(&f)
        .assert()
        .success();

    let rules = elasticctl_api::codec::decode_yaml(&fs::read_to_string(&f).unwrap()).unwrap();
    assert_eq!(rules.len(), 2);
    assert_eq!(rules[0].rule_id().unwrap(), "a");
}

/// With `--out` and `--json`, stdout carries a JSON confirmation report, not
/// the rule file. The report must identify the requested output path.
#[tokio::test]
async fn export_with_out_reports_the_count_and_path_without_reprinting_the_file() {
    let server = exporting_server().await;
    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let out_file = dir.path().join("rules.ndjson");

    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "export", "--json", "--config"])
        .arg(&cfg)
        .arg("--out")
        .arg(&out_file)
        .output()
        .unwrap();
    assert!(out.status.success());

    let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(v["exported"], 2);
    assert_eq!(v["path"], out_file.display().to_string());

    // Stdout must not overwrite the file passed to --out.
    let body = fs::read_to_string(&out_file).unwrap();
    assert_eq!(
        body.lines().count(),
        2,
        "the report must not overwrite the exported file: {body}"
    );
}

#[tokio::test]
async fn export_without_out_prints_the_file_body_to_stdout() {
    let server = exporting_server().await;
    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());

    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "export", "--config"])
        .arg(&cfg)
        .output()
        .unwrap();
    assert!(out.status.success());

    let text = String::from_utf8_lossy(&out.stdout);
    assert_eq!(text.lines().count(), 2, "{text}");
    assert!(!text.contains("updated_at"), "{text}");
}

/// Exported rule content is the payload, not a report. Report formats such as
/// `--format csv` must not re-encode it, or object fields would be lost.
#[tokio::test]
async fn export_without_out_bypasses_the_report_format_pipeline() {
    let server = exporting_server().await;
    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());

    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "export", "--format", "csv", "--config"])
        .arg(&cfg)
        .output()
        .unwrap();

    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    let text = String::from_utf8_lossy(&out.stdout);
    assert_eq!(text.lines().count(), 2, "{text}");
    assert!(
        text.lines().all(|l| l.starts_with('{')),
        "stdout must carry NDJSON, not a CSV report: {text}"
    );
    assert!(text.contains("rule_id"), "{text}");
}

/// `--json` controls report rendering. Without `--out`, stdout is the export
/// file, so `--json` must not wrap the NDJSON in a report envelope.
#[tokio::test]
async fn export_without_out_emits_raw_ndjson_even_under_json() {
    let server = exporting_server().await;
    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());

    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "export", "--json", "--config"])
        .arg(&cfg)
        .output()
        .unwrap();

    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    let text = String::from_utf8_lossy(&out.stdout);
    assert_eq!(text.lines().count(), 2, "one line per rule: {text}");
    for line in text.lines() {
        let v: serde_json::Value =
            serde_json::from_str(line).expect("every line must be one rule object");
        assert!(v.get("rule_id").is_some(), "{line}");
    }
    assert!(
        !text.contains("\"ndjson\""),
        "the body must not be wrapped in an envelope: {text}"
    );
}

/// Stdout bytes must not depend on the report format.
#[tokio::test]
async fn export_to_stdout_is_identical_under_every_report_format() {
    let server = exporting_server().await;
    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());

    let run = |extra: &[&str]| {
        let mut cmd = Command::cargo_bin("elasticctl").unwrap();
        cmd.args(["rules", "export", "--config"]).arg(&cfg);
        cmd.args(extra);
        let out = cmd.output().unwrap();
        assert!(out.status.success());
        String::from_utf8(out.stdout).unwrap()
    };

    let plain = run(&[]);
    assert_eq!(plain, run(&["--json"]));
    assert_eq!(plain, run(&["--format", "yaml"]));
    assert_eq!(plain, run(&["--format", "csv"]));
}

/// `--format-file` selects the file encoding, including when stdout is the
/// destination.
#[tokio::test]
async fn export_to_stdout_honours_format_file_not_format() {
    let server = exporting_server().await;
    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());

    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args([
            "rules",
            "export",
            "--format-file",
            "yaml",
            "--json",
            "--config",
        ])
        .arg(&cfg)
        .output()
        .unwrap();

    assert!(out.status.success());
    let text = String::from_utf8_lossy(&out.stdout);
    let rules = elasticctl_api::codec::decode_yaml(&text).expect("stdout must be the YAML file");
    assert_eq!(rules.len(), 2);
    assert_eq!(rules[0].rule_id().unwrap(), "a");
}

#[tokio::test]
async fn import_is_guarded_and_sends_nothing_on_a_dry_run() {
    let server = MockServer::start().await;
    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let src = dir.path().join("in.ndjson");
    fs::write(
        &src,
        "{\"rule_id\":\"a\",\"name\":\"A\",\"type\":\"query\"}\n",
    )
    .unwrap();

    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "import", "--json", "--config"])
        .arg(&cfg)
        .arg("--path")
        .arg(&src)
        .output()
        .unwrap();

    assert!(out.status.success());
    assert!(String::from_utf8_lossy(&out.stderr).contains("[DRY RUN]"));
    let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(v["applied"], false);
    assert!(
        !server
            .received_requests()
            .await
            .unwrap()
            .iter()
            .any(|r| r.url.path().contains("_import")),
        "a dry run must not upload"
    );
}

#[tokio::test]
async fn import_dry_run_reports_total_not_a_differently_named_count() {
    let server = MockServer::start().await;
    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let src = dir.path().join("in.ndjson");
    fs::write(
        &src,
        "{\"rule_id\":\"a\",\"name\":\"A\",\"type\":\"query\"}\n{\"rule_id\":\"b\",\"name\":\"B\",\"type\":\"query\"}\n",
    )
    .unwrap();

    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "import", "--json", "--config"])
        .arg(&cfg)
        .arg("--path")
        .arg(&src)
        .output()
        .unwrap();

    let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(
        v["total"], 2,
        "dry run and apply must share one count field: {v}"
    );
}

#[tokio::test]
async fn yes_uploads_the_ndjson_and_reports_the_outcome() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/detection_engine/rules/_import"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "success": true, "success_count": 1, "rules_count": 1, "errors": []
        })))
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let src = dir.path().join("in.ndjson");
    fs::write(
        &src,
        "{\"rule_id\":\"a\",\"name\":\"A\",\"type\":\"query\"}\n",
    )
    .unwrap();

    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "import", "--yes", "--json", "--config"])
        .arg(&cfg)
        .arg("--path")
        .arg(&src)
        .output()
        .unwrap();

    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(v["applied"], true);
    assert_eq!(v["succeeded"], 1);
    assert_eq!(v["total"], 1);
    assert!(v["failed"].as_array().unwrap().is_empty());

    let requests = server.received_requests().await.unwrap();
    assert_eq!(requests.len(), 1);
    let content_type = requests[0]
        .headers
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or_default();
    assert!(
        content_type.starts_with("multipart/form-data"),
        "{content_type}"
    );
}

/// Kibana reports import errors per rule instead of returning a `failed` count.
/// A partial failure must still set the shared non-zero exit code and report
/// which rules failed.
#[tokio::test]
async fn a_partial_import_failure_exits_non_zero_and_names_the_failed_rule() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/detection_engine/rules/_import"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "success": false,
            "success_count": 1,
            "rules_count": 2,
            "errors": [
                {"rule_id": "b", "error": {"status_code": 409, "message": "already exists"}}
            ]
        })))
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let src = dir.path().join("in.ndjson");
    fs::write(
        &src,
        "{\"rule_id\":\"a\",\"name\":\"A\",\"type\":\"query\"}\n{\"rule_id\":\"b\",\"name\":\"B\",\"type\":\"query\"}\n",
    )
    .unwrap();

    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "import", "--yes", "--json", "--config"])
        .arg(&cfg)
        .arg("--path")
        .arg(&src)
        .output()
        .unwrap();

    assert_eq!(
        out.status.code(),
        Some(1),
        "a partial import failure must not exit 0: {}",
        String::from_utf8_lossy(&out.stdout)
    );
    let v: serde_json::Value = serde_json::from_slice(&out.stdout)
        .expect("the operator must still get the report, not just the code");
    assert_eq!(v["applied"], true);
    assert_eq!(v["succeeded"], 1);
    assert_eq!(v["total"], 2);
    let failed = v["failed"].as_array().unwrap();
    assert_eq!(failed.len(), 1);
    assert_eq!(failed[0]["rule_id"], "b");
}

/// A selector must export only the named rules, not the whole corpus for the
/// operator to filter by hand.
#[tokio::test]
async fn export_scopes_the_request_to_the_named_selectors() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/detection_engine/rules"))
        .and(query_param("rule_id", "a"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(json!({"rule_id": "a", "name": "Alpha"})),
        )
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/detection_engine/rules/_export"))
        .and(body_partial_json(json!({"objects": [{"rule_id": "a"}]})))
        .respond_with(ResponseTemplate::new(200).set_body_string(concat!(
            r#"{"rule_id":"a","name":"Alpha"}"#,
            "\n",
            r#"{"exported_count":1,"exported_rules_count":1,"missing_rules_count":0}"#,
            "\n"
        )))
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "export", "a", "--config"])
        .arg(&cfg)
        .output()
        .unwrap();

    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    let text = String::from_utf8_lossy(&out.stdout);
    assert_eq!(text.lines().count(), 1, "{text}");
    assert!(text.contains("\"rule_id\":\"a\""), "{text}");
}

#[tokio::test]
async fn export_by_tag_resolves_the_tag_to_ids_first() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/detection_engine/rules/_find"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "page": 1, "perPage": 100, "total": 2,
            "data": [
                {"rule_id": "a", "name": "Alpha", "tags": ["mine"]},
                {"rule_id": "b", "name": "Beta", "tags": ["mine"]}
            ]
        })))
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/detection_engine/rules/_export"))
        .and(body_partial_json(
            json!({"objects": [{"rule_id": "a"}, {"rule_id": "b"}]}),
        ))
        .respond_with(ResponseTemplate::new(200).set_body_string(concat!(
            r#"{"rule_id":"a","name":"Alpha"}"#,
            "\n",
            r#"{"rule_id":"b","name":"Beta"}"#,
            "\n",
            r#"{"exported_count":2,"exported_rules_count":2,"missing_rules_count":0}"#,
            "\n"
        )))
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "export", "--tag", "mine", "--config"])
        .arg(&cfg)
        .output()
        .unwrap();

    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(String::from_utf8_lossy(&out.stdout).lines().count(), 2);
}

/// A tag that matches nothing must fail instead of widening to every rule.
#[tokio::test]
async fn a_selection_matching_nothing_is_refused_not_widened() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/detection_engine/rules/_find"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "page": 1, "perPage": 100, "total": 0, "data": []
        })))
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args([
            "rules",
            "export",
            "--tag",
            "nothing-has-this",
            "--json",
            "--config",
        ])
        .arg(&cfg)
        .output()
        .unwrap();

    assert_eq!(out.status.code(), Some(1));
    let v: serde_json::Value = serde_json::from_slice(&out.stderr).unwrap();
    assert_eq!(v["error"]["kind"], "not_found");
    assert!(
        v["error"]["message"]
            .as_str()
            .unwrap()
            .contains("nothing-has-this"),
        "{v}"
    );
    assert!(
        !server
            .received_requests()
            .await
            .unwrap()
            .iter()
            .any(|r| r.url.path().contains("_export")),
        "nothing may be exported when the selection is empty"
    );
}

/// A tag that matches nothing must remain a `not_found` error even when a
/// separate selector resolves successfully.
#[tokio::test]
async fn a_tag_that_matched_nothing_is_refused_even_when_a_selector_resolved() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/detection_engine/rules"))
        .and(query_param("rule_id", "a"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(json!({"rule_id": "a", "name": "Alpha"})),
        )
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/api/detection_engine/rules/_find"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "page": 1, "perPage": 100, "total": 0, "data": []
        })))
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "export", "a", "--tag", "no-such-tag", "--config"])
        .arg(&cfg)
        .output()
        .unwrap();

    assert_eq!(out.status.code(), Some(1));
    let v: serde_json::Value = serde_json::from_slice(&out.stderr).unwrap();
    assert_eq!(v["error"]["kind"], "not_found");
    assert!(
        v["error"]["message"]
            .as_str()
            .unwrap()
            .contains("no-such-tag"),
        "{v}"
    );
    assert!(
        !server
            .received_requests()
            .await
            .unwrap()
            .iter()
            .any(|r| r.url.path().contains("_export")),
        "nothing may be exported when the tag matched nothing"
    );
}

/// A rule deleted between selection and export is reported as missing, not as
/// a successful short export.
#[tokio::test]
async fn a_missing_rule_is_reported_as_a_failure() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/detection_engine/rules"))
        .and(query_param("rule_id", "gone"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(json!({"rule_id": "gone", "name": "Gone"})),
        )
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/detection_engine/rules/_export"))
        .respond_with(ResponseTemplate::new(200).set_body_string(
            "{\"exported_count\":0,\"exported_rules_count\":0,\"missing_rules\":[{\"rule_id\":\"gone\"}],\"missing_rules_count\":1}\n",
        ))
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let f = dir.path().join("out.ndjson");
    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "export", "gone", "--json", "--config"])
        .arg(&cfg)
        .arg("--out")
        .arg(&f)
        .output()
        .unwrap();

    assert_eq!(
        out.status.code(),
        Some(1),
        "a short export is not a success"
    );
    let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(v["exported"], 0);
    assert_eq!(v["failed"][0]["rule_id"], "gone");
}

/// With `--out`, a short export reports `failed` on stdout. When stdout is the
/// file, the same failure must be conveyed by the exit code.
#[tokio::test]
async fn a_short_export_to_stdout_exits_non_zero() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/detection_engine/rules"))
        .and(query_param("rule_id", "gone"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(json!({"rule_id": "gone", "name": "Gone"})),
        )
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/detection_engine/rules/_export"))
        .respond_with(ResponseTemplate::new(200).set_body_string(
            "{\"exported_count\":0,\"exported_rules_count\":0,\"missing_rules\":[{\"rule_id\":\"gone\"}],\"missing_rules_count\":1}\n",
        ))
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "export", "gone", "--config"])
        .arg(&cfg)
        .output()
        .unwrap();

    assert_eq!(
        out.status.code(),
        Some(1),
        "a short export is not a success"
    );
    assert!(
        out.stdout.is_empty(),
        "stdout must stay the file content, not a report: {}",
        String::from_utf8_lossy(&out.stdout)
    );
}

/// Existing rules should be skipped before import, so scripts can distinguish
/// them from failed uploads.
#[tokio::test]
async fn skip_existing_uploads_only_the_new_rules() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/detection_engine/rules/_find"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "page": 1, "perPage": 2, "total": 1,
            "data": [{"rule_id": "a", "name": "A"}]
        })))
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/detection_engine/rules/_import"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "success": true, "success_count": 1, "rules_count": 1, "errors": []
        })))
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let src = dir.path().join("in.ndjson");
    fs::write(
        &src,
        "{\"rule_id\":\"a\",\"name\":\"A\",\"type\":\"query\"}\n{\"rule_id\":\"b\",\"name\":\"B\",\"type\":\"query\"}\n",
    )
    .unwrap();

    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args([
            "rules",
            "import",
            "--skip-existing",
            "--yes",
            "--json",
            "--config",
        ])
        .arg(&cfg)
        .arg("--path")
        .arg(&src)
        .output()
        .unwrap();

    assert!(
        out.status.success(),
        "a skip is not a failure: {}",
        String::from_utf8_lossy(&out.stdout)
    );
    let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(v["succeeded"], 1);
    assert_eq!(v["skipped"][0]["rule_id"], "a");
    assert_eq!(v["total"], 2);
    assert!(v["failed"].as_array().unwrap().is_empty());

    let uploads: Vec<_> = server
        .received_requests()
        .await
        .unwrap()
        .into_iter()
        .filter(|r| r.url.path().contains("_import"))
        .collect();
    let body = String::from_utf8_lossy(&uploads[0].body);
    assert!(body.contains("\"rule_id\":\"b\""), "{body}");
    assert!(
        !body.contains("\"rule_id\":\"a\""),
        "an existing rule must not be uploaded at all: {body}"
    );
}

/// A dry run must identify rules that would be skipped instead of listing all
/// rules as pending imports.
#[tokio::test]
async fn the_skip_existing_dry_run_names_what_would_be_skipped() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/detection_engine/rules/_find"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "page": 1, "perPage": 2, "total": 1,
            "data": [{"rule_id": "a", "name": "A"}]
        })))
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let src = dir.path().join("in.ndjson");
    fs::write(
        &src,
        "{\"rule_id\":\"a\",\"name\":\"A\",\"type\":\"query\"}\n{\"rule_id\":\"b\",\"name\":\"B\",\"type\":\"query\"}\n",
    )
    .unwrap();

    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "import", "--skip-existing", "--json", "--config"])
        .arg(&cfg)
        .arg("--path")
        .arg(&src)
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(stderr.contains("[DRY RUN]"), "{stderr}");
    assert!(
        stderr.contains("skip"),
        "the preview must name the skip: {stderr}"
    );
    let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(v["applied"], false);
    assert_eq!(v["pending"], 1);
    assert_eq!(v["skipped"][0]["rule_id"], "a");
    assert!(
        !server
            .received_requests()
            .await
            .unwrap()
            .iter()
            .any(|r| r.url.path().contains("_import")),
        "a dry run must not upload"
    );
}

#[tokio::test]
async fn skip_existing_and_overwrite_cannot_be_combined() {
    // These flags give opposite answers to the same conflict.
    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), "http://127.0.0.1:1");
    let src = dir.path().join("in.ndjson");
    fs::write(
        &src,
        "{\"rule_id\":\"a\",\"name\":\"A\",\"type\":\"query\"}\n",
    )
    .unwrap();

    Command::cargo_bin("elasticctl")
        .unwrap()
        .args([
            "rules",
            "import",
            "--overwrite",
            "--skip-existing",
            "--config",
        ])
        .arg(&cfg)
        .arg("--path")
        .arg(&src)
        .assert()
        .code(2);
}

/// Without either flag, an import conflict remains a reported failure and
/// exits 1.
#[tokio::test]
async fn the_default_import_still_reports_a_conflict_as_a_failure() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/detection_engine/rules/_import"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "success": false, "success_count": 0, "rules_count": 1,
            "errors": [{"rule_id": "a", "error": {"status_code": 409, "message": "already exists"}}]
        })))
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let cfg = config_for(dir.path(), &server.uri());
    let src = dir.path().join("in.ndjson");
    fs::write(
        &src,
        "{\"rule_id\":\"a\",\"name\":\"A\",\"type\":\"query\"}\n",
    )
    .unwrap();

    let out = Command::cargo_bin("elasticctl")
        .unwrap()
        .args(["rules", "import", "--yes", "--json", "--config"])
        .arg(&cfg)
        .arg("--path")
        .arg(&src)
        .output()
        .unwrap();

    assert_eq!(out.status.code(), Some(1));
    let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(v["failed"][0]["rule_id"], "a");
    assert!(v["skipped"].as_array().unwrap().is_empty());
}