flwr 1.13.0__py3-none-any.whl → 1.14.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. flwr/cli/app.py +5 -0
  2. flwr/cli/build.py +1 -37
  3. flwr/cli/cli_user_auth_interceptor.py +86 -0
  4. flwr/cli/config_utils.py +19 -2
  5. flwr/cli/example.py +1 -0
  6. flwr/cli/install.py +2 -19
  7. flwr/cli/log.py +18 -36
  8. flwr/cli/login/__init__.py +22 -0
  9. flwr/cli/login/login.py +81 -0
  10. flwr/cli/ls.py +205 -106
  11. flwr/cli/new/__init__.py +1 -0
  12. flwr/cli/new/new.py +25 -14
  13. flwr/cli/new/templates/app/.gitignore.tpl +3 -0
  14. flwr/cli/new/templates/app/pyproject.baseline.toml.tpl +1 -1
  15. flwr/cli/new/templates/app/pyproject.flowertune.toml.tpl +3 -3
  16. flwr/cli/new/templates/app/pyproject.huggingface.toml.tpl +1 -1
  17. flwr/cli/new/templates/app/pyproject.jax.toml.tpl +1 -1
  18. flwr/cli/new/templates/app/pyproject.mlx.toml.tpl +2 -3
  19. flwr/cli/new/templates/app/pyproject.numpy.toml.tpl +1 -1
  20. flwr/cli/new/templates/app/pyproject.pytorch.toml.tpl +1 -1
  21. flwr/cli/new/templates/app/pyproject.sklearn.toml.tpl +1 -1
  22. flwr/cli/new/templates/app/pyproject.tensorflow.toml.tpl +1 -1
  23. flwr/cli/run/__init__.py +1 -0
  24. flwr/cli/run/run.py +89 -39
  25. flwr/cli/stop.py +130 -0
  26. flwr/cli/utils.py +172 -8
  27. flwr/client/app.py +14 -3
  28. flwr/client/client.py +1 -32
  29. flwr/client/clientapp/app.py +4 -8
  30. flwr/client/clientapp/utils.py +1 -0
  31. flwr/client/grpc_adapter_client/connection.py +1 -1
  32. flwr/client/grpc_client/connection.py +1 -1
  33. flwr/client/grpc_rere_client/connection.py +13 -7
  34. flwr/client/message_handler/message_handler.py +1 -2
  35. flwr/client/mod/comms_mods.py +1 -0
  36. flwr/client/mod/localdp_mod.py +1 -1
  37. flwr/client/nodestate/__init__.py +1 -0
  38. flwr/client/nodestate/nodestate.py +1 -0
  39. flwr/client/nodestate/nodestate_factory.py +1 -0
  40. flwr/client/numpy_client.py +0 -44
  41. flwr/client/rest_client/connection.py +3 -3
  42. flwr/client/supernode/app.py +2 -2
  43. flwr/common/address.py +1 -0
  44. flwr/common/args.py +1 -0
  45. flwr/common/auth_plugin/__init__.py +24 -0
  46. flwr/common/auth_plugin/auth_plugin.py +111 -0
  47. flwr/common/config.py +3 -1
  48. flwr/common/constant.py +17 -1
  49. flwr/common/logger.py +40 -0
  50. flwr/common/message.py +1 -0
  51. flwr/common/object_ref.py +57 -54
  52. flwr/common/pyproject.py +1 -0
  53. flwr/common/record/__init__.py +1 -0
  54. flwr/common/record/parametersrecord.py +1 -0
  55. flwr/common/retry_invoker.py +77 -0
  56. flwr/common/secure_aggregation/secaggplus_utils.py +2 -2
  57. flwr/common/telemetry.py +15 -4
  58. flwr/common/typing.py +12 -0
  59. flwr/common/version.py +1 -0
  60. flwr/proto/exec_pb2.py +38 -14
  61. flwr/proto/exec_pb2.pyi +107 -2
  62. flwr/proto/exec_pb2_grpc.py +102 -0
  63. flwr/proto/exec_pb2_grpc.pyi +39 -0
  64. flwr/proto/fab_pb2.py +4 -4
  65. flwr/proto/fab_pb2.pyi +4 -1
  66. flwr/proto/serverappio_pb2.py +18 -18
  67. flwr/proto/serverappio_pb2.pyi +8 -2
  68. flwr/proto/serverappio_pb2_grpc.py +34 -0
  69. flwr/proto/serverappio_pb2_grpc.pyi +13 -0
  70. flwr/proto/simulationio_pb2.py +2 -2
  71. flwr/proto/simulationio_pb2_grpc.py +34 -0
  72. flwr/proto/simulationio_pb2_grpc.pyi +13 -0
  73. flwr/server/app.py +62 -7
  74. flwr/server/compat/app_utils.py +7 -1
  75. flwr/server/driver/grpc_driver.py +11 -63
  76. flwr/server/driver/inmemory_driver.py +5 -1
  77. flwr/server/run_serverapp.py +8 -9
  78. flwr/server/serverapp/app.py +25 -10
  79. flwr/server/strategy/dpfedavg_fixed.py +1 -0
  80. flwr/server/superlink/driver/serverappio_grpc.py +1 -0
  81. flwr/server/superlink/driver/serverappio_servicer.py +82 -23
  82. flwr/server/superlink/ffs/disk_ffs.py +1 -0
  83. flwr/server/superlink/fleet/grpc_adapter/grpc_adapter_servicer.py +1 -0
  84. flwr/server/superlink/fleet/grpc_bidi/flower_service_servicer.py +1 -0
  85. flwr/server/superlink/fleet/grpc_rere/fleet_servicer.py +32 -12
  86. flwr/server/superlink/fleet/grpc_rere/server_interceptor.py +12 -11
  87. flwr/server/superlink/fleet/message_handler/message_handler.py +32 -5
  88. flwr/server/superlink/fleet/rest_rere/rest_api.py +4 -1
  89. flwr/server/superlink/fleet/vce/__init__.py +1 -0
  90. flwr/server/superlink/fleet/vce/backend/__init__.py +1 -0
  91. flwr/server/superlink/fleet/vce/backend/raybackend.py +1 -0
  92. flwr/server/superlink/linkstate/in_memory_linkstate.py +21 -30
  93. flwr/server/superlink/linkstate/linkstate.py +17 -2
  94. flwr/server/superlink/linkstate/sqlite_linkstate.py +30 -49
  95. flwr/server/superlink/simulation/simulationio_servicer.py +33 -0
  96. flwr/server/superlink/utils.py +65 -0
  97. flwr/simulation/app.py +59 -52
  98. flwr/simulation/ray_transport/ray_actor.py +1 -0
  99. flwr/simulation/ray_transport/utils.py +1 -0
  100. flwr/simulation/run_simulation.py +36 -22
  101. flwr/simulation/simulationio_connection.py +3 -0
  102. flwr/superexec/app.py +1 -0
  103. flwr/superexec/deployment.py +1 -0
  104. flwr/superexec/exec_grpc.py +19 -1
  105. flwr/superexec/exec_servicer.py +76 -2
  106. flwr/superexec/exec_user_auth_interceptor.py +101 -0
  107. flwr/superexec/executor.py +1 -0
  108. {flwr-1.13.0.dist-info → flwr-1.14.0.dist-info}/METADATA +8 -8
  109. {flwr-1.13.0.dist-info → flwr-1.14.0.dist-info}/RECORD +112 -112
  110. flwr/proto/common_pb2.py +0 -36
  111. flwr/proto/common_pb2.pyi +0 -121
  112. flwr/proto/common_pb2_grpc.py +0 -4
  113. flwr/proto/common_pb2_grpc.pyi +0 -4
  114. flwr/proto/control_pb2.py +0 -27
  115. flwr/proto/control_pb2.pyi +0 -7
  116. flwr/proto/control_pb2_grpc.py +0 -135
  117. flwr/proto/control_pb2_grpc.pyi +0 -53
  118. {flwr-1.13.0.dist-info → flwr-1.14.0.dist-info}/LICENSE +0 -0
  119. {flwr-1.13.0.dist-info → flwr-1.14.0.dist-info}/WHEEL +0 -0
  120. {flwr-1.13.0.dist-info → flwr-1.14.0.dist-info}/entry_points.txt +0 -0
