-
Notifications
You must be signed in to change notification settings - Fork 898
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(framework) Add user auth CLI integration (#4602)
Co-authored-by: Heng Pan <[email protected]> Co-authored-by: Chong Shen Ng <[email protected]> Co-authored-by: Daniel J. Beutel <[email protected]>
- Loading branch information
1 parent
1daee43
commit 59809ec
Showing
13 changed files
with
345 additions
and
104 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Copyright 2024 Flower Labs GmbH. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
"""Flower run interceptor.""" | ||
|
||
|
||
from typing import Any, Callable, Union | ||
|
||
import grpc | ||
|
||
from flwr.common.auth_plugin import CliAuthPlugin | ||
from flwr.proto.exec_pb2 import ( # pylint: disable=E0611 | ||
StartRunRequest, | ||
StreamLogsRequest, | ||
) | ||
|
||
Request = Union[ | ||
StartRunRequest, | ||
StreamLogsRequest, | ||
] | ||
|
||
|
||
class CliUserAuthInterceptor( | ||
grpc.UnaryUnaryClientInterceptor, grpc.UnaryStreamClientInterceptor # type: ignore | ||
): | ||
"""CLI interceptor for user authentication.""" | ||
|
||
def __init__(self, auth_plugin: CliAuthPlugin): | ||
self.auth_plugin = auth_plugin | ||
|
||
def _authenticated_call( | ||
self, | ||
continuation: Callable[[Any, Any], Any], | ||
client_call_details: grpc.ClientCallDetails, | ||
request: Request, | ||
) -> grpc.Call: | ||
"""Send and receive tokens via metadata.""" | ||
new_metadata = self.auth_plugin.write_tokens_to_metadata( | ||
client_call_details.metadata or [] | ||
) | ||
|
||
details = client_call_details._replace(metadata=new_metadata) | ||
|
||
response = continuation(details, request) | ||
if response.initial_metadata(): | ||
retrieved_metadata = dict(response.initial_metadata()) | ||
self.auth_plugin.store_tokens(retrieved_metadata) | ||
|
||
return response | ||
|
||
def intercept_unary_unary( | ||
self, | ||
continuation: Callable[[Any, Any], Any], | ||
client_call_details: grpc.ClientCallDetails, | ||
request: Request, | ||
) -> grpc.Call: | ||
"""Intercept a unary-unary call for user authentication. | ||
This method intercepts a unary-unary RPC call initiated from the CLI and adds | ||
the required authentication tokens to the RPC metadata. | ||
""" | ||
return self._authenticated_call(continuation, client_call_details, request) | ||
|
||
def intercept_unary_stream( | ||
self, | ||
continuation: Callable[[Any, Any], Any], | ||
client_call_details: grpc.ClientCallDetails, | ||
request: Request, | ||
) -> grpc.Call: | ||
"""Intercept a unary-stream call for user authentication. | ||
This method intercepts a unary-stream RPC call initiated from the CLI and adds | ||
the required authentication tokens to the RPC metadata. | ||
""" | ||
return self._authenticated_call(continuation, client_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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright 2024 Flower Labs GmbH. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
"""Flower command line interface `login` command.""" | ||
|
||
from .login import login as login | ||
|
||
__all__ = [ | ||
"login", | ||
] |
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Copyright 2024 Flower Labs GmbH. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
"""Flower command line interface `login` command.""" | ||
|
||
from pathlib import Path | ||
from typing import Annotated, Optional | ||
|
||
import typer | ||
|
||
from flwr.cli.config_utils import ( | ||
load_and_validate, | ||
validate_federation_in_project_config, | ||
validate_project_config, | ||
) | ||
from flwr.common.constant import AUTH_TYPE | ||
from flwr.proto.exec_pb2 import ( # pylint: disable=E0611 | ||
GetLoginDetailsRequest, | ||
GetLoginDetailsResponse, | ||
) | ||
from flwr.proto.exec_pb2_grpc import ExecStub | ||
|
||
from ..utils import init_channel, try_obtain_cli_auth_plugin | ||
|
||
|
||
def login( # pylint: disable=R0914 | ||
app: Annotated[ | ||
Path, | ||
typer.Argument(help="Path of the Flower App to run."), | ||
] = Path("."), | ||
federation: Annotated[ | ||
Optional[str], | ||
typer.Argument(help="Name of the federation to login into."), | ||
] = None, | ||
) -> None: | ||
"""Login to Flower SuperLink.""" | ||
typer.secho("Loading project configuration... ", fg=typer.colors.BLUE) | ||
|
||
pyproject_path = app / "pyproject.toml" if app else None | ||
config, errors, warnings = load_and_validate(path=pyproject_path) | ||
|
||
config = validate_project_config(config, errors, warnings) | ||
federation, federation_config = validate_federation_in_project_config( | ||
federation, config | ||
) | ||
|
||
if "address" not in federation_config: | ||
typer.secho( | ||
"❌ `flwr login` currently works with a SuperLink. Ensure that the correct" | ||
"SuperLink (Exec API) address is provided in `pyproject.toml`.", | ||
fg=typer.colors.RED, | ||
bold=True, | ||
) | ||
raise typer.Exit(code=1) | ||
|
||
channel = init_channel(app, federation_config, None) | ||
stub = ExecStub(channel) | ||
|
||
login_request = GetLoginDetailsRequest() | ||
login_response: GetLoginDetailsResponse = stub.GetLoginDetails(login_request) | ||
|
||
# Get the auth plugin | ||
auth_type = login_response.login_details.get(AUTH_TYPE) | ||
auth_plugin = try_obtain_cli_auth_plugin( | ||
app, federation, federation_config, auth_type | ||
) | ||
if auth_plugin is None: | ||
typer.secho( | ||
f'❌ Authentication type "{auth_type}" not found', | ||
fg=typer.colors.RED, | ||
bold=True, | ||
) | ||
raise typer.Exit(code=1) | ||
|
||
# Login | ||
auth_config = auth_plugin.login(dict(login_response.login_details), stub) | ||
|
||
# Store the tokens | ||
auth_plugin.store_tokens(auth_config) |
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.