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

Fix for keyring errors when initializing Flyte for_sandbox config client #2962

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
21ebec7
Fix for keyring errors when initializing Flyte for_sandbox config cli…
taieeuu Nov 25, 2024
f358477
no_msg
taieeuu Nov 27, 2024
77bc862
no_msg
taieeuu Nov 27, 2024
5c9bed0
Merge branch 'master' into issue_4354
taieeuu Nov 27, 2024
b42a90f
fix: run linting on codebase
taieeuu Nov 27, 2024
6cb2d3f
add grpc 401 comments
taieeuu Dec 3, 2024
2214e3d
fix: import
taieeuu Dec 7, 2024
a82cd3a
no_msg
taieeuu Dec 7, 2024
4475cc5
Merge branch 'master' into issue_4354
taieeuu Dec 7, 2024
8f28333
fix: update method for initializing authenticator
taieeuu Dec 22, 2024
db7cb2f
fix: update the unit_test
taieeuu Dec 22, 2024
48600a7
fix: add grpc's health check to requirements.txt
taieeuu Dec 22, 2024
2542435
no_msg
taieeuu Dec 22, 2024
3e27029
no_msg
taieeuu Dec 22, 2024
61b35fd
fix: package dependency
taieeuu Dec 23, 2024
42509fb
fix: ci dependencies
taieeuu Dec 23, 2024
0a5afe0
fix: dependencies
taieeuu Dec 23, 2024
a50078e
no_msg
taieeuu Dec 24, 2024
456cd5d
fix: lint
taieeuu Dec 24, 2024
9253dc0
fix: add dependencies
taieeuu Dec 24, 2024
c5d9426
no_msg
taieeuu Dec 24, 2024
7bb5ef3
no_msg
taieeuu Dec 24, 2024
b7e5146
no_msg
taieeuu Dec 24, 2024
b97107b
no_msg
taieeuu Dec 24, 2024
89cf6f6
no_msg
taieeuu Dec 27, 2024
17c840f
no_msg
taieeuu Dec 27, 2024
27a6527
Merge branch 'master' into issue_4354
taieeuu Dec 27, 2024
857db92
no_msg
taieeuu Dec 27, 2024
1e56ac7
no_msg
taieeuu Dec 27, 2024
34f4aba
no_msg
taieeuu Dec 27, 2024
3bbbf41
no_msg
taieeuu Dec 27, 2024
9b3916c
no_msg
taieeuu Dec 27, 2024
0028436
no_msg
taieeuu Dec 27, 2024
d3ea8e4
no_msg
taieeuu Dec 28, 2024
5c19192
no_msg
taieeuu Dec 28, 2024
cf442e6
no_msg
taieeuu Dec 28, 2024
569558e
no_msg
taieeuu Dec 28, 2024
c4817cc
no_msg
taieeuu Dec 28, 2024
3d142c3
no_msg
taieeuu Dec 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions flytekit/clients/auth_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def get_authenticator(cfg: PlatformConfig, cfg_store: ClientConfigStore) -> Auth

session = get_session(cfg)

if cfg_auth == AuthType.NO_AUTH:
logging.warning("No authentication required for this configuration.")
return Authenticator(cfg.endpoint, header_key="", verify=verify)

if cfg_auth == AuthType.STANDARD or cfg_auth == AuthType.PKCE:
return PKCEAuthenticator(cfg.endpoint, cfg_store, scopes=cfg.scopes, verify=verify, session=session)
elif cfg_auth == AuthType.BASIC or cfg_auth == AuthType.CLIENT_CREDENTIALS or cfg_auth == AuthType.CLIENTSECRET:
Expand Down
58 changes: 50 additions & 8 deletions flytekit/clients/grpc_utils/auth_interceptor.py
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(
Expand All @@ -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:
"""
Expand Down Expand Up @@ -64,9 +68,10 @@ def intercept_unary_unary(
if not hasattr(e, "code"):
raise e
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)
Copy link
Member

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...

# self._authenticator.refresh_credentials()
# updated_call_details = self._call_details_with_auth_metadata(client_call_details)
# return continuation(updated_call_details, request)
return fut

def intercept_unary_stream(self, continuation, client_call_details, request):
Expand All @@ -76,7 +81,44 @@ 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)
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)
# self._authenticator.refresh_credentials()
# updated_call_details = self._call_details_with_auth_metadata(client_call_details)
# return continuation(updated_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)
Copy link
Member

Choose a reason for hiding this comment

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

should we move import under try: ...?
why this work for Newton?
this shouldn't work.


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)
3 changes: 2 additions & 1 deletion flytekit/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ class AuthType(enum.Enum):
PKCE = "Pkce"
EXTERNALCOMMAND = "ExternalCommand"
DEVICEFLOW = "DeviceFlow"
NO_AUTH = "no_auth"


@dataclass(init=True, repr=True, eq=True, frozen=True)
Expand Down Expand Up @@ -749,7 +750,7 @@ def for_sandbox(cls) -> Config:
:return: Config
"""
return Config(
platform=PlatformConfig(endpoint="localhost:30080", auth_mode="Pkce", insecure=True),
platform=PlatformConfig(endpoint="localhost:30080", auth_mode="no_auth", insecure=True),
data_config=DataConfig(
s3=S3Config(endpoint="http://localhost:30002", access_key_id="minio", secret_access_key="miniostorage")
),
Expand Down
Loading