-
Notifications
You must be signed in to change notification settings - Fork 300
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
Fix for keyring errors when initializing Flyte for_sandbox config client #2962
Open
taieeuu
wants to merge
24
commits into
flyteorg:master
Choose a base branch
from
taieeuu:issue_4354
base: master
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.
+64
−13
Open
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
21ebec7
Fix for keyring errors when initializing Flyte for_sandbox config cli…
taieeuu f358477
no_msg
taieeuu 77bc862
no_msg
taieeuu 5c9bed0
Merge branch 'master' into issue_4354
taieeuu b42a90f
fix: run linting on codebase
taieeuu 6cb2d3f
add grpc 401 comments
taieeuu 2214e3d
fix: import
taieeuu a82cd3a
no_msg
taieeuu 4475cc5
Merge branch 'master' into issue_4354
taieeuu 8f28333
fix: update method for initializing authenticator
taieeuu db7cb2f
fix: update the unit_test
taieeuu 48600a7
fix: add grpc's health check to requirements.txt
taieeuu 2542435
no_msg
taieeuu 3e27029
no_msg
taieeuu 61b35fd
fix: package dependency
taieeuu 42509fb
fix: ci dependencies
taieeuu 0a5afe0
fix: dependencies
taieeuu a50078e
no_msg
taieeuu 456cd5d
fix: lint
taieeuu 9253dc0
fix: add dependencies
taieeuu c5d9426
no_msg
taieeuu 7bb5ef3
no_msg
taieeuu b7e5146
no_msg
taieeuu b97107b
no_msg
taieeuu 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 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 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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import logging | ||
import typing | ||
from collections import namedtuple | ||
|
||
import grpc | ||
|
||
from flytekit.clients.auth.authenticator import Authenticator | ||
from flytekit.clients.auth.authenticator import Authenticator, ClientConfigStore | ||
from flytekit.configuration import PlatformConfig | ||
|
||
|
||
class _ClientCallDetails( | ||
|
@@ -25,8 +27,10 @@ class AuthUnaryInterceptor(grpc.UnaryUnaryClientInterceptor, grpc.UnaryStreamCli | |
is needed. | ||
""" | ||
|
||
def __init__(self, authenticator: Authenticator): | ||
def __init__(self, authenticator: Authenticator, cfg: PlatformConfig = None, cfg_store: ClientConfigStore = None): | ||
self._authenticator = authenticator | ||
self._cfg = cfg | ||
self._cfg_store = cfg_store | ||
|
||
def _call_details_with_auth_metadata(self, client_call_details: grpc.ClientCallDetails) -> grpc.ClientCallDetails: | ||
""" | ||
|
@@ -63,10 +67,9 @@ def intercept_unary_unary( | |
if e: | ||
if not hasattr(e, "code"): | ||
raise e | ||
# When gRPC is UNAUTHENTICATED (401) or grpc is UNKNOWN, handle the authentication error and trigger the PKCE authenticator. | ||
if e.code() == grpc.StatusCode.UNAUTHENTICATED or e.code() == grpc.StatusCode.UNKNOWN: | ||
self._authenticator.refresh_credentials() | ||
updated_call_details = self._call_details_with_auth_metadata(client_call_details) | ||
return continuation(updated_call_details, request) | ||
return self._handle_unauthenticated_error(fut, client_call_details, request) | ||
return fut | ||
|
||
def intercept_unary_stream(self, continuation, client_call_details, request): | ||
|
@@ -75,8 +78,45 @@ def intercept_unary_stream(self, continuation, client_call_details, request): | |
""" | ||
updated_call_details = self._call_details_with_auth_metadata(client_call_details) | ||
c: grpc.Call = continuation(updated_call_details, request) | ||
# When gRPC is UNAUTHENTICATED (401), handle the authentication error and trigger the PKCE authenticator. | ||
if c.code() == grpc.StatusCode.UNAUTHENTICATED: | ||
self._authenticator.refresh_credentials() | ||
updated_call_details = self._call_details_with_auth_metadata(client_call_details) | ||
return continuation(updated_call_details, request) | ||
return self._handle_unauthenticated_error(c, client_call_details, request) | ||
return c | ||
|
||
def _handle_unauthenticated_error(self, continuation, client_call_details, request): | ||
""" | ||
Handling Unauthenticated Errors and Triggering the PKCE Flow | ||
""" | ||
|
||
logging.info("Received authentication error, starting PKCE authentication flow") | ||
|
||
try: | ||
if isinstance(self._authenticator, Authenticator) and not isinstance( | ||
self._authenticator, PKCEAuthenticator | ||
): | ||
logging.info("Current authenticator is 'None', switching to PKCEAuthenticator") | ||
|
||
from flytekit.clients.auth.authenticator import PKCEAuthenticator | ||
from flytekit.clients.auth_helper import get_session | ||
|
||
session = get_session(self._cfg) | ||
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. should we move import under |
||
|
||
verify = None | ||
if self._cfg.insecure_skip_verify: | ||
verify = False | ||
elif self._cfg.ca_cert_file_path: | ||
verify = self._cfg.ca_cert_file_path | ||
|
||
self._authenticator = PKCEAuthenticator( | ||
self._cfg.endpoint, self._cfg_store, scopes=self._cfg.scopes, verify=verify, session=session | ||
) | ||
|
||
self._authenticator.refresh_credentials() | ||
logging.info("Authentication flow completed successfully") | ||
|
||
except Exception as e: | ||
logging.error(f"Authentication failed: {str(e)}") | ||
raise | ||
|
||
updated_call_details = self._call_details_with_auth_metadata(client_call_details) | ||
return continuation(updated_call_details, request) |
This file contains 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
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.
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.
add comments about response 401...