-
Notifications
You must be signed in to change notification settings - Fork 76
[AAP-87587] Provide validation patterns for nested fields and top level fields in metadata #1668
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
Open
daphnemaeve
wants to merge
4
commits into
ansible:main
Choose a base branch
from
daphnemaeve:aap-87587
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+152
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5acb574
add validation pattern for credentials nested fields
daphnemaeve 282ac95
call inject_clean_text_pattern in metadata class to provide pattern i…
daphnemaeve 5afc67a
Update src/aap_eda/api/serializers/credential_type.py
daphnemaeve 403d75f
Wrap DAB metadata imports in try/except for graceful fallback
redhat-chai-bot 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 |
|---|---|---|
|
|
@@ -12,6 +12,18 @@ | |
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| try: | ||
| from ansible_base.lib.metadata import get_tier2_pattern, validation_enabled | ||
| except ImportError: # pragma: no cover - DAB without AAP-85987 | ||
| get_tier2_pattern = None | ||
| validation_enabled = None | ||
|
|
||
| try: | ||
| from ansible_base.lib.serializers.mixins import CleanTextMixin | ||
| except ImportError: # pragma: no cover - DAB without AAP-85987 | ||
| # Provide a no-op stand-in so the class definition is valid | ||
| class CleanTextMixin: | ||
| pass | ||
| from rest_framework import serializers | ||
|
|
||
| from aap_eda.core import models, validators | ||
|
|
@@ -37,8 +49,51 @@ class Meta: | |
| *read_only_fields, | ||
| ] | ||
|
|
||
| def to_representation(self, instance): | ||
| data = super().to_representation(instance) | ||
| inputs = data.get("inputs") | ||
| if validation_enabled is not None and validation_enabled() and isinstance(inputs, dict): | ||
| data["inputs"] = _with_field_patterns(inputs) | ||
| return data | ||
|
|
||
|
|
||
| def _with_field_patterns(inputs: dict) -> dict: | ||
| """Return a copy of the inputs schema with patterns for string fields. | ||
|
|
||
| CleanTextMixin (from DAB) enforces free-text validation rules on | ||
| serializer string fields at write time (when the ENHANCED_INPUT_VALIDATION_ENABLED setting is turned on). Only non-secret "string" | ||
| sub-fields get a pattern here, since those are the only ones its | ||
| JSON sub-key validation applies to; secret and boolean fields are | ||
| left untouched. | ||
| """ | ||
| fields = inputs.get("fields") | ||
| if not isinstance(fields, list): | ||
| return inputs | ||
|
|
||
| if get_tier2_pattern is None: | ||
| return inputs | ||
| pattern = get_tier2_pattern() | ||
| new_fields = [ | ||
| { | ||
| **field, | ||
| "pattern": pattern["pattern"], | ||
| "pattern_description": pattern["description"], | ||
| } | ||
| if isinstance(field, dict) | ||
| and field.get("type") == "string" | ||
| and not field.get("secret") | ||
| else field | ||
| for field in fields | ||
| ] | ||
| return {**inputs, "fields": new_fields} | ||
|
|
||
| class CredentialTypeCreateSerializer( | ||
| CleanTextMixin, serializers.ModelSerializer | ||
| ): | ||
| # injectors commonly contain Jinja2 template syntax, so it is excluded | ||
| # from free-text checks. | ||
| excluded_fields = frozenset({"injectors"}) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this change live in this PR? |
||
|
|
||
| class CredentialTypeCreateSerializer(serializers.ModelSerializer): | ||
| inputs = serializers.JSONField( | ||
| required=False, | ||
| default=dict, | ||
|
|
||
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.
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.