flwr-nightly 1.9.0.dev20240425__py3-none-any.whl → 1.9.0.dev20240429__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.

Potentially problematic release.


This version of flwr-nightly might be problematic. Click here for more details.

flwr/client/__init__.py CHANGED
@@ -15,12 +15,12 @@
15
15
  """Flower client."""
16
16
 
17
17
 
18
- from .app import run_client_app as run_client_app
19
18
  from .app import start_client as start_client
20
19
  from .app import start_numpy_client as start_numpy_client
21
20
  from .client import Client as Client
22
21
  from .client_app import ClientApp as ClientApp
23
22
  from .numpy_client import NumPyClient as NumPyClient
23
+ from .supernode import run_client_app as run_client_app
24
24
  from .supernode import run_supernode as run_supernode
25
25
  from .typing import ClientFn as ClientFn
26
26
 
flwr/client/app.py CHANGED
@@ -14,13 +14,12 @@
14
14
  # ==============================================================================
15
15
  """Flower client app."""
16
16
 
17
- import argparse
18
17
  import sys
19
18
  import time
20
19
  from logging import DEBUG, ERROR, INFO, WARN
21
- from pathlib import Path
22
20
  from typing import Callable, ContextManager, Optional, Tuple, Type, Union
23
21
 
22
+ from cryptography.hazmat.primitives.asymmetric import ec
24
23
  from grpc import RpcError
25
24
 
26
25
  from flwr.client.client import Client
@@ -36,10 +35,8 @@ from flwr.common.constant import (
36
35
  TRANSPORT_TYPES,
37
36
  ErrorCode,
38
37
  )
39
- from flwr.common.exit_handlers import register_exit_handlers
40
38
  from flwr.common.logger import log, warn_deprecated_feature
41
39
  from flwr.common.message import Error
42
- from flwr.common.object_ref import load_app, validate
43
40
  from flwr.common.retry_invoker import RetryInvoker, exponential
44
41
 
45
42
  from .grpc_client.connection import grpc_connection
@@ -47,94 +44,6 @@ from .grpc_rere_client.connection import grpc_request_response
47
44
  from .message_handler.message_handler import handle_control_message
48
45
  from .node_state import NodeState
49
46
  from .numpy_client import NumPyClient
50
- from .supernode.app import parse_args_run_client_app
51
-
52
-
53
- def run_client_app() -> None:
54
- """Run Flower client app."""
55
- log(INFO, "Long-running Flower client starting")
56
-
57
- event(EventType.RUN_CLIENT_APP_ENTER)
58
-
59
- args = _parse_args_run_client_app().parse_args()
60
-
61
- # Obtain certificates
62
- if args.insecure:
63
- if args.root_certificates is not None:
64
- sys.exit(
65
- "Conflicting options: The '--insecure' flag disables HTTPS, "
66
- "but '--root-certificates' was also specified. Please remove "
67
- "the '--root-certificates' option when running in insecure mode, "
68
- "or omit '--insecure' to use HTTPS."
69
- )
70
- log(
71
- WARN,
72
- "Option `--insecure` was set. "
73
- "Starting insecure HTTP client connected to %s.",
74
- args.server,
75
- )
76
- root_certificates = None
77
- else:
78
- # Load the certificates if provided, or load the system certificates
79
- cert_path = args.root_certificates
80
- if cert_path is None:
81
- root_certificates = None
82
- else:
83
- root_certificates = Path(cert_path).read_bytes()
84
- log(
85
- DEBUG,
86
- "Starting secure HTTPS client connected to %s "
87
- "with the following certificates: %s.",
88
- args.server,
89
- cert_path,
90
- )
91
-
92
- log(
93
- DEBUG,
94
- "Flower will load ClientApp `%s`",
95
- getattr(args, "client-app"),
96
- )
97
-
98
- client_app_dir = args.dir
99
- if client_app_dir is not None:
100
- sys.path.insert(0, client_app_dir)
101
-
102
- app_ref: str = getattr(args, "client-app")
103
- valid, error_msg = validate(app_ref)
104
- if not valid and error_msg:
105
- raise LoadClientAppError(error_msg) from None
106
-
107
- def _load() -> ClientApp:
108
- client_app = load_app(app_ref, LoadClientAppError)
109
-
110
- if not isinstance(client_app, ClientApp):
111
- raise LoadClientAppError(
112
- f"Attribute {app_ref} is not of type {ClientApp}",
113
- ) from None
114
-
115
- return client_app
116
-
117
- _start_client_internal(
118
- server_address=args.server,
119
- load_client_app_fn=_load,
120
- transport="rest" if args.rest else "grpc-rere",
121
- root_certificates=root_certificates,
122
- insecure=args.insecure,
123
- max_retries=args.max_retries,
124
- max_wait_time=args.max_wait_time,
125
- )
126
- register_exit_handlers(event_type=EventType.RUN_CLIENT_APP_LEAVE)
127
-
128
-
129
- def _parse_args_run_client_app() -> argparse.ArgumentParser:
130
- """Parse flower-client-app command line arguments."""
131
- parser = argparse.ArgumentParser(
132
- description="Start a Flower client app",
133
- )
134
-
135
- parse_args_run_client_app(parser=parser)
136
-
137
- return parser
138
47
 
139
48
 
