-
Notifications
You must be signed in to change notification settings - Fork 919
MAINT: Extract technique factory resolution from MatrixAtomicAttackBuilder (#2869) #2879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Roman Lutz (romanlutz)
merged 4 commits into
microsoft:main
from
Kaap10:feature/extract-technique-resolution-2869
Sep 28, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bcd7395
MAINT: Extract technique factory resolution from MatrixAtomicAttackBu…
Kaap10 90a4131
Merge branch 'main' into feature/extract-technique-resolution-2869
Kaap10 b0417ef
MAINT: Make technique resolution private and deduplicate resolver tes…
Kaap10 2868e99
Merge branch 'main' into feature/extract-technique-resolution-2869
romanlutz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Sequence | ||
|
|
||
| from pyrit.scenario.core.attack_technique_factory import AttackTechniqueFactory | ||
| from pyrit.scenario.core.scenario_context import ScenarioContext | ||
| from pyrit.scenario.core.scenario_technique import ScenarioTechnique | ||
|
|
||
|
|
||
| class TechniqueResolutionError(ValueError): | ||
| """ | ||
| Raised when a selected scenario technique has no registered factory. | ||
|
|
||
| Subclasses ``ValueError`` so existing ``except ValueError`` handlers keep working, | ||
| mirroring ``DatasetConstraintError``. | ||
| """ | ||
|
|
||
|
|
||
| def resolve_technique_factories( | ||
| *, | ||
| context: ScenarioContext, | ||
| extra_factories: dict[str, AttackTechniqueFactory] | None = None, | ||
| ) -> dict[str, AttackTechniqueFactory]: | ||
| """ | ||
| Resolve a run's selected techniques to their registered ``AttackTechniqueFactory`` instances. | ||
|
|
||
| Reads the ``AttackTechniqueRegistry`` singleton and keeps only the factories whose name | ||
| matches a selected technique, preserving selection order. Raises if any selected | ||
| technique has no registered factory so the run cannot silently omit requested work. | ||
|
|
||
| Args: | ||
| context (ScenarioContext): The resolved runtime inputs for this run. | ||
| extra_factories (dict[str, AttackTechniqueFactory] | None): Scenario-local factories | ||
| merged on top of the registry before filtering, so a scenario can offer techniques | ||
| without registering them globally. Entries override registry factories of the same | ||
| name. | ||
|
|
||
| Returns: | ||
| dict[str, AttackTechniqueFactory]: Mapping of technique name to factory, ordered by | ||
| the selected techniques. | ||
|
|
||
| Raises: | ||
| TechniqueResolutionError: If any selected technique has no registered factory. | ||
| """ | ||
| return resolve_technique_factories_for_techniques( | ||
| scenario_techniques=context.scenario_techniques, | ||
| extra_factories=extra_factories, | ||
| ) | ||
|
|
||
|
|
||
| def resolve_technique_factories_for_techniques( | ||
| *, | ||
| scenario_techniques: Sequence[ScenarioTechnique], | ||
| extra_factories: dict[str, AttackTechniqueFactory] | None = None, | ||
| ) -> dict[str, AttackTechniqueFactory]: | ||
| """ | ||
| Resolve selected concrete techniques to their canonical factories. | ||
|
|
||
| Accepts any sequence of scenario techniques so callers without a full | ||
| `ScenarioContext` (e.g. run-size estimators, dry runs, scenario builders) can | ||
| inspect factory metadata or validate coverage. | ||
|
|
||
| Args: | ||
| scenario_techniques (Sequence[ScenarioTechnique]): Concrete techniques to resolve. | ||
| extra_factories (dict[str, AttackTechniqueFactory] | None): Scenario-local factories | ||
| merged on top of the registry. Entries override registry factories of the same name. | ||
|
|
||
| Returns: | ||
| dict[str, AttackTechniqueFactory]: Selected factories in technique order. | ||
|
|
||
| Raises: | ||
| TechniqueResolutionError: If any selected technique has no registered factory. | ||
| """ | ||
| from pyrit.registry.components.attack_technique_registry import AttackTechniqueRegistry | ||
|
|
||
| all_factories = dict(AttackTechniqueRegistry.get_registry_singleton().get_factories_or_raise()) | ||
| if extra_factories: | ||
| all_factories.update(extra_factories) | ||
|
|
||
| missing = list(dict.fromkeys(t.value for t in scenario_techniques if t.value not in all_factories)) | ||
|
|
||
| if missing: | ||
| raise TechniqueResolutionError( | ||
| "The following selected attack techniques have no registered factory: " | ||
| f"{', '.join(missing)}. Register the techniques (or pass them via " | ||
| "extra_factories) before starting the run." | ||
| ) | ||
|
|
||
| return {technique.value: all_factories[technique.value] for technique in scenario_techniques} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.