-
Notifications
You must be signed in to change notification settings - Fork 746
Expand file tree
/
Copy pathseed_objective.py
More file actions
62 lines (47 loc) · 1.9 KB
/
seed_objective.py
File metadata and controls
62 lines (47 loc) · 1.9 KB
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
"""
SeedObjective class for representing seed objectives.
"""
from __future__ import annotations
import logging
from dataclasses import dataclass
from typing import TYPE_CHECKING, Optional, Union
from pyrit.common.path import PATHS_DICT
from pyrit.models.seeds.seed import Seed
if TYPE_CHECKING:
from pathlib import Path
logger = logging.getLogger(__name__)
@dataclass
class SeedObjective(Seed):
"""Represents a seed objective with various attributes and metadata."""
is_general_technique: bool = False
def __post_init__(self) -> None:
"""
Post-initialization to render the template to replace existing values.
Raises:
ValueError: If is_general_technique is True.
"""
if self.is_general_technique:
raise ValueError("SeedObjective cannot be a general technique.")
if not self.jinja_template:
self.value = self.escape_for_jinja(self.value)
self.value = super().render_template_value_silent(**PATHS_DICT)
@classmethod
def from_yaml_with_required_parameters(
cls,
template_path: Union[str, Path],
required_parameters: list[str],
error_message: Optional[str] = None,
) -> SeedObjective:
"""
Load a Seed from a YAML file. Because SeedObjectives do not have any parameters, the required_parameters
and error_message arguments are unused.
Args:
template_path: Path to the YAML file containing the template.
required_parameters: List of parameter names that must exist in the template.
error_message: Custom error message if validation fails. If None, a default message is used.
Returns:
SeedObjective: The loaded and validated seed of the specific subclass type.
"""
return cls.from_yaml_file(template_path)