airs-cli 0.9.0

Native Rust CLI for Prisma AIRS (executable airs-cli-rs until 1.0), built on the scm-sdk crate
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
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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
//! M6d planning and dispatch (DESIGN §18): every §18.3 argument check runs here, before
//! the tenant is read (D220); dispatch connects the management client with the planes the
//! command needs (`ManagementClients::build_for`, §18.3) and runs it.
//!
//! Step 1 (§18.1) plans the reads: `api-keys list`, `customer-apps list|get|consumption`,
//! `deployment-profiles list`, the `scan-logs query` stub and every `dashboard` and
//! `sessions` read. Step 2 plans the writes (`api-keys create|regenerate|delete`,
//! `customer-apps update|delete`: their arguments and the D81 gate) and `runtime report`.
//! Step 3 adds `profiles backup|restore`: their plans come from `profile_backup::plan`
//! and `profile_restore::plan` (§18.15, §18.16) and are dispatched here like the others.

use super::report_command::ReportDestination;
use super::runtime_apps::UpdatePlan;
use super::runtime_keys::RegeneratePlan;
use super::runtime_report::RuntimeReportPlan;
use super::{Invocation, Plan};
use crate::cli::{
    ApiKeysCommand, CustomerAppsCommand, DashboardCommand, DashboardPageArgs, DashboardWindowArgs,
    DashboardWindowDaysArgs, DeploymentProfilesCommand, ProfilesCommand, RuntimeCommand,
    ScanIdentityArgs, ScanLogsCommand, SessionIdentityArgs, SessionsCommand,
};
use crate::clients::{ManagementClients, Planes};
use crate::error::{CliError, InterruptContext};
use crate::output::{self, Format};
use crate::pagination::{self, ListParams};
use crate::runtime::keys;
use crate::runtime::{self, APPLICATION_UNITS, DETAIL_UNITS, TIME_UNITS};
use crate::tenant::TenantContext;
use std::ffi::OsString;
use std::path::PathBuf;

/// A time-range dashboard read (TS `window`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TimeRead {
    TopApplications,
    ViolationsTrend,
    AppsList,
    SessionsChart,
}

/// One dashboard or sessions read (§18.12, §18.13), validated.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DashRead {
    Applications {
        interval: u64,
        unit: String,
        limit: u64,
        offset: u64,
    },
    Application {
        violations: bool,
        app_id: String,
        app_name: String,
        interval: u64,
    },
    TimeRange {
        read: TimeRead,
        interval: u64,
        unit: String,
    },
    Session {
        identity: SessionIdentity,
        limit: u64,
        offset: u64,
    },
    Transaction {
        identity: SessionIdentity,
        scan_id: String,
        sub_request: u64,
    },
}

/// The session identity and its window (TS `identity`).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SessionIdentity {
    pub session_id: String,
    pub app_id: String,
    pub app_name: String,
    pub interval: u64,
    pub unit: String,
}

/// `sessions list|ls` (§18.13).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SessionsList {
    pub interval: u64,
    pub unit: String,
    pub limit: u64,
    pub offset: u64,
    pub all: bool,
    pub max: u64,
}

/// A validated M6d invocation; `format` is the explicit `--output`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RxPlan {
    KeysList {
        params: ListParams,
        format: Option<Format>,
    },
    AppsList {
        params: ListParams,
        format: Option<Format>,
    },
    AppsGet {
        name: String,
        format: Option<Format>,
    },
    Consumption {
        app: Option<String>,
        interval: u64,
        format: Option<Format>,
    },
    DeploymentProfiles {
        unactivated: bool,
        format: Option<Format>,
    },
    /// The TypeScript stub (§18.11): reads the tenant selection, sends nothing.
    ScanLogs,
    Dashboard {
        read: DashRead,
        format: Option<Format>,
    },
    SessionsList {
        list: SessionsList,
        format: Option<Format>,
    },
    ScanContent {
        scan_id: String,
        sub_request: u64,
        /// `--output-file` as given; `None`: `--show-content`.
        file: Option<PathBuf>,
        format: Option<Format>,
    },
    KeysCreate {
        config: OsString,
        key_file: PathBuf,
        format: Option<Format>,
    },
    KeysRegenerate(Box<RegeneratePlan>),
    KeysDelete {
        name: String,
        updated_by: String,
        force: bool,
    },
    AppsUpdate(Box<UpdatePlan>),
    AppsDelete {
        name: String,
        updated_by: String,
        force: bool,
    },
    Report(Box<RuntimeReportPlan>),
    /// `runtime profiles backup [profile]` (§18.15).
    ProfilesBackup(Box<super::profile_backup::BackupPlan>),
    /// `runtime profiles restore <file>` (§18.16).
    ProfilesRestore(Box<super::profile_restore::RestoreRun>),
}