140
49
  def _check_actionable_client(
@@ -165,6 +74,9 @@ def start_client(
165
74
  root_certificates: Optional[Union[bytes, str]] = None,
166
75
  insecure: Optional[bool] = None,
167
76
  transport: Optional[str] = None,
77
+ authentication_keys: Optional[
78
+ Tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]
79
+ ] = None,
168
80
  max_retries: Optional[int] = None,
169
81
  max_wait_time: Optional[float] = None,
170
82
  ) -> None:
@@ -249,6 +161,7 @@ def start_client(
249
161
  root_certificates=root_certificates,
250
162
  insecure=insecure,
251
163
  transport=transport,
164
+ authentication_keys=authentication_keys,
252
165
  max_retries=max_retries,
253
166
  max_wait_time=max_wait_time,
254
167
  )
@@ -269,6 +182,9 @@ def _start_client_internal(
269
182
  root_certificates: Optional[Union[bytes, str]] = None,
270
183
  insecure: Optional[bool] = None,
271
184
  transport: Optional[str] = None,
185
+ authentication_keys: Optional[
186
+ Tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]
187
+ ] = None,
272
188
  max_retries: Optional[int] = None,
273
189
  max_wait_time: Optional[float] = None,
274
190
  ) -> None:
@@ -393,6 +309,7 @@ def _start_client_internal(
393
309
  retry_invoker,
394
310
  grpc_max_message_length,
395
311
  root_certificates,
312
+ authentication_keys,
396
313
  ) as conn:
397
314
  # pylint: disable-next=W0612
398
315
  receive, send, create_node, delete_node, get_run = conn
