airs-cli 0.8.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
//! Every model-security list and get leaf (DESIGN §17.4–§17.7).
//!
//! A list is one of three shapes:
//! - a single page, with `skip` and `limit` as TypeScript sends them (§17.3) and a status
//!   line only when the page does not show everything (choice 6);
//! - an `--all` walk from `skip=0` (the shared page loop, `src/walk.rs`, D183), with the
//!   scans `count_capped` latch (D182) and the D14 line when `--max` stopped it;
//! - `scans list --scan-origin`: every page of 100 walked with the other filters, the rows
//!   of that origin kept, then sliced (D181, G18).
//!
//! Every SDK call validates its answer without the frozen response defaults (G20, D180).

use super::Invocation;
use super::modelsec::{Connected, MsGet, MsList};
use super::redteam::page_value;
use crate::clients::{SINGLE_REQUEST_BUDGET, WALK_BUDGET};
use crate::error::CliError;
use crate::json::OValue;
use crate::modelsec::views::{self, Kind, List};
use crate::modelsec::{self, ORIGIN_PAGE_SIZE};
use crate::output::Format;
use crate::walk::{self, ListParams, Page};
use scm::airs::model_security::{
    ModelSecurityClient, ModelSecurityEvaluationListOptions, ModelSecurityFileListOptions,
    ModelSecurityGroupListOptions, ModelSecurityLabelListOptions, ModelSecurityModelListOptions,
    ModelSecurityModelVersionListOptions, ModelSecurityRuleInstanceListOptions,
    ModelSecurityRuleListOptions, ModelSecurityScanListOptions,
};

/// One page of `list` as ordered JSON: its rows (under the list's own key), the
/// `pagination.total_items` count and the scans `count_capped` flag. `walk`: the page of a
/// walk (scans send `--search` as `search_query` there, choice 4).
async fn fetch(
    ms: &ModelSecurityClient,
    list: &MsList,
    skip: Option<u64>,
    limit: Option<u32>,
    walk: bool,
    options: &scm::RequestOptions,
) -> Result<Page<OValue>, scm::Error> {
    let options = Some(options);
    let (answer, key) = match list {
        MsList::Groups {
            source_types,
            search,
            sort_field,
            sort_dir,
            enabled_rules,
        } => {
            let mut query = ModelSecurityGroupListOptions::default();
            query.skip = skip;
            query.limit = limit;
            query.sort_field = sort_field.clone();
            query.sort_dir = sort_dir.clone();
            query.source_types = source_types.clone();
            query.search_query = search.clone();
            query.enabled_rules = enabled_rules.clone();
            (
                page_value(&ms.security_groups().list(&query, options).await?)?,
                "security_groups",
            )
        }
        MsList::Rules {
            source_type,
            search,
        } => {
            let mut query = ModelSecurityRuleListOptions::default();
            query.skip = skip;
            query.limit = limit;
            query.source_type = source_type.clone();
            query.search_query = search.clone();
            (
                page_value(&ms.security_rules().list(&query, options).await?)?,
                "rules",
            )
        }
        MsList::RuleInstances {
            group,
            security_rule_uuid,
            state,
        } => {
            let mut query = ModelSecurityRuleInstanceListOptions::default();
            query.skip = skip;
            query.limit = limit;
            query.security_rule_uuid = security_rule_uuid.clone();
            query.state = state.clone();
            (
                page_value(
                    &ms.security_groups()
                        .list_rule_instances(group, &query, options)
                        .await?,
                )?,
                "rule_instances",
            )
        }
        MsList::Scans {
            window,
            eval_outcome,
            source_type,
            search,
            scan_origin: _,
        } => {
            let mut query = ModelSecurityScanListOptions::default();
            query.skip = skip;
            query.limit = limit;
            if walk {
                query.search_query = search.clone();
            } else {
                query.search = search.clone();
            }
            query.model_version_uuid = window.model_version.clone();
            query.eval_outcomes = eval_outcome.clone().map(|value| vec![value]);
            query.source_types = source_type.clone().map(|value| vec![value]);
            query.start_time = window.start.clone();
            query.end_time = window.end.clone();
            (
                page_value(&ms.scans().list(&query, options).await?)?,
                "scans",
            )
        }
        MsList::Evaluations { scan } => {
            let mut query = ModelSecurityEvaluationListOptions::default();
            query.skip = skip;
            query.limit = limit;
            (
                page_value(&ms.scans().get_evaluations(scan, &query, options).await?)?,
                "evaluations",
            )
        }
        MsList::Violations { scan } => {
            let mut query = ModelSecurityLabelListOptions::default();
            query.skip = skip;
            query.limit = limit;
            (
                page_value(&ms.scans().get_violations(scan, &query, options).await?)?,
                "violations",
            )
        }
        MsList::Files {
            scan,
            file_type,
            result,
        } => {
            let mut query = ModelSecurityFileListOptions::default();
            query.skip = skip;
            query.limit = limit;
            query.type_ = file_type.clone();
            query.result = result.clone();
            (
                page_value(&ms.scans().get_files(scan, &query, options).await?)?,
                "files",
            )
        }
        MsList::Models {
            window,
            search,
            search_query,
            sort_field,
            sort_order,
        } => {
            let mut query = ModelSecurityModelListOptions::default();
            query.skip = skip;
            query.limit = limit;
            query.search = search.clone();
            query.search_query = search_query.clone();
            query.sort_field = sort_field.clone();
            query.sort_order = sort_order.clone();
            query.start_time = window.start.clone();
            query.end_time = window.end.clone();
            (
                page_value(&ms.models().list_models(&query, options).await?)?,
                "models",
            )
        }
        MsList::Versions { model, sort_order } => {
            let mut query = ModelSecurityModelVersionListOptions::default();
            query.skip = skip;
            query.limit = limit;
            query.sort_order = sort_order.clone();
            (
                page_value(
                    &ms.models()
                        .list_model_versions(model, &query, options)
                        .await?,
                )?,
                "model_versions",
            )
        }
        MsList::ModelFiles { version } => {
            let mut query = ModelSecurityLabelListOptions::default();
            query.skip = skip;
            query.limit = limit;
            (
                page_value(
                    &ms.models()
                        .list_model_version_files(version, &query, options)
                        .await?,
                )?,
                "files",
            )
        }
        MsList::LabelKeys => {
            let mut query = ModelSecurityLabelListOptions::default();
            query.skip = skip;
            query.limit = limit;
            (
                page_value(&ms.scans().get_label_keys(&query, options).await?)?,
                "keys",
            )
        }
        MsList::LabelValues { key } => {
            let mut query = ModelSecurityLabelListOptions::default();
            query.skip = skip;
            query.limit = limit;
            (
                page_value(&ms.scans().get_label_values(key, &query, options).await?)?,
                "values",
            )
        }
    };
    let rows = answer
        .get(key)
        .and_then(OValue::as_array)
        .unwrap_or_default()
        .to_vec();
    let total = answer
        .get("pagination")
        .and_then(|pagination| crate::redteam::views::number(pagination, "total_items"));
    let mut page = Page::new(rows, walk::total_of(total));
    page.capped = matches!(answer.get("count_capped"), Some(OValue::Bool(true)));
    Ok(page)
}

