def process_middleware(mock_networking): if mock_networking: middleware = MockRestMiddleware() else: middleware = RestMiddleware() return 'middleware', middleware
def nucypher_cli(click_config, verbose, mock_networking, json_ipc, no_logs, quiet, debug, no_registry, log_level): # Session Emitter for pre and post character control engagement. if json_ipc: emitter = JSONRPCStdoutEmitter( quiet=quiet, capture_stdout=NucypherClickConfig.capture_stdout) else: emitter = StdoutEmitter( quiet=quiet, capture_stdout=NucypherClickConfig.capture_stdout) click_config.attach_emitter(emitter) if not json_ipc: click_config.emit(message=NUCYPHER_BANNER) if log_level: GlobalConsoleLogger.set_log_level(log_level_name=log_level) globalLogPublisher.addObserver(SimpleObserver()) if debug and quiet: raise click.BadOptionUsage( option_name="quiet", message="--debug and --quiet cannot be used at the same time.") if debug: click_config.log_to_sentry = False click_config.log_to_file = True # File Logging globalLogPublisher.addObserver(SimpleObserver()) # Console Logging globalLogPublisher.removeObserver(logToSentry) # No Sentry GlobalConsoleLogger.set_log_level(log_level_name='debug') elif quiet: # Disable Logging globalLogPublisher.removeObserver(logToSentry) globalLogPublisher.removeObserver(SimpleObserver) globalLogPublisher.removeObserver(getJsonFileObserver()) # Logging if not no_logs: GlobalConsoleLogger.start_if_not_started() # CLI Session Configuration click_config.verbose = verbose click_config.mock_networking = mock_networking click_config.json_ipc = json_ipc click_config.no_logs = no_logs click_config.quiet = quiet click_config.no_registry = no_registry click_config.debug = debug # Only used for testing outputs; # Redirects outputs to in-memory python containers. if mock_networking: click_config.emit(message="WARNING: Mock networking is enabled") click_config.middleware = MockRestMiddleware() else: click_config.middleware = RestMiddleware() # Global Warnings if click_config.verbose: click_config.emit(message="Verbose mode is enabled", color='blue')
def bob_federated_test_config(): config = BobConfiguration(temp=True, auto_initialize=True, network_middleware=MockRestMiddleware(), start_learning_now=False, abort_on_learning_error=True, federated_only=True) yield config config.cleanup()
def process_middleware(mock_networking): from nucypher.network.middleware import RestMiddleware from nucypher.utilities.sandbox.middleware import MockRestMiddleware if mock_networking: middleware = MockRestMiddleware() else: middleware = RestMiddleware() return 'middleware', middleware
def enacted_federated_policy(idle_federated_policy, federated_ursulas): # Alice has a policy in mind and knows of enough qualifies Ursulas; she crafts an offer for them. network_middleware = MockRestMiddleware() idle_federated_policy.make_arrangements(network_middleware, handpicked_ursulas=federated_ursulas) # REST call happens here, as does population of TreasureMap. responses = idle_federated_policy.enact(network_middleware) return idle_federated_policy
def alice_federated_test_config(federated_ursulas): config = AliceConfiguration(temp=True, auto_initialize=True, is_me=True, network_middleware=MockRestMiddleware(), known_nodes=federated_ursulas, federated_only=True, abort_on_learning_error=True) yield config config.cleanup()
def alice_federated_test_config(federated_ursulas): config = AliceConfiguration(dev_mode=True, network_middleware=MockRestMiddleware(), known_nodes=federated_ursulas, federated_only=True, abort_on_learning_error=True, save_metadata=False, reload_metadata=False) yield config config.cleanup()
def bob_federated_test_config(): config = BobConfiguration(dev_mode=True, network_middleware=MockRestMiddleware(), start_learning_now=False, abort_on_learning_error=True, federated_only=True, save_metadata=False, reload_metadata=False) yield config config.cleanup()
def test_alice_sets_treasure_map(enacted_federated_policy, federated_ursulas): """ Having enacted all the policies of a PolicyGroup, Alice creates a TreasureMap and ...... TODO """ enacted_federated_policy.publish_treasure_map(network_middleware=MockRestMiddleware()) treasure_map_as_set_on_network = list(federated_ursulas)[0].treasure_maps[ digest(enacted_federated_policy.treasure_map.public_id())] assert treasure_map_as_set_on_network == enacted_federated_policy.treasure_map
def ursula_federated_test_config(): ursula_config = UrsulaConfiguration(dev_mode=True, rest_port=MOCK_URSULA_STARTING_PORT, start_learning_now=False, abort_on_learning_error=True, federated_only=True, network_middleware=MockRestMiddleware(), save_metadata=False, reload_metadata=False,) yield ursula_config ursula_config.cleanup()
def test_bob_can_follow_treasure_map_even_if_he_only_knows_of_one_node( enacted_federated_policy, federated_ursulas, certificates_tempdir): """ Similar to above, but this time, we'll show that if Bob can connect to a single node, he can learn enough to follow the TreasureMap. Also, we'll get the TreasureMap from the hrac alone (ie, not via a side channel). """ from nucypher.characters.lawful import Bob bob = Bob(network_middleware=MockRestMiddleware(), known_certificates_dir=certificates_tempdir, start_learning_now=False, abort_on_learning_error=True, federated_only=True) # Again, let's assume that he received the TreasureMap via a side channel. hrac, treasure_map = enacted_federated_policy.hrac( ), enacted_federated_policy.treasure_map map_id = treasure_map.public_id() bob.treasure_maps[map_id] = treasure_map # Now, let's create a scenario in which Bob knows of only one node. assert len(bob.known_nodes) == 0 first_ursula = list(federated_ursulas).pop(0) bob.remember_node(first_ursula) assert len(bob.known_nodes) == 1 # This time, when he follows the TreasureMap... unknown_nodes, known_nodes = bob.peek_at_treasure_map(map_id=map_id) # Bob already knew about one node; the rest are unknown. assert len(unknown_nodes) == len(treasure_map) - 1 # He needs to actually follow the treasure map to get the rest. bob.follow_treasure_map(map_id=map_id) # The nodes in the learning loop are now his top target, but he's not learning yet. assert not bob._learning_task.running # ...so he hasn't learned anything (ie, Bob still knows of just one node). assert len(bob.known_nodes) == 1 # Now, we'll start his learning loop. bob.start_learning_loop() # ...and block until the unknown_nodes have all been found. d = threads.deferToThread(bob.block_until_specific_nodes_are_known, unknown_nodes) yield d # ...and he now has no more unknown_nodes. assert len(bob.known_nodes) == len(treasure_map)
def alice_blockchain_test_config(blockchain_ursulas, testerchain): config = AliceConfiguration(dev_mode=True, provider_uri=TEST_PROVIDER_URI, checksum_address=testerchain.alice_account, network_middleware=MockRestMiddleware(), known_nodes=blockchain_ursulas, abort_on_learning_error=True, download_registry=False, save_metadata=False, reload_metadata=False) yield config config.cleanup()
def enacted_blockchain_policy(idle_blockchain_policy, blockchain_ursulas): # Alice has a policy in mind and knows of enough qualified Ursulas; she crafts an offer for them. deposit = NON_PAYMENT(b"0000000") contract_end_datetime = maya.now() + datetime.timedelta(days=5) network_middleware = MockRestMiddleware() idle_blockchain_policy.make_arrangements(network_middleware, value=deposit, expiration=contract_end_datetime, ursulas=list(blockchain_ursulas)) idle_blockchain_policy.enact(network_middleware) # REST call happens here, as does population of TreasureMap. return idle_blockchain_policy
def bob_federated_test_config(): config = BobConfiguration(temp=True, auto_initialize=True, auto_generate_keys=True, passphrase=TEST_URSULA_INSECURE_DEVELOPMENT_PASSWORD, network_middleware=MockRestMiddleware(), start_learning_now=False, abort_on_learning_error=True, federated_only=True, save_metadata=False, load_metadata=False) yield config config.cleanup()
def ursula_decentralized_test_config(test_registry): ursula_config = UrsulaConfiguration(dev_mode=True, provider_uri=TEST_PROVIDER_URI, rest_port=MOCK_URSULA_STARTING_PORT, start_learning_now=False, abort_on_learning_error=True, federated_only=False, network_middleware=MockRestMiddleware(), save_metadata=False, reload_metadata=False, registry=test_registry) yield ursula_config ursula_config.cleanup()
def ursula_decentralized_test_config(three_agents): ursula_config = UrsulaConfiguration(dev_mode=True, is_me=True, provider_uri="tester://pyevm", rest_port=MOCK_URSULA_STARTING_PORT, start_learning_now=False, abort_on_learning_error=True, federated_only=False, network_middleware=MockRestMiddleware(), import_seed_registry=False, save_metadata=False, reload_metadata=False) yield ursula_config ursula_config.cleanup()
def bob_blockchain_test_config(blockchain_ursulas, three_agents): token_agent, miner_agent, policy_agent = three_agents etherbase, alice_address, bob_address, *everyone_else = token_agent.blockchain.interface.w3.eth.accounts config = BobConfiguration(temp=True, auto_initialize=True, checksum_address=bob_address, network_middleware=MockRestMiddleware(), known_nodes=blockchain_ursulas, start_learning_now=False, abort_on_learning_error=True, federated_only=False) yield config config.cleanup()
def bob_blockchain_test_config(blockchain_ursulas, testerchain, test_registry): config = BobConfiguration(dev_mode=True, provider_uri=TEST_PROVIDER_URI, checksum_address=testerchain.bob_account, network_middleware=MockRestMiddleware(), known_nodes=blockchain_ursulas, start_learning_now=False, abort_on_learning_error=True, federated_only=False, save_metadata=False, reload_metadata=False, registry=test_registry) yield config config.cleanup()
def enacted_federated_policy(idle_federated_policy, federated_ursulas): # Alice has a policy in mind and knows of enough qualifies Ursulas; she crafts an offer for them. deposit = constants.NON_PAYMENT contract_end_datetime = maya.now() + datetime.timedelta(days=5) network_middleware = MockRestMiddleware() idle_federated_policy.make_arrangements(network_middleware, deposit=deposit, expiration=contract_end_datetime, handpicked_ursulas=federated_ursulas) responses = idle_federated_policy.enact(network_middleware) # REST call happens here, as does population of TreasureMap. return idle_federated_policy
def alice_federated_test_config(federated_ursulas): config = AliceConfiguration(temp=True, auto_initialize=True, auto_generate_keys=True, passphrase=TEST_URSULA_INSECURE_DEVELOPMENT_PASSWORD, is_me=True, network_middleware=MockRestMiddleware(), known_nodes=federated_ursulas, federated_only=True, abort_on_learning_error=True, save_metadata=False, load_metadata=False) yield config config.cleanup()
def test_vladimir_illegal_interface_key_does_not_propagate(blockchain_ursulas): """ Although Ursulas propagate each other's interface information, as demonstrated above, they do not propagate interface information for Vladimir. Specifically, if Vladimir tries to perform the most obvious imitation attack - propagating his own wallet address along with Ursula's information - the validity check will catch it and Ursula will refuse to propagate it and also record Vladimir's details. """ ursulas = list(blockchain_ursulas) ursula_whom_vladimir_will_imitate, other_ursula = ursulas[0], ursulas[1] # Vladimir sees Ursula on the network and tries to use her public information. vladimir = Vladimir.from_target_ursula(ursula_whom_vladimir_will_imitate) # This Ursula is totally legit... ursula_whom_vladimir_will_imitate.verify_node(MockRestMiddleware(), accept_federated_only=True) learning_callers = [] crosstown_traffic.decorator = crosstownTaskListDecoratorFactory( learning_callers) vladimir.network_middleware.propagate_shitty_interface_id( other_ursula, bytes(vladimir)) # So far, Ursula hasn't noticed any Vladimirs. assert other_ursula.suspicious_activities_witnessed['vladimirs'] == [] # ...but now, Ursula will now try to learn about Vladimir on a different thread. # We only passed one node (Vladimir)... learn_about_vladimir = learning_callers.pop() # ...so there was only one learning caller in the queue (now none since we popped it just now). assert len(learning_callers) == 0 # OK, so cool, let's see what happens when Ursula tries to learn about Vlad. learn_about_vladimir() # And indeed, Ursula noticed the situation. # She didn't record Vladimir's address. assert vladimir not in other_ursula.known_nodes # But she *did* record the actual Ursula's address. assert ursula_whom_vladimir_will_imitate in other_ursula.known_nodes # Furthermore, she properly marked Vladimir as suspicious. assert vladimir in other_ursula.suspicious_activities_witnessed[ 'vladimirs']
def alice_blockchain_test_config(blockchain_ursulas, three_agents): token_agent, miner_agent, policy_agent = three_agents config = AliceConfiguration( dev_mode=True, is_me=True, provider_uri="tester://pyevm", checksum_public_address=token_agent.blockchain.alice_account, network_middleware=MockRestMiddleware(), known_nodes=blockchain_ursulas, abort_on_learning_error=True, import_seed_registry=False, save_metadata=False, reload_metadata=False) yield config config.cleanup()
def charlie_blockchain_test_config(blockchain_ursulas, agency): token_agent, staking_agent, policy_agent = agency etherbase, alice_address, bob_address, *everyone_else = token_agent.blockchain.client.accounts config = BobConfiguration(dev_mode=True, provider_uri=TEST_PROVIDER_URI, checksum_address=bob_address, network_middleware=MockRestMiddleware(), known_nodes=blockchain_ursulas, start_learning_now=False, abort_on_learning_error=True, federated_only=False, save_metadata=False, reload_metadata=False) yield config config.cleanup()
def charlie_blockchain_test_config(blockchain_ursulas, three_agents): token_agent, miner_agent, policy_agent = three_agents etherbase, alice_address, bob_address, *everyone_else = token_agent.blockchain.interface.w3.eth.accounts config = BobConfiguration(dev_mode=True, provider_uri="tester://pyevm", checksum_public_address=bob_address, network_middleware=MockRestMiddleware(), known_nodes=blockchain_ursulas, start_learning_now=False, abort_on_learning_error=True, federated_only=False, import_seed_registry=False, save_metadata=False, reload_metadata=False) yield config config.cleanup()
def ursula_decentralized_test_config(three_agents): token_agent, miner_agent, policy_agent = three_agents ursula_config = UrsulaConfiguration( temp=True, auto_initialize=True, auto_generate_keys=True, passphrase=TEST_URSULA_INSECURE_DEVELOPMENT_PASSWORD, is_me=True, start_learning_now=False, abort_on_learning_error=True, federated_only=False, network_middleware=MockRestMiddleware(), import_seed_registry=False, save_metadata=False, load_metadata=False) yield ursula_config ursula_config.cleanup()
def alice_blockchain_test_config(blockchain_ursulas, three_agents): token_agent, miner_agent, policy_agent = three_agents etherbase, alice_address, bob_address, *everyone_else = token_agent.blockchain.interface.w3.eth.accounts config = AliceConfiguration( temp=True, is_me=True, auto_initialize=True, auto_generate_keys=True, checksum_address=alice_address, passphrase=TEST_URSULA_INSECURE_DEVELOPMENT_PASSWORD, network_middleware=MockRestMiddleware(), known_nodes=blockchain_ursulas, abort_on_learning_error=True, import_seed_registry=False, save_metadata=False, load_metadata=False) yield config config.cleanup()
def nucypher_cli(click_config, verbose, mock_networking, json_ipc, no_logs, quiet, debug, no_registry): # Session Emitter for pre and post character control engagement. if json_ipc: emitter = IPCStdoutEmitter(quiet=quiet, capture_stdout=NucypherClickConfig.capture_stdout) else: emitter = StdoutEmitter(quiet=quiet, capture_stdout=NucypherClickConfig.capture_stdout) NucypherClickConfig.emitter = emitter click_config.emitter(message=NUCYPHER_BANNER) # Logging if not no_logs: GlobalConsoleLogger.start_if_not_started() # CLI Session Configuration click_config.verbose = verbose click_config.mock_networking = mock_networking click_config.json_ipc = json_ipc click_config.no_logs = no_logs click_config.quiet = quiet click_config.no_registry = no_registry click_config.debug = debug # only used for testing outputs; # Redirects outputs to in-memory python containers. if mock_networking: click_config.emitter(message="WARNING: Mock networking is enabled") click_config.middleware = MockRestMiddleware() else: click_config.middleware = RestMiddleware() # Global Warnings if click_config.verbose: click_config.emitter("Verbose mode is enabled", color='blue')
def test_new_federated_ursula_announces_herself(ursula_federated_test_config): ursula_in_a_house, ursula_with_a_mouse = make_federated_ursulas( ursula_config=ursula_federated_test_config, quantity=2, know_each_other=False, network_middleware=MockRestMiddleware()) # Neither Ursula knows about the other. assert ursula_in_a_house.known_nodes == ursula_with_a_mouse.known_nodes ursula_in_a_house.remember_node(ursula_with_a_mouse) # OK, now, ursula_in_a_house knows about ursula_with_a_mouse, but not vice-versa. assert ursula_with_a_mouse in ursula_in_a_house.known_nodes assert ursula_in_a_house not in ursula_with_a_mouse.known_nodes # But as ursula_in_a_house learns, she'll announce herself to ursula_with_a_mouse. ursula_in_a_house.learn_from_teacher_node() assert ursula_with_a_mouse in ursula_in_a_house.known_nodes assert ursula_in_a_house in ursula_with_a_mouse.known_nodes
def test_node_posts_future_version(federated_ursulas): ursula = list(federated_ursulas)[0] middleware = MockRestMiddleware() warnings = [] def warning_trapper(event): if event['log_level'] == LogLevel.warn: warnings.append(event) globalLogPublisher.addObserver(warning_trapper) crazy_node = b"invalid-node" middleware.get_nodes_via_rest(node=ursula, announce_nodes=(crazy_node, )) assert len(warnings) == 1 future_node = list(federated_ursulas)[1] future_node.TEACHER_VERSION = future_node.TEACHER_VERSION + 10 future_node_bytes = bytes(future_node) middleware.get_nodes_via_rest(node=ursula, announce_nodes=(future_node_bytes, )) assert len(warnings) == 2
def test_alices_powers_are_persistent(federated_ursulas, tmpdir): # Create a non-learning AliceConfiguration alice_config = AliceConfiguration(config_root=os.path.join( tmpdir, 'nucypher-custom-alice-config'), network_middleware=MockRestMiddleware(), known_nodes=federated_ursulas, start_learning_now=False, federated_only=True, save_metadata=False, reload_metadata=False) # Generate keys and write them the disk alice_config.initialize(password=INSECURE_DEVELOPMENT_PASSWORD) # Unlock Alice's keyring alice_config.keyring.unlock(password=INSECURE_DEVELOPMENT_PASSWORD) # 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. m, n = 3, 4 policy_end_datetime = maya.now() + datetime.timedelta(days=5) bob = Bob(federated_only=True, start_learning_now=False, network_middleware=MockRestMiddleware()) bob_policy = alice.grant(bob, label, m=m, n=n, expiration=policy_end_datetime) assert policy_pubkey == bob_policy.public_key # ... and Alice and her configuration disappear. 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, network_middleware=MockRestMiddleware(), known_nodes=federated_ursulas, start_learning_now=False, ) # Alice unlocks her restored keyring from disk new_alice_config.attach_keyring() new_alice_config.keyring.unlock(password=INSECURE_DEVELOPMENT_PASSWORD) 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 roberto = Bob(federated_only=True, start_learning_now=False, network_middleware=MockRestMiddleware()) # 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 m, n = 2, 5 policy_end_datetime = maya.now() + datetime.timedelta(days=3) roberto_policy = new_alice.grant(roberto, label, m=m, n=n, expiration=policy_end_datetime) # Both policies must share the same public key (i.e., the policy public key) assert policy_pubkey == roberto_policy.public_key