|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +""" |
| 5 | +Scenario preset model. |
| 6 | +
|
| 7 | +A scenario preset is a named, reusable, target-agnostic answer to *what to test*: |
| 8 | +a scenario plus the scenario-owned configuration fields. It deliberately excludes |
| 9 | +everything environment-specific — target, concurrency, retries, and labels — which |
| 10 | +belongs to a launch rather than to the preset. That split is what lets one preset |
| 11 | +run unchanged against dev, staging, and production. |
| 12 | +
|
| 13 | +Every configurable field is tri-state. ``None`` means "not set by this preset, use |
| 14 | +the scenario's own default", which is distinct from an explicit value that happens |
| 15 | +to equal that default. Collapsing the two would silently pin a scenario default at |
| 16 | +the moment a preset was saved and stop it tracking upstream changes. |
| 17 | +
|
| 18 | +``PresetIssue`` lives here rather than with the REST envelopes because checking a |
| 19 | +preset against the live registry is the registry's job, not the wire layer's, and |
| 20 | +both the registry and its callers need to name the result type. |
| 21 | +""" |
| 22 | + |
| 23 | +from pydantic import BaseModel, ConfigDict, Field, field_validator |
| 24 | + |
| 25 | +from pyrit.models.catalog.scenario import ( |
| 26 | + _RequestFilters, |
| 27 | + _RequestName, |
| 28 | + _RequestNames, |
| 29 | + _RequestParams, |
| 30 | + _RequestTechniques, |
| 31 | + _validate_dataset_filter_mapping, |
| 32 | +) |
| 33 | +from pyrit.models.identifiers.class_name_utils import validate_registry_name |
| 34 | + |
| 35 | + |
| 36 | +class ScenarioPreset(BaseModel): |
| 37 | + """ |
| 38 | + A named, reusable, target-agnostic scenario configuration. |
| 39 | +
|
| 40 | + Presets own *what to test*. A launch owns *how and where* — the target, |
| 41 | + concurrency, retries, and labels — so those fields are absent here by design |
| 42 | + and the two sets are combined by union rather than by precedence. |
| 43 | +
|
| 44 | + Unknown keys are rejected rather than ignored. These documents are hand-edited, and |
| 45 | + because an absent field is a meaningful state, a misspelled key would otherwise parse |
| 46 | + cleanly and leave the preset silently testing the scenario default instead of the |
| 47 | + value the file plainly states. |
| 48 | +
|
| 49 | + ``description`` and ``author`` describe the preset rather than configure the run, so |
| 50 | + they are not tri-state: an absent value means unknown, not "use the scenario default". |
| 51 | + ``author`` is filled in from the signed-in user when a preset is created through the |
| 52 | + API and is informational only — these documents are hand-editable, so it records |
| 53 | + provenance rather than proving it. |
| 54 | + """ |
| 55 | + |
| 56 | + model_config = ConfigDict(extra="forbid") |
| 57 | + |
| 58 | + name: str = Field(..., description="Unique preset name, used as the storage key and as the reference from scans") |
| 59 | + scenario_name: _RequestName = Field(..., min_length=1, description="Registered scenario this preset configures") |
| 60 | + description: str | None = Field(None, description="Human-readable summary of what this preset tests") |
| 61 | + author: str | None = Field( |
| 62 | + None, |
| 63 | + description="Who created this preset; descriptive only, never used to authorize a read or a write", |
| 64 | + ) |
| 65 | + techniques: _RequestTechniques | None = Field(None, description="Technique names; None uses the scenario default") |
| 66 | + dataset_names: _RequestNames | None = Field(None, description="Dataset names; None uses the scenario default") |
| 67 | + max_dataset_size: int | None = Field(None, ge=1, description="Maximum selected logical seed groups") |
| 68 | + dataset_filters: _RequestFilters | None = Field( |
| 69 | + None, |
| 70 | + description="Dataset seed filters keyed by field. Accepted keys: harm_categories, data_types.", |
| 71 | + ) |
| 72 | + include_baseline: bool | None = Field(None, description="Override the scenario baseline default") |
| 73 | + scenario_params: _RequestParams | None = Field( |
| 74 | + None, description="Scenario-declared parameters such as template names and attempt counts" |
| 75 | + ) |
| 76 | + |
| 77 | + @field_validator("name") |
| 78 | + @classmethod |
| 79 | + def _validate_name(cls, value: str) -> str: |
| 80 | + """ |
| 81 | + Validate that the preset name is a legal registry name. |
| 82 | +
|
| 83 | + Returns: |
| 84 | + str: The validated name. |
| 85 | +
|
| 86 | + Raises: |
| 87 | + ValueError: If the name is not a legal registry name. |
| 88 | + """ |
| 89 | + validate_registry_name(value) |
| 90 | + return value |
| 91 | + |
| 92 | + @field_validator("dataset_filters") |
| 93 | + @classmethod |
| 94 | + def _validate_dataset_filters(cls, value: dict[str, list[str]] | None) -> dict[str, list[str]] | None: |
| 95 | + """ |
| 96 | + Validate dataset filters against the shared allow-list. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + dict[str, list[str]] | None: Validated filters. |
| 100 | + """ |
| 101 | + return _validate_dataset_filter_mapping(value) |
| 102 | + |
| 103 | + |
| 104 | +class StoredPreset(BaseModel): |
| 105 | + """ |
| 106 | + A preset together with the version of the document it was read from. |
| 107 | +
|
| 108 | + The version describes the *stored document*, not the preset, so it is paired with |
| 109 | + the preset rather than carried as a field on it. Storing the token inside the file |
| 110 | + it guards would let a hand-edit rewrite the very value used to detect that edit. |
| 111 | +
|
| 112 | + The token is opaque. Callers round-trip it from a read back into a write and must |
| 113 | + not parse, compare, or order it. |
| 114 | + """ |
| 115 | + |
| 116 | + preset: ScenarioPreset = Field(..., description="The stored preset") |
| 117 | + version: str = Field(..., description="Opaque version of the document this preset was read from") |
| 118 | + |
| 119 | + |
| 120 | +class PresetIssue(BaseModel): |
| 121 | + """ |
| 122 | + One advisory problem found while checking a preset against the live registry. |
| 123 | +
|
| 124 | + Issues are reported, never enforced. A preset that names a scenario this |
| 125 | + deployment has not registered is still a valid preset; it simply cannot run |
| 126 | + here yet. Blocking the save would make presets un-portable between |
| 127 | + deployments, which is the one property they exist to have. |
| 128 | + """ |
| 129 | + |
| 130 | + field: str = Field(..., description="Preset field the issue applies to, e.g. 'scenario_name' or 'techniques'") |
| 131 | + message: str = Field(..., description="Human-readable description of why the reference does not resolve") |
0 commit comments