impl RxPlan {
    /// What an interruption reports (§18.3 "Interruption"): the reads write no data
    /// (`scan-content --output-file` names its file once it is published).
    pub fn interrupt_context(&self) -> InterruptContext {
        match self {
            Self::KeysCreate { .. }
            | Self::KeysRegenerate(_)
            | Self::KeysDelete { .. }
            | Self::AppsUpdate(_)
            | Self::AppsDelete { .. } => InterruptContext::NothingChanged,
            Self::Report(_) => {
                InterruptContext::Context(super::report_command::NO_REPORT.to_owned())
            }
            Self::ProfilesBackup(_) => {
                InterruptContext::Context(super::profile_backup::NO_BACKUP_WRITTEN.to_owned())
            }
            Self::ProfilesRestore(_) => InterruptContext::NothingChanged,
            _ => InterruptContext::NoDataWritten,
        }
    }

    /// The `--debug` policy (§18.3 "Debug log"): every command that sends requests logs
    /// status and headers only; the `scan-logs` stub sends nothing; the report refuses
    /// `--debug` during planning.
    pub fn debug_policy(&self) -> crate::debug::DebugPolicy {
        match self {
            Self::ScanLogs => crate::debug::DebugPolicy::None,
            Self::Report(_) | Self::ProfilesBackup(_) | Self::ProfilesRestore(_) => {
                crate::debug::DebugPolicy::Refused
            }
            _ => crate::debug::DebugPolicy::Log,
        }
    }
}

/// Whether the command line runs an M6d command (they check `--output` before the
/// tenant flag, §18.3 "Validation order").
pub fn is_runtime_extras(command: &RuntimeCommand) -> bool {
    matches!(
        command,
        RuntimeCommand::ApiKeys(_)
            | RuntimeCommand::CustomerApps(_)
            | RuntimeCommand::Dashboard(_)
            | RuntimeCommand::DeploymentProfiles(_)
            | RuntimeCommand::Report(_)
            | RuntimeCommand::ScanLogs(_)
            | RuntimeCommand::Sessions(_)
            | RuntimeCommand::Profiles(crate::cli::ProfilesGroup {
                command: Some(ProfilesCommand::Backup(_) | ProfilesCommand::Restore(_)),
            })
    )
}

/// The checks of `api-keys regenerate` (§18.5 "Arguments", in order): the id segment
/// (G29), `--interval`, `--unit`, `--updated-by`, `--key-file`, the body schema, the D81
/// gate.
fn plan_regenerate(
    args: &crate::cli::ApiKeyRegenerateArgs,
    format: Option<Format>,
) -> Result<RegeneratePlan, CliError> {
    crate::segment::check_segment("API key id", &args.api_key_id)?;
    let interval = runtime::rotation_interval(&args.interval)?;
    if crate::tenant::config::js_trim(&args.unit).is_empty() {
        return Err(CliError::usage("--unit must not be empty"));
    }
    runtime::check_text("--unit", &args.unit)?;
    let updated_by = args
        .updated_by
        .as_deref()
        .map(runtime::updated_by)
        .transpose()?;
    let key_file = keys::plan_key_file(&args.key_file)?;
    let mut body = serde_json::json!({
        "rotation_time_interval": interval,
        "rotation_time_unit": args.unit,
    });
    if let Some(updated_by) = updated_by {
        body["updated_by"] = serde_json::Value::String(updated_by);
    }
    scm::airs::management::models::ApiKeyRegenerateRequest::parse(
        body.clone(),
        scm::validation::Mode::Request,
    )
    .map_err(|_| {
        CliError::usage(
            crate::writes::invalid_body(
                "POST",
                &format!("/v1/mgmt/apikey/regenerate/{}", args.api_key_id),
            )
            .message(),
        )
    })?;
    super::runtime_write::gate(
        args.force,
        &format!("regenerate API key {}", args.api_key_id),
    )?;
    Ok(RegeneratePlan {
        id: args.api_key_id.clone(),
        body,
        key_file,
        force: args.force,
        format,
    })
}

/// The checks of `runtime report` (§18.14, in TS order).
fn plan_report(
    args: &crate::cli::RuntimeReportArgs,
    explicit: Option<Format>,
    debug: bool,
) -> Result<RuntimeReportPlan, CliError> {
    if debug {
        return Err(CliError::usage(
            "Disable --debug for environment reports to avoid persisting sensitive traffic content",
        ));
    }
    if explicit.is_some() {
        return Err(CliError::usage(
            "The environment report is written as HTML; omit --output",
        ));
    }
    let max_pages = args.max_pages.as_deref().unwrap_or("40");
    let pages = (!max_pages.is_empty()
        && !max_pages.starts_with('0')
        && max_pages.bytes().all(|byte| byte.is_ascii_digit()))
    .then(|| max_pages.parse::<u32>().ok())
    .flatten()
    .filter(|pages| (1..=100).contains(pages))
    .ok_or_else(|| CliError::usage("--max-pages must be an integer from 1 to 100"))?;
    let title = args
        .title
        .clone()
        .unwrap_or_else(|| "Daily environment report".to_owned());
    if crate::tenant::config::js_trim(&title).is_empty() || title.encode_utf16().count() > 240 {
        return Err(CliError::usage("--title must contain 1–240 characters"));
    }
    let destination = match &args.output_file {
        Some(path) if crate::tenant::config::js_trim(&path.to_string_lossy()).is_empty() => {
            return Err(CliError::usage("--output-file cannot be empty"));
        }
        Some(path) if path == "-" => ReportDestination::Stdout,
        Some(path) => ReportDestination::File(PathBuf::from(path)),
        None => ReportDestination::File(PathBuf::from(super::runtime_report::default_name(
            std::time::SystemTime::now(),
        ))),
    };
    Ok(RuntimeReportPlan {
        destination,
        title,
        max_pages: pages,
        strict: args.strict,
    })
}