flwr/cli/app.py CHANGED
@@ -14,15 +14,18 @@
14
14
  # ==============================================================================
15
15
  """Flower command line interface."""
16
16
 
17
+
17
18
  import typer
18
19
  from typer.main import get_command
19
20
 
20
21
  from .build import build
21
22
  from .install import install
22
23
  from .log import log
24
+ from .login import login
23
25
  from .ls import ls
24
26
  from .new import new
25
27
  from .run import run
28
+ from .stop import stop
26
29
 
27
30
  app = typer.Typer(
28
31
  help=typer.style(
@@ -39,6 +42,8 @@ app.command()(build)
39
42
  app.command()(install)
40
43
  app.command()(log)
41
44
  app.command()(ls)
45
+ app.command()(stop)
46
+ app.command()(login)
42
47
 
43
48
  typer_click_object = get_command(app)
44
49
 
flwr/cli/build.py CHANGED
@@ -14,23 +14,20 @@
14
14
  # ==============================================================================
15
15
  """Flower command line interface `build` command."""
16
16
 
17
+
17
18
  import hashlib
18
19
  import os
19
20
  import shutil
20
21
  import tempfile
21
22
  import zipfile
22
- from logging import DEBUG, ERROR
23
23
  from pathlib import Path
24
24
  from typing import Annotated, Any, Optional, Union
25
25
 
26
26
  import pathspec
27
27
  import tomli_w
28
28
  import typer
29
- from hatchling.builders.wheel import WheelBuilder
30
- from hatchling.metadata.core import ProjectMetadata
31
29
 
32
30
  from flwr.common.constant import FAB_ALLOWED_EXTENSIONS, FAB_DATE, FAB_HASH_TRUNCATION
33
- from flwr.common.logger import log
34
31
 
35
32
  from .config_utils import load_and_validate
36
33
  from .utils import is_valid_project_name
@@ -55,27 +52,6 @@ def get_fab_filename(conf: dict[str, Any], fab_hash: str) -> str:
55
52
  return f"{publisher}.{name}.{version}.{fab_hash_truncated}.fab"
56
53
 
57
54
 
58
- def _build_app_wheel(app: Path) -> Path:
59
- """Build app as a wheel and return its path."""
60
- # Path to your project directory
61
- app_dir = str(app.resolve())
62
- try:
63
-
64
- # Initialize the WheelBuilder
65
- builder = WheelBuilder(
66
- app_dir, metadata=ProjectMetadata(root=app_dir, plugin_manager=None)
67
- )
68
-
69
- # Build
70
- whl_path = Path(next(builder.build(directory=app_dir)))
71
- log(DEBUG, "Wheel succesfully built: %s", str(whl_path))
72
- except Exception as ex:
73
- log(ERROR, "Exception encountered when building wheel.", exc_info=ex)
74
- raise typer.Exit(code=1) from ex
75
-
76
- return whl_path
77
-
78
-
79
55
  # pylint: disable=too-many-locals, too-many-statements
80
56
  def build(
81
57
  app: Annotated[
@@ -131,12 +107,6 @@ def build(
131
107
  bold=True,
132
108
  )
133
109
 
134
- # Build wheel
135
- whl_path = _build_app_wheel(app)
136
-
137
- # Add path to .whl to `[tool.flwr.app]`
138
- conf["tool"]["flwr"]["app"]["whl"] = str(whl_path.name)
139
-
140
110
  # Load .gitignore rules if present
141
111
  ignore_spec = _load_gitignore(app)
142
112
 
@@ -168,9 +138,6 @@ def build(
168
138
  and f.name != "pyproject.toml" # Exclude the original pyproject.toml
169
139
  ]
170
140
 
171
- # Include FAB .whl
172
- all_files.append(whl_path)
173
-
174
141
  for file_path in all_files:
175
142
  # Read the file content manually
176
143
  with open(file_path, "rb") as f:
@@ -187,9 +154,6 @@ def build(
187
154
  # Add CONTENT and CONTENT.jwt to the zip file
188
155
  write_to_zip(fab_file, ".info/CONTENT", list_file_content)
189
156
 
190
- # Erase FAB .whl in app directory
191
- whl_path.unlink()
192
-
193
157
  # Get hash of FAB file
194
158
  content = Path(temp_filename).read_bytes()
195
159
  fab_hash = hashlib.sha256(content).hexdigest()
@@ -0,0 +1,86 @@
1
+ # Copyright 2024 Flower Labs GmbH. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # ==============================================================================
15
+ """Flower run interceptor."""
16
+
17
+
18
+ from typing import Any, Callable, Union
19
+
20
+ import grpc
21
+
22
+ from flwr.common.auth_plugin import CliAuthPlugin
23
+ from flwr.proto.exec_pb2 import ( # pylint: disable=E0611
24
+ StartRunRequest,
25
+ StreamLogsRequest,
26
+ )
27
+
28
+ Request = Union[
29
+ StartRunRequest,
30
+ StreamLogsRequest,
31
+ ]
32
+
33
+
34
+ class CliUserAuthInterceptor(
35
+ grpc.UnaryUnaryClientInterceptor, grpc.UnaryStreamClientInterceptor # type: ignore
36
+ ):
37
+ """CLI interceptor for user authentication."""
38
+
39
+ def __init__(self, auth_plugin: CliAuthPlugin):
40
+ self.auth_plugin = auth_plugin
41
+
42
+ def _authenticated_call(
43
+ self,
44
+ continuation: Callable[[Any, Any], Any],
45
+ client_call_details: grpc.ClientCallDetails,
46
+ request: Request,
47
+ ) -> grpc.Call:
48
+ """Send and receive tokens via metadata."""
49
+ new_metadata = self.auth_plugin.write_tokens_to_metadata(
50
+ client_call_details.metadata or []
51
+ )
52
+
53
+ details = client_call_details._replace(metadata=new_metadata)
54
+
55
+ response = continuation(details, request)
56
+ if response.initial_metadata():
57
+ retrieved_metadata = dict(response.initial_metadata())
58
+ self.auth_plugin.store_tokens(retrieved_metadata)
59
+
60
+ return response
61
+
62
+ def intercept_unary_unary(
63
+ self,
64
+ continuation: Callable[[Any, Any], Any],
65
+ client_call_details: grpc.ClientCallDetails,
66
+ request: Request,
67
+ ) -> grpc.Call:
68
+ """Intercept a unary-unary call for user authentication.
69
+
70
+ This method intercepts a unary-unary RPC call initiated from the CLI and adds
71
+ the required authentication tokens to the RPC metadata.
72
+ """
73
+ return self._authenticated_call(continuation, client_call_details, request)
74
+
75
+ def intercept_unary_stream(
76
+ self,
77
+ continuation: Callable[[Any, Any], Any],
78
+ client_call_details: grpc.ClientCallDetails,
79
+ request: Request,
80
+ ) -> grpc.Call:
81
+ """Intercept a unary-stream call for user authentication.
82
+
83
+ This method intercepts a unary-stream RPC call initiated from the CLI and adds
84
+ the required authentication tokens to the RPC metadata.
85
+ """
86
+ return self._authenticated_call(continuation, client_call_details, request)
flwr/cli/config_utils.py CHANGED
@@ -14,6 +14,7 @@
14
14
  # ==============================================================================
15
15
  """Utility to validate the `pyproject.toml` file."""
16
16
 
17
+
17
18
  import zipfile
18
19
  from io import BytesIO
19
20
  from pathlib import Path
@@ -230,10 +231,14 @@ def load_from_string(toml_content: str) -> Optional[dict[str, Any]]:
230
231
  return None
231
232
 
232
233
 
233
- def validate_project_config(
234
+ def process_loaded_project_config(
234
235
  config: Union[dict[str, Any], None], errors: list[str], warnings: list[str]
235
236
  ) -> dict[str, Any]:
236
- """Validate and return the Flower project configuration."""
237
+ """Process and return the loaded project configuration.
238
+
239
+ This function handles errors and warnings from the `load_and_validate` function,
240
+ exits on critical issues, and returns the validated configuration.
241
+ """
237
242
  if config is None:
238
243
  typer.secho(
239
244
  "Project configuration could not be loaded.\n"
@@ -324,3 +329,15 @@ def validate_certificate_in_federation_config(
324
329
  raise typer.Exit(code=1)
325
330
 
326
331
  return insecure, root_certificates_bytes
332
+
333
+
334
+ def exit_if_no_address(federation_config: dict[str, Any], cmd: str) -> None:
335
+ """Exit if the provided federation_config has no "address" key."""
336
+ if "address" not in federation_config:
337
+ typer.secho(
338
+ f"❌ `flwr {cmd}` currently works with a SuperLink. Ensure that the correct"
339
+ "SuperLink (Exec API) address is provided in `pyproject.toml`.",
340
+ fg=typer.colors.RED,
341
+ bold=True,
342
+ )
343
+ raise typer.Exit(code=1)
flwr/cli/example.py CHANGED
@@ -14,6 +14,7 @@
14
14
  # ==============================================================================
15
15
  """Flower command line interface `example` command."""
16
16
 
17
+
17
18
  import json
18
19
  import os
19
20
  import subprocess
flwr/cli/install.py CHANGED
@@ -14,9 +14,9 @@
14
14
  # ==============================================================================
15
15
  """Flower command line interface `install` command."""
16
16
 
17
+
17
18
  import hashlib
18
19
  import shutil
19
- import subprocess
20
20
  import tempfile
21
21
  import zipfile
22
22
  from io import BytesIO
@@ -188,25 +188,8 @@ def validate_and_install(
188
188
  else:
189
189
  shutil.copy2(item, install_dir / item.name)
190
190
 
191
- whl_file = config["tool"]["flwr"]["app"]["whl"]
192
- install_whl = install_dir / whl_file
193
- try:
194
- subprocess.run(
195
- ["pip", "install", "--no-deps", install_whl],
196
- capture_output=True,
197
- text=True,
198
- check=True,
199
- )
200
- except subprocess.CalledProcessError as e:
201
- typer.secho(
202
- f"❌ Failed to install {project_name}:\n{e.stderr}",
203
- fg=typer.colors.RED,
204
- bold=True,
205
- )
206
- raise typer.Exit(code=1) from e
207
-
208
191
  typer.secho(
209
- f"🎊 Successfully installed {project_name}.",
192
+ f"🎊 Successfully installed {project_name} to {install_dir}.",
210
193
  fg=typer.colors.GREEN,
211
194
  bold=True,
212
195
  )
flwr/cli/log.py CHANGED
@@ -14,6 +14,7 @@
14
14
  # ==============================================================================
15
15
  """Flower command line interface `log` command."""
16
16
 
17
+
17
18
  import time
18
19
  from logging import DEBUG, ERROR, INFO
19
20
  from pathlib import Path
@@ -23,17 +24,18 @@ import grpc
23
24
  import typer
24
25
 
25
26
  from flwr.cli.config_utils import (
27
+ exit_if_no_address,
26
28
  load_and_validate,
27
- validate_certificate_in_federation_config,
29
+ process_loaded_project_config,
28
30
  validate_federation_in_project_config,
29
- validate_project_config,
30
31
  )
31
32
  from flwr.common.constant import CONN_RECONNECT_INTERVAL, CONN_REFRESH_PERIOD
32
- from flwr.common.grpc import GRPC_MAX_MESSAGE_LENGTH, create_channel
33
33
  from flwr.common.logger import log as logger
34
34
  from flwr.proto.exec_pb2 import StreamLogsRequest # pylint: disable=E0611
35
35
  from flwr.proto.exec_pb2_grpc import ExecStub
36
36
 
37
+ from .utils import init_channel, try_obtain_cli_auth_plugin, unauthenticated_exc_handler
38
+
37
39
 
38
40
  def start_stream(
39
41
  run_id: int, channel: grpc.Channel, refresh_period: int = CONN_REFRESH_PERIOD
@@ -86,8 +88,9 @@ def stream_logs(
86
88
  latest_timestamp = 0.0
87
89
  res = None
88
90
  try:
89
- for res in stub.StreamLogs(req, timeout=duration):
90
- print(res.log_output, end="")
91
+ with unauthenticated_exc_handler():
92
+ for res in stub.StreamLogs(req, timeout=duration):
93
+ print(res.log_output, end="")
91
94
  except grpc.RpcError as e:
92
95
  # pylint: disable=E1101
93
96
  if e.code() != grpc.StatusCode.DEADLINE_EXCEEDED:
@@ -107,9 +110,10 @@ def print_logs(run_id: int, channel: grpc.Channel, timeout: int) -> None:
107
110
  try:
108
111
  while True:
109
112
  try:
110
- # Enforce timeout for graceful exit
111
- for res in stub.StreamLogs(req, timeout=timeout):
112
- print(res.log_output)
113
+ with unauthenticated_exc_handler():
114
+ # Enforce timeout for graceful exit
115
+ for res in stub.StreamLogs(req, timeout=timeout):
116
+ print(res.log_output)
113
117
  except grpc.RpcError as e:
114
118
  # pylint: disable=E1101
115
119
  if e.code() == grpc.StatusCode.DEADLINE_EXCEEDED:
@@ -126,11 +130,6 @@ def print_logs(run_id: int, channel: grpc.Channel, timeout: int) -> None:
126
130
  logger(DEBUG, "Channel closed")
127
131
 
128
132
 
129
- def on_channel_state_change(channel_connectivity: str) -> None:
130
- """Log channel connectivity."""
131
- logger(DEBUG, channel_connectivity)
132
-
133
-
134
133
  def log(
135
134
  run_id: Annotated[
136
135
  int,
@@ -157,41 +156,24 @@ def log(
157
156
 
158
157
  pyproject_path = app / "pyproject.toml" if app else None
159
158
  config, errors, warnings = load_and_validate(path=pyproject_path)
160
- config = validate_project_config(config, errors, warnings)
159
+ config = process_loaded_project_config(config, errors, warnings)
161
160
  federation, federation_config = validate_federation_in_project_config(
162
161
  federation, config
163
162
  )
163
+ exit_if_no_address(federation_config, "log")
164
164
 
165
- if "address" not in federation_config:
166
- typer.secho(
167
- "❌ `flwr log` currently works with Exec API. Ensure that the correct"
168
- "Exec API address is provided in the `pyproject.toml`.",
169
- fg=typer.colors.RED,
170
- bold=True,
171
- )
172
- raise typer.Exit(code=1)
173
-
174
- _log_with_exec_api(app, federation_config, run_id, stream)
165
+ _log_with_exec_api(app, federation, federation_config, run_id, stream)
175
166
 
176
167
 
177
168
  def _log_with_exec_api(
178
169
  app: Path,
170
+ federation: str,
179
171
  federation_config: dict[str, Any],
180
172
  run_id: int,
181
173
  stream: bool,
182
174
  ) -> None:
183
-
184
- insecure, root_certificates_bytes = validate_certificate_in_federation_config(
185
- app, federation_config
186
- )
187
- channel = create_channel(
188
- server_address=federation_config["address"],
189
- insecure=insecure,
190
- root_certificates=root_certificates_bytes,
191
- max_message_length=GRPC_MAX_MESSAGE_LENGTH,
192
- interceptors=None,
193
- )
194
- channel.subscribe(on_channel_state_change)
175
+ auth_plugin = try_obtain_cli_auth_plugin(app, federation)
176
+ channel = init_channel(app, federation_config, auth_plugin)
195
177
 
196
178
  if stream:
197
179
  start_stream(run_id, channel, CONN_REFRESH_PERIOD)
@@ -0,0 +1,22 @@
1
+ # Copyright 2024 Flower Labs GmbH. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # ==============================================================================
15
+ """Flower command line interface `login` command."""
16
+
17
+
18
+ from .login import login as login
19
+
20
+ __all__ = [
21
+ "login",
22
+ ]
@@ -0,0 +1,81 @@
1
+ # Copyright 2024 Flower Labs GmbH. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # ==============================================================================
15
+ """Flower command line interface `login` command."""
16
+
17
+
18
+ from pathlib import Path
19
+ from typing import Annotated, Optional
20
+
21
+ import typer
22
+
23
+ from flwr.cli.config_utils import (
24
+ exit_if_no_address,
25
+ load_and_validate,
26
+ process_loaded_project_config,
27
+ validate_federation_in_project_config,
28
+ )
29
+ from flwr.common.constant import AUTH_TYPE
30
+ from flwr.proto.exec_pb2 import ( # pylint: disable=E0611
31
+ GetLoginDetailsRequest,
32
+ GetLoginDetailsResponse,
33
+ )
34
+ from flwr.proto.exec_pb2_grpc import ExecStub
35
+
36
+ from ..utils import init_channel, try_obtain_cli_auth_plugin
37
+
38
+
39
+ def login( # pylint: disable=R0914
40
+ app: Annotated[
41
+ Path,
42
+ typer.Argument(help="Path of the Flower App to run."),
43
+ ] = Path("."),
44
+ federation: Annotated[
45
+ Optional[str],
46
+ typer.Argument(help="Name of the federation to login into."),
47
+ ] = None,
48
+ ) -> None:
49
+ """Login to Flower SuperLink."""
50
+ typer.secho("Loading project configuration... ", fg=typer.colors.BLUE)
51
+
52
+ pyproject_path = app / "pyproject.toml" if app else None
53
+ config, errors, warnings = load_and_validate(path=pyproject_path)
54
+
55
+ config = process_loaded_project_config(config, errors, warnings)
56
+ federation, federation_config = validate_federation_in_project_config(
57
+ federation, config
58
+ )
59
+ exit_if_no_address(federation_config, "login")
60
+ channel = init_channel(app, federation_config, None)
61
+ stub = ExecStub(channel)
62
+
63
+ login_request = GetLoginDetailsRequest()
64
+ login_response: GetLoginDetailsResponse = stub.GetLoginDetails(login_request)
65
+
66
+ # Get the auth plugin
67
+ auth_type = login_response.login_details.get(AUTH_TYPE)
68
+ auth_plugin = try_obtain_cli_auth_plugin(app, federation, auth_type)
69
+ if auth_plugin is None:
70
+ typer.secho(
71
+ f'❌ Authentication type "{auth_type}" not found',
72
+ fg=typer.colors.RED,
73
+ bold=True,
74
+ )
75
+ raise typer.Exit(code=1)
76
+
77
+ # Login
78
+ auth_config = auth_plugin.login(dict(login_response.login_details), stub)
79
+
80
+ # Store the tokens
81
+ auth_plugin.store_tokens(auth_config)