Clean up key gen

This commit is contained in:
Alex Stokes 2019-08-15 18:36:50 -07:00
parent d17e6f3392
commit 7535a02da7
No known key found for this signature in database
GPG Key ID: 51CE1721B245C086
7 changed files with 44 additions and 98 deletions

View File

@ -1,7 +1,7 @@
import asyncio import asyncio
from typing import Mapping, Sequence from typing import Mapping, Sequence
from libp2p.crypto.keys import KeyPair, PrivateKey from libp2p.crypto.keys import KeyPair
from libp2p.crypto.rsa import create_new_key_pair from libp2p.crypto.rsa import create_new_key_pair
from libp2p.host.basic_host import BasicHost from libp2p.host.basic_host import BasicHost
from libp2p.kademlia.network import KademliaServer from libp2p.kademlia.network import KademliaServer
@ -69,7 +69,7 @@ def initialize_default_kademlia_router(
def initialize_default_swarm( def initialize_default_swarm(
private_key: PrivateKey, key_pair: KeyPair,
id_opt: ID = None, id_opt: ID = None,
transport_opt: Sequence[str] = None, transport_opt: Sequence[str] = None,
muxer_opt: Sequence[str] = None, muxer_opt: Sequence[str] = None,
@ -89,7 +89,7 @@ def initialize_default_swarm(
""" """
if not id_opt: if not id_opt:
id_opt = generate_peer_id_from_rsa_identity() id_opt = generate_peer_id_from_rsa_identity(key_pair)
# TODO parse transport_opt to determine transport # TODO parse transport_opt to determine transport
transport_opt = transport_opt or ["/ip4/127.0.0.1/tcp/8001"] transport_opt = transport_opt or ["/ip4/127.0.0.1/tcp/8001"]
@ -99,9 +99,7 @@ def initialize_default_swarm(
# TODO parse muxer and sec to pass into TransportUpgrader # TODO parse muxer and sec to pass into TransportUpgrader
muxer = muxer_opt or ["mplex/6.7.0"] muxer = muxer_opt or ["mplex/6.7.0"]
security_transports_by_protocol = sec_opt or { security_transports_by_protocol = sec_opt or {
TProtocol("insecure/1.0.0"): InsecureTransport( TProtocol("insecure/1.0.0"): InsecureTransport(key_pair)
private_key, private_key.get_public_key()
)
} }
upgrader = TransportUpgrader(security_transports_by_protocol, muxer) upgrader = TransportUpgrader(security_transports_by_protocol, muxer)
@ -113,7 +111,7 @@ def initialize_default_swarm(
async def new_node( async def new_node(
private_key: PrivateKey = None, key_pair: KeyPair = None,
swarm_opt: INetwork = None, swarm_opt: INetwork = None,
id_opt: ID = None, id_opt: ID = None,
transport_opt: Sequence[str] = None, transport_opt: Sequence[str] = None,
@ -124,6 +122,7 @@ async def new_node(
) -> BasicHost: ) -> BasicHost:
""" """
create new libp2p node create new libp2p node
:param key_pair: key pair for deriving an identity
:param swarm_opt: optional swarm :param swarm_opt: optional swarm
:param id_opt: optional id for host :param id_opt: optional id for host
:param transport_opt: optional choice of transport upgrade :param transport_opt: optional choice of transport upgrade
@ -134,16 +133,15 @@ async def new_node(
:return: return a host instance :return: return a host instance
""" """
if not private_key: if not key_pair:
key_pair = generate_new_rsa_identity() key_pair = generate_new_rsa_identity()
private_key = key_pair.private_key
if not id_opt: if not id_opt:
id_opt = generate_peer_id_from_rsa_identity(key_pair) id_opt = generate_peer_id_from_rsa_identity(key_pair)
if not swarm_opt: if not swarm_opt:
swarm_opt = initialize_default_swarm( swarm_opt = initialize_default_swarm(
private_key=private_key, key_pair=key_pair,
id_opt=id_opt, id_opt=id_opt,
transport_opt=transport_opt, transport_opt=transport_opt,
muxer_opt=muxer_opt, muxer_opt=muxer_opt,

View File

@ -1,4 +1,4 @@
from libp2p.crypto.keys import PrivateKey, PublicKey from libp2p.crypto.keys import KeyPair
from libp2p.peer.id import ID from libp2p.peer.id import ID
from libp2p.security.secure_transport_interface import ISecureTransport from libp2p.security.secure_transport_interface import ISecureTransport
@ -9,8 +9,6 @@ class BaseSecureTransport(ISecureTransport):
is only meant to be used in clases that derive from it. is only meant to be used in clases that derive from it.
""" """
def __init__( def __init__(self, local_key_pair: KeyPair) -> None:
self, local_private_key: PrivateKey, local_public_key: PublicKey self.local_private_key = local_key_pair.private_key
) -> None: self.local_peer = ID.from_pubkey(local_key_pair.public_key)
self.local_private_key = local_private_key
self.local_peer = ID.from_pubkey(local_public_key)

View File

@ -1,5 +1,6 @@
import asyncio import asyncio
from libp2p.crypto.keys import KeyPair
from libp2p.network.connection.raw_connection_interface import IRawConnection from libp2p.network.connection.raw_connection_interface import IRawConnection
from libp2p.peer.id import ID from libp2p.peer.id import ID
from libp2p.security.base_transport import BaseSecureTransport from libp2p.security.base_transport import BaseSecureTransport
@ -10,10 +11,8 @@ from libp2p.security.secure_conn_interface import ISecureConn
class SimpleSecurityTransport(BaseSecureTransport): class SimpleSecurityTransport(BaseSecureTransport):
key_phrase: str key_phrase: str
def __init__( def __init__(self, local_key_pair: KeyPair, key_phrase: str) -> None:
self, local_private_key: bytes, local_public_key: bytes, key_phrase: str super().__init__(local_key_pair)
) -> None:
super().__init__(local_private_key, local_public_key)
self.key_phrase = key_phrase self.key_phrase = key_phrase
async def secure_inbound(self, conn: IRawConnection) -> ISecureConn: async def secure_inbound(self, conn: IRawConnection) -> ISecureConn:

View File

@ -13,12 +13,12 @@ import multiaddr
import pytest import pytest
from libp2p import initialize_default_swarm, new_node from libp2p import initialize_default_swarm, new_node
from libp2p.crypto.rsa import create_new_key_pair
from libp2p.host.basic_host import BasicHost from libp2p.host.basic_host import BasicHost
from libp2p.network.notifee_interface import INotifee from libp2p.network.notifee_interface import INotifee
from tests.utils import ( from tests.utils import (
cleanup, cleanup,
echo_stream_handler, echo_stream_handler,
generate_new_private_key,
perform_two_host_set_up_custom_handler, perform_two_host_set_up_custom_handler,
) )
@ -173,17 +173,17 @@ async def test_one_notifier_on_two_nodes():
async def test_one_notifier_on_two_nodes_with_listen(): async def test_one_notifier_on_two_nodes_with_listen():
events_b = [] events_b = []
node_a_key = generate_new_private_key() node_a_key_pair = create_new_key_pair()
node_a_transport_opt = ["/ip4/127.0.0.1/tcp/0"] node_a_transport_opt = ["/ip4/127.0.0.1/tcp/0"]
node_a = await new_node(node_a_key, transport_opt=node_a_transport_opt) node_a = await new_node(node_a_key_pair, transport_opt=node_a_transport_opt)
await node_a.get_network().listen(multiaddr.Multiaddr(node_a_transport_opt[0])) await node_a.get_network().listen(multiaddr.Multiaddr(node_a_transport_opt[0]))
# Set up node_b swarm to pass into host # Set up node_b swarm to pass into host
node_b_key = generate_new_private_key() node_b_key_pair = create_new_key_pair()
node_b_transport_opt = ["/ip4/127.0.0.1/tcp/0"] node_b_transport_opt = ["/ip4/127.0.0.1/tcp/0"]
node_b_multiaddr = multiaddr.Multiaddr(node_b_transport_opt[0]) node_b_multiaddr = multiaddr.Multiaddr(node_b_transport_opt[0])
node_b_swarm = initialize_default_swarm( node_b_swarm = initialize_default_swarm(
node_b_key, transport_opt=node_b_transport_opt node_b_key_pair, transport_opt=node_b_transport_opt
) )
node_b = BasicHost(node_b_swarm) node_b = BasicHost(node_b_swarm)

View File

@ -1,6 +1,7 @@
import factory import factory
from libp2p import initialize_default_swarm from libp2p import initialize_default_swarm
from libp2p.crypto.rsa import create_new_key_pair
from libp2p.host.basic_host import BasicHost from libp2p.host.basic_host import BasicHost
from libp2p.pubsub.floodsub import FloodSub from libp2p.pubsub.floodsub import FloodSub
from libp2p.pubsub.gossipsub import GossipSub from libp2p.pubsub.gossipsub import GossipSub
@ -11,11 +12,10 @@ from tests.pubsub.configs import (
GOSSIPSUB_PARAMS, GOSSIPSUB_PARAMS,
GOSSIPSUB_PROTOCOL_ID, GOSSIPSUB_PROTOCOL_ID,
) )
from tests.utils import generate_new_private_key
def swarm_factory(): def swarm_factory():
private_key = generate_new_private_key() private_key = create_new_key_pair()
return initialize_default_swarm(private_key, transport_opt=[str(LISTEN_MADDR)]) return initialize_default_swarm(private_key, transport_opt=[str(LISTEN_MADDR)])

View File

@ -4,11 +4,12 @@ import multiaddr
import pytest import pytest
from libp2p import new_node from libp2p import new_node
from libp2p.crypto.rsa import create_new_key_pair
from libp2p.peer.peerinfo import info_from_p2p_addr from libp2p.peer.peerinfo import info_from_p2p_addr
from libp2p.protocol_muxer.multiselect_client import MultiselectClientError from libp2p.protocol_muxer.multiselect_client import MultiselectClientError
from libp2p.security.insecure.transport import InsecureSession, InsecureTransport from libp2p.security.insecure.transport import InsecureSession, InsecureTransport
from libp2p.security.simple.transport import SimpleSecurityTransport from libp2p.security.simple.transport import SimpleSecurityTransport
from tests.utils import cleanup, connect, generate_new_private_key from tests.utils import cleanup, connect
# TODO: Add tests for multiple streams being opened on different # TODO: Add tests for multiple streams being opened on different
# protocols through the same connection # protocols through the same connection
@ -20,13 +21,9 @@ def peer_id_for_node(node):
return info.peer_id return info.peer_id
initiator_private_key = generate_new_private_key() initiator_key_pair = create_new_key_pair()
initiator_private_key_bytes = initiator_private_key.export_key("DER")
initiator_public_key_bytes = initiator_private_key.publickey().export_key("DER")
noninitiator_private_key = generate_new_private_key() noninitiator_key_pair = create_new_key_pair()
noninitiator_private_key_bytes = noninitiator_private_key.export_key("DER")
noninitiator_public_key_bytes = noninitiator_private_key.publickey().export_key("DER")
async def perform_simple_test( async def perform_simple_test(
@ -68,16 +65,8 @@ async def perform_simple_test(
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_single_insecure_security_transport_succeeds(): async def test_single_insecure_security_transport_succeeds():
transports_for_initiator = { transports_for_initiator = {"foo": InsecureTransport(initiator_key_pair)}
"foo": InsecureTransport( transports_for_noninitiator = {"foo": InsecureTransport(noninitiator_key_pair)}
initiator_private_key_bytes, initiator_public_key_bytes
)
}
transports_for_noninitiator = {
"foo": InsecureTransport(
noninitiator_private_key_bytes, noninitiator_public_key_bytes
)
}
def assertion_func(conn): def assertion_func(conn):
assert isinstance(conn, InsecureSession) assert isinstance(conn, InsecureSession)
@ -90,14 +79,10 @@ async def test_single_insecure_security_transport_succeeds():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_single_simple_test_security_transport_succeeds(): async def test_single_simple_test_security_transport_succeeds():
transports_for_initiator = { transports_for_initiator = {
"tacos": SimpleSecurityTransport( "tacos": SimpleSecurityTransport(initiator_key_pair, "tacos")
initiator_private_key_bytes, initiator_public_key_bytes, "tacos"
)
} }
transports_for_noninitiator = { transports_for_noninitiator = {
"tacos": SimpleSecurityTransport( "tacos": SimpleSecurityTransport(noninitiator_key_pair, "tacos")
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "tacos"
)
} }
def assertion_func(conn): def assertion_func(conn):
@ -111,17 +96,11 @@ async def test_single_simple_test_security_transport_succeeds():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_two_simple_test_security_transport_for_initiator_succeeds(): async def test_two_simple_test_security_transport_for_initiator_succeeds():
transports_for_initiator = { transports_for_initiator = {
"tacos": SimpleSecurityTransport( "tacos": SimpleSecurityTransport(initiator_key_pair, "tacos"),
initiator_private_key_bytes, initiator_public_key_bytes, "tacos" "shleep": SimpleSecurityTransport(initiator_key_pair, "shleep"),
),
"shleep": SimpleSecurityTransport(
initiator_private_key_bytes, initiator_public_key_bytes, "shleep"
),
} }
transports_for_noninitiator = { transports_for_noninitiator = {
"shleep": SimpleSecurityTransport( "shleep": SimpleSecurityTransport(noninitiator_key_pair, "shleep")
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "shleep"
)
} }
def assertion_func(conn): def assertion_func(conn):
@ -135,17 +114,11 @@ async def test_two_simple_test_security_transport_for_initiator_succeeds():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_two_simple_test_security_transport_for_noninitiator_succeeds(): async def test_two_simple_test_security_transport_for_noninitiator_succeeds():
transports_for_initiator = { transports_for_initiator = {
"tacos": SimpleSecurityTransport( "tacos": SimpleSecurityTransport(initiator_key_pair, "tacos")
initiator_private_key_bytes, initiator_public_key_bytes, "tacos"
)
} }
transports_for_noninitiator = { transports_for_noninitiator = {
"shleep": SimpleSecurityTransport( "shleep": SimpleSecurityTransport(noninitiator_key_pair, "shleep"),
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "shleep" "tacos": SimpleSecurityTransport(noninitiator_key_pair, "tacos"),
),
"tacos": SimpleSecurityTransport(
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "tacos"
),
} }
def assertion_func(conn): def assertion_func(conn):
@ -159,20 +132,12 @@ async def test_two_simple_test_security_transport_for_noninitiator_succeeds():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_two_simple_test_security_transport_for_both_succeeds(): async def test_two_simple_test_security_transport_for_both_succeeds():
transports_for_initiator = { transports_for_initiator = {
"a": SimpleSecurityTransport( "a": SimpleSecurityTransport(initiator_key_pair, "a"),
initiator_private_key_bytes, initiator_public_key_bytes, "a" "b": SimpleSecurityTransport(initiator_key_pair, "b"),
),
"b": SimpleSecurityTransport(
initiator_private_key_bytes, initiator_public_key_bytes, "b"
),
} }
transports_for_noninitiator = { transports_for_noninitiator = {
"b": SimpleSecurityTransport( "b": SimpleSecurityTransport(noninitiator_key_pair, "b"),
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "b" "c": SimpleSecurityTransport(noninitiator_key_pair, "c"),
),
"c": SimpleSecurityTransport(
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "c"
),
} }
def assertion_func(conn): def assertion_func(conn):
@ -186,20 +151,12 @@ async def test_two_simple_test_security_transport_for_both_succeeds():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_multiple_security_none_the_same_fails(): async def test_multiple_security_none_the_same_fails():
transports_for_initiator = { transports_for_initiator = {
"a": SimpleSecurityTransport( "a": SimpleSecurityTransport(initiator_key_pair, "a"),
initiator_private_key_bytes, initiator_public_key_bytes, "a" "b": SimpleSecurityTransport(initiator_key_pair, "b"),
),
"b": SimpleSecurityTransport(
initiator_private_key_bytes, initiator_public_key_bytes, "b"
),
} }
transports_for_noninitiator = { transports_for_noninitiator = {
"d": SimpleSecurityTransport( "d": SimpleSecurityTransport(noninitiator_key_pair, "d"),
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "d" "c": SimpleSecurityTransport(noninitiator_key_pair, "c"),
),
"c": SimpleSecurityTransport(
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "c"
),
} }
def assertion_func(_): def assertion_func(_):

View File

@ -1,7 +1,6 @@
import asyncio import asyncio
from contextlib import suppress from contextlib import suppress
from Crypto.PublicKey import RSA
import multiaddr import multiaddr
from libp2p import new_node from libp2p import new_node
@ -54,8 +53,3 @@ async def perform_two_host_set_up_custom_handler(handler):
# Associate the peer with local ip address (see default parameters of Libp2p()) # Associate the peer with local ip address (see default parameters of Libp2p())
node_a.get_peerstore().add_addrs(node_b.get_id(), node_b.get_addrs(), 10) node_a.get_peerstore().add_addrs(node_b.get_id(), node_b.get_addrs(), 10)
return node_a, node_b return node_a, node_b
def generate_new_private_key():
new_key = RSA.generate(2048, e=65537)
return new_key