fn or<'a>(value: Option<&'a String>, default: &'a str) -> &'a str {
    value.map_or(default, String::as_str)
}

/// A command's `--interval` and `--unit` (with their defaults) once the unit vocabulary
/// is checked: TypeScript checks the unit while parsing (`rd.ts:52-70`), so this is the
/// command's first check, and the only place the vocabulary is checked.
struct Window<'a> {
    interval: &'a str,
    unit: &'a str,
}

impl<'a> Window<'a> {
    fn new(
        interval: Option<&'a String>,
        unit: Option<&'a String>,
        defaults: (&'a str, &'a str),
        units: &[&str],
    ) -> Result<Self, CliError> {
        runtime::check_unit(unit.map(String::as_str), units)?;
        Ok(Self {
            interval: or(interval, defaults.0),
            unit: or(unit, defaults.1),
        })
    }

    /// TS `window` (`rd.ts:28-33`).
    fn parse(&self) -> Result<(u64, String), CliError> {
        let interval = runtime::dash_integer(self.interval, "--interval", 1)?;
        let unit = runtime::dash_text(self.unit, "--unit")?;
        Ok((interval, unit))
    }
}

/// The unit vocabulary, then TS `window`.
fn window(
    interval: Option<&String>,
    unit: Option<&String>,
    defaults: (&str, &str),
    units: &[&str],
) -> Result<(u64, String), CliError> {
    Window::new(interval, unit, defaults, units)?.parse()
}

fn page(page: &DashboardPageArgs) -> Result<(u64, u64), CliError> {
    Ok((
        runtime::dash_integer(or(page.limit.as_ref(), "25"), "--limit", 1)?,
        runtime::dash_integer(or(page.offset.as_ref(), "0"), "--offset", 0)?,
    ))
}

fn required(value: Option<&String>, flag: &str) -> Result<String, CliError> {
    runtime::dash_text(or(value, ""), flag)
}

/// TS `identity` (`rd.ts:35-42`): the unit vocabulary first (parse time), then the three
/// names, then the window.
fn identity(args: &SessionIdentityArgs) -> Result<SessionIdentity, CliError> {
    let window = Window::new(
        args.window.interval.as_ref(),
        args.window.unit.as_ref(),
        ("30", "days"),
        TIME_UNITS,
    )?;
    let session_id = required(args.session_id.as_ref(), "--session-id")?;
    let app_id = required(args.app_id.as_ref(), "--app-id")?;
    let app_name = required(args.app_name.as_ref(), "--app-name")?;
    let (interval, unit) = window.parse()?;
    Ok(SessionIdentity {
        session_id,
        app_id,
        app_name,
        interval,
        unit,
    })
}

fn scan(args: &ScanIdentityArgs) -> Result<(String, u64), CliError> {
    Ok((
        required(args.scan_id.as_ref(), "--scan-id")?,
        runtime::dash_integer(
            or(args.scan_sub_req_id.as_ref(), ""),
            "--scan-sub-req-id",
            0,
        )?,
    ))
}

fn time_read(
    read: TimeRead,
    args_interval: Option<&String>,
    args_unit: Option<&String>,
    defaults: (&str, &str),
    format: Option<Format>,
) -> Result<RxPlan, CliError> {
    let (interval, unit) = window(args_interval, args_unit, defaults, TIME_UNITS)?;
    Ok(RxPlan::Dashboard {
        read: DashRead::TimeRange {
            read,
            interval,
            unit,
        },
        format,
    })
}

fn day_window(args: &DashboardWindowArgs) -> (Option<&String>, Option<&String>) {
    (args.interval.as_ref(), args.unit.as_ref())
}

fn days_window(args: &DashboardWindowDaysArgs) -> (Option<&String>, Option<&String>) {
    (args.interval.as_ref(), args.unit.as_ref())
}

