Skip to content

Commit

Permalink
refactor(framework) Update credentials file name format (#4717)
Browse files Browse the repository at this point in the history
  • Loading branch information
panh99 authored Dec 16, 2024
1 parent c312858 commit e1408f8
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/py/flwr/cli/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def _log_with_exec_api(
run_id: int,
stream: bool,
) -> None:
auth_plugin = try_obtain_cli_auth_plugin(app, federation, federation_config)
auth_plugin = try_obtain_cli_auth_plugin(app, federation)
channel = init_channel(app, federation_config, auth_plugin)

if stream:
Expand Down
4 changes: 1 addition & 3 deletions src/py/flwr/cli/login/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ def login( # pylint: disable=R0914

# 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
)
auth_plugin = try_obtain_cli_auth_plugin(app, federation, auth_type)
if auth_plugin is None:
typer.secho(
f'❌ Authentication type "{auth_type}" not found',
Expand Down
2 changes: 1 addition & 1 deletion src/py/flwr/cli/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def ls( # pylint: disable=too-many-locals, too-many-branches
raise ValueError(
"The options '--runs' and '--run-id' are mutually exclusive."
)
auth_plugin = try_obtain_cli_auth_plugin(app, federation, federation_config)
auth_plugin = try_obtain_cli_auth_plugin(app, federation)
channel = init_channel(app, federation_config, auth_plugin)
stub = ExecStub(channel)

Expand Down
2 changes: 1 addition & 1 deletion src/py/flwr/cli/run/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def _run_with_exec_api(
stream: bool,
output_format: str,
) -> None:
auth_plugin = try_obtain_cli_auth_plugin(app, federation, federation_config)
auth_plugin = try_obtain_cli_auth_plugin(app, federation)
channel = init_channel(app, federation_config, auth_plugin)
stub = ExecStub(channel)

Expand Down
2 changes: 1 addition & 1 deletion src/py/flwr/cli/stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def stop(
exit_if_no_address(federation_config, "stop")

try:
auth_plugin = try_obtain_cli_auth_plugin(app, federation, federation_config)
auth_plugin = try_obtain_cli_auth_plugin(app, federation)
channel = init_channel(app, federation_config, auth_plugin)
stub = ExecStub(channel) # pylint: disable=unused-variable # noqa: F841

Expand Down
19 changes: 3 additions & 16 deletions src/py/flwr/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import typer

from flwr.cli.cli_user_auth_interceptor import CliUserAuthInterceptor
from flwr.common.address import parse_address
from flwr.common.auth_plugin import CliAuthPlugin
from flwr.common.constant import AUTH_TYPE, CREDENTIALS_DIR, FLWR_DIR
from flwr.common.grpc import GRPC_MAX_MESSAGE_LENGTH, create_channel
Expand Down Expand Up @@ -159,33 +158,21 @@ def get_sha256_hash(file_path: Path) -> str:
return sha256.hexdigest()


def get_user_auth_config_path(
root_dir: Path, federation: str, server_address: str
) -> Path:
def get_user_auth_config_path(root_dir: Path, federation: str) -> Path:
"""Return the path to the user auth config file."""
# Parse the server address
parsed_addr = parse_address(server_address)
if parsed_addr is None:
raise ValueError(f"Invalid server address: {server_address}")
host, port, is_v6 = parsed_addr
formatted_addr = f"[{host}]_{port}" if is_v6 else f"{host}_{port}"

# Locate the credentials directory
credentials_dir = root_dir.absolute() / FLWR_DIR / CREDENTIALS_DIR
credentials_dir.mkdir(parents=True, exist_ok=True)
return credentials_dir / f"{federation}_{formatted_addr}.json"
return credentials_dir / f"{federation}.json"


def try_obtain_cli_auth_plugin(
root_dir: Path,
federation: str,
federation_config: dict[str, Any],
auth_type: Optional[str] = None,
) -> Optional[CliAuthPlugin]:
"""Load the CLI-side user auth plugin for the given auth type."""
config_path = get_user_auth_config_path(
root_dir, federation, federation_config["address"]
)
config_path = get_user_auth_config_path(root_dir, federation)

# Load the config file if it exists
config: dict[str, Any] = {}
Expand Down

0 comments on commit e1408f8

Please sign in to comment.