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 the onboarding state to account for zenml login #3270

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
24 changes: 22 additions & 2 deletions src/zenml/zen_server/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@
LOGIN,
VERSION_1,
)
from zenml.enums import AuthScheme, ExecutionStatus, OAuthDeviceStatus
from zenml.enums import (
AuthScheme,
ExecutionStatus,
OAuthDeviceStatus,
OnboardingStep,
)
from zenml.exceptions import (
AuthorizationException,
CredentialsNotValid,
Expand Down Expand Up @@ -630,12 +635,15 @@ def authenticate_device(client_id: UUID, device_code: str) -> AuthContext:
return AuthContext(user=device_model.user, device=device_model)


def authenticate_external_user(external_access_token: str) -> AuthContext:
def authenticate_external_user(
external_access_token: str, request: Request
) -> AuthContext:
"""Implement external authentication.

Args:
external_access_token: The access token used to authenticate the user
to the external authenticator.
request: The request object.

Returns:
The authentication context reflecting the authenticated user.
Expand Down Expand Up @@ -761,6 +769,18 @@ def authenticate_external_user(external_access_token: str) -> AuthContext:
)
context.alias(user_id=external_user.id, previous_id=user.id)

# This is the best spot to update the onboarding state to mark the
# "zenml login" step as completed for ZenML Pro servers, because the
# user has just successfully logged in. However, we need to differentiate
# between web clients (i.e. the dashboard) and CLI clients (i.e. the
# zenml CLI).
user_agent = request.headers.get("User-Agent", "").lower()
logger.info(f"User agent: {user_agent}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be a debug log instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was merely a left-over debug log. Removed now.

if "zenml/" in user_agent:
store.update_onboarding_state(
completed_steps={OnboardingStep.DEVICE_VERIFIED}
)

return AuthContext(user=user)


Expand Down
3 changes: 2 additions & 1 deletion src/zenml/zen_server/routers/auth_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ def token(
return OAuthRedirectResponse(authorization_url=authorization_url)

auth_context = authenticate_external_user(
external_access_token=external_access_token
external_access_token=external_access_token,
request=request,
)

else:
Expand Down
12 changes: 11 additions & 1 deletion src/zenml/zen_stores/rest_zen_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -4053,7 +4053,12 @@ def get_or_generate_api_token(self) -> str:
)

data: Optional[Dict[str, str]] = None
headers: Dict[str, str] = {}

# Use a custom user agent to identify the ZenML client in the server
# logs.
headers: Dict[str, str] = {
"User-Agent": "zenml/" + zenml.__version__,
}

# Check if an API key is configured
api_key = credentials_store.get_api_key(self.url)
Expand Down Expand Up @@ -4218,6 +4223,11 @@ def session(self) -> requests.Session:
self._session.mount("https://", HTTPAdapter(max_retries=retries))
self._session.mount("http://", HTTPAdapter(max_retries=retries))
self._session.verify = self.config.verify_ssl
# Use a custom user agent to identify the ZenML client in the server
# logs.
self._session.headers.update(
{"User-Agent": "zenml/" + zenml.__version__}
)

# Note that we return an unauthenticated session here. An API token
# is only fetched and set in the authorization header when and if it is
Expand Down
Loading