-
Notifications
You must be signed in to change notification settings - Fork 81
[AAP-49757] Add fallback authentication support to local authenticator #868
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
john-westcott-iv
wants to merge
19
commits into
ansible:devel
Choose a base branch
from
john-westcott-iv:AAP-49757
base: devel
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.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3d2fae4
Refactor duration conversion utility to lib/utils module
john-westcott-iv 6676d26
Add fallback authentication support to local authenticator
john-westcott-iv 3002fd8
Fixing minor issues
john-westcott-iv e8656fa
Refactor module path validation to use regex pattern
john-westcott-iv 3fd4266
Add comprehensive test coverage for duration.py utility
john-westcott-iv 112616d
Optimize module path validation by compiling regex once
john-westcott-iv 93fb36b
Refactor convert_to_seconds with cleaner implementation
john-westcott-iv 2f1779d
[AAP-49757] Add exception logging to convert_to_seconds
john-westcott-iv 0d8360d
[AAP-49757] Further parameterize test_duration tests to reduce duplic…
john-westcott-iv 18806a7
[AAP-49757] Completely parameterize test_duration with descriptive IDs
john-westcott-iv f6a45ae
[AAP-49757] Add tests for valid inputs ignoring default parameter
john-westcott-iv e9ea19b
Add runtime validation for default parameter in convert_to_seconds
john-westcott-iv e8653b7
Create import_object utility and consolidate import logic
john-westcott-iv 34c4b45
Fix test_local.py to use import_object instead of removed _load_fallb…
john-westcott-iv 3aebe8c
Revert incorrect fallback parameter passing and fix test
john-westcott-iv 06c2ef2
Use concise character class syntax '\w' in regex patterns
john-westcott-iv c7a435d
Refactor regex patterns to eliminate duplication
john-westcott-iv 156d9c4
test
BrennanPaciorek e8da3f4
Revert "test"
BrennanPaciorek 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
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,83 @@ | ||
| """ | ||
| Utility functions for parsing and converting duration/time strings. | ||
| """ | ||
|
|
||
| import logging | ||
| import re | ||
| from typing import Optional | ||
|
|
||
| logger = logging.getLogger('ansible_base.lib.utils.duration') | ||
|
|
||
|
|
||
| DURATION_CHAR_TO_SECONDS = { | ||
| 's': 1, | ||
| 'm': 60, | ||
| 'h': 3600, | ||
| 'd': 86400, | ||
| 'w': 604800, | ||
| } | ||
|
|
||
| DURATION_RE = re.compile(r"^(-?\d+)([smhdw]?)$") | ||
|
|
||
|
|
||
| def convert_to_seconds(duration_string: Optional[str], default: int = 10) -> int: | ||
bhavenst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Converts a duration string like '15s', '5m', '1h', '2d', '3w' to seconds. | ||
| This function parses duration strings and converts them to seconds. It allows | ||
| negative values, leaving validation to the caller based on their use case. | ||
| Args: | ||
| duration_string: A string representing a duration with a unit suffix. | ||
| Supported units: s (seconds), m (minutes), h (hours), | ||
| d (days), w (weeks). Can also be a plain integer string | ||
| for seconds. Negative values are supported. Case-insensitive. | ||
| default: The default value to return if the input is invalid or cannot | ||
| be parsed. Must be an integer. Defaults to 10 seconds. If a non-integer | ||
| value is provided, a warning with stack trace is logged and 10 is used instead. | ||
| Returns: | ||
| int: The duration in seconds (can be negative), or the default value if invalid. | ||
| Examples: | ||
| >>> convert_to_seconds('15s') | ||
| 15 | ||
| >>> convert_to_seconds('5m') | ||
| 300 | ||
| >>> convert_to_seconds('1h') | ||
| 3600 | ||
| >>> convert_to_seconds('2d') | ||
| 172800 | ||
| >>> convert_to_seconds('1w') | ||
| 604800 | ||
| >>> convert_to_seconds('30') | ||
| 30 | ||
| >>> convert_to_seconds('-5s') | ||
| -5 | ||
| >>> convert_to_seconds('-1d') | ||
| -86400 | ||
| >>> convert_to_seconds('invalid') | ||
| 10 | ||
| >>> convert_to_seconds('invalid', default=42) | ||
| 42 | ||
| >>> convert_to_seconds('invalid', default='not_an_int') # Logs warning with stack trace, returns 10 | ||
| 10 | ||
| """ | ||
| # Validate that default is an integer (but not a boolean, which is a subclass of int in Python) | ||
| if isinstance(default, bool) or not isinstance(default, int): | ||
| logger.warning(f"Invalid default value: '{default}' (type: {type(default).__name__}). Must be an integer. Using default of 10.", stack_info=True) | ||
| default = 10 | ||
|
|
||
| try: | ||
| if duration_string is None: | ||
| raise ValueError("Duration string is None") | ||
|
|
||
| if matches := DURATION_RE.match(duration_string.lower()): | ||
| number = int(matches.group(1)) # The numeric part (can be negative) | ||
| unit = matches.group(2) or 's' # The unit character, default to 's' | ||
| return number * DURATION_CHAR_TO_SECONDS[unit] | ||
| else: | ||
| raise ValueError("Invalid duration format") | ||
| except Exception as e: | ||
| logger.warning(f"Invalid duration format: '{duration_string}' ({e}), return default of {default}") | ||
| return default | ||
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.