/// The identity of a walked row (D37): a record's `uuid`, a label key or value itself.
fn row_key(row: &OValue) -> Option<String> {
    match row {
        OValue::String(text) => Some(text.clone()),
        other => other
            .get("uuid")
            .and_then(OValue::as_str)
            .filter(|uuid| !uuid.is_empty())
            .map(str::to_owned),
    }
}

/// Walk every page of `size` rows from `skip=0`, keeping at most `max` (`0`: the safety
/// bound only).
async fn walk_rows(
    invocation: &Invocation,
    ms: &ModelSecurityClient,
    list: &MsList,
    size: u32,
    max: u64,
    options: &scm::RequestOptions,
) -> Result<walk::Walked<OValue>, CliError> {
    walk::walk(
        size,
        max,
        |skip, limit| fetch(ms, list, Some(skip), Some(limit), true, options),
        |row| crate::json::to_compact(row).map_or(0, |text| text.len()),
        row_key,
    )
    .await
    .map_err(|error| invocation.management_error(&error, WALK_BUDGET))
}

/// The table/json/pretty layout of a list's rows.
fn layout(list: &MsList) -> Option<List> {
    Some(match list {
        MsList::Groups { .. } => List::Groups,
        MsList::Rules { .. } => List::Rules,
        MsList::RuleInstances { .. } => List::RuleInstances,
        MsList::Scans { .. } => List::Scans,
        MsList::Evaluations { .. } => List::Evaluations,
        MsList::Violations { .. } => List::Violations,
        MsList::Files { .. } | MsList::ModelFiles { .. } => List::Files,
        MsList::Models { .. } => List::Models,
        MsList::Versions { .. } => List::Versions,
        MsList::LabelKeys | MsList::LabelValues { .. } => return None,
    })
}