@@ -606,7 +523,14 @@ def start_numpy_client(
606
523
 
607
524
  def _init_connection(transport: Optional[str], server_address: str) -> Tuple[
608
525
  Callable[
609
- [str, bool, RetryInvoker, int, Union[bytes, str, None]],
526
+ [
527
+ str,
528
+ bool,
529
+ RetryInvoker,
530
+ int,
531
+ Union[bytes, str, None],
532
+ Optional[Tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]],
533
+ ],
610
534
  ContextManager[
611
535
  Tuple[
612
536
  Callable[[], Optional[Message]],
@@ -15,8 +15,10 @@
15
15
  """Flower SuperNode."""
16
16
 
17
17
 
18
+ from .app import run_client_app as run_client_app
18
19
  from .app import run_supernode as run_supernode
19
20
 
20
21
  __all__ = [
22
+ "run_client_app",
21
23
  "run_supernode",
22
24
  ]
@@ -15,11 +15,27 @@
15
15
  """Flower SuperNode."""
16
16
 
17
17
  import argparse
18
- from logging import DEBUG, INFO
18
+ import sys
19
+ from logging import DEBUG, INFO, WARN
20
+ from pathlib import Path
21
+ from typing import Callable, Optional, Tuple
19
22
 
23
+ from cryptography.hazmat.primitives.asymmetric import ec
24
+ from cryptography.hazmat.primitives.serialization import (
25
+ load_ssh_private_key,
26
+ load_ssh_public_key,
27
+ )
28
+
29
+ from flwr.client.client_app import ClientApp, LoadClientAppError
20
30
  from flwr.common import EventType, event
21
31
  from flwr.common.exit_handlers import register_exit_handlers
22
32
  from flwr.common.logger import log
33
+ from flwr.common.object_ref import load_app, validate
34
+ from flwr.common.secure_aggregation.crypto.symmetric_encryption import (
35
+ ssh_types_to_elliptic_curve,
36
+ )
37
+
38
+ from ..app import _start_client_internal
23
39
 
24
40
 
25
41
  def run_supernode() -> None:
@@ -41,6 +57,97 @@ def run_supernode() -> None:
41
57
  )
42
58
 
43
59
 
60
+ def run_client_app() -> None:
61
+ """Run Flower client app."""
62
+ log(INFO, "Long-running Flower client starting")
63
+
64
+ event(EventType.RUN_CLIENT_APP_ENTER)
65
+
66
+ args = _parse_args_run_client_app().parse_args()
67
+
68
+ root_certificates = _get_certificates(args)
69
+ log(
70
+ DEBUG,
71
+ "Flower will load ClientApp `%s`",
72
+ getattr(args, "client-app"),
73
+ )
74
+ load_fn = _get_load_client_app_fn(args)
75
+ authentication_keys = _try_setup_client_authentication(args)
76
+
77
+ _start_client_internal(
78
+ server_address=args.server,
79
+ load_client_app_fn=load_fn,
80
+ transport="rest" if args.rest else "grpc-rere",
81
+ root_certificates=root_certificates,
82
+ insecure=args.insecure,
83
+ authentication_keys=authentication_keys,
84
+ max_retries=args.max_retries,
85
+ max_wait_time=args.max_wait_time,
86
+ )
87
+ register_exit_handlers(event_type=EventType.RUN_CLIENT_APP_LEAVE)
88
+
89
+
90
+ def _get_certificates(args: argparse.Namespace) -> Optional[bytes]:
91
+ """Load certificates if specified in args."""
92
+ # Obtain certificates
93
+ if args.insecure:
94
+ if args.root_certificates is not None:
95
+ sys.exit(
96
+ "Conflicting options: The '--insecure' flag disables HTTPS, "
97
+ "but '--root-certificates' was also specified. Please remove "
98
+ "the '--root-certificates' option when running in insecure mode, "
99
+ "or omit '--insecure' to use HTTPS."
100
+ )
101
+ log(
102
+ WARN,
103
+ "Option `--insecure` was set. "
104
+ "Starting insecure HTTP client connected to %s.",
105
+ args.server,
106
+ )
107
+ root_certificates = None
108
+ else:
109
+ # Load the certificates if provided, or load the system certificates
110
+ cert_path = args.root_certificates
111
+ if cert_path is None:
112
+ root_certificates = None
113
+ else:
114
+ root_certificates = Path(cert_path).read_bytes()
115
+ log(
116
+ DEBUG,
117
+ "Starting secure HTTPS client connected to %s "
118
+ "with the following certificates: %s.",
119
+ args.server,
120
+ cert_path,
121
+ )
122
+ return root_certificates
123
+
124
+
125
+ def _get_load_client_app_fn(
126
+ args: argparse.Namespace,
127
+ ) -> Callable[[], ClientApp]:
128
+ """Get the load_client_app_fn function."""
129
+ client_app_dir = args.dir
130
+ if client_app_dir is not None:
131
+ sys.path.insert(0, client_app_dir)
132
+
133
+ app_ref: str = getattr(args, "client-app")
134
+ valid, error_msg = validate(app_ref)
135
+ if not valid and error_msg:
136
+ raise LoadClientAppError(error_msg) from None
137
+
138
+ def _load() -> ClientApp:
139
+ client_app = load_app(app_ref, LoadClientAppError)
140
+
141
+ if not isinstance(client_app, ClientApp):
142
+ raise LoadClientAppError(
143
+ f"Attribute {app_ref} is not of type {ClientApp}",
144
+ ) from None
145
+
146
+ return client_app
147
+
148
+ return _load
149
+
150
+
44
151
  def _parse_args_run_supernode() -> argparse.ArgumentParser:
45
152
  """Parse flower-supernode command line arguments."""
46
153
  parser = argparse.ArgumentParser(
@@ -57,17 +164,34 @@ def _parse_args_run_supernode() -> argparse.ArgumentParser:
57
164
  "If not provided, defaults to an empty string.",
58
165
  )
59
166
  _parse_args_common(parser)
167
+ parser.add_argument(
168
+ "--flwr-dir",
169
+ default=None,
170
+ help="""The path containing installed Flower Apps.
171
+ By default, this value isequal to:
172
+
173
+ - `$FLWR_HOME/` if `$FLWR_HOME` is defined
174
+ - `$XDG_DATA_HOME/.flwr/` if `$XDG_DATA_HOME` is defined
175
+ - `$HOME/.flwr/` in all other cases
176
+ """,
177
+ )
60
178
 
61
179
  return parser
62
180
 
63
181
 
64
- def parse_args_run_client_app(parser: argparse.ArgumentParser) -> None:
65
- """Parse command line arguments."""
182
+ def _parse_args_run_client_app() -> argparse.ArgumentParser:
183
+ """Parse flower-client-app command line arguments."""
184
+ parser = argparse.ArgumentParser(
185
+ description="Start a Flower client app",
186
+ )
187
+
66
188
  parser.add_argument(
67
189
  "client-app",
68
190
  help="For example: `client:app` or `project.package.module:wrapper.app`",
69
191
  )
70
- _parse_args_common(parser)
192
+ _parse_args_common(parser=parser)
193
+
194
+ return parser
71
195
 
72
196
 
73
197
  def _parse_args_common(parser: argparse.ArgumentParser) -> None:
@@ -117,3 +241,41 @@ def _parse_args_common(parser: argparse.ArgumentParser) -> None:
117
241
  "app from there."
118
242
  " Default: current working directory.",
119
243
  )
244
+ parser.add_argument(
245
+ "--authentication-keys",
246
+ nargs=2,
247
+ metavar=("CLIENT_PRIVATE_KEY", "CLIENT_PUBLIC_KEY"),
248
+ type=str,
249
+ help="Provide two file paths: (1) the client's private "
250
+ "key file, and (2) the client's public key file.",
251
+ )
252
+
253
+
254
+ def _try_setup_client_authentication(
255
+ args: argparse.Namespace,
256
+ ) -> Optional[Tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]]:
257
+ if not args.authentication_keys:
258
+ return None
259
+
260
+ ssh_private_key = load_ssh_private_key(
261
+ Path(args.authentication_keys[0]).read_bytes(),
262
+ None,
263
+ )
264
+ ssh_public_key = load_ssh_public_key(Path(args.authentication_keys[1]).read_bytes())
265
+
266
+ try:
267
+ client_private_key, client_public_key = ssh_types_to_elliptic_curve(
268
+ ssh_private_key, ssh_public_key
269
+ )
270
+ except TypeError:
271
+ sys.exit(
272
+ "The file paths provided could not be read as a private and public "
273
+ "key pair. Client authentication requires an elliptic curve public and "
274
+ "private key pair. Please provide the file paths containing elliptic "
275
+ "curve private and public keys to '--authentication-keys'."
276
+ )
277
+
278
+ return (
279
+ client_private_key,
280
+ client_public_key,
281
+ )
@@ -117,3 +117,18 @@ def verify_hmac(key: bytes, message: bytes, hmac_value: bytes) -> bool:
117
117
  return True
118
118
  except InvalidSignature:
119
119
  return False
120
+
121
+
122
+ def ssh_types_to_elliptic_curve(
123
+ private_key: serialization.SSHPrivateKeyTypes,
124
+ public_key: serialization.SSHPublicKeyTypes,
125
+ ) -> Tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]:
126
+ """Cast SSH key types to elliptic curve."""
127
+ if isinstance(private_key, ec.EllipticCurvePrivateKey) and isinstance(
128
+ public_key, ec.EllipticCurvePublicKey
129
+ ):
130
+ return (private_key, public_key)
131
+
132
+ raise TypeError(
133
+ "The provided key is not an EllipticCurvePrivateKey or EllipticCurvePublicKey"
134
+ )
flwr/server/app.py CHANGED
@@ -16,15 +16,21 @@
16
16
 
