Flower Authentication with PyTorch 🧪#

View on GitHub

🧪 = This example covers experimental features that might change in future versions of Flower Please consult the regular PyTorch code examples (quickstart, advanced) 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.

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:

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:

$ 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)

Install dependencies#

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

# From a new python environment, run:
pip install .

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

python3 -c "import flwr"

If you don’t see any errors you’re good to go!

Generate public and private keys#


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.

./generate.sh {your_number_of_clients}

Start the long-running Flower client (SuperNode)#

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

flower-client-app client:app \
    --root-certificates certificates/ca.crt \
    --server \
    --auth-supernode-private-key keys/client_credentials_1 \
    --auth-supernode-public-key keys/client_credentials_1.pub

In yet another new terminal window, start the second long-running Flower client:

flower-client-app client:app \
    --root-certificates certificates/ca.crt \
    --server \
    --auth-supernode-private-key keys/client_credentials_2 \
    --auth-supernode-public-key keys/client_credentials_2.pub

If you generated more than 2 client credentials, you can add more clients by opening new terminal windows and running the command above. Don’t forget to specify the correct client private and public keys for each client instance you created.

Run the Flower App#

With both the long-running server (SuperLink) and two clients (SuperNode) up and running, we can now run the actual Flower ServerApp:

flower-server-app server:app --root-certificates certificates/ca.crt --dir ./ --server