/// A list leaf (§17.3–§17.7).
pub async fn list(
    invocation: &mut Invocation,
    list: &MsList,
    params: &ListParams,
    format: Option<Format>,
) -> Result<(), CliError> {
    let origin = match list {
        MsList::Scans { scan_origin, .. } => scan_origin.clone(),
        _ => None,
    };
    let walking = params.all || origin.is_some();
    let options = modelsec::options(if walking {
        WALK_BUDGET
    } else {
        SINGLE_REQUEST_BUDGET
    })?;
    let connected: Connected = invocation
        .connect_model_security(walking.then_some(&options))
        .await?;
    let format = connected.format(format, list.tenant_default());
    let ms = connected.client;
    let paging = list.paging();
    let (rows, status) = if let Some(origin) = origin {
        // D181: the exact local filter over a complete walk, then the slice.
        let walked = walk_rows(invocation, &ms, list, ORIGIN_PAGE_SIZE, 0, &options).await?;
        let matching: Vec<OValue> = walked
            .rows
            .into_iter()
            .filter(|row| row.get("scan_origin").and_then(OValue::as_str) == Some(origin.as_str()))
            .collect();
        let total = matching.len();
        let (rows, cut) = walk::slice(
            matching,
            &ListParams {
                limit: Some(params.limit.unwrap_or(modelsec::DEFAULT_LIMIT)),
                ..params.clone()
            },
        );
        let status = if params.all {
            cut.then(|| crate::pagination::showing_first(rows.len(), params.max))
        } else {
            Some(modelsec::origin_status(
                rows.len(),
                total,
                params.offset.unwrap_or(0),
                params.limit.unwrap_or(modelsec::DEFAULT_LIMIT),
            ))
        };
        (rows, status)
    } else if params.all {
        let walked = walk_rows(
            invocation,
            &ms,
            list,
            paging.walk_size(params),
            params.max,
            &options,
        )
        .await?;
        let status = walked
            .stopped_at_max
            .then(|| crate::pagination::showing_first(walked.rows.len(), params.max));
        (walked.rows, status)
    } else {
        let (skip, limit) = paging.page(params);
        let page = fetch(&ms, list, skip, limit, false, &options)
            .await
            .map_err(|error| invocation.management_error(&error, SINGLE_REQUEST_BUDGET))?;
        // D182: a capped total is a cap, not a count, so a capped page is judged as one
        // without a total (a full page says more may be available).
        let total = if page.capped { None } else { page.total };
        let status = modelsec::page_status(page.rows.len(), skip.unwrap_or(0), limit, total);
        (page.rows, status)
    };
    match layout(list) {
        Some(layout) => {
            if layout == List::Scans {
                views::register_uris(&rows, &invocation.secrets);
            }
            let mut docs = views::project_all(layout.kind(), &rows);
            for doc in &mut docs {
                views::withhold(doc, &invocation.secrets);
            }
            views::render_list(&mut invocation.output, layout, format, &docs)?;
        }
        None => {
            let mut items: Vec<String> = rows
                .iter()
                .map(|row| row.as_str().unwrap_or_default().to_owned())
                .collect();
            views::withhold_strings(&mut items, &invocation.secrets);
            let key = match list {
                MsList::LabelValues { key } => Some(key.as_str()),
                _ => None,
            };
            views::render_labels(&mut invocation.output, key, format, &items)?;
        }
    }
    if let Some(status) = status {
        invocation.output.status(&status);
    }
    Ok(())
}

/// A get leaf (§17.4–§17.6).
pub async fn get(
    invocation: &mut Invocation,
    get: &MsGet,
    format: Option<Format>,
) -> Result<(), CliError> {
    let options = modelsec::options(SINGLE_REQUEST_BUDGET)?;
    let connected = invocation.connect_model_security(None).await?;
    let format = connected.format(format, get.tenant_default());
    let ms = connected.client;
    let read = async {
        let options = Some(&options);
        Ok::<_, scm::Error>(match get {
            MsGet::Group(id) => (
                Kind::Group,
                page_value(&ms.security_groups().get(id, options).await?)?,
            ),
            MsGet::Rule(id) => (
                Kind::Rule,
                page_value(&ms.security_rules().get(id, options).await?)?,
            ),
            MsGet::RuleInstance { group, instance } => (
                Kind::RuleInstance,
                page_value(
                    &ms.security_groups()
                        .get_rule_instance(group, instance, options)
                        .await?,
                )?,
            ),
            MsGet::Scan(id) => (Kind::Scan, page_value(&ms.scans().get(id, options).await?)?),
            MsGet::Evaluation(id) => (
                Kind::Evaluation,
                page_value(&ms.scans().get_evaluation(id, options).await?)?,
            ),
            MsGet::Violation(id) => (
                Kind::Violation,
                page_value(&ms.scans().get_violation(id, options).await?)?,
            ),
            MsGet::Model(id) => (
                Kind::Model,
                page_value(&ms.models().get_model(id, options).await?)?,
            ),
            MsGet::Version(id) => (
                Kind::ModelVersion,
                page_value(&ms.models().get_model_version(id, options).await?)?,
            ),
        })
    };
    let (kind, raw) = read
        .await
        .map_err(|error| invocation.management_error(&error, SINGLE_REQUEST_BUDGET))?;
    if kind == Kind::Scan {
        views::register_uris(std::slice::from_ref(&raw), &invocation.secrets);
    }
    let mut doc = views::project(kind, &raw);
    views::withhold(&mut doc, &invocation.secrets);
    views::render_get(&mut invocation.output, kind, format, &doc)
}