scm-sdk 0.2.3

Native async Strata Cloud Manager SDK, beginning with Prisma AIRS
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
//! Typed target configuration, profiling and Copilot authorization operations.
use super::{common, models::*};
use crate::{
    Client, RawRequest, RequestOptions, Result,
    data::{JsonObject, Optional},
    pagination::{CollectOptions, CollectionResult, Items, Pages},
};
use http::Method;
const PATH: &str = "/v1/target";
// The inline getTargetMetadata z.record(z.unknown()) has this exact frozen shape:
// TargetCreateRequestSchema.extra_info -> Optional s012085 -> Nullable s012086
// -> Record s012087 (unconstrained string key s012088, unknown value s012089).
const METADATA_SCHEMA: &str = "s012087";

/// Target query options. Invalid values return [`crate::ErrorKind::Validation`]
/// before authentication or dispatch, without echoing inputs or carrying a
/// dispatched operation name.
#[derive(Clone, Default)]
#[non_exhaustive]
pub struct TargetListOptions {
    pub skip: Option<u64>,
    pub limit: Option<u32>,
    pub search: Option<String>,
    pub adapter_uuid: Option<String>,
    pub target_type: Option<String>,
    /// Legacy status filter, transmitted unchanged for compatibility.
    ///
    /// Management API 0.86.0 declares only `DRAFT` and `ACTIVE` here.
    /// `FAILED`, `QUEUED`, `IN_PROGRESS`, `COMPLETED`, and `PARTIALLY_COMPLETE`
    /// are no longer declared for this query. Use [`Self::profiling_status`]
    /// for `FAILED`, `IN_PROGRESS`, or `COMPLETED`; no equivalent is declared
    /// for `QUEUED` or `PARTIALLY_COMPLETE`. The service may reject legacy
    /// values. This SDK does not rewrite them automatically.
    pub status: Option<String>,
    /// Profiling filter: `NOT_STARTED`, `IN_PROGRESS`, `COMPLETED`, or `FAILED`.
    ///
    /// The value is preserved on every page of a paginated collection.
    /// Unsupported values fail validation before authentication or dispatch.
    /// Responses may contain `INIT`, `QUEUED`, or `PARTIALLY_COMPLETE`, but the vendor
    /// declares no filter for these values. These response values are not mapped
    /// to `NOT_STARTED` or any other filter automatically. Leaving this option
    /// unset omits the query parameter.
    ///
    /// The accepted values come from the pinned
    /// [management 0.86.0 ProfilingStatusFilter schema](https://github.com/PaloAltoNetworks/pan.dev/blob/582e30543bf2adfd8b0ee457170d22b5c380dfbe/openapi-specs/prisma-airs-redteam/management/mp-openapi.yaml).
    pub profiling_status: Option<String>,
}
crate::redaction::redacted_debug!(TargetListOptions);
#[derive(Clone, Default)]
#[non_exhaustive]
pub struct TargetOperationOptions {
    /// Omitted by default. Explicit false and true are transmitted.
    pub validate: Option<bool>,
}
crate::redaction::redacted_debug!(TargetOperationOptions);
#[derive(Clone, Debug)]
pub struct RedTeamTargetsClient {
    pub(super) client: Client,
}
impl RedTeamTargetsClient {
    pub fn new(client: Client) -> Result<Self> {
        client.require_auth(crate::engine::AuthKind::OAuth)?;
        Ok(Self {
            client: client.with_tsg_header(false)?,
        })
    }
    pub fn client(&self) -> &Client {
        &self.client
    }
    fn query(params: &TargetListOptions) -> Result<Vec<(String, String)>> {
        let mut query = common::listing(params.skip, params.limit, params.search.as_deref())?;
        if let Some(value) = &params.adapter_uuid {
            common::uuid(value)?;
            query.push(("adapter_uuid".into(), value.clone()));
        }
        if let Some(value) = &params.target_type {
            common::filter(value)?;
            query.push(("target_type".into(), value.clone()));
        }
        if let Some(value) = &params.status {
            common::filter(value)?;
            query.push(("status".into(), value.clone()));
        }
        if let Some(value) = &params.profiling_status {
            if !crate::models::red_team_management::TARGET_PROFILING_FILTER_VALUES
                .contains(&value.as_str())
            {
                return Err(crate::Error::new(crate::ErrorKind::Validation));
            }
            query.push(("profiling_status".into(), value.clone()));
        }
        Ok(query)
    }
    fn operation(operation: Option<&TargetOperationOptions>) -> Vec<(String, String)> {
        operation
            .and_then(|o| o.validate)
            .map(|v| vec![("validate".into(), v.to_string())])
            .unwrap_or_default()
    }
    pub async fn start_profiling(
        &self,
        uuid: &str,
        options: Option<&RequestOptions>,
    ) -> Result<StartProfilingResponse> {
        common::uuid(uuid)?;
        self.client
            .send_contract(
                RawRequest::new(Method::POST, PATH)
                    .segment(uuid)?
                    .segment("profile")?
                    .named("red_team.targets.start_profiling"),
                "StartProfilingResponseSchema",
                options,
            )
            .await
    }
    pub async fn get_copilot_auth_url(
        &self,
        body: &MSCopilotStudioAuthUrlRequest,
        options: Option<&RequestOptions>,
    ) -> Result<MSCopilotStudioAuthUrlResponse> {
        self.client
            .send_model(
                RawRequest::new(Method::POST, "/v1/target/ms-copilot-studio/auth-url")
                    .named("red_team.targets.get_copilot_auth_url"),
                body,
                "MSCopilotStudioAuthUrlRequestSchema",
                "MSCopilotStudioAuthUrlResponseSchema",
                options,
            )
            .await
    }
    pub async fn exchange_copilot_token(
        &self,
        body: &MSCopilotStudioTokenRequest,
        options: Option<&RequestOptions>,
    ) -> Result<MSCopilotStudioTokenResponse> {
        self.client
            .send_model(
                RawRequest::new(Method::POST, "/v1/target/ms-copilot-studio/token")
                    .named("red_team.targets.exchange_copilot_token"),
                body,
                "MSCopilotStudioTokenRequestSchema",
                "MSCopilotStudioTokenResponseSchema",
                options,
            )
            .await
    }
    pub async fn delete_copilot_token(
        &self,
        uuid: &str,
        options: Option<&RequestOptions>,
    ) -> Result<()> {
        common::uuid(uuid)?;
        self.client
            .send_empty(
                RawRequest::new(Method::DELETE, "/v1/target/ms-copilot-studio/token")
                    .segment(uuid)?
                    .named("red_team.targets.delete_copilot_token"),
                options,
            )
            .await
    }
    pub async fn create(
        &self,
        body: &TargetCreateRequest,
        operation: Option<&TargetOperationOptions>,
        options: Option<&RequestOptions>,
    ) -> Result<TargetResponse> {
        self.client
            .send_model(
                RawRequest::new(Method::POST, PATH)
                    .query(Self::operation(operation))
                    .named("red_team.targets.create"),
                body,
                "TargetCreateRequestSchema",
                "TargetResponseSchema",
                options,
            )
            .await
    }
    pub async fn list(
        &self,
        params: &TargetListOptions,
        options: Option<&RequestOptions>,
    ) -> Result<TargetList> {
        self.client
            .send_contract(
                RawRequest::read(PATH)
                    .query(Self::query(params)?)
                    .named("red_team.targets.list"),
                "TargetListSchema",
                options,
            )
            .await
    }
    pub async fn get(
        &self,
        uuid: &str,
        options: Option<&RequestOptions>,
    ) -> Result<TargetResponse> {
        common::uuid(uuid)?;
        self.client
            .send_contract(
                RawRequest::read(PATH)
                    .segment(uuid)?
                    .named("red_team.targets.get"),
                "TargetResponseSchema",
                options,
            )
            .await
    }
    pub async fn update(
        &self,
        uuid: &str,
        body: &TargetUpdateRequest,
        operation: Option<&TargetOperationOptions>,
        options: Option<&RequestOptions>,
    ) -> Result<TargetResponse> {
        common::uuid(uuid)?;
        self.client
            .send_model(
                RawRequest::new(Method::PUT, PATH)
                    .segment(uuid)?
                    .query(Self::operation(operation))
                    .named("red_team.targets.update"),
                body,
                "TargetUpdateRequestSchema",
                "TargetResponseSchema",
                options,
            )
            .await
    }
    pub async fn delete(
        &self,
        uuid: &str,
        options: Option<&RequestOptions>,
    ) -> Result<Option<BaseResponse>> {
        common::uuid(uuid)?;
        self.client
            .send_optional_contract(
                RawRequest::new(Method::DELETE, PATH)
                    .segment(uuid)?
                    .named("red_team.targets.delete"),
                "BaseResponseSchema",
                options,
            )
            .await
    }
    pub async fn probe(
        &self,
        body: &TargetProbeRequest,
        options: Option<&RequestOptions>,
    ) -> Result<TargetResponse> {
        self.client
            .send_model(
                RawRequest::new(Method::POST, "/v1/target/probe").named("red_team.targets.probe"),
                body,
                "TargetProbeRequestSchema",
                "TargetResponseSchema",
                options,
            )
            .await
    }
    pub async fn get_profile(
        &self,
        uuid: &str,
        options: Option<&RequestOptions>,
    ) -> Result<TargetProfileResponse> {
        common::uuid(uuid)?;
        self.client
            .send_contract(
                RawRequest::read(PATH)
                    .segment(uuid)?
                    .segment("profile")?
                    .named("red_team.targets.get_profile"),
                "TargetProfileResponseSchema",
                options,
            )
            .await
    }
    pub async fn update_profile(
        &self,
        uuid: &str,
        body: &TargetContextUpdate,
        options: Option<&RequestOptions>,
    ) -> Result<TargetResponse> {
        common::uuid(uuid)?;
        self.client
            .send_model(
                RawRequest::new(Method::PUT, PATH)
                    .segment(uuid)?
                    .segment("profile")?
                    .named("red_team.targets.update_profile"),
                body,
                "TargetContextUpdateSchema",
                "TargetResponseSchema",
                options,
            )
            .await
    }
    pub async fn validate_auth(
        &self,
        body: &TargetAuthValidationRequest,
        options: Option<&RequestOptions>,
    ) -> Result<TargetAuthValidationResponse> {
        self.client
            .send_model(
                RawRequest::new(Method::POST, "/v1/target/validate-auth")
                    .named("red_team.targets.validate_auth"),
                body,
                "TargetAuthValidationRequestSchema",
                "TargetAuthValidationResponseSchema",
                options,
            )
            .await
    }
    pub async fn get_target_metadata(
        &self,
        options: Option<&RequestOptions>,
    ) -> Result<JsonObject> {
        self.client
            .send_contract(
                RawRequest::read("/v1/template/target-metadata")
                    .named("red_team.targets.get_target_metadata"),
                METADATA_SCHEMA,
                options,
            )
            .await
    }
    pub async fn get_target_templates(
        &self,
        options: Option<&RequestOptions>,
    ) -> Result<TargetTemplateCollection> {
        self.client
            .send_contract(
                RawRequest::read("/v1/template/target-templates")
                    .named("red_team.targets.get_target_templates"),
                "TargetTemplateCollectionSchema",
                options,
            )
            .await
    }
    pub fn list_pages(
        &self,
        params: &TargetListOptions,
        max_pages: usize,
        options: Option<&RequestOptions>,
    ) -> Result<Pages<'static, TargetListItem, u64>> {
        Self::query(params)?;
        let client = self.clone();
        let mut params = params.clone();
        let initial = params.skip.unwrap_or(0);
        let limit = params.limit.unwrap_or(50);
        params.limit = Some(limit);
        Pages::new(
            initial,
            self.client.operation_options(options)?,
            self.client.session(),
            max_pages,
            move |skip, options| {
                let client = client.clone();
                let mut params = params.clone();
                params.skip = Some(skip);
                async move {
                    let page = client.list(&params, Some(&options)).await?;
                    let items = match page.data {
                        Optional::Unset => Vec::new(),
                        Optional::Value(items) => items,
                    };
                    common::skip_page(items, page.pagination, skip, limit)
                }
            },
        )
    }
    pub fn list_items(
        &self,
        params: &TargetListOptions,
        max_pages: usize,
        options: Option<&RequestOptions>,
    ) -> Result<Items<'static, TargetListItem, u64>> {
        Ok(self.list_pages(params, max_pages, options)?.items())
    }
    pub async fn list_all(
        &self,
        params: &TargetListOptions,
        collect: CollectOptions,
        options: Option<&RequestOptions>,
    ) -> CollectionResult<TargetListItem, u64> {
        match self.list_pages(params, collect.max_pages, options) {
            Ok(pages) => pages.collect(collect).await,
            Err(error) => Err(error.into()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn profiling_filter_preserves_legacy_status_without_translation() {
        for status in [
            "DRAFT",
            "ACTIVE",
            "FAILED",
            "QUEUED",
            "IN_PROGRESS",
            "COMPLETED",
            "PARTIALLY_COMPLETE",
            "future-status",
        ] {
            for profiling in ["NOT_STARTED", "IN_PROGRESS", "COMPLETED", "FAILED"] {
                let params = TargetListOptions {
                    status: Some(status.into()),
                    profiling_status: Some(profiling.into()),
                    ..Default::default()
                };
                assert_eq!(
                    RedTeamTargetsClient::query(&params).unwrap(),
                    vec![
                        ("status".into(), status.into()),
                        ("profiling_status".into(), profiling.into()),
                    ]
                );
            }
        }
        assert!(
            RedTeamTargetsClient::query(&TargetListOptions::default())
                .unwrap()
                .is_empty()
        );
    }

    #[test]
    fn profiling_filter_rejects_undeclared_values() {
        for value in [
            "",
            "INIT",
            "QUEUED",
            "PARTIALLY_COMPLETE",
            "failed",
            "FAILED\n",
        ] {
            let params = TargetListOptions {
                profiling_status: Some(value.into()),
                ..Default::default()
            };
            assert_eq!(
                RedTeamTargetsClient::query(&params).unwrap_err().kind(),
                crate::ErrorKind::Validation
            );
        }
    }
}