/// Plan an M6d command line; `--output` and `--tenant` were validated already.
pub fn plan(
    command: &RuntimeCommand,
    explicit: Option<&str>,
    debug: bool,
) -> Result<Plan, CliError> {
    let format = output::validate_explicit(explicit)?;
    let plan = match command {
        RuntimeCommand::Report(args) => RxPlan::Report(Box::new(plan_report(args, format, debug)?)),
        RuntimeCommand::Profiles(group) => match &group.command {
            Some(ProfilesCommand::Backup(args)) => {
                RxPlan::ProfilesBackup(Box::new(super::profile_backup::plan(args, format, debug)?))
            }
            Some(ProfilesCommand::Restore(args)) => RxPlan::ProfilesRestore(Box::new(
                super::profile_restore::plan(args, format, debug)?,
            )),
            _ => unreachable!("planned by commands::plan"),
        },
        RuntimeCommand::ApiKeys(group) => match &group.command {
            None => return Ok(Plan::GroupHelp(&["runtime", "api-keys"])),
            Some(ApiKeysCommand::List(args)) => RxPlan::KeysList {
                params: pagination::parse_list(&args.paging, false)?,
                format,
            },
            Some(ApiKeysCommand::Create(args)) => RxPlan::KeysCreate {
                config: args.config.clone(),
                key_file: keys::plan_key_file(&args.key_file)?,
                format,
            },
            Some(ApiKeysCommand::Regenerate(args)) => {
                RxPlan::KeysRegenerate(Box::new(plan_regenerate(args, format)?))
            }
            Some(ApiKeysCommand::Delete(args)) => {
                crate::segment::check_segment("API key name", &args.api_key_name)?;
                let updated_by = runtime::updated_by(&args.updated_by)?;
                super::runtime_write::gate(
                    args.force,
                    &format!("delete API key {}", args.api_key_name),
                )?;
                RxPlan::KeysDelete {
                    name: args.api_key_name.clone(),
                    updated_by,
                    force: args.force,
                }
            }
        },
        RuntimeCommand::CustomerApps(group) => match &group.command {
            None => return Ok(Plan::GroupHelp(&["runtime", "customer-apps"])),
            Some(CustomerAppsCommand::List(args)) => RxPlan::AppsList {
                params: pagination::parse_list(&args.paging, false)?,
                format,
            },
            Some(CustomerAppsCommand::Get(args)) => RxPlan::AppsGet {
                name: runtime::non_blank(
                    &args.app_name,
                    "Customer app name must not be empty",
                    "appName",
                )?,
                format,
            },
            Some(CustomerAppsCommand::Update(args)) => {
                let id =
                    runtime::non_blank(&args.app_id, "Customer app id must not be empty", "appId")?;
                super::runtime_write::gate(args.force, &format!("replace customer app {id}"))?;
                RxPlan::AppsUpdate(Box::new(UpdatePlan {
                    id,
                    config: args.config.clone(),
                    force: args.force,
                    format,
                }))
            }
            Some(CustomerAppsCommand::Delete(args)) => {
                let name = runtime::non_blank(
                    &args.app_name,
                    "Customer app name must not be empty",
                    "appName",
                )?;
                let updated_by = runtime::updated_by(&args.updated_by)?;
                super::runtime_write::gate(args.force, &format!("delete customer app {name}"))?;
                RxPlan::AppsDelete {
                    name,
                    updated_by,
                    force: args.force,
                }
            }
            Some(CustomerAppsCommand::Consumption(args)) => {
                let interval = runtime::time_interval(args.time_interval.as_deref())?;
                let app = args
                    .app_name
                    .as_deref()
                    .map(|name| {
                        runtime::non_blank(
                            name,
                            "appName must not be empty; omit it to report every dashboard application",
                            "appName",
                        )
                    })
                    .transpose()?;
                RxPlan::Consumption {
                    app,
                    interval,
                    format,
                }
            }
        },
        RuntimeCommand::DeploymentProfiles(group) => match &group.command {
            None => return Ok(Plan::GroupHelp(&["runtime", "deployment-profiles"])),
            Some(DeploymentProfilesCommand::List(args)) => RxPlan::DeploymentProfiles {
                unactivated: args.unactivated,
                format,
            },
        },
        RuntimeCommand::ScanLogs(group) => match &group.command {
            None => return Ok(Plan::GroupHelp(&["runtime", "scan-logs"])),
            // Every value is accepted and ignored (TS).
            Some(ScanLogsCommand::Query(_)) => RxPlan::ScanLogs,
        },
        RuntimeCommand::Dashboard(group) => match &group.command {
            None => return Ok(Plan::GroupHelp(&["runtime", "dashboard"])),
            Some(DashboardCommand::Applications(args)) => {
                // The vocabulary check leaves only TS's interval test (`rd.ts:166`).
                let (interval, unit) = window(
                    args.interval.as_ref(),
                    args.unit.as_ref(),
                    ("1", "day"),
                    APPLICATION_UNITS,
                )?;
                if ![1, 7, 30, 60].contains(&interval) {
                    return Err(CliError::usage(
                        "Application overview supports interval 1/7/30/60 and unit day/days/hour",
                    ));
                }
                let (limit, offset) = page(&args.page)?;
                RxPlan::Dashboard {
                    read: DashRead::Applications {
                        interval,
                        unit,
                        limit,
                        offset,
                    },
                    format,
                }
            }
            Some(
                DashboardCommand::Application(args) | DashboardCommand::ApplicationViolations(args),
            ) => {
                let violations = matches!(
                    group.command,
                    Some(DashboardCommand::ApplicationViolations(_))
                );
                // The vocabulary is `days` alone, so TS's unit test (`rd.ts:195`) cannot
                // fail after it; the unit is not sent (`time_unit=days`, §18.12).
                let window = Window::new(
                    args.interval.as_ref(),
                    args.unit.as_ref(),
                    ("30", "days"),
                    DETAIL_UNITS,
                )?;
                let interval = runtime::dash_integer(window.interval, "--interval", 1)?;
                if ![7, 30, 60].contains(&interval) {
                    return Err(CliError::usage(
                        "Application detail requires 7, 30, or 60 days",
                    ));
                }
                RxPlan::Dashboard {
                    read: DashRead::Application {
                        violations,
                        app_id: required(args.app_id.as_ref(), "--app-id")?,
                        app_name: required(args.app_name.as_ref(), "--app-name")?,
                        interval,
                    },
                    format,
                }
            }
            Some(DashboardCommand::TopApplications(args)) => {
                let (interval, unit) = day_window(args);
                time_read(
                    TimeRead::TopApplications,
                    interval,
                    unit,
                    ("1", "day"),
                    format,
                )?
            }
            Some(DashboardCommand::ViolationsTrend(args)) => {
                let (interval, unit) = day_window(args);
                time_read(
                    TimeRead::ViolationsTrend,
                    interval,
                    unit,
                    ("1", "day"),
                    format,
                )?
            }
            Some(DashboardCommand::AppsList(args)) => {
                let (interval, unit) = days_window(args);
                time_read(TimeRead::AppsList, interval, unit, ("30", "days"), format)?
            }
        },
        RuntimeCommand::Sessions(group) => match &group.command {
            None => return Ok(Plan::GroupHelp(&["runtime", "sessions"])),
            Some(SessionsCommand::Chart(args)) => {
                let (interval, unit) = day_window(args);
                time_read(
                    TimeRead::SessionsChart,
                    interval,
                    unit,
                    ("1", "day"),
                    format,
                )?
            }
            Some(SessionsCommand::List(args)) => {
                let (interval, unit) = window(
                    args.window.interval.as_ref(),
                    args.window.unit.as_ref(),
                    ("1", "day"),
                    TIME_UNITS,
                )?;
                let (limit, offset) = page(&args.page)?;
                let max = runtime::dash_integer(or(args.max.as_ref(), "10000"), "--max", 0)?;
                RxPlan::SessionsList {
                    list: SessionsList {
                        interval,
                        unit,
                        limit,
                        offset,
                        all: args.all,
                        max,
                    },
                    format,
                }
            }
            Some(SessionsCommand::Get(args)) => {
                let identity = identity(&args.identity)?;
                let (limit, offset) = page(&args.page)?;
                RxPlan::Dashboard {
                    read: DashRead::Session {
                        identity,
                        limit,
                        offset,
                    },
                    format,
                }
            }
            Some(SessionsCommand::Transaction(args)) => {
                let identity = identity(&args.identity)?;
                let (scan_id, sub_request) = scan(&args.scan)?;
                RxPlan::Dashboard {
                    read: DashRead::Transaction {
                        identity,
                        scan_id,
                        sub_request,
                    },
                    format,
                }
            }
            Some(SessionsCommand::ScanContent(args)) => {
                let (scan_id, sub_request) = scan(&args.scan)?;
                // TS: `Boolean(showContent) === Boolean(outputFile)`, so `--output-file ''`
                // counts as absent.
                let file = args.output_file.as_ref().filter(|path| !path.is_empty());
                if args.show_content == file.is_some() {
                    return Err(CliError::usage(
                        "Scan content requires exactly one of --show-content or --output-file <new-path>",
                    ));
                }
                if let Some(path) = file
                    && path
                        .to_str()
                        .is_some_and(|text| crate::tenant::config::js_trim(text).is_empty())
                {
                    return Err(CliError::usage("--output-file is required"));
                }
                RxPlan::ScanContent {
                    scan_id,
                    sub_request,
                    file: file.map(PathBuf::from),
                    format,
                }
            }
        },
        _ => unreachable!("planned by commands::plan"),
    };
    Ok(Plan::RuntimeExtras(Box::new(plan)))
}

