Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/update-flower-auth-…
Browse files Browse the repository at this point in the history
…example' into update-flower-auth-example
  • Loading branch information
danielnugraha committed Oct 18, 2024
2 parents d2788c5 + 7ab3edf commit 23daf43
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 52 deletions.
100 changes: 51 additions & 49 deletions examples/flower-authentication/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,74 @@ dataset: [CIFAR-10]
framework: [torch, torchvision]
---

# Flower Authentication with PyTorch 🧪
# Flower Federations with Authentication 🧪

> 🧪 = This example covers experimental features that might change in future versions of Flower
> Please consult the regular PyTorch code examples ([quickstart](https://github.com/adap/flower/tree/main/examples/quickstart-pytorch), [advanced](https://github.com/adap/flower/tree/main/examples/advanced-pytorch)) to learn how to use Flower with PyTorch.
> \[!NOTE\]
> 🧪 = This example covers experimental features that might change in future versions of Flower.
> Please consult the regular PyTorch examples ([quickstart](https://github.com/adap/flower/tree/main/examples/quickstart-pytorch), [advanced](https://github.com/adap/flower/tree/main/examples/advanced-pytorch)) to learn how to use Flower with PyTorch.
The following steps describe how to start a long-running Flower server (SuperLink) and a long-running Flower client (SuperNode) with authentication enabled.
The following steps describe how to start a long-running Flower server (SuperLink+SuperExec) and a long-running Flower clients (SuperNode) with authentication enabled. The task is to train a simple CNN for image classification using PyTorch.

## Project Setup

Start by cloning the example project. We prepared a single-line command that you can copy into your shell which will checkout the example for you:

```shell
git clone --depth=1 https://github.com/adap/flower.git _tmp && mv _tmp/examples/flower-authentication . && rm -rf _tmp && cd flower-authentication
git clone --depth=1 https://github.com/adap/flower.git _tmp \
&& mv _tmp/examples/flower-authentication . \
&& rm -rf _tmp && cd flower-authentication
```

This will create a new directory called `flower-authentication` with the following project structure:

```bash
$ tree .
.
├── certificate.conf # <-- configuration for OpenSSL
├── generate.sh # <-- generate certificates and keys
├── pyproject.toml # <-- project dependencies
├── client.py # <-- contains `ClientApp`
├── server.py # <-- contains `ServerApp`
└── task.py # <-- task-specific code (model, data)
```shell
flower-authentication
├── authexample
│ ├── __init__.py
│ ├── client_app.py # Defines your ClientApp
│ ├── server_app.py # Defines your ServerApp
│ └── task.py # Defines your model, training and data loading
├── pyproject.toml # Project metadata like dependencies and configs
├── certificate.conf # Configuration for OpenSSL
├── generate.sh # Generate certificates and keys
└── README.md
```

## Install dependencies
### Install dependencies and project

Project dependencies (such as `torch` and `flwr`) are defined in `pyproject.toml`. You can install the dependencies by invoking `pip`:
Install the dependencies defined in `pyproject.toml` as well as the `authexample` package.

```shell
# From a new python environment, run:
pip install .
```bash
pip install -e .
```

Then, to verify that everything works correctly you can run the following command:

```shell
python3 -c "import flwr"
```
## Generate public and private keys

If you don't see any errors you're good to go!
The `generate.sh` script by default generates certificates for creating a secure TLS connection
and three private and public key pairs for one server and two clients.

## Generate public and private keys
> \[!NOTE\]
> Note that this script should only be used for development purposes and not for creating production key pairs.
```bash
./generate.sh
```

`generate.sh` is a script that (by default) generates certificates for creating a secure TLS connection
and three private and public key pairs for one server and two clients.
You can generate more keys by specifying the number of client credentials that you wish to generate.
The script also generates a CSV file that includes each of the generated (client) public keys.

⚠️ Note that this script should only be used for development purposes and not for creating production key pairs.

```bash
./generate.sh {your_number_of_clients}
```

## Start the long-running Flower server (SuperLink)
## Start the long-running Flower server-side (SuperLink+SuperExec)

To start a long-running Flower server (SuperLink) and enable authentication is very easy; all you need to do is type
Starting long-running Flower server-side components (SuperLink+SuperExec) and enable authentication is very easy; all you need to do is type
`--auth-list-public-keys` containing file path to the known `client_public_keys.csv`, `--auth-superlink-private-key`
containing file path to the SuperLink's private key `server_credentials`, and `--auth-superlink-public-key` containing file path to the SuperLink's public key `server_credentials.pub`. Notice that you can only enable authentication with a secure TLS connection.

Let's first launche the `SuperLink`:

```bash
flower-superlink \
--ssl-ca-certfile certificates/ca.crt \
Expand All @@ -82,12 +82,26 @@ flower-superlink \
--auth-superlink-public-key keys/server_credentials.pub
```

## Start the long-running Flower client (SuperNode)
Then launch the `SuperExec`:

```bash
flower-superexec \
--ssl-ca-certfile certificates/ca.crt \
--ssl-certfile certificates/server.pem \
--ssl-keyfile certificates/server.key \
--executor-config '--executor-config 'root-certificates=\"certificates/ca.crt\"'' \
--executor flwr.superexec.deployment:executor

```

At this point your server-side is idling. First, let's connect two `SuperNodes`, and then we'll start a run.

## Start the long-running Flower client-side (SuperNode)

In a new terminal window, start the first long-running Flower client (SuperNode):

```bash
flower-supernode ./ \
flower-supernode \
--root-certificates certificates/ca.crt \
--superlink 127.0.0.1:9092 \
--auth-supernode-private-key keys/client_credentials_1 \
Expand All @@ -98,7 +112,7 @@ flower-supernode ./ \
In yet another new terminal window, start the second long-running Flower client:

```bash
flower-supernode ./ \
flower-supernode \
--root-certificates certificates/ca.crt \
--superlink 127.0.0.1:9092 \
--auth-supernode-private-key keys/client_credentials_2 \
Expand All @@ -111,20 +125,8 @@ above. Don't forget to specify the correct client private and public keys for ea

## Run the Flower App

With both the long-running server (SuperLink) and two clients (SuperNode) up and running, we can now start the SuperExec:

```bash
flower-superexec \
--ssl-ca-certfile certificates/ca.crt \
--ssl-certfile certificates/server.pem \
--ssl-keyfile certificates/server.key \
--executor-config '--executor-config 'root-certificates=\"certificates/ca.crt\"'' \
--executor flwr.superexec.deployment:executor

```

Then, we run the `flwr run` command:
With both the long-running server-side (SuperLink+SuperExec) and two SuperNodes up and running, we can now start run.

```bash
flwr run . superexec
flwr run . my-federation
```
6 changes: 3 additions & 3 deletions examples/flower-authentication/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version = "1.0.0"
description = "Federated Learning with PyTorch and authenticated Flower "
license = "Apache-2.0"
dependencies = [
"flwr[simulation]>=1.11.0",
"flwr>=1.12.0",
"flwr-datasets[vision]>=0.3.0",
"torch==2.2.1",
"torchvision==0.17.1",
Expand All @@ -34,6 +34,6 @@ batch-size = 32
[tool.flwr.federations]
default = "superexec"

[tool.flwr.federations.superexec]
address = "127.0.0.1:9093"
[tool.flwr.federations.my-federation]
address = "127.0.0.1:9093" # Address of the SuperExec
root-certificates = "certificates/ca.crt"

0 comments on commit 23daf43

Please sign in to comment.