Skip to content

refactor: use shared code for IAM based authenticators #118

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
merged 7 commits into from
Aug 6, 2021

Conversation

pyrooka
Copy link
Member

@pyrooka pyrooka commented Aug 5, 2021

This PR introduces two new classes: IAMRequestBasedTokenManager and IAMRequestBasedAuthenticator. These contain common functionalities for IAM auth based flows.

I haven't changed the tests on purpose. (Well, there is a minor change to fix the codecov patch check). The reason is to make sure that no interface change has been made, so everything is working just like before.

@codecov
Copy link

codecov bot commented Aug 5, 2021

Codecov Report

Merging #118 (9b73630) into main (814a596) will increase coverage by 0.14%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #118      +/-   ##
==========================================
+ Coverage   98.77%   98.92%   +0.14%     
==========================================
  Files          18       20       +2     
  Lines         737      746       +9     
==========================================
+ Hits          728      738      +10     
+ Misses          9        8       -1     
Impacted Files Coverage Δ
ibm_cloud_sdk_core/authenticators/authenticator.py 75.00% <ø> (ø)
..._core/authenticators/bearer_token_authenticator.py 100.00% <ø> (ø)
...d_sdk_core/authenticators/no_auth_authenticator.py 100.00% <ø> (ø)
ibm_cloud_sdk_core/get_authenticator.py 100.00% <ø> (ø)
...loud_sdk_core/token_managers/cp4d_token_manager.py 100.00% <ø> (ø)
ibm_cloud_sdk_core/utils.py 99.33% <ø> (ø)
ibm_cloud_sdk_core/__init__.py 100.00% <100.00%> (ø)
ibm_cloud_sdk_core/api_exception.py 100.00% <100.00%> (ø)
...loud_sdk_core/authenticators/cp4d_authenticator.py 100.00% <100.00%> (ø)
...cloud_sdk_core/authenticators/iam_authenticator.py 100.00% <100.00%> (+3.12%) ⬆️
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 814a596...9b73630. Read the comment docs.

@pyrooka
Copy link
Member Author

pyrooka commented Aug 5, 2021

I am thinking about one more thing that could improve the project structure: put all the token managers into a single directory, just like the authenticators. Not sure would this break anything or not, I only found this.
Screenshot 2021-08-05 at 19 13 29

@pyrooka pyrooka requested review from padamstx and rmkeezer August 5, 2021 17:54
@padamstx
Copy link
Member

padamstx commented Aug 5, 2021

Not sure would this break anything or not,

if you move the token manager files to a new "token_managers" directory, couldn't you just update the ibm_cloud_sdk_core/__init__.py file like the snippet below to import them from the new location and effectively preserve the fact that they can be imported by users from the ibm_cloud_sdk_core package?

from token_managers.iam_token_manager import IAMTokenManager
from token_managers.jwt_token_manager import JWTTokenManager
from token_managers.cp4d_token_manager import CP4DTokenManager

Copy link
Member

@padamstx padamstx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I spotted one minor thing that needs to be changed but I'll approve now to avoid a re-review. It looks like this refactoring work will greatly reduce the amount of new code needed for the ContainerAuthenticator and (later on) the VPCInstanceAuthenticator. I've found the same thing in the Java core as well and I suspect @dpopp07 is seeing the same in the Node core. @dpopp07 Thank you for the idea to do this refactoring :)

Attributes:
token_manager (TokenManager): Retrives and manages IAM tokens from the endpoint specified by the url.
"""
authentication_type = 'iam'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't apply to all IAM-based authenticators, so it probably belongs only in the IamAuthenticator class.

@padamstx padamstx requested a review from dpopp07 August 5, 2021 18:21
Copy link

@dpopp07 dpopp07 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I second Phil's comment but otherwise, looks good! 👍

@pyrooka
Copy link
Member Author

pyrooka commented Aug 6, 2021

I've pushed 3 a few more commits since your review.
The first 2 contain the authentication_type = 'iam' fix and the restructuring of the token managers.
The last third commit is really just about the code style. I found that there was a bit inconsistency so I checked every single file, reordered/grouped the imports and added missing blank lines (where it was necessary).

More info:

(Since there is a lot of changes (number of files) in the last commit, it makes the overall diff a bit messy, sorry for that! I didn't want to create a separate PR for this.)

Copy link
Member

@padamstx padamstx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest changes LGTM!

@@ -95,7 +96,7 @@ def request_token(self) -> dict:
A dictionary containing the bearer token to be subsequently used service requests.
"""
headers = {
'Content-type': 'applicaton/x-www-form-urlencoded',
'Content-type': 'application/x-www-form-urlencoded',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, the misspelled "applicaton" didn't cause a problem before??? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was surprised too! The container authenticator tests will cover this, so I didn't change the tests here.

@pyrooka pyrooka merged commit e0aeed7 into main Aug 6, 2021
@pyrooka pyrooka deleted the shared-iam-related-code branch August 6, 2021 15:52
@ibm-devx-sdk
Copy link

🎉 This PR is included in version 3.11.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants