Skip to content
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

[#3755] improvement(client-python): Support OAuth2TokenProvider for Python client #4011

Merged
merged 13 commits into from
Jul 19, 2024

Conversation

noidname01
Copy link
Collaborator

@noidname01 noidname01 commented Jul 1, 2024

What changes were proposed in this pull request?

Why are the changes needed?

Fix: #3755, #4136

Does this PR introduce any user-facing change?

No

How was this patch tested?

Add UT and tested by ./gradlew clients:client-python:unittest

@noidname01 noidname01 marked this pull request as draft July 1, 2024 07:49
@noidname01 noidname01 marked this pull request as ready for review July 1, 2024 08:13
@jerryshao
Copy link
Contributor

@xloya can you please help to review this?

@xloya
Copy link
Contributor

xloya commented Jul 2, 2024

@xloya can you please help to review this?

Sure, will take a look later.

from gravitino.auth.auth_constants import AuthConstants


class OAuth2TokenProvider(AuthDataProvider):
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that do not have docs, please add some docs for the new classes which have docs in Java.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

if data:
request_data = urlencode(data.to_dict()).encode()
headers = {
"Content-Type": "application/x-www-form-urlencoded",
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we manage these uniformly as static variables?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added as static variables of HTTPClient

@noidname01 noidname01 force-pushed the python-oauth branch 2 times, most recently from 5b0fb82 to 255cabd Compare July 3, 2024 06:45
OAUTH_PORT = 1082


class TestOAuth2TokenProvider(unittest.TestCase):
Copy link
Contributor

Choose a reason for hiding this comment

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

It's better to add an expired token test case, WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually the last testcase is the test for expired token, but it does not so obvious.
https://github.com/noidname01/gravitino/blob/255cabdb882228f81de1a1907d34fba31cf5b351/clients/client-python/tests/unittests/auth/test_oauth2_token_provider.py#L65

Theold_access_token will be loaded when DefaultOAuth2TokenProvider initializing, then the old_access_token will be replaced with new_access_token by calling get_token_data()

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. I think we also need to test abnormal cases, such as using an expired token to access the Server again, which should be rejected. I am not sure whether it is convenient to test the expired token to access the Server in a unit test. If it is not convenient, I think you can add a test case for authentication failure when using an expired token to access the Server in the integration tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, it seems it's better to test in integration tests because it needs to interact with server.

@noidname01 noidname01 force-pushed the python-oauth branch 2 times, most recently from ea69828 to 8f8b5db Compare July 5, 2024 07:58
@jerryshao
Copy link
Contributor

@noidname01 do you need to update the code base on #4012 ?

@noidname01
Copy link
Collaborator Author

@noidname01 do you need to update the code base on #4012 ?

Yes, I think so.

@jerryshao
Copy link
Contributor

@noidname01 Do you have time continue on this?

@noidname01
Copy link
Collaborator Author

@noidname01 Do you have time continue on this?

@jerryshao Sorry for the inactiveness recently, I will finish this PR tonight

@jerryshao
Copy link
Contributor

Thanks a lot, appreciated. @noidname01

@noidname01
Copy link
Collaborator Author

@jerryshao I have done with rebasing and some fix, please review again.

JWT_EXPIRE = "exp"


class DefaultOAuth2TokenProvider(OAuth2TokenProvider):
Copy link
Contributor

Choose a reason for hiding this comment

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

The file name is strange that 2 doesn't equal to "to", oauth2 is a auth name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah yes, I know but I miss to modify this file name.

@jerryshao
Copy link
Contributor

@xloya @jerqi would you please help to review this PR.

@@ -50,3 +50,4 @@ def test_auth_provider(self):
token[len(AuthConstants.AUTHORIZATION_BASIC_HEADER) :]
).decode("utf-8")
self.assertEqual(f"{user}:dummy", token_string)
os.environ["GRAVITINO_USER"] = ""
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess to fix this issue: #4136 .

Copy link
Contributor

Choose a reason for hiding this comment

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

@noidname01 How about set it to the origin value, rather than set it to empty string?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@noidname01 How about set it to the origin value, rather than set it to empty string?

Sure, fixed!

@jerqi
Copy link
Contributor

jerqi commented Jul 18, 2024

Have you tested this using a real OAuth server?

@xloya
Copy link
Contributor

xloya commented Jul 18, 2024

Overall looks good to me, I think you need create an issue to track that add some integration tests for the truly Oauth2 auth.

@noidname01
Copy link
Collaborator Author

Have you tested this using a real OAuth server?

No, I haven't. I plan to do this in another PR. I've created #4208 to track.

@jerryshao jerryshao merged commit 213bcc9 into apache:main Jul 19, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Improvement] Support Oauth2AuthProvider for Python client
4 participants