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

Add client auth interceptor #2946

Merged
merged 66 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
d0d4ecd
Add authentication state and test
danielnugraha Feb 10, 2024
91a2f18
Fix isort
danielnugraha Feb 10, 2024
db16c10
Fix isort
danielnugraha Feb 10, 2024
28876bc
Run format.sh
danielnugraha Feb 10, 2024
42a7d38
Add init.py
danielnugraha Feb 10, 2024
8ec63c9
Fix line too long
danielnugraha Feb 10, 2024
8f04e25
Fix line too long
danielnugraha Feb 10, 2024
e8813fc
Fix line too long
danielnugraha Feb 10, 2024
d9f3fb0
Fix subclassing
danielnugraha Feb 10, 2024
caf6695
Fix subclassing
danielnugraha Feb 10, 2024
fa217ae
Fix subclassing
danielnugraha Feb 10, 2024
6edddd6
Fix subclassing
danielnugraha Feb 10, 2024
8bb15a5
Fix subclassing
danielnugraha Feb 10, 2024
c5bac4f
fixes
jafermarq Feb 11, 2024
c856b7c
Fix state tests
danielnugraha Feb 11, 2024
4758507
Fix too broad exception
danielnugraha Feb 11, 2024
e666da5
Add sqlite auth state test
danielnugraha Feb 11, 2024
151a619
Merge remote-tracking branch 'origin' into add-auth-state
danielnugraha Feb 11, 2024
6c8c346
Merge branch 'add-auth-state' into add-client-auth-interceptor
danielnugraha Feb 13, 2024
e6421cb
Add client interceptor
danielnugraha Feb 13, 2024
3ee430a
Add mock servicer
danielnugraha Feb 13, 2024
bcc481e
Add unittest initializer
danielnugraha Feb 13, 2024
abe18a5
Integrate client interceptor
danielnugraha Feb 13, 2024
8d0b7c5
Integrate client interceptor
danielnugraha Feb 13, 2024
328118e
Add type:ignore to grpc primitives
danielnugraha Feb 14, 2024
a63d664
Remove auth state and format client interceptor test
danielnugraha Feb 14, 2024
01be61b
Revert auth state changes
danielnugraha Feb 14, 2024
d922ccc
add auth-token metadata client interceptor test
danielnugraha Feb 14, 2024
bf95bee
Merge branch 'main' into add-client-auth-interceptor
danielnugraha Feb 14, 2024
f8fefe1
Remove pass
danielnugraha Feb 14, 2024
33b12b4
Merge remote-tracking branch 'refs/remotes/origin/add-client-auth-int…
danielnugraha Feb 14, 2024
16285aa
Merge remote-tracking branch 'origin' into add-client-auth-interceptor
danielnugraha Feb 21, 2024
bcde875
Update client-interceptor-test
danielnugraha Feb 21, 2024
aa3722e
Merge branch 'main' into add-client-auth-interceptor
danielnugraha Feb 22, 2024
3dcef3d
Merge remote-tracking branch 'origin' into add-client-auth-interceptor
danielnugraha Feb 28, 2024
cdd0eb9
Merge branch 'main' into add-client-auth-interceptor
danielnugraha Feb 29, 2024
9845331
Merge remote-tracking branch 'origin' into add-client-auth-interceptor
danielnugraha Feb 29, 2024
cc96e81
Merge remote-tracking branch 'refs/remotes/origin/add-client-auth-int…
danielnugraha Feb 29, 2024
b047391
Address review
danielnugraha Feb 29, 2024
c43c3b5
Fix docstring
danielnugraha Feb 29, 2024
031c9f7
Format
danielnugraha Mar 1, 2024
97c24b3
Fix merge conflict
danielnugraha Apr 3, 2024
f6832e8
Add retry invoker
danielnugraha Apr 4, 2024
9ca4e56
Fix mypy
danielnugraha Apr 4, 2024
c769682
Merge branch 'main' into add-client-auth-interceptor
danielnugraha Apr 4, 2024
a8e86f8
Merge branch 'main' into add-client-auth-interceptor
danielnugraha Apr 15, 2024
9c87756
Merge remote-tracking branch 'origin' into add-client-auth-interceptor
danielnugraha Apr 24, 2024
03039fc
Format
danielnugraha Apr 24, 2024
4a6dc38
Add more tests
danielnugraha Apr 24, 2024
05edfbd
Format
danielnugraha Apr 24, 2024
005e158
Merge branch 'main' into add-client-auth-interceptor
danieljanes Apr 24, 2024
0d93631
Add interceptors to other contextmanager
danielnugraha Apr 24, 2024
a60f9f8
Merge remote-tracking branch 'refs/remotes/origin/add-client-auth-int…
danielnugraha Apr 24, 2024
7f695f5
Replace interceptors with authentication keys
danielnugraha Apr 24, 2024
8a80ead
Replace interceptors with authentication keys
danielnugraha Apr 24, 2024
03e7c12
Move to grpc_rere_client
danielnugraha Apr 24, 2024
c655c7b
Format
danielnugraha Apr 24, 2024
537e05e
Add get_run
danielnugraha Apr 24, 2024
f313480
Encode only once
danielnugraha Apr 24, 2024
28b2f4c
Check if shared secret is not none
danielnugraha Apr 24, 2024
5da8cdb
Update src/py/flwr/client/grpc_rere_client/client_interceptor.py
danieljanes Apr 24, 2024
c678f11
Simplify if/else
danielnugraha Apr 24, 2024
9f6d8ec
Simplify
danielnugraha Apr 24, 2024
b2fee0f
Merge remote-tracking branch 'refs/remotes/origin/add-client-auth-int…
danielnugraha Apr 24, 2024
4e2a0c5
Format
danielnugraha Apr 24, 2024
ca7267a
Add docstring to ClientCallDetails
danielnugraha Apr 24, 2024
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
139 changes: 139 additions & 0 deletions src/py/flwr/client/client_interceptor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# 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 client interceptor."""