/// Run an M6d plan.
pub async fn dispatch(invocation: &mut Invocation, plan: RxPlan) -> Result<(), CliError> {
    use super::{runtime_apps as apps, runtime_dashboard as dash, runtime_inventory as inv};
    match plan {
        RxPlan::KeysList { params, format } => {
            super::runtime_keys::list(invocation, &params, format).await
        }
        RxPlan::AppsList { params, format } => apps::list(invocation, &params, format).await,
        RxPlan::AppsGet { name, format } => apps::get(invocation, &name, format).await,
        RxPlan::Consumption {
            app,
            interval,
            format,
        } => apps::consumption(invocation, app.as_deref(), interval, format).await,
        RxPlan::DeploymentProfiles {
            unactivated,
            format,
        } => inv::deployment_profiles(invocation, unactivated, format).await,
        RxPlan::ScanLogs => inv::scan_logs(invocation),
        RxPlan::Dashboard { read, format } => dash::read(invocation, &read, format).await,
        RxPlan::SessionsList { list, format } => {
            dash::sessions_list(invocation, &list, format).await
        }
        RxPlan::ScanContent {
            scan_id,
            sub_request,
            file,
            format,
        } => dash::scan_content(invocation, &scan_id, sub_request, file.as_deref(), format).await,
        RxPlan::KeysCreate {
            config,
            key_file,
            format,
        } => super::runtime_keys::create(invocation, &config, &key_file, format).await,
        RxPlan::KeysRegenerate(plan) => super::runtime_keys::regenerate(invocation, &plan).await,
        RxPlan::KeysDelete {
            name,
            updated_by,
            force,
        } => super::runtime_keys::delete(invocation, &name, &updated_by, force).await,
        RxPlan::AppsUpdate(plan) => apps::update(invocation, &plan).await,
        RxPlan::AppsDelete {
            name,
            updated_by,
            force,
        } => apps::delete(invocation, &name, &updated_by, force).await,
        RxPlan::Report(plan) => super::runtime_report::run(invocation, &plan).await,
        RxPlan::ProfilesBackup(plan) => super::profile_backup::run(invocation, &plan).await,
        RxPlan::ProfilesRestore(plan) => super::profile_restore::run(invocation, &plan).await,
    }
}

