nucypher/tests/integration/config/test_configuration_persiste...

120 lines
4.5 KiB
Python
Raw Normal View History

2020-05-23 00:02:00 +00:00
import datetime
2020-05-23 00:02:00 +00:00
import maya
from nucypher.characters.lawful import Bob
from nucypher.config.characters import AliceConfiguration
from nucypher.config.constants import TEMPORARY_DOMAIN
2020-05-23 00:02:00 +00:00
from nucypher.crypto.powers import DecryptingPower, SigningPower
2023-06-16 13:59:21 +00:00
from tests.constants import INSECURE_DEVELOPMENT_PASSWORD, MOCK_ETH_PROVIDER_URI
2020-05-23 00:02:00 +00:00
from tests.utils.middleware import MockRestMiddleware
def test_alices_powers_are_persistent(
ursulas, temp_dir_path, test_registry_source_manager, testerchain
):
2020-05-23 00:02:00 +00:00
# Create a non-learning AliceConfiguration
2021-05-11 17:55:13 +00:00
config_root = temp_dir_path / 'nucypher-custom-alice-config'
2020-05-23 00:02:00 +00:00
alice_config = AliceConfiguration(
2023-06-16 13:59:21 +00:00
eth_provider_uri=MOCK_ETH_PROVIDER_URI,
2021-01-22 08:37:30 +00:00
config_root=config_root,
2023-06-16 13:59:21 +00:00
network_middleware=MockRestMiddleware(eth_provider_uri=MOCK_ETH_PROVIDER_URI),
domain=TEMPORARY_DOMAIN,
payment_network=TEMPORARY_DOMAIN,
checksum_address=testerchain.alice_account,
2020-05-23 00:02:00 +00:00
start_learning_now=False,
save_metadata=False,
reload_metadata=False,
known_nodes=ursulas,
2020-05-23 00:02:00 +00:00
)
# Generate keys and write them the disk
alice_config.initialize(password=INSECURE_DEVELOPMENT_PASSWORD)
2021-06-08 20:59:43 +00:00
# Unlock Alice's keystore
alice_config.keystore.unlock(password=INSECURE_DEVELOPMENT_PASSWORD)
2020-05-23 00:02:00 +00:00
# Produce an Alice
alice = alice_config() # or alice_config.produce()
# Save Alice's node configuration file to disk for later use
alice_config_file = alice_config.to_configuration_file()
# Let's save Alice's public keys too to check they are correctly restored later
alices_verifying_key = alice.public_keys(SigningPower)
alices_receiving_key = alice.public_keys(DecryptingPower)
# Next, let's fix a label for all the policies we will create later.
label = b"this_is_the_path_to_which_access_is_being_granted"
# Even before creating the policies, we can know what will be its public key.
# This can be used by Enrico (i.e., a Data Source) to encrypt messages
# before Alice grants access to Bobs.
policy_pubkey = alice.get_policy_encrypting_key_from_label(label)
# Now, let's create a policy for some Bob.
threshold, shares = 3, 4
2020-05-23 00:02:00 +00:00
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
2023-06-16 13:59:21 +00:00
bob = Bob(
start_learning_now=False,
domain=TEMPORARY_DOMAIN,
eth_provider_uri=MOCK_ETH_PROVIDER_URI,
network_middleware=MockRestMiddleware(eth_provider_uri=MOCK_ETH_PROVIDER_URI),
)
2020-05-23 00:02:00 +00:00
bob_policy = alice.grant(bob, label, threshold=threshold, shares=shares, expiration=policy_end_datetime)
2020-05-23 00:02:00 +00:00
assert policy_pubkey == bob_policy.public_key
# ... and Alice and her configuration disappear.
alice.disenchant()
2020-05-23 00:02:00 +00:00
del alice
del alice_config
###################################
# Some time passes. #
# ... #
# (jmyles plays the Song of Time) #
# ... #
# Alice appears again. #
###################################
# A new Alice is restored from the configuration file
new_alice_config = AliceConfiguration.from_configuration_file(
filepath=alice_config_file,
2023-06-16 13:59:21 +00:00
network_middleware=MockRestMiddleware(eth_provider_uri=MOCK_ETH_PROVIDER_URI),
2020-05-23 00:02:00 +00:00
start_learning_now=False,
config_root=config_root,
known_nodes=ursulas,
2020-05-23 00:02:00 +00:00
)
2021-06-08 20:59:43 +00:00
# Alice unlocks her restored keystore from disk
new_alice_config.keystore.unlock(password=INSECURE_DEVELOPMENT_PASSWORD)
2020-05-23 00:02:00 +00:00
new_alice = new_alice_config()
# First, we check that her public keys are correctly restored
assert alices_verifying_key == new_alice.public_keys(SigningPower)
assert alices_receiving_key == new_alice.public_keys(DecryptingPower)
# Bob's eldest brother, Roberto, appears too
2023-06-16 13:59:21 +00:00
roberto = Bob(
domain=TEMPORARY_DOMAIN,
eth_provider_uri=MOCK_ETH_PROVIDER_URI,
start_learning_now=False,
network_middleware=MockRestMiddleware(eth_provider_uri=MOCK_ETH_PROVIDER_URI),
)
2020-05-23 00:02:00 +00:00
# Alice creates a new policy for Roberto. Note how all the parameters
# except for the label (i.e., recipient, m, n, policy_end) are different
# from previous policy
threshold, shares = 2, 5
2020-05-23 00:02:00 +00:00
policy_end_datetime = maya.now() + datetime.timedelta(days=3)
roberto_policy = new_alice.grant(roberto, label, threshold=threshold, shares=shares, expiration=policy_end_datetime)
2020-05-23 00:02:00 +00:00
# Both policies must share the same public key (i.e., the policy public key)
assert policy_pubkey == roberto_policy.public_key
new_alice.disenchant()