-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: use the Aleph.im P2P service (#328)
Problem: P2P communication with the JS P2P daemon is sometimes unstable. Solution: develop a new P2P service for Aleph.im nodes to replace this daemon. * Rewrote all the calls to the P2P daemon to use the new service instead * Adapted Docker Compose files * Decommissioned the P2P protocol implementation as streams are not supported by the new service for now * The key generation mechanism now writes the private key file in PKCS8 DER format, for compatibility with the P2P service * Added a migration script to migrate the private key of existing nodes to the new format
- Loading branch information
1 parent
1bc15fe
commit f887c6f
Showing
35 changed files
with
292 additions
and
657 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,10 +28,6 @@ jobs: | |
with: | ||
# Fetch the whole history for all tags and branches (required for aleph.__version__) | ||
fetch-depth: 0 | ||
# Install nodejs for jsp2pd | ||
- uses: actions/setup-node@v2 | ||
with: | ||
node-version: '16' | ||
- name: Set up Python 3.8 | ||
id: setup-python | ||
uses: actions/setup-python@v2 | ||
|
@@ -45,8 +41,6 @@ jobs: | |
with: | ||
path: ${{ steps.pip-cache.outputs.dir }} | ||
key: ${{ runner.os }}-python-${{ steps.setup-python.outputs.python-version }}-pip-${{ hashFiles('setup.cfg') }} | ||
- name: Install jsp2pd | ||
run: npm install --global [email protected] | ||
- name: Install Python dependencies | ||
run: | | ||
pip install .[testing] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,6 @@ p2p: | |
http_port: 4024 | ||
port: 4025 | ||
control_port: 4030 | ||
listen_port: 4031 | ||
reconnect_delay: 60 | ||
|
||
sentry: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
deployment/migrations/scripts/0003-convert-key-to-pkcs8-der.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
This migration converts the PEM private key file to PKCS8 DER for compatibility | ||
with the new Aleph.im P2P service. The Rust implementation of libp2p can only load | ||
RSA keys in that format. | ||
""" | ||
|
||
|
||
import logging | ||
import os | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
import yaml | ||
from Crypto.PublicKey import RSA | ||
from p2pclient.libp2p_stubs.crypto.rsa import RSAPrivateKey | ||
|
||
from aleph.exceptions import InvalidKeyDirException | ||
|
||
LOGGER = logging.getLogger(os.path.basename(__file__)) | ||
|
||
|
||
PKCS8_DER_KEY_FILE = "node-secret.pkcs8.der" | ||
|
||
|
||
def convert_pem_key_file_to_pkcs8_der( | ||
pem_key_file: Path, pkcs8_der_key_file: Path | ||
) -> None: | ||
with pem_key_file.open() as pem: | ||
private_key = RSAPrivateKey(RSA.import_key(pem.read())) | ||
|
||
with pkcs8_der_key_file.open("wb") as der: | ||
der.write(private_key.impl.export_key(format="DER", pkcs=8)) | ||
|
||
|
||
def get_key_from_config(config_file: Path) -> Optional[str]: | ||
""" | ||
In previous versions of the CCN, it was possible to set the key value directly | ||
in the config file. This function tries to find it in the config or returns None. | ||
:param config_file: Path to the CCN configuration file. | ||
:return: The private key used to identify the node on the P2P network, or None | ||
if the key is not provided in the config file. | ||
""" | ||
with open(config_file) as f: | ||
config = yaml.safe_load(f) | ||
|
||
try: | ||
return config["p2p"]["key"] | ||
except KeyError: | ||
return None | ||
|
||
|
||
def upgrade(**kwargs): | ||
key_dir = Path(kwargs["key_dir"]) | ||
pem_key_file = key_dir / "node-secret.key" | ||
|
||
# Nothing to do if the PKCS8 DER key file already exists | ||
pkcs8_der_key_file = key_dir / PKCS8_DER_KEY_FILE | ||
if pkcs8_der_key_file.is_file(): | ||
LOGGER.info( | ||
"Key file %s already exists, nothing to do", | ||
pkcs8_der_key_file, | ||
) | ||
return | ||
|
||
if not key_dir.is_dir(): | ||
raise InvalidKeyDirException( | ||
f"The specified key directory ('{key_dir}') is not a directory." | ||
) | ||
|
||
LOGGER.info("Converting the private key file to PKCS8 DER format...") | ||
convert_pem_key_file_to_pkcs8_der(pem_key_file, pkcs8_der_key_file) | ||
LOGGER.info("Successfully created %s.", pkcs8_der_key_file) | ||
|
||
|
||
def downgrade(**kwargs): | ||
# Nothing to do, the key file is still present in the key directory | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.