def test_initialize_bob_with_custom_configuration_root(custom_filepath, click_runner): # Use a custom local filepath for configuration init_args = ('bob', 'init', '--network', TEMPORARY_DOMAIN, '--federated-only', '--config-root', custom_filepath) user_input = '{password}\n{password}'.format(password=INSECURE_DEVELOPMENT_PASSWORD, ip=MOCK_IP_ADDRESS) result = click_runner.invoke(nucypher_cli, init_args, input=user_input, catch_exceptions=False) assert result.exit_code == 0, result.exception # CLI Output assert MOCK_CUSTOM_INSTALLATION_PATH in result.output, "Configuration not in system temporary directory" assert "nucypher bob run" in result.output, 'Help message is missing suggested command' assert 'IPv4' not in result.output # Files and Directories assert os.path.isdir(custom_filepath), 'Configuration file does not exist' assert os.path.isdir(os.path.join(custom_filepath, 'keyring')), 'Keyring does not exist' assert os.path.isdir(os.path.join(custom_filepath, 'known_nodes')), 'known_nodes directory does not exist' custom_config_filepath = os.path.join(custom_filepath, BobConfiguration.generate_filename()) assert os.path.isfile(custom_config_filepath), 'Configuration file does not exist' # Auth assert 'Enter NuCypher keyring password' in result.output, 'WARNING: User was not prompted for password' assert 'Repeat for confirmation:' in result.output, 'User was not prompted to confirm password'
def test_bob_control_starts_with_preexisting_configuration(click_runner, custom_filepath: Path): custom_config_filepath = custom_filepath / BobConfiguration.generate_filename() init_args = ('bob', 'run', '--dry-run', '--lonely', '--config-file', str(custom_config_filepath.absolute())) result = click_runner.invoke(nucypher_cli, init_args, input=FAKE_PASSWORD_CONFIRMED) assert result.exit_code == 0, result.exception assert "Bob Verifying Key" in result.output assert "Bob Encrypting Key" in result.output
def test_initialize_bob_with_custom_configuration_root(click_runner, custom_filepath: Path): # Use a custom local filepath for configuration init_args = ('bob', 'init', '--network', TEMPORARY_DOMAIN, '--federated-only', '--config-root', str(custom_filepath.absolute())) result = click_runner.invoke(nucypher_cli, init_args, input=FAKE_PASSWORD_CONFIRMED, catch_exceptions=False) assert result.exit_code == 0, result.exception # CLI Output assert str(MOCK_CUSTOM_INSTALLATION_PATH) in result.output, "Configuration not in system temporary directory" assert "nucypher bob run" in result.output, 'Help message is missing suggested command' assert 'IPv4' not in result.output # Files and Directories assert custom_filepath.is_dir(), 'Configuration file does not exist' assert (custom_filepath / 'keystore').is_dir(), 'Keystore does not exist' assert (custom_filepath / 'known_nodes').is_dir(), 'known_nodes directory does not exist' custom_config_filepath = custom_filepath / BobConfiguration.generate_filename() assert custom_config_filepath.is_file(), 'Configuration file does not exist' # Auth assert COLLECT_NUCYPHER_PASSWORD in result.output, 'WARNING: User was not prompted for password' assert 'Repeat for confirmation:' in result.output, 'User was not prompted to confirm password'
def test_bob_destroy(click_runner, custom_filepath: Path): custom_config_filepath = custom_filepath / BobConfiguration.generate_filename() destroy_args = ('bob', 'destroy', '--config-file', str(custom_config_filepath.absolute()), '--force') result = click_runner.invoke(nucypher_cli, destroy_args, catch_exceptions=False) assert result.exit_code == 0, result.exception assert SUCCESSFUL_DESTRUCTION in result.output assert not custom_config_filepath.exists(), "Bob config file was deleted"
def test_bob_make_card(click_runner, custom_filepath: Path, mocker): mock_save_card = mocker.patch.object(Card, 'save') custom_config_filepath = custom_filepath / BobConfiguration.generate_filename() command = ('bob', 'make-card', '--nickname', 'anders', '--config-file', str(custom_config_filepath.absolute())) result = click_runner.invoke(nucypher_cli, command, input=FAKE_PASSWORD_CONFIRMED, catch_exceptions=False) assert result.exit_code == 0 assert "Saved new character card " in result.output mock_save_card.assert_called_once()
def test_bob_view_with_preexisting_configuration(click_runner, custom_filepath: Path): custom_config_filepath = custom_filepath / BobConfiguration.generate_filename() view_args = ('bob', 'config', '--config-file', str(custom_config_filepath.absolute())) result = click_runner.invoke(nucypher_cli, view_args, input=FAKE_PASSWORD_CONFIRMED) assert result.exit_code == 0, result.exception assert "checksum_address" in result.output assert "domain" in result.output assert TEMPORARY_DOMAIN in result.output assert str(custom_filepath) in result.output
def test_bob_make_card(click_runner, custom_filepath): custom_config_filepath = os.path.join(custom_filepath, BobConfiguration.generate_filename()) command = ('bob', 'make-card', '--nickname', 'anders', '--config-file', custom_config_filepath) result = click_runner.invoke(nucypher_cli, command, input=FAKE_PASSWORD_CONFIRMED, catch_exceptions=False) assert result.exit_code == 0 assert "Saved new character card " in result.output
def test_bob_control_starts_with_preexisting_configuration(click_runner, custom_filepath): custom_config_filepath = os.path.join(custom_filepath, BobConfiguration.generate_filename()) init_args = ('bob', 'run', '--dry-run', '--config-file', custom_config_filepath) user_input = '{password}\n{password}\n'.format(password=INSECURE_DEVELOPMENT_PASSWORD) result = click_runner.invoke(nucypher_cli, init_args, input=user_input) assert result.exit_code == 0, result.exception assert "Bob Verifying Key" in result.output assert "Bob Encrypting Key" in result.output
def test_bob_view_with_preexisting_configuration(click_runner, custom_filepath): custom_config_filepath = os.path.join(custom_filepath, BobConfiguration.generate_filename()) view_args = ('bob', 'config', '--config-file', custom_config_filepath) user_input = '{password}\n{password}\n'.format(password=INSECURE_DEVELOPMENT_PASSWORD) result = click_runner.invoke(nucypher_cli, view_args, input=user_input) assert result.exit_code == 0, result.exception assert "checksum_address" in result.output assert "domains" in result.output assert TEMPORARY_DOMAIN in result.output assert custom_filepath in result.output
def test_cli_lifecycle(click_runner, testerchain, random_policy_label, federated_ursulas, blockchain_ursulas, custom_filepath, custom_filepath_2, federated): """ This is an end to end integration test that runs each cli call in it's own process using only CLI character control entry points, and a mock side channel that runs in the control process """ # Boring Setup Stuff alice_config_root = custom_filepath bob_config_root = custom_filepath_2 envvars = {'NUCYPHER_KEYRING_PASSWORD': INSECURE_DEVELOPMENT_PASSWORD} # A side channel exists - Perhaps a dApp side_channel = MockSideChannel() shutil.rmtree(custom_filepath, ignore_errors=True) shutil.rmtree(custom_filepath_2, ignore_errors=True) """ Scene 1: Alice Installs nucypher to a custom filepath and examines her configuration """ # Alice performs an installation for the first time alice_init_args = ('alice', 'init', '--network', TEMPORARY_DOMAIN, '--config-root', alice_config_root) if federated: alice_init_args += ('--federated-only', ) else: alice_init_args += ('--provider', TEST_PROVIDER_URI, '--no-registry', '--pay-with', testerchain.alice_account) alice_init_response = click_runner.invoke(nucypher_cli, alice_init_args, catch_exceptions=False, env=envvars) assert alice_init_response.exit_code == 0 # Alice uses her configuration file to run the character "view" command alice_configuration_file_location = os.path.join( alice_config_root, AliceConfiguration.generate_filename()) alice_view_args = ('alice', 'public-keys', '--json-ipc', '--config-file', alice_configuration_file_location) alice_view_result = click_runner.invoke( nucypher_cli, alice_view_args, input=INSECURE_DEVELOPMENT_PASSWORD, catch_exceptions=False, env=envvars) assert alice_view_result.exit_code == 0 alice_view_response = json.loads(alice_view_result.output) # Alice expresses her desire to participate in data sharing with nucypher # by saving her public key somewhere Bob and Enrico can find it. side_channel.save_alice_pubkey( alice_view_response['result']['alice_verifying_key']) """ Scene 2: Bob installs nucypher, examines his configuration and expresses his interest to participate in data retrieval by posting his public keys somewhere public (side-channel). """ bob_init_args = ('bob', 'init', '--network', TEMPORARY_DOMAIN, '--config-root', bob_config_root) if federated: bob_init_args += ('--federated-only', ) else: bob_init_args += ('--provider', TEST_PROVIDER_URI, '--no-registry', '--checksum-address', testerchain.bob_account) bob_init_response = click_runner.invoke(nucypher_cli, bob_init_args, catch_exceptions=False, env=envvars) assert bob_init_response.exit_code == 0 # Alice uses her configuration file to run the character "view" command bob_configuration_file_location = os.path.join( bob_config_root, BobConfiguration.generate_filename()) bob_view_args = ('bob', 'public-keys', '--json-ipc', '--config-file', bob_configuration_file_location) bob_view_result = click_runner.invoke(nucypher_cli, bob_view_args, catch_exceptions=False, env=envvars) assert bob_view_result.exit_code == 0 bob_view_response = json.loads(bob_view_result.output) # Bob interacts with the sidechannel bob_public_keys = MockSideChannel.BobPublicKeys( bob_view_response['result']['bob_encrypting_key'], bob_view_response['result']['bob_verifying_key']) side_channel.save_bob_public_keys(bob_public_keys) """ Scene 3: Alice derives a policy keypair, and saves it's public key to a sidechannel. """ random_label = random_policy_label.decode() # Unicode string derive_args = ('alice', 'derive-policy-pubkey', '--mock-networking', '--json-ipc', '--config-file', alice_configuration_file_location, '--label', random_label) derive_response = click_runner.invoke(nucypher_cli, derive_args, catch_exceptions=False, env=envvars) assert derive_response.exit_code == 0 derive_response = json.loads(derive_response.output) assert derive_response['result']['label'] == random_label # Alice and the sidechannel: at Tinagre policy = MockSideChannel.PolicyAndLabel( encrypting_key=derive_response['result']['policy_encrypting_key'], label=derive_response['result']['label']) side_channel.save_policy(policy=policy) """ Scene 4: Enrico encrypts some data for some policy public key and saves it to a side channel. """ def enrico_encrypts(): # Fetch! policy = side_channel.fetch_policy() enrico_args = ('enrico', 'encrypt', '--json-ipc', '--policy-encrypting-key', policy.encrypting_key, '--message', PLAINTEXT) encrypt_result = click_runner.invoke(nucypher_cli, enrico_args, catch_exceptions=False, env=envvars) assert encrypt_result.exit_code == 0 encrypt_result = json.loads(encrypt_result.output) encrypted_message = encrypt_result['result'][ 'message_kit'] # type: str side_channel.save_message_kit(message_kit=encrypted_message) return encrypt_result def _alice_decrypts(encrypt_result): """ alice forgot what exactly she encrypted for bob. she decrypts it just to make sure. """ policy = side_channel.fetch_policy() alice_signing_key = side_channel.fetch_alice_pubkey() message_kit = encrypt_result['result']['message_kit'] decrypt_args = ( 'alice', 'decrypt', '--mock-networking', '--json-ipc', '--config-file', alice_configuration_file_location, '--message-kit', message_kit, '--label', policy.label, ) if federated: decrypt_args += ('--federated-only', ) decrypt_response_fail = click_runner.invoke(nucypher_cli, decrypt_args[0:7], catch_exceptions=False, env=envvars) assert decrypt_response_fail.exit_code == 2 decrypt_response = click_runner.invoke(nucypher_cli, decrypt_args, catch_exceptions=False, env=envvars) decrypt_result = json.loads(decrypt_response.output) for cleartext in decrypt_result['result']['cleartexts']: assert b64decode(cleartext.encode()).decode() == PLAINTEXT # replenish the side channel side_channel.save_policy(policy=policy) side_channel.save_alice_pubkey(alice_signing_key) return encrypt_result """ Scene 5: Alice grants access to Bob: We catch up with Alice later on, but before she has learned about existing Ursulas... """ if federated: teacher = list(federated_ursulas)[0] else: teacher = list(blockchain_ursulas)[1] teacher_uri = teacher.seed_node_metadata(as_teacher_uri=True) # Some Ursula is running somewhere def _run_teacher(_encrypt_result): start_pytest_ursula_services(ursula=teacher) return teacher_uri def _grant(teacher_uri): # Alice fetched Bob's public keys from the side channel bob_keys = side_channel.fetch_bob_public_keys() bob_encrypting_key = bob_keys.bob_encrypting_key bob_verifying_key = bob_keys.bob_verifying_key grant_args = ('alice', 'grant', '--mock-networking', '--json-ipc', '--network', TEMPORARY_DOMAIN, '--teacher', teacher_uri, '--config-file', alice_configuration_file_location, '--m', 2, '--n', 3, '--value', Web3.toWei(1, 'ether'), '--expiration', (maya.now() + datetime.timedelta(days=3)).iso8601(), '--label', random_label, '--bob-encrypting-key', bob_encrypting_key, '--bob-verifying-key', bob_verifying_key) if federated: grant_args += ('--federated-only', ) else: grant_args += ('--provider', TEST_PROVIDER_URI) grant_result = click_runner.invoke(nucypher_cli, grant_args, catch_exceptions=False, env=envvars) assert grant_result.exit_code == 0 grant_result = json.loads(grant_result.output) # TODO: Expand test to consider manual treasure map handing # # Alice puts the Treasure Map somewhere Bob can get it. # side_channel.save_treasure_map(treasure_map=grant_result['result']['treasure_map']) return grant_result def _bob_retrieves(_grant_result): """ Scene 6: Bob retrieves encrypted data from the side channel and uses nucypher to re-encrypt it """ # Bob interacts with a sidechannel ciphertext_message_kit = side_channel.fetch_message_kit() policy = side_channel.fetch_policy() policy_encrypting_key, label = policy alice_signing_key = side_channel.fetch_alice_pubkey() retrieve_args = ('bob', 'retrieve', '--mock-networking', '--json-ipc', '--teacher', teacher_uri, '--config-file', bob_configuration_file_location, '--message-kit', ciphertext_message_kit, '--label', label, '--policy-encrypting-key', policy_encrypting_key, '--alice-verifying-key', alice_signing_key) if federated: retrieve_args += ('--federated-only', ) retrieve_response = click_runner.invoke(nucypher_cli, retrieve_args, catch_exceptions=False, env=envvars) assert retrieve_response.exit_code == 0 retrieve_response = json.loads(retrieve_response.output) for cleartext in retrieve_response['result']['cleartexts']: assert b64decode(cleartext.encode()).decode() == PLAINTEXT return # Run the Callbacks d = threads.deferToThread(enrico_encrypts) # scene 4 d.addCallback(_alice_decrypts) # scene 5 (uncertainty) d.addCallback(_run_teacher) # scene 6 (preamble) d.addCallback(_grant) # scene 7 d.addCallback(_bob_retrieves) # scene 8 yield d
def test_bob_retrieves_twice_via_cli(click_runner, capsule_side_channel, enacted_federated_policy, federated_ursulas, custom_filepath_2, federated_alice ): teacher = list(federated_ursulas)[0] first_message = capsule_side_channel.reset(plaintext_passthrough=True) three_message_kits = [capsule_side_channel(), capsule_side_channel(), capsule_side_channel()] bob_config_root = custom_filepath_2 bob_configuration_file_location = os.path.join(bob_config_root, BobConfiguration.generate_filename()) label = enacted_federated_policy.label # I already have a Bob. # Need to init so that the config file is made, even though we won't use this Bob. bob_init_args = ('bob', 'init', '--network', TEMPORARY_DOMAIN, '--config-root', bob_config_root, '--federated-only') envvars = {'NUCYPHER_KEYRING_PASSWORD': INSECURE_DEVELOPMENT_PASSWORD} log.info("Init'ing a normal Bob; we'll substitute the Policy Bob in shortly.") bob_init_response = click_runner.invoke(nucypher_cli, bob_init_args, catch_exceptions=False, env=envvars) message_kit_bytes = bytes(three_message_kits[0]) message_kit_b64_bytes = b64encode(message_kit_bytes) UmbralMessageKit.from_bytes(message_kit_bytes) retrieve_args = ('bob', 'retrieve', '--mock-networking', '--json-ipc', '--teacher', teacher.seed_node_metadata(as_teacher_uri=True), '--config-file', bob_configuration_file_location, '--message-kit', message_kit_b64_bytes, '--label', label, '--policy-encrypting-key', bytes(federated_alice.get_policy_encrypting_key_from_label(label)).hex(), '--alice-verifying-key', bytes(federated_alice.public_keys(SigningPower)).hex() ) from nucypher.cli import actions def substitute_bob(*args, **kwargs): log.info("Substituting the Policy's Bob in CLI runtime.") this_fuckin_guy = enacted_federated_policy.bob somebody_else = Ursula.from_teacher_uri(teacher_uri=kwargs['teacher_uri'], min_stake=0, federated_only=True, network_middleware=this_fuckin_guy.network_middleware) this_fuckin_guy.remember_node(somebody_else) this_fuckin_guy.controller.emitter = JSONRPCStdoutEmitter() return this_fuckin_guy _old_make_character_function = actions.make_cli_character try: log.info("Patching make_cli_character with substitute_bob") actions.make_cli_character = substitute_bob # Once... with GlobalLoggerSettings.pause_all_logging_while(): retrieve_response = click_runner.invoke(nucypher_cli, retrieve_args, catch_exceptions=False, env=envvars) log.info(f"First retrieval response: {retrieve_response.output}") assert retrieve_response.exit_code == 0 retrieve_response = json.loads(retrieve_response.output) for cleartext in retrieve_response['result']['cleartexts']: assert cleartext.encode() == capsule_side_channel.plaintexts[1] # and again! with GlobalLoggerSettings.pause_all_logging_while(): retrieve_response = click_runner.invoke(nucypher_cli, retrieve_args, catch_exceptions=False, env=envvars) log.info(f"Second retrieval response: {retrieve_response.output}") assert retrieve_response.exit_code == 0 retrieve_response = json.loads(retrieve_response.output) for cleartext in retrieve_response['result']['cleartexts']: assert cleartext.encode() == capsule_side_channel.plaintexts[1] finally: log.info("un-patching make_cli_character") actions.make_cli_character = _old_make_character_function
def run_entire_cli_lifecycle(click_runner, random_policy_label, ursulas, custom_filepath, custom_filepath_2, registry_filepath=None, testerchain=None): """ This is an end to end integration test that runs each cli call in it's own process using only CLI character control entry points, and a mock side channel that runs in the control process """ federated = list(ursulas)[0].federated_only # Boring Setup Stuff alice_config_root = custom_filepath bob_config_root = custom_filepath_2 envvars = { NUCYPHER_ENVVAR_KEYSTORE_PASSWORD: INSECURE_DEVELOPMENT_PASSWORD, NUCYPHER_ENVVAR_ALICE_ETH_PASSWORD: INSECURE_DEVELOPMENT_PASSWORD, NUCYPHER_ENVVAR_BOB_ETH_PASSWORD: INSECURE_DEVELOPMENT_PASSWORD } # A side channel exists - Perhaps a dApp side_channel = MockSideChannel() shutil.rmtree(str(custom_filepath), ignore_errors=True) shutil.rmtree(str(custom_filepath_2), ignore_errors=True) """ Scene 1: Alice Installs nucypher to a custom filepath and examines her configuration """ # Alice performs an installation for the first time alice_init_args = ('alice', 'init', '--network', TEMPORARY_DOMAIN, '--config-root', str(alice_config_root.absolute())) if federated: alice_init_args += ('--federated-only', ) else: alice_init_args += ('--eth-provider', TEST_ETH_PROVIDER_URI, '--pay-with', testerchain.alice_account, '--payment-network', TEMPORARY_DOMAIN, '--registry-filepath', str(registry_filepath.absolute())) alice_init_response = click_runner.invoke(nucypher_cli, alice_init_args, catch_exceptions=False, env=envvars) assert alice_init_response.exit_code == 0 # Prevent previous global logger settings set by above command from writing non-IPC messages to stdout GlobalLoggerSettings.stop_console_logging() # Alice uses her configuration file to run the character "view" command alice_configuration_file_location = alice_config_root / AliceConfiguration.generate_filename( ) alice_view_args = ('alice', 'public-keys', '--json-ipc', '--config-file', str(alice_configuration_file_location.absolute())) alice_view_result = click_runner.invoke( nucypher_cli, alice_view_args, input=INSECURE_DEVELOPMENT_PASSWORD, catch_exceptions=False, env=envvars) assert alice_view_result.exit_code == 0 try: alice_view_response = json.loads(alice_view_result.output) except JSONDecodeError: pytest.fail("Invalid JSON response from JSON-RPC Character process.") # Alice expresses her desire to participate in data sharing with nucypher # by saving her public key somewhere Bob and Enrico can find it. side_channel.save_alice_pubkey( alice_view_response['result']['alice_verifying_key']) """ Scene 2: Bob installs nucypher, examines his configuration and expresses his interest to participate in data retrieval by posting his public keys somewhere public (side-channel). """ bob_init_args = ('bob', 'init', '--network', TEMPORARY_DOMAIN, '--config-root', str(bob_config_root.absolute())) if federated: bob_init_args += ('--federated-only', ) else: bob_init_args += ('--eth-provider', TEST_ETH_PROVIDER_URI, '--registry-filepath', str(registry_filepath.absolute()), '--checksum-address', testerchain.bob_account) bob_init_response = click_runner.invoke(nucypher_cli, bob_init_args, catch_exceptions=False, env=envvars) assert bob_init_response.exit_code == 0 # Alice uses her configuration file to run the character "view" command bob_configuration_file_location = bob_config_root / BobConfiguration.generate_filename( ) bob_view_args = ( 'bob', 'public-keys', '--json-ipc', '--mock-networking', # TODO: It's absurd for this public-keys command to connect at all. 1710 '--lonely', # TODO: This needs to be implied by `public-keys`. '--config-file', str(bob_configuration_file_location.absolute())) bob_view_result = click_runner.invoke(nucypher_cli, bob_view_args, catch_exceptions=False, env=envvars) assert bob_view_result.exit_code == 0 bob_view_response = json.loads(bob_view_result.output) # Bob interacts with the sidechannel bob_public_keys = MockSideChannel.BobPublicKeys( bob_view_response['result']['bob_encrypting_key'], bob_view_response['result']['bob_verifying_key']) side_channel.save_bob_public_keys(bob_public_keys) """ Scene 3: Alice derives a policy keypair, and saves its public key to a sidechannel. """ random_label = random_policy_label.decode() # Unicode string derive_args = ('alice', 'derive-policy-pubkey', '--mock-networking', '--json-ipc', '--config-file', str(alice_configuration_file_location.absolute()), '--label', random_label) derive_response = click_runner.invoke(nucypher_cli, derive_args, catch_exceptions=False, env=envvars) assert derive_response.exit_code == 0 derive_response = json.loads(derive_response.output) assert derive_response['result']['label'] == random_label # Alice and the sidechannel: at Tinagre policy = MockSideChannel.PolicyAndLabel( encrypting_key=derive_response['result']['policy_encrypting_key'], label=derive_response['result']['label']) side_channel.save_policy(policy=policy) """ Scene 4: Enrico encrypts some data for some policy public key and saves it to a side channel. """ def enrico_encrypts(): # Fetch! policy = side_channel.fetch_policy() enrico_args = ('enrico', 'encrypt', '--json-ipc', '--policy-encrypting-key', policy.encrypting_key, '--message', PLAINTEXT) encrypt_result = click_runner.invoke(nucypher_cli, enrico_args, catch_exceptions=False, env=envvars) assert encrypt_result.exit_code == 0 encrypt_result = json.loads(encrypt_result.output) encrypted_message = encrypt_result['result'][ 'message_kit'] # type: str side_channel.save_message_kit(message_kit=encrypted_message) return encrypt_result def _alice_decrypts(encrypt_result): """ alice forgot what exactly she encrypted for bob. she decrypts it just to make sure. """ policy = side_channel.fetch_policy() alice_signing_key = side_channel.fetch_alice_pubkey() message_kit = encrypt_result['result']['message_kit'] decrypt_args = ( 'alice', 'decrypt', '--mock-networking', '--json-ipc', '--config-file', str(alice_configuration_file_location.absolute()), '--message-kit', message_kit, '--label', policy.label, ) if federated: decrypt_args += ('--federated-only', ) decrypt_response_fail = click_runner.invoke(nucypher_cli, decrypt_args[0:7], catch_exceptions=False, env=envvars) assert decrypt_response_fail.exit_code == 2 decrypt_response = click_runner.invoke(nucypher_cli, decrypt_args, catch_exceptions=False, env=envvars) decrypt_result = json.loads(decrypt_response.output) for cleartext in decrypt_result['result']['cleartexts']: assert b64decode(cleartext.encode()).decode() == PLAINTEXT # replenish the side channel side_channel.save_policy(policy=policy) side_channel.save_alice_pubkey(alice_signing_key) return encrypt_result """ Scene 5: Alice grants access to Bob: We catch up with Alice later on, but before she has learned about existing Ursulas... """ if federated: teacher = list(ursulas)[0] else: teacher = list(ursulas)[1] teacher_uri = teacher.seed_node_metadata(as_teacher_uri=True) # Some Ursula is running somewhere def _run_teacher(_encrypt_result): # start_pytest_ursula_services(ursula=teacher) return teacher_uri def _grant(teacher_uri): # Alice fetched Bob's public keys from the side channel bob_keys = side_channel.fetch_bob_public_keys() bob_encrypting_key = bob_keys.bob_encrypting_key bob_verifying_key = bob_keys.bob_verifying_key if federated: current_time = maya.now() else: current_time = maya.MayaDT( epoch=testerchain.client.get_blocktime()) expiration = (current_time + datetime.timedelta(days=2) ).datetime().strftime("%Y-%m-%d %H:%M:%S") grant_args = ('alice', 'grant', '--mock-networking', '--json-ipc', '--network', TEMPORARY_DOMAIN, '--teacher', teacher_uri, '--config-file', str(alice_configuration_file_location.absolute()), '-m', 2, '-n', 3, '--expiration', expiration, '--label', random_label, '--bob-encrypting-key', bob_encrypting_key, '--bob-verifying-key', bob_verifying_key) if federated: grant_args += ('--federated-only', ) else: grant_args += ('--eth-provider', TEST_ETH_PROVIDER_URI, '--value', Web3.toWei(9, 'gwei')) grant_result = click_runner.invoke(nucypher_cli, grant_args, catch_exceptions=False, env=envvars) assert grant_result.exit_code == 0, (grant_result.output, grant_result.exception) grant_result = json.loads(grant_result.output) # Alice puts the Treasure Map somewhere Bob can get it. side_channel.save_treasure_map( treasure_map=grant_result['result']['treasure_map']) return grant_result def _bob_retrieves(_grant_result): """ Scene 6: Bob retrieves encrypted data from the side channel and uses nucypher to re-encrypt it """ # Bob interacts with a sidechannel ciphertext_message_kit = side_channel.fetch_message_kit() policy = side_channel.fetch_policy() policy_encrypting_key, label = policy alice_signing_key = side_channel.fetch_alice_pubkey() retrieve_args = ('bob', 'retrieve-and-decrypt', '--mock-networking', '--json-ipc', '--teacher', teacher_uri, '--config-file', str(bob_configuration_file_location.absolute()), '--message-kit', ciphertext_message_kit, '--treasure-map', side_channel.fetch_treasure_map(), '--alice-verifying-key', alice_signing_key) retrieve_response = click_runner.invoke(nucypher_cli, retrieve_args, catch_exceptions=False, env=envvars) assert retrieve_response.exit_code == 0 retrieve_response = json.loads(retrieve_response.output) for cleartext in retrieve_response['result']['cleartexts']: assert b64decode(cleartext.encode()).decode() == PLAINTEXT return # Run the Callbacks d = threads.deferToThread(enrico_encrypts) # scene 4 d.addCallback(_alice_decrypts) # scene 5 (uncertainty) d.addCallback(_run_teacher) # scene 6 (preamble) d.addCallback(_grant) # scene 7 d.addCallback(_bob_retrieves) # scene 8 return d
def test_bob_retrieve_and_decrypt(click_runner, capsule_side_channel, enacted_federated_policy, federated_ursulas, custom_filepath_2: Path, federated_alice, federated_bob, mocker): teacher = list(federated_ursulas)[0] first_message, _ = capsule_side_channel.reset(plaintext_passthrough=True) message_kits_b64 = [ b64encode(bytes(message_kit)).decode() for message_kit in [ first_message, capsule_side_channel(), capsule_side_channel(), capsule_side_channel() ] ] bob_config_root = custom_filepath_2 bob_configuration_file_location = bob_config_root / BobConfiguration.generate_filename( ) # I already have a Bob. # Need to init so that the config file is made, even though we won't use this Bob. bob_init_args = ('bob', 'init', '--network', TEMPORARY_DOMAIN, '--config-root', str(bob_config_root.absolute()), '--federated-only') envvars = {'NUCYPHER_KEYSTORE_PASSWORD': INSECURE_DEVELOPMENT_PASSWORD} log.info( "Init'ing a normal Bob; we'll substitute the Policy Bob in shortly.") bob_init_response = click_runner.invoke(nucypher_cli, bob_init_args, catch_exceptions=False, env=envvars) assert bob_init_response.exit_code == 0, bob_init_response.output teacher_uri = teacher.seed_node_metadata(as_teacher_uri=True) bob_config_file = str(bob_configuration_file_location.absolute()) policy_encrypting_key_hex = bytes( enacted_federated_policy.public_key).hex() alice_verifying_key_hex = bytes( federated_alice.public_keys(SigningPower)).hex() encrypted_treasure_map_b64 = b64encode( bytes(enacted_federated_policy.treasure_map)).decode() # Retrieve without --alice_verifying_key or --alice specified - tests override of schema definition for CLI retrieve_args = ( 'bob', 'retrieve-and-decrypt', '--mock-networking', '--json-ipc', '--teacher', teacher_uri, '--config-file', bob_config_file, '--message-kit', message_kits_b64[0], '--treasure-map', encrypted_treasure_map_b64, ) retrieve_response = click_runner.invoke(nucypher_cli, retrieve_args, catch_exceptions=False, env=envvars) assert retrieve_response.exit_code != 0, "no alice_verifying_key specified" assert "Pass either '--alice_verifying_key' or '--alice'; got neither" in retrieve_response.output, retrieve_response.output # Retrieve with both --alice_verifying_key and --alice specified - should not be allowed retrieve_args = ( 'bob', 'retrieve-and-decrypt', '--mock-networking', '--json-ipc', '--teacher', teacher_uri, '--config-file', bob_config_file, '--message-kit', message_kits_b64[0], '--alice-verifying-key', alice_verifying_key_hex, '--alice', 'rando-card-nickname', '--treasure-map', encrypted_treasure_map_b64, ) retrieve_response = click_runner.invoke(nucypher_cli, retrieve_args, catch_exceptions=False, env=envvars) assert retrieve_response.exit_code != 0, "both alice_verifying_key and alice can't be specified" assert "Pass either '--alice_verifying_key' or '--alice'; got both" in retrieve_response.output, retrieve_response.output # # Perform actual retrieve and decrypts # def substitute_bob(*args, **kwargs): log.info("Substituting the Bob used in the CLI runtime.") this_fuckin_guy = federated_bob this_fuckin_guy.controller.emitter = JSONRPCStdoutEmitter() return this_fuckin_guy with mocker.patch.object(BobCharacterOptions, 'create_character', side_effect=substitute_bob): # # Retrieve one message kit # retrieve_args = ( 'bob', 'retrieve-and-decrypt', '--mock-networking', '--json-ipc', '--teacher', teacher_uri, '--config-file', bob_config_file, '--message-kit', message_kits_b64[0], '--alice-verifying-key', alice_verifying_key_hex, '--treasure-map', encrypted_treasure_map_b64, ) with GlobalLoggerSettings.pause_all_logging_while(): retrieve_response = click_runner.invoke(nucypher_cli, retrieve_args, catch_exceptions=False, env=envvars) log.info(f"Retrieval response: {retrieve_response.output}") assert retrieve_response.exit_code == 0, retrieve_response.output retrieve_response = json.loads(retrieve_response.output) cleartexts = retrieve_response['result']['cleartexts'] assert len(cleartexts) == 1 assert cleartexts[0].encode() == capsule_side_channel.plaintexts[0] # # Retrieve and decrypt multiple message kits # retrieve_args = ( 'bob', 'retrieve-and-decrypt', '--mock-networking', '--json-ipc', '--teacher', teacher_uri, '--config-file', bob_config_file, # use multiple message kits '--message-kit', message_kits_b64[0], '--message-kit', message_kits_b64[1], '--message-kit', message_kits_b64[2], '--message-kit', message_kits_b64[3], '--alice-verifying-key', alice_verifying_key_hex, '--treasure-map', encrypted_treasure_map_b64) with GlobalLoggerSettings.pause_all_logging_while(): retrieve_response = click_runner.invoke(nucypher_cli, retrieve_args, catch_exceptions=False, env=envvars) log.info(f"Retrieval response: {retrieve_response.output}") assert retrieve_response.exit_code == 0, retrieve_response.output retrieve_response = json.loads(retrieve_response.output) cleartexts = retrieve_response['result']['cleartexts'] assert len(cleartexts) == len(message_kits_b64) for index, cleartext in enumerate(cleartexts): assert cleartext.encode() == capsule_side_channel.plaintexts[index]