17
17
  import argparse
18
18
  import asyncio
19
+ import csv
19
20
  import importlib.util
20
21
  import sys
21
22
  import threading
22
23
  from logging import ERROR, INFO, WARN
23
24
  from os.path import isfile
24
25
  from pathlib import Path
25
- from typing import List, Optional, Tuple
26
+ from typing import List, Optional, Sequence, Set, Tuple
26
27
 
27
28
  import grpc
29
+ from cryptography.hazmat.primitives.asymmetric import ec
30
+ from cryptography.hazmat.primitives.serialization import (
31
+ load_ssh_private_key,
32
+ load_ssh_public_key,
33
+ )
28
34
 
29
35
  from flwr.common import GRPC_MAX_MESSAGE_LENGTH, EventType, event
30
36
  from flwr.common.address import parse_address
@@ -36,6 +42,10 @@ from flwr.common.constant import (
36
42
  )
37
43
  from flwr.common.exit_handlers import register_exit_handlers
38
44
  from flwr.common.logger import log
45
+ from flwr.common.secure_aggregation.crypto.symmetric_encryption import (
46
+ public_key_to_bytes,
47
+ ssh_types_to_elliptic_curve,
48
+ )
39
49
  from flwr.proto.fleet_pb2_grpc import ( # pylint: disable=E0611
40
50
  add_FleetServicer_to_server,
41
51
  )
@@ -51,6 +61,7 @@ from .superlink.fleet.grpc_bidi.grpc_server import (
51
61
  start_grpc_server,
52
62
  )
53
63
  from .superlink.fleet.grpc_rere.fleet_servicer import FleetServicer
64
+ from .superlink.fleet.grpc_rere.server_interceptor import AuthenticateServerInterceptor
54
65
  from .superlink.fleet.vce import start_vce
55
66
  from .superlink.state import StateFactory
56
67
 
@@ -354,10 +365,28 @@ def run_superlink() -> None:
354
365
  sys.exit(f"Fleet IP address ({address_arg}) cannot be parsed.")
355
366
  host, port, is_v6 = parsed_address
356
367
  address = f"[{host}]:{port}" if is_v6 else f"{host}:{port}"
368
+
369
+ maybe_keys = _try_setup_client_authentication(args, certificates)
370
+ interceptors: Optional[Sequence[grpc.ServerInterceptor]] = None
371
+ if maybe_keys is not None:
372
+ (
373
+ client_public_keys,
374
+ server_private_key,
375
+ server_public_key,
376
+ ) = maybe_keys
377
+ interceptors = [
378
+ AuthenticateServerInterceptor(
379
+ client_public_keys,
380
+ server_private_key,
381
+ server_public_key,
382
+ )
383
+ ]
384
+
357
385
  fleet_server = _run_fleet_api_grpc_rere(
358
386
  address=address,
359
387
  state_factory=state_factory,
360
388
  certificates=certificates,
389
+ interceptors=interceptors,
361
390
  )
362
391
  grpc_servers.append(fleet_server)
363
392
  elif args.fleet_api_type == TRANSPORT_TYPE_VCE:
@@ -390,6 +419,70 @@ def run_superlink() -> None:
390
419
  driver_server.wait_for_termination(timeout=1)
391
420
 
392
421
 
422
+ def _try_setup_client_authentication(
423
+ args: argparse.Namespace,
424
+ certificates: Optional[Tuple[bytes, bytes, bytes]],
425
+ ) -> Optional[Tuple[Set[bytes], ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]]:
426
+ if not args.require_client_authentication:
427
+ return None
428
+
429
+ if certificates is None:
430
+ sys.exit(
431
+ "Client authentication only works over secure connections. "
432
+ "Please provide certificate paths using '--certificates' when "
433
+ "enabling '--require-client-authentication'."
434
+ )
435
+
436
+ client_keys_file_path = Path(args.require_client_authentication[0])
437
+ if not client_keys_file_path.exists():
438
+ sys.exit(
439
+ "The provided path to the client public keys CSV file does not exist: "
440
+ f"{client_keys_file_path}. "
441
+ "Please provide the CSV file path containing known client public keys "
442
+ "to '--require-client-authentication'."
443
+ )
444
+
445
+ client_public_keys: Set[bytes] = set()
446
+ ssh_private_key = load_ssh_private_key(
447
+ Path(args.require_client_authentication[1]).read_bytes(),
448
+ None,
449
+ )
450
+ ssh_public_key = load_ssh_public_key(
451
+ Path(args.require_client_authentication[2]).read_bytes()
452
+ )
453
+
454
+ try:
455
+ server_private_key, server_public_key = ssh_types_to_elliptic_curve(
456
+ ssh_private_key, ssh_public_key
457
+ )
458
+ except TypeError:
459
+ sys.exit(
460
+ "The file paths provided could not be read as a private and public "
461
+ "key pair. Client authentication requires an elliptic curve public and "
462
+ "private key pair. Please provide the file paths containing elliptic "
463
+ "curve private and public keys to '--require-client-authentication'."
464
+ )
465
+
466
+ with open(client_keys_file_path, newline="", encoding="utf-8") as csvfile:
467
+ reader = csv.reader(csvfile)
468
+ for row in reader:
469
+ for element in row:
470
+ public_key = load_ssh_public_key(element.encode())
471
+ if isinstance(public_key, ec.EllipticCurvePublicKey):
472
+ client_public_keys.add(public_key_to_bytes(public_key))
473
+ else:
474
+ sys.exit(
475
+ "Error: Unable to parse the public keys in the .csv "
476
+ "file. Please ensure that the .csv file contains valid "
477
+ "SSH public keys and try again."
478
+ )
479
+ return (
480
+ client_public_keys,
481
+ server_private_key,
482
+ server_public_key,
483
+ )
484
+
485
+
393
486
  def _try_obtain_certificates(
394
487
  args: argparse.Namespace,
395
488
  ) -> Optional[Tuple[bytes, bytes, bytes]]:
@@ -417,6 +510,7 @@ def _run_fleet_api_grpc_rere(
417
510
  address: str,
418
511
  state_factory: StateFactory,
419
512
  certificates: Optional[Tuple[bytes, bytes, bytes]],
513
+ interceptors: Optional[Sequence[grpc.ServerInterceptor]] = None,
420
514
  ) -> grpc.Server:
421
515
  """Run Fleet API (gRPC, request-response)."""
422
516
  # Create Fleet API gRPC server
@@ -429,6 +523,7 @@ def _run_fleet_api_grpc_rere(
429
523
  server_address=address,
430
524
  max_message_length=GRPC_MAX_MESSAGE_LENGTH,
431
525
  certificates=certificates,
526
+ interceptors=interceptors,
432
527
  )
433
528
 
434
529
  log(INFO, "Flower ECE: Starting Fleet API (gRPC-rere) on %s", address)
@@ -606,6 +701,15 @@ def _add_args_common(parser: argparse.ArgumentParser) -> None:
606
701
  "Flower will just create a state in memory.",
607
702
  default=DATABASE,
608
703
  )
704
+ parser.add_argument(
705
+ "--require-client-authentication",
706
+ nargs=3,
707
+ metavar=("CLIENT_KEYS", "SERVER_PRIVATE_KEY", "SERVER_PUBLIC_KEY"),
708
+ type=str,
709
+ help="Provide three file paths: (1) a .csv file containing a list of "
710
+ "known client public keys for authentication, (2) the server's private "
711
+ "key file, and (3) the server's public key file.",
712
+ )
609
713
 
610
714
 
611
715
  def _add_args_driver_api(parser: argparse.ArgumentParser) -> None:
@@ -18,7 +18,7 @@
18
18
  import concurrent.futures
19
19
  import sys
20
20
  from logging import ERROR
21
- from typing import Any, Callable, Optional, Tuple, Union
21
+ from typing import Any, Callable, Optional, Sequence, Tuple, Union
22
22
 
23
23
  import grpc
24
24
 
@@ -162,6 +162,7 @@ def generic_create_grpc_server( # pylint: disable=too-many-arguments
162
162
  max_message_length: int = GRPC_MAX_MESSAGE_LENGTH,
163
163
  keepalive_time_ms: int = 210000,
164
164
  certificates: Optional[Tuple[bytes, bytes, bytes]] = None,
165
+ interceptors: Optional[Sequence[grpc.ServerInterceptor]] = None,
165
166
  ) -> grpc.Server:
166
167
  """Create a gRPC server with a single servicer.
167
168
 
@@ -249,6 +250,7 @@ def generic_create_grpc_server( # pylint: disable=too-many-arguments
249
250
  # returning RESOURCE_EXHAUSTED status, or None to indicate no limit.
250
251
  maximum_concurrent_rpcs=max_concurrent_workers,
251
252
  options=options,
253
+ interceptors=interceptors,
252
254
  )
253
255
  add_servicer_to_server_fn(servicer, server)
254
256
 
@@ -0,0 +1,169 @@
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 server interceptor."""
16
+
17
+
18
+ import base64
19
+ from logging import INFO
20
+ from typing import Any, Callable, Sequence, Set, Tuple, Union
21
+
22
+ import grpc
23
+ from cryptography.hazmat.primitives.asymmetric import ec
24
+
25
+ from flwr.common.logger import log
26
+ from flwr.common.secure_aggregation.crypto.symmetric_encryption import (
27
+ bytes_to_public_key,
28
+ generate_shared_key,
29
+ public_key_to_bytes,
30
+ verify_hmac,
31
+ )
32
+ from flwr.proto.fleet_pb2 import ( # pylint: disable=E0611
33
+ CreateNodeRequest,
34
+ CreateNodeResponse,
35
+ DeleteNodeRequest,
36
+ DeleteNodeResponse,
37
+ GetRunRequest,
38
+ GetRunResponse,
39
+ PullTaskInsRequest,
40
+ PullTaskInsResponse,
41
+ PushTaskResRequest,
42
+ PushTaskResResponse,
43
+ )
44
+
45
+ _PUBLIC_KEY_HEADER = "public-key"
46
+ _AUTH_TOKEN_HEADER = "auth-token"
47
+
48
+ Request = Union[
49
+ CreateNodeRequest,
50
+ DeleteNodeRequest,
51
+ PullTaskInsRequest,
52
+ PushTaskResRequest,
53
+ GetRunRequest,
54
+ ]
55
+
56
+ Response = Union[
57
+ CreateNodeResponse,
58
+ DeleteNodeResponse,
59
+ PullTaskInsResponse,
60
+ PushTaskResResponse,
61
+ GetRunResponse,
62
+ ]
63
+
64
+
65
+ def _get_value_from_tuples(
66
+ key_string: str, tuples: Sequence[Tuple[str, Union[str, bytes]]]
67
+ ) -> bytes:
68
+ value = next((value for key, value in tuples if key == key_string), "")
69
+ if isinstance(value, str):
70
+ return value.encode()
71
+
72
+ return value
73
+
74
+
75
+ class AuthenticateServerInterceptor(grpc.ServerInterceptor): # type: ignore
76
+ """Server interceptor for client authentication."""
77
+
78
+ def __init__(
79
+ self,
80
+ client_public_keys: Set[bytes],
81
+ private_key: ec.EllipticCurvePrivateKey,
82
+ public_key: ec.EllipticCurvePublicKey,
83
+ ):
84
+ self.server_private_key = private_key
85
+ self.client_public_keys = client_public_keys
86
+ self.encoded_server_public_key = base64.urlsafe_b64encode(
87
+ public_key_to_bytes(public_key)
88
+ )
89
+ log(
90
+ INFO,
91
+ "Client authentication enabled with %d known public keys",
92
+ len(client_public_keys),
93
+ )
94
+
95
+ def intercept_service(
96
+ self,
97
+ continuation: Callable[[Any], Any],
98
+ handler_call_details: grpc.HandlerCallDetails,
99
+ ) -> grpc.RpcMethodHandler:
100
+ """Flower server interceptor authentication logic.
101
+
102
+ Intercept all unary calls from clients and authenticate clients by validating
103
+ auth metadata sent by the client. Continue RPC call if client is authenticated,
104
+ else, terminate RPC call by setting context to abort.
105
+ """
106
+ # One of the method handlers in
107
+ # `flwr.server.superlink.fleet.grpc_rere.fleet_server.FleetServicer`
108
+ method_handler: grpc.RpcMethodHandler = continuation(handler_call_details)
109
+ return self._generic_auth_unary_method_handler(method_handler)
110
+
111
+ def _generic_auth_unary_method_handler(
112
+ self, method_handler: grpc.RpcMethodHandler
113
+ ) -> grpc.RpcMethodHandler:
114
+ def _generic_method_handler(
115
+ request: Request,
116
+ context: grpc.ServicerContext,
117
+ ) -> Response:
118
+ client_public_key_bytes = base64.urlsafe_b64decode(
119
+ _get_value_from_tuples(
120
+ _PUBLIC_KEY_HEADER, context.invocation_metadata()
121
+ )
122
+ )
123
+ is_public_key_known = client_public_key_bytes in self.client_public_keys
124
+ if not is_public_key_known:
125
+ context.abort(grpc.StatusCode.UNAUTHENTICATED, "Access denied")
126
+
127
+ if isinstance(request, CreateNodeRequest):
128
+ context.send_initial_metadata(
129
+ (
130
+ (
131
+ _PUBLIC_KEY_HEADER,
132
+ self.encoded_server_public_key,
133
+ ),
134
+ )
135
+ )
136
+ elif isinstance(
137
+ request,
138
+ (
139
+ DeleteNodeRequest,
140
+ PullTaskInsRequest,
141
+ PushTaskResRequest,
142
+ GetRunRequest,
143
+ ),
144
+ ):
145
+ hmac_value = base64.urlsafe_b64decode(
146
+ _get_value_from_tuples(
147
+ _AUTH_TOKEN_HEADER, context.invocation_metadata()
148
+ )
149
+ )
150
+ client_public_key = bytes_to_public_key(client_public_key_bytes)
151
+ shared_secret = generate_shared_key(
152
+ self.server_private_key,
153
+ client_public_key,
154
+ )
155
+ verify = verify_hmac(
156
+ shared_secret, request.SerializeToString(True), hmac_value
157
+ )
158
+ if not verify:
159
+ context.abort(grpc.StatusCode.UNAUTHENTICATED, "Access denied")
160
+ else:
161
+ context.abort(grpc.StatusCode.UNAUTHENTICATED, "Access denied")
162
+
163
+ return method_handler.unary_unary(request, context) # type: ignore
164
+
165
+ return grpc.unary_unary_rpc_method_handler(
166
+ _generic_method_handler,
167
+ request_deserializer=method_handler.request_deserializer,
168
+ response_serializer=method_handler.response_serializer,
169
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: flwr-nightly
3
- Version: 1.9.0.dev20240425
3
+ Version: 1.9.0.dev20240429
4
4
  Summary: Flower: A Friendly Federated Learning Framework
5
5
  Home-page: https://flower.ai
6
6
  License: Apache-2.0
@@ -27,8 +27,8 @@ flwr/cli/new/templates/app/pyproject.tensorflow.toml.tpl,sha256=BJvyU78wnym8R7kh
27
27
  flwr/cli/run/__init__.py,sha256=oCd6HmQDx-sqver1gecgx-uMA38BLTSiiKpl7RGNceg,789
28
28
  flwr/cli/run/run.py,sha256=jr_J7Cbcyuj1MXIbuwU86htHdFI7XogsBrdGl7P4aYY,2334
29
29
  flwr/cli/utils.py,sha256=px-M-IlBLu6Ez-Sc9tWhsJRjWurRaZTmxB9ASz8wurk,4119
30
- flwr/client/__init__.py,sha256=8LuIrd2GGWJXG2CFWihywicJtntIvCoPLssIUnHqZaA,1262
31
- flwr/client/app.py,sha256=zs5yeFavIIX-407b25xLapVruprohKSB0Ckk0CjW1Vw,24670
30
+ flwr/client/__init__.py,sha256=CivBxRFjK6gXHN5kUUf9UaZQHa_NHlEM867WWB-H0D8,1268
31
+ flwr/client/app.py,sha256=rzfaHiXxrtjwyhHrHb3epRD6NNw07YzL5DoZO6eW7RA,22313
32
32
  flwr/client/client.py,sha256=Vp9UkOkoHdNfn6iMYZsj_5m_GICiFfUlKEVaLad-YhM,8183
33
33
  flwr/client/client_app.py,sha256=-Cs0084tLQUoBCeYZdG2KgU7cjp95_ZJ4MfjoaN4Fzk,8636
34
34
  flwr/client/dpfedavg_numpy_client.py,sha256=9Tnig4iml2J88HBKNahegjXjbfvIQyBtaIQaqjbeqsA,7435
@@ -54,8 +54,8 @@ flwr/client/node_state_tests.py,sha256=gPwz0zf2iuDSa11jedkur_u3Xm7lokIDG5ALD2MCv
54
54
  flwr/client/numpy_client.py,sha256=u76GWAdHmJM88Agm2EgLQSvO8Jnk225mJTk-_TmPjFE,10283
55
55
  flwr/client/rest_client/__init__.py,sha256=ThwOnkMdzxo_UuyTI47Q7y9oSpuTgNT2OuFvJCfuDiw,735
56
56
  flwr/client/rest_client/connection.py,sha256=MspqM5RjrQe09_2BUEEVGstA5x9Qz_RWdXXraOic3i8,11520
57
- flwr/client/supernode/__init__.py,sha256=D5swXxemuRbA2rB_T9B8LwJW-_PucXwmlFQQerwIUv0,793
58
- flwr/client/supernode/app.py,sha256=gauvN8elkIy0vuT0GxT7MmkuBRY74ckZfpxejE7dduM,3861
57
+ flwr/client/supernode/__init__.py,sha256=SUhWOzcgXRNXk1V9UgB5-FaWukqqrOEajVUHEcPkwyQ,865
58
+ flwr/client/supernode/app.py,sha256=6OWaD_4ZfguN6sukDfhbZzCgDmsSqHPlAmtcyU6TfJQ,9052
59
59
  flwr/client/typing.py,sha256=c9EvjlEjasxn1Wqx6bGl6Xg6vM1gMFfmXht-E2i5J-k,1006
60
60
  flwr/common/__init__.py,sha256=dHOptgKxna78CEQLD5Yu0QIsoSgpIIw5AhIUZCHDWAU,3721
61
61
  flwr/common/address.py,sha256=iTAN9jtmIGMrWFnx9XZQl45ZEtQJVZZLYPRBSNVARGI,1882
@@ -84,7 +84,7 @@ flwr/common/retry_invoker.py,sha256=dQY5fPIKhy9OiFswZhLxA9fB455u-DYCvDVcFJmrPDk,
84
84
  flwr/common/secure_aggregation/__init__.py,sha256=29nHIUO2L8-KhNHQ2KmIgRo_4CPkq4LgLCUN0on5FgI,731
85
85
  flwr/common/secure_aggregation/crypto/__init__.py,sha256=dz7pVx2aPrHxr_AwgO5mIiTzu4PcvUxRq9NLBbFcsf8,738
86
86
  flwr/common/secure_aggregation/crypto/shamir.py,sha256=yY35ZgHlB4YyGW_buG-1X-0M-ejXuQzISgYLgC_Z9TY,2792
87
- flwr/common/secure_aggregation/crypto/symmetric_encryption.py,sha256=BP2qzZLz1FAdJt-E_7D--4yfyXsmfNqtvQG1XJfS4J0,4173
87
+ flwr/common/secure_aggregation/crypto/symmetric_encryption.py,sha256=zlACMLahJEIqhcss0-1xz_iCXUGTlL2G-i9hi8spu-8,4707
88
88
  flwr/common/secure_aggregation/ndarrays_arithmetic.py,sha256=66mNQCz64r7qzvXwFrXP6zz7YMi8EkTOABN7KulkKc4,3026
89
89
  flwr/common/secure_aggregation/quantization.py,sha256=appui7GGrkRPsupF59TkapeV4Na_CyPi73JtJ1pimdI,2310
90
90
  flwr/common/secure_aggregation/secaggplus_constants.py,sha256=Fh7-n6pgL4TUnHpNYXo8iW-n5cOGQgQa-c7RcU80tqQ,2183
@@ -124,7 +124,7 @@ flwr/proto/transport_pb2_grpc.py,sha256=vLN3EHtx2aEEMCO4f1Upu-l27BPzd3-5pV-u8wPc
124
124
  flwr/proto/transport_pb2_grpc.pyi,sha256=AGXf8RiIiW2J5IKMlm_3qT3AzcDa4F3P5IqUjve_esA,766
125
125
  flwr/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
126
126
  flwr/server/__init__.py,sha256=dNLbXIERZ6X9aA_Bit3R9AARwcaZZzEfDuFmEx8VVOE,1785
127
- flwr/server/app.py,sha256=FriloRrkDHTlB5G7EBn6sH4v5GhiYFf_ZhbdROgjKbY,24199
127
+ flwr/server/app.py,sha256=W5AzN9Gg3nQVnekXgCOzpN3dlpP3T-oh-N2ugdfuF7o,28282
128
128
  flwr/server/client_manager.py,sha256=T8UDSRJBVD3fyIDI7NTAA-NA7GPrMNNgH2OAF54RRxE,6127
129
129
  flwr/server/client_proxy.py,sha256=4G-oTwhb45sfWLx2uZdcXD98IZwdTS6F88xe3akCdUg,2399
130
130
  flwr/server/compat/__init__.py,sha256=VxnJtJyOjNFQXMNi9hIuzNlZM5n0Hj1p3aq_Pm2udw4,892
@@ -174,9 +174,10 @@ flwr/server/superlink/fleet/grpc_bidi/__init__.py,sha256=mgGJGjwT6VU7ovC1gdnnqtt
174
174
  flwr/server/superlink/fleet/grpc_bidi/flower_service_servicer.py,sha256=57b3UL5-baGdLwgCtB0dCUTTSbmmfMAXcXV5bjPZNWQ,5993
175
175
  flwr/server/superlink/fleet/grpc_bidi/grpc_bridge.py,sha256=LSOmabFXAQxKycQOliplKmigbmVwdm-D4CI-hJ0Pav0,6458
176
176
  flwr/server/superlink/fleet/grpc_bidi/grpc_client_proxy.py,sha256=kuD7R1yB1Ite0sNfvjsrnZu83LWGk8fP-yihE1mjQm0,4887
177
- flwr/server/superlink/fleet/grpc_bidi/grpc_server.py,sha256=1QyBX5qcFPjMVlv7TrvnQkcET4muvg94Fy9hAQUBYnY,11818
177
+ flwr/server/superlink/fleet/grpc_bidi/grpc_server.py,sha256=_zWknjP7CRjwLDvofzmv1QoSI8Qq1cZC5nNw9nkSS7I,11932
178
178
  flwr/server/superlink/fleet/grpc_rere/__init__.py,sha256=bEJOMWbSlqkw-y5ZHtEXczhoSlAxErcRYffmTMQAV8M,758
179
179
  flwr/server/superlink/fleet/grpc_rere/fleet_servicer.py,sha256=YGn1IPpuX-6NDgaG1UbyREbI9iAyKDimZuNeWxbG6s0,3387
180
+ flwr/server/superlink/fleet/grpc_rere/server_interceptor.py,sha256=hewkFjp4VJnZi-CCuBIooLenmSz6KJdzZBsKOfxbk5Y,5799
180
181
  flwr/server/superlink/fleet/message_handler/__init__.py,sha256=hEY0l61ojH8Iz30_K1btm1HJ6J49iZJSFUsVYqUTw3A,731
181
182
  flwr/server/superlink/fleet/message_handler/message_handler.py,sha256=lG3BkiONcikDVowK0An06V7p2SNkwGbWE5hfN2xlsZw,3622
182
183
  flwr/server/superlink/fleet/rest_rere/__init__.py,sha256=VKDvDq5H8koOUztpmQacVzGJXPLEEkL1Vmolxt3mvnY,735
@@ -209,8 +210,8 @@ flwr/simulation/ray_transport/ray_actor.py,sha256=_wv2eP7qxkCZ-6rMyYWnjLrGPBZRxj
209
210
  flwr/simulation/ray_transport/ray_client_proxy.py,sha256=oDu4sEPIOu39vrNi-fqDAe10xtNUXMO49bM2RWfRcyw,6738
210
211
  flwr/simulation/ray_transport/utils.py,sha256=TYdtfg1P9VfTdLMOJlifInGpxWHYs9UfUqIv2wfkRLA,2392
211
212
  flwr/simulation/run_simulation.py,sha256=nxXNv3r8ODImd5o6f0sa_w5L0I08LD2Udw2OTXStRnQ,15694
212
- flwr_nightly-1.9.0.dev20240425.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
213
- flwr_nightly-1.9.0.dev20240425.dist-info/METADATA,sha256=yoGpV_gxcQe4bqM02SbGJLu4szBi9An4jfQDSXfYm44,15260
214
- flwr_nightly-1.9.0.dev20240425.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
215
- flwr_nightly-1.9.0.dev20240425.dist-info/entry_points.txt,sha256=DBrrf685V2W9NbbchQwvuqBEpj5ik8tMZNoZg_W2bZY,363
216
- flwr_nightly-1.9.0.dev20240425.dist-info/RECORD,,
213
+ flwr_nightly-1.9.0.dev20240429.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
214
+ flwr_nightly-1.9.0.dev20240429.dist-info/METADATA,sha256=LiieeRHvskFf0ZMyAnDUP0hiSS0oYScCuLsX4GyUi1U,15260
215
+ flwr_nightly-1.9.0.dev20240429.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
216
+ flwr_nightly-1.9.0.dev20240429.dist-info/entry_points.txt,sha256=DBrrf685V2W9NbbchQwvuqBEpj5ik8tMZNoZg_W2bZY,363
217
+ flwr_nightly-1.9.0.dev20240429.dist-info/RECORD,,