/// The selected tenant of an M6d command once its management credentials are checked
/// (§3.5), before any client is built: what the transfer commands' tenant checks need
/// (§18.16 "Tenant checks ... before the client and any request").
pub struct RuntimeTenant {
    context: TenantContext,
    tsg: String,
}

impl RuntimeTenant {
    /// The tenant's store name.
    pub fn name(&self) -> &str {
        self.context.name()
    }

    /// The tenant's `mgmtTsgId`, trimmed (§3.7).
    pub fn tsg(&self) -> &str {
        &self.tsg
    }
}

/// A connected M6d client: the management clients (with the planes the command needs)
/// and the tenant's `defaultOutput`.
pub struct Connected {
    pub clients: ManagementClients,
    pub default_output: Option<String>,
}

impl Connected {
    /// The format of a read: the explicit `--output`, else the tenant's `defaultOutput`
    /// (choice 18), else `pretty`.
    pub fn format(&self, explicit: Option<Format>) -> Format {
        output::resolve_tenant_default(explicit, self.default_output.as_deref())
    }
}

impl Invocation {
    /// Load the tenant and check the management credentials (§3.5), without building a
    /// client or sending a request.
    pub fn runtime_tenant(&mut self) -> Result<RuntimeTenant, CliError> {
        let context = self.load_tenant()?;
        crate::tenant::config::require_management(&context.config, Some(context.name()))?;
        let tsg = crate::tenant::config::js_trim(
            context
                .config
                .effective_str("mgmtTsgId")
                .unwrap_or_default(),
        )
        .to_owned();
        Ok(RuntimeTenant { context, tsg })
    }

    /// Build the management client of `tenant` with `planes` (§18.3; each endpoint
    /// admitted by §5), without any request: the caller fetches the token.
    pub fn connect_tenant(
        &self,
        tenant: &RuntimeTenant,
        planes: Planes,
    ) -> Result<Connected, CliError> {
        let config = &tenant.context.config;
        let clients = ManagementClients::build_for(
            config,
            &self.session,
            &self.secrets,
            self.debug.as_ref(),
            planes,
        )?;
        Ok(Connected {
            clients,
            default_output: config.default_output().map(str::to_owned),
        })
    }