import base64
import collections
from typing import Any, Callable, Optional, Sequence, Tuple, Union

import grpc
from cryptography.hazmat.primitives.asymmetric import ec

from flwr.common.secure_aggregation.crypto.symmetric_encryption import (
bytes_to_public_key,
compute_hmac,
generate_shared_key,
public_key_to_bytes,
)
from flwr.proto.fleet_pb2 import ( # pylint: disable=E0611
CreateNodeRequest,
DeleteNodeRequest,
PullTaskInsRequest,
PushTaskResRequest,
)

_PUBLIC_KEY_HEADER = "public-key"
_AUTH_TOKEN_HEADER = "auth-token"

Request = Union[
CreateNodeRequest, DeleteNodeRequest, PullTaskInsRequest, PushTaskResRequest
]


def _get_value_from_tuples(
key_string: str, tuples: Sequence[Tuple[str, Union[str, bytes]]]
) -> bytes:
value = next((value for key, value in tuples if key == key_string), "")
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't it check if tuples is not empty?

Copy link
Contributor

Choose a reason for hiding this comment

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

Plus, is the "" intended? so if it doesn't find anything, it returns ""?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If tuples empty, then it returns empty bytes, if there is no key value pair in the tuple, it also returns empty bytes.

if isinstance(value, str):
return value.encode()

return value


class _ClientCallDetails(
collections.namedtuple(
"_ClientCallDetails", ("method", "timeout", "metadata", "credentials")
),
grpc.ClientCallDetails, # type: ignore
):
pass


class AuthenticateClientInterceptor(grpc.UnaryUnaryClientInterceptor): # type: ignore
"""Client interceptor for client authentication."""

def __init__(
self,
private_key: ec.EllipticCurvePrivateKey,
public_key: ec.EllipticCurvePublicKey,
):
self.private_key = private_key
self.public_key = public_key
self.shared_secret = b""
self.server_public_key: Optional[ec.EllipticCurvePublicKey] = None

def intercept_unary_unary(
self,
continuation: Callable[[Any, Any], Any],
client_call_details: grpc.ClientCallDetails,
request: Request,
) -> grpc.Call:
"""Flower client interceptor.

Intercept unary call from client and add necessary authentication header in the
RPC metadata.
"""
metadata = []
postprocess = False
if client_call_details.metadata is not None:
metadata = list(client_call_details.metadata)

if isinstance(request, CreateNodeRequest):
metadata.append(
(
_PUBLIC_KEY_HEADER,
base64.urlsafe_b64encode(public_key_to_bytes(self.public_key)),
)
)
postprocess = True

elif isinstance(
request, (DeleteNodeRequest, PullTaskInsRequest, PushTaskResRequest)
):
metadata.append(
(
_PUBLIC_KEY_HEADER,
base64.urlsafe_b64encode(public_key_to_bytes(self.public_key)),
)
)
metadata.append(
(
_AUTH_TOKEN_HEADER,
base64.urlsafe_b64encode(
compute_hmac(
self.shared_secret, request.SerializeToString(True)
)
),
)
)

client_call_details = _ClientCallDetails(
client_call_details.method,
client_call_details.timeout,
metadata,
client_call_details.credentials,
)

response = continuation(client_call_details, request)
if postprocess:
server_public_key_bytes = base64.urlsafe_b64decode(
_get_value_from_tuples(_PUBLIC_KEY_HEADER, response.initial_metadata())
)
self.server_public_key = bytes_to_public_key(server_public_key_bytes)
self.shared_secret = generate_shared_key(
self.private_key, self.server_public_key
)
return response
Loading