-
Notifications
You must be signed in to change notification settings - Fork 8
PAPP-36780-cert auth method #59
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
NiemySpiewak-splunk
wants to merge
3
commits into
msankowska/PSAAS-24763-porting_HTTP_to_SDK
Choose a base branch
from
msankowska/PAPP-36780-cert-based-auth-new
base: msankowska/PSAAS-24763-porting_HTTP_to_SDK
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -23,6 +23,8 @@ | |
| from .asset import Asset | ||
| from .auth import OAuth | ||
| from .common import logger | ||
| from .auth import Authorization, CertificateAuth | ||
| from .helpers import temp_cert_files | ||
|
|
||
|
|
||
| def make_request( | ||
|
|
@@ -49,20 +51,37 @@ def make_request( | |
|
|
||
| logger.info(f"Making {method} request to: {full_url}") | ||
|
|
||
| body = ( | ||
| UnicodeDammit(body).unicode_markup.encode("utf-8") | ||
| if isinstance(body, str) | ||
| else body | ||
| ) | ||
| body = UnicodeDammit(body).unicode_markup.encode("utf-8") if isinstance(body, str) else body | ||
|
|
||
| from .app import get_auth_method | ||
|
|
||
| auth_method = get_auth_method(asset, soar) | ||
|
|
||
| if isinstance(auth_method, CertificateAuth): | ||
| return _execute_certificate_request(auth_method, full_url, method, body, verify, parsed_headers, output, asset) | ||
| else: | ||
| return _execute_standard_request(auth_method, full_url, method, body, verify, parsed_headers, output, asset) | ||
|
|
||
|
|
||
| def _execute_standard_request( | ||
| auth_method: Authorization, | ||
| full_url: str, | ||
| method: str, | ||
| body: Optional[str], | ||
| verify: bool, | ||
| headers: dict, | ||
| output_cls: type[ActionOutput], | ||
| asset: Asset, | ||
| ) -> ActionOutput: | ||
| """ | ||
| Executes a standard HTTP request (non-certificate based). | ||
| Handles OAuth token refresh logic. | ||
| """ | ||
| retries = 1 | ||
| response = None | ||
|
|
||
| while retries >= 0: | ||
| from .app import get_auth_method | ||
|
|
||
| auth_method = get_auth_method(asset, soar) | ||
| auth_object, final_headers = auth_method.create_auth(parsed_headers) | ||
| auth_object, final_headers = auth_method.create_auth(headers.copy()) | ||
|
|
||
| try: | ||
| response = requests.request( | ||
|
|
@@ -75,26 +94,68 @@ def make_request( | |
| timeout=asset.timeout, | ||
| ) | ||
| response.raise_for_status() | ||
|
|
||
| break | ||
|
|
||
| except requests.exceptions.RequestException as e: | ||
| if isinstance(auth_method, OAuth) and retries > 0: | ||
| logger.warning( | ||
| "Request failed with 401, token might be expired. Forcing a refresh." | ||
| ) | ||
| auth_method.get_token(force_new=True) | ||
| logger.warning("Request failed with 401, token might be expired. Forcing a refresh.") | ||
| auth_method.get_token(force_new=True, full_url=full_url) | ||
| retries -= 1 | ||
| continue | ||
| else: | ||
| raise ActionFailure( | ||
| f"Request failed for {full_url}. Details: {e}" | ||
| ) from e | ||
| raise ActionFailure(f"Request failed for {full_url}. Details: {e}") from e | ||
|
|
||
| if response is None: | ||
| raise ActionFailure(f"Request failed for {full_url} and no response was received after retries.") | ||
|
|
||
| parsed_body, raw_body = helpers.handle_various_response(response) | ||
| logger.info(f"Successfully processed data. Status: {response.status_code}") | ||
|
|
||
| return output_cls( | ||
| status_code=response.status_code, | ||
| location=full_url, | ||
| method=method, | ||
| parsed_response_body=parsed_body, | ||
| response_body=raw_body, | ||
| response_headers=str(dict(response.headers)), | ||
| ) | ||
|
|
||
|
||
|
|
||
| def _execute_certificate_request( | ||
| auth_method: CertificateAuth, | ||
| full_url: str, | ||
| method: str, | ||
| body: Optional[str], | ||
| verify: bool, | ||
| headers: dict, | ||
| output_cls: type[ActionOutput], | ||
| asset: Asset, | ||
| ) -> ActionOutput: | ||
| """ | ||
| Executes an HTTP request using client-side certificates. | ||
| """ | ||
| public_cert_data, private_key_data = auth_method.create_auth(headers.copy()) | ||
|
|
||
| with temp_cert_files(public_cert_data, private_key_data) as cert_param: | ||
| try: | ||
| response = requests.request( | ||
| method=method, | ||
| url=full_url, | ||
| cert=cert_param, | ||
| data=body, | ||
| verify=verify, | ||
| headers=headers, | ||
| timeout=asset.timeout, | ||
| ) | ||
| response.raise_for_status() | ||
|
|
||
| except requests.exceptions.RequestException as e: | ||
| raise ActionFailure(f"Certificate-based request failed for {full_url}. Details: {e}") from e | ||
|
|
||
| parsed_body, raw_body = helpers.handle_various_response(response) | ||
| logger.info(f"Successfully processed data. Status: {response.status_code}") | ||
|
|
||
| return output( | ||
| return output_cls( | ||
| status_code=response.status_code, | ||
| location=full_url, | ||
| method=method, | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think cert based required both public and private keys to be not null. We should raise if both aren't a non empty string