    /// [`Invocation::runtime_tenant`] then [`Invocation::connect_tenant`]: the commands
    /// with no check between the tenant and the client.
    pub fn connect_runtime(&mut self, planes: Planes) -> Result<Connected, CliError> {
        let tenant = self.runtime_tenant()?;
        self.connect_tenant(&tenant, planes)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::Cli;
    use clap::FromArgMatches;

    fn plan_of(args: &[&str]) -> Result<Plan, CliError> {
        let mut full = vec!["airs-cli-rs", "runtime"];
        full.extend_from_slice(args);
        let matches = crate::cli::command().try_get_matches_from(&full).unwrap();
        crate::commands::plan(&Cli::from_arg_matches(&matches).unwrap())
    }

    fn rx(args: &[&str]) -> RxPlan {
        match plan_of(args) {
            Ok(Plan::RuntimeExtras(plan)) => *plan,
            Ok(_) => panic!("not an M6d plan: {args:?}"),
            Err(error) => panic!("{args:?}: {error:?}"),
        }
    }

    fn usage(args: &[&str]) -> String {
        match plan_of(args) {
            Err(CliError::Usage(message)) => message,
            Err(other) => panic!("expected a usage error, got {other:?}"),
            Ok(_) => panic!("expected a usage error: {args:?}"),
        }
    }

    /// §18.3 "Validation order": `--output`, then `--tenant`, then the arguments (D220).
    #[test]
    fn output_and_tenant_flag_come_before_the_arguments() {
        let parse = |args: &[&str]| {
            let matches = crate::cli::command().try_get_matches_from(args).unwrap();
            crate::commands::plan(&Cli::from_arg_matches(&matches).unwrap())
        };
        for dead in ["yaml", "markdown", "csv"] {
            assert!(matches!(
                parse(&["airs-cli-rs", "--tenant", "-bad", "runtime", "sessions", "chart", "--unit", "week", "--output", dead]),
                Err(CliError::Usage(message)) if message == format!("Invalid output format '{dead}'. Expected: pretty, table, json")
            ));
        }
        assert!(matches!(
            parse(&["airs-cli-rs", "--tenant", "-bad", "runtime", "api-keys", "list", "--limit", "0"]),
            Err(CliError::Usage(message)) if message.starts_with("Tenant name must be")
        ));
    }

    /// Every §18.3 check, with the TS texts (TR§3.1, TX§3.1, D220).
    #[test]
    fn argument_checks() {
        for (args, message) in [
            (
                &["api-keys", "list", "--limit", "0"][..],
                "--limit must be a positive integer",
            ),
            (
                &["customer-apps", "ls", "--all", "--offset", "0"],
                "--all cannot be combined with --offset",
            ),
            (
                &["customer-apps", "get", " "],
                "Customer app name must not be empty",
            ),
            (
                &["customer-apps", "get", "a\u{1b}"],
                "appName must be at most 4096 bytes and contain no control characters",
            ),
            (
                &["customer-apps", "consumption", ""],
                "appName must not be empty; omit it to report every dashboard application",
            ),
            (
                &["customer-apps", "consumption", "--time-interval", "7.5"],
                "--time-interval must be 7, 30, or 60 (the API rejects other values)",
            ),
            (
                &["sessions", "list", "--unit", "week"],
                "Unsupported --unit. Supported units: hour, hours, day, days. For one week, use --interval 7 --unit days.",
            ),
            (
                &["sessions", "list", "--limit", "0"],
                "--limit must be a safe integer >= 1",
            ),
            (
                &["sessions", "list", "--offset", "-1"],
                "--offset must be a safe integer >= 0",
            ),
            (
                &["sessions", "list", "--max", "NaN"],
                "--max must be a safe integer >= 0",
            ),
            (
                &["sessions", "chart", "--interval", "1oops"],
                "--interval must be a safe integer >= 1",
            ),
            (
                &["dashboard", "applications", "--unit", "hours"],
                "Unsupported --unit. Supported units: day, days, hour. For one week, use --interval 7 --unit days.",
            ),
            (
                &["dashboard", "applications", "--interval", "2"],
                "Application overview supports interval 1/7/30/60 and unit day/days/hour",
            ),
            (
                &[
                    "dashboard",
                    "application",
                    "--app-id",
                    "a",
                    "--app-name",
                    "n",
                    "--interval",
                    "1",
                ],
                "Application detail requires 7, 30, or 60 days",
            ),
            (
                &[
                    "dashboard",
                    "application",
                    "--app-id",
                    "a",
                    "--app-name",
                    "n",
                    "--unit",
                    "day",
                ],
                "Unsupported --unit. Supported units: days. For one week, use --interval 7 --unit days.",
            ),
            (
                &[
                    "dashboard",
                    "application-violations",
                    "--app-id",
                    " ",
                    "--app-name",
                    "n",
                ],
                "--app-id is required",
            ),
            (
                &[
                    "sessions",
                    "get",
                    "--session-id",
                    "s",
                    "--app-id",
                    "a",
                    "--app-name",
                    "\t",
                ],
                "--app-name is required",
            ),
            // TS order: the unit vocabulary (parse time), the names, then the window.
            (
                &[
                    "sessions",
                    "get",
                    "--session-id",
                    " ",
                    "--app-id",
                    "a",
                    "--app-name",
                    "n",
                    "--unit",
                    "week",
                ],
                "Unsupported --unit. Supported units: hour, hours, day, days. For one week, use --interval 7 --unit days.",
            ),
            (
                &[
                    "sessions",
                    "get",
                    "--session-id",
                    " ",
                    "--app-id",
                    "a",
                    "--app-name",
                    "n",
                    "--interval",
                    "0",
                ],
                "--session-id is required",
            ),
            (
                &[
                    "sessions",
                    "transaction",
                    "--session-id",
                    "s",
                    "--app-id",
                    "a",
                    "--app-name",
                    "n",
                    "--scan-id",
                    "x",
                    "--scan-sub-req-id",
                    "-1",
                ],
                "--scan-sub-req-id must be a safe integer >= 0",
            ),
            (
                &[
                    "sessions",
                    "scan-content",
                    "--scan-id",
                    "x",
                    "--scan-sub-req-id",
                    "0",
                ],
                "Scan content requires exactly one of --show-content or --output-file <new-path>",
            ),
            (
                &[
                    "sessions",
                    "scan-content",
                    "--scan-id",
                    "x",
                    "--scan-sub-req-id",
                    "0",
                    "--show-content",
                    "--output-file",
                    "f",
                ],
                "Scan content requires exactly one of --show-content or --output-file <new-path>",
            ),
            (
                &[
                    "sessions",
                    "scan-content",
                    "--scan-id",
                    "x",
                    "--scan-sub-req-id",
                    "0",
                    "--output-file",
                    " ",
                ],
                "--output-file is required",
            ),
        ] {
            assert_eq!(usage(args), message, "{args:?}");
        }
    }

    /// Step 2 (§18.5, §18.6, §18.8, §18.14): the write plans change nothing when
    /// interrupted before a write; the report refuses `--debug` and names its context.
    #[test]
    fn write_and_report_plans() {
        let plan = rx(&[
            "api-keys",
            "regenerate",
            "a/b",
            "--interval",
            "030",
            "--unit",
            "days",
            "--key-file",
            "k.key",
            "--force",
        ]);
        let RxPlan::KeysRegenerate(regenerate) = &plan else {
            panic!("{plan:?}");
        };
        assert_eq!(regenerate.id, "a/b");
        assert_eq!(
            regenerate.body,
            serde_json::json!({"rotation_time_interval": 30, "rotation_time_unit": "days"})
        );
        assert_eq!(plan.interrupt_context(), InterruptContext::NothingChanged);
        assert_eq!(plan.debug_policy(), crate::debug::DebugPolicy::Log);
        let report = rx(&["report", "--max-pages", "5", "--output-file", "-"]);
        assert_eq!(report.debug_policy(), crate::debug::DebugPolicy::Refused);
        assert_eq!(
            report.interrupt_context(),
            InterruptContext::Context("no report was written".into())
        );
        for (args, message) in [
            (
                &[
                    "api-keys",
                    "create",
                    "--config",
                    "c.json",
                    "--key-file",
                    "-",
                ][..],
                "--key-file - would print the key; give a file path",
            ),
            (
                &[
                    "api-keys",
                    "delete",
                    "%2e%2e",
                    "--updated-by",
                    "u",
                    "--force",
                ],
                "Invalid API key name: %2e%2e",
            ),
            (
                &[
                    "customer-apps",
                    "update",
                    "\t",
                    "--config",
                    "c.json",
                    "--force",
                ],
                "Customer app id must not be empty",
            ),
            (
                &["report", "--max-pages", "100", "--title", " "],
                "--title must contain 1–240 characters",
            ),
        ] {
            assert_eq!(usage(args), message, "{args:?}");
        }
    }

    #[test]
    fn plans_and_defaults() {
        assert_eq!(
            rx(&["dashboard", "applications"]),
            RxPlan::Dashboard {
                read: DashRead::Applications {
                    interval: 1,
                    unit: "day".into(),
                    limit: 25,
                    offset: 0
                },
                format: None
            }
        );
        assert_eq!(
            rx(&[
                "dashboard",
                "apps-list",
                "--interval",
                "01",
                "--output",
                "table"
            ]),
            RxPlan::Dashboard {
                read: DashRead::TimeRange {
                    read: TimeRead::AppsList,
                    interval: 1,
                    unit: "days".into()
                },
                format: Some(Format::Table)
            }
        );
        assert_eq!(
            rx(&["customer-apps", "consumption"]),
            RxPlan::Consumption {
                app: None,
                interval: 30,
                format: None
            }
        );
        let plan = rx(&[
            "scan-logs",
            "query",
            "--interval",
            "x",
            "--unit",
            "y",
            "--page",
            "4",
            "--page-size",
            "9",
            "--limit",
            "-1",
        ]);
        assert_eq!(plan, RxPlan::ScanLogs);
        assert_eq!(plan.debug_policy(), crate::debug::DebugPolicy::None);
        let list = rx(&["sessions", "ls", "--all", "--offset", "2"]);
        assert_eq!(list.debug_policy(), crate::debug::DebugPolicy::Log);
        assert_eq!(list.interrupt_context(), InterruptContext::NoDataWritten);
        assert!(matches!(
            plan_of(&["sessions"]),
            Ok(Plan::GroupHelp(["runtime", "sessions"]))
        ));
    }
}