-
Notifications
You must be signed in to change notification settings - Fork 10
PAPP-36780 cert based auth #58
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
base: msankowska/PSAAS-24763-porting_HTTP_to_SDK
Are you sure you want to change the base?
Changes from all commits
41f46f0
258ac45
66821ac
6c58252
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |||||||||||||
| from .asset import Asset | ||||||||||||||
| from .auth import OAuth, get_auth_method | ||||||||||||||
| from .common import logger | ||||||||||||||
| from .helpers import temp_cert_files | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def make_request( | ||||||||||||||
|
|
@@ -49,48 +50,39 @@ 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 | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| retries = 1 | ||||||||||||||
| response = None | ||||||||||||||
|
|
||||||||||||||
| while retries >= 0: | ||||||||||||||
| auth_method = get_auth_method(asset, soar) | ||||||||||||||
| auth_object, final_headers = auth_method.create_auth(parsed_headers) | ||||||||||||||
|
|
||||||||||||||
| try: | ||||||||||||||
| response = requests.request( | ||||||||||||||
| method=method, | ||||||||||||||
| url=full_url, | ||||||||||||||
| auth=auth_object, | ||||||||||||||
| data=body, | ||||||||||||||
| verify=verify, | ||||||||||||||
| headers=final_headers, | ||||||||||||||
| timeout=asset.timeout, | ||||||||||||||
| ) | ||||||||||||||
| response.raise_for_status() | ||||||||||||||
|
|
||||||||||||||
| break | ||||||||||||||
|
|
||||||||||||||
| except requests.exceptions.RequestException as e: | ||||||||||||||
| if ( | ||||||||||||||
| isinstance(auth_method, OAuth) | ||||||||||||||
| and e.response | ||||||||||||||
| and e.response.status_code == 401 | ||||||||||||||
| and retries > 0 | ||||||||||||||
| ): | ||||||||||||||
| logger.warning( | ||||||||||||||
| "Request failed with 401, token might be expired. Forcing a refresh." | ||||||||||||||
| body = UnicodeDammit(body).unicode_markup.encode("utf-8") if isinstance(body, str) else body | ||||||||||||||
|
|
||||||||||||||
| with temp_cert_files(asset.public_cert, asset.private_key) as cert_param: | ||||||||||||||
|
Contributor
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. This seems like somewhat of an anti-pattern. It implies that we can have token based auth and then get_auth_method can still return a different type of auth. We're implementing standalone cert based auth. Let's change this logic by creating a I also think we shouldn't surround the entire function with a context manager when it's not going to be used most of the time. Lets do something like this
Suggested change
|
||||||||||||||
| retries = 1 | ||||||||||||||
| response = None | ||||||||||||||
|
|
||||||||||||||
| while retries >= 0: | ||||||||||||||
| auth_method = get_auth_method(asset, soar) | ||||||||||||||
| auth_object, final_headers = auth_method.create_auth(parsed_headers) | ||||||||||||||
|
|
||||||||||||||
| try: | ||||||||||||||
| response = requests.request( | ||||||||||||||
| method=method, | ||||||||||||||
| url=full_url, | ||||||||||||||
| auth=auth_object, | ||||||||||||||
| data=body, | ||||||||||||||
| verify=verify, | ||||||||||||||
| headers=final_headers, | ||||||||||||||
| cert=cert_param, | ||||||||||||||
| timeout=asset.timeout, | ||||||||||||||
| ) | ||||||||||||||
| auth_method.get_token(force_new=True) | ||||||||||||||
| retries -= 1 | ||||||||||||||
| continue | ||||||||||||||
| else: | ||||||||||||||
| raise ActionFailure(f"Request failed for {full_url}. Details: {e}") | ||||||||||||||
| response.raise_for_status() | ||||||||||||||
|
|
||||||||||||||
| break | ||||||||||||||
|
|
||||||||||||||
| except requests.exceptions.RequestException as e: | ||||||||||||||
| if isinstance(auth_method, OAuth) and e.response and e.response.status_code == 401 and retries > 0: | ||||||||||||||
|
Contributor
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. This looks like an old version of the base branch. This should be
you probably need to merge the base branch with this branch and fix any merge conflicts |
||||||||||||||
| logger.warning("Request failed with 401, token might be expired. Forcing a refresh.") | ||||||||||||||
| auth_method.get_token(force_new=True) | ||||||||||||||
| retries -= 1 | ||||||||||||||
| continue | ||||||||||||||
| else: | ||||||||||||||
| raise ActionFailure(f"Request failed for {full_url}. Details: {e}") | ||||||||||||||
|
|
||||||||||||||
| parsed_body, raw_body = helpers.handle_various_response(response) | ||||||||||||||
| logger.info(f"Successfully processed data. Status: {response.status_code}") | ||||||||||||||
|
|
||||||||||||||
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.
shouldn't these use
cert_b64andkey_b64respectively