예제 #1
0
def fleet_of_highperf_mocked_ursulas(ursula_federated_test_config, request):
    # good_serials = _determine_good_serials(10000, 50000)
    try:
        quantity = request.param
    except AttributeError:
        quantity = 5000  # Bigass fleet by default; that's kinda the point.
    with GlobalLoggerSettings.pause_all_logging_while():
        with mock_secret_source():
            with mock_cert_storage, mock_cert_loading, mock_rest_app_creation, mock_cert_generation, mock_remember_node, mock_message_verification:
                _ursulas = make_federated_ursulas(
                    ursula_config=ursula_federated_test_config,
                    quantity=quantity,
                    know_each_other=False)
                all_ursulas = {u.checksum_address: u for u in _ursulas}

                for ursula in _ursulas:
                    # FIXME #2588: FleetSensor should not own fully-functional Ursulas.
                    # It only needs to see whatever public info we can normally get via REST.
                    # Also sharing mutable Ursulas like that can lead to unpredictable results.
                    ursula.known_nodes.current_state._nodes = all_ursulas
                    ursula.known_nodes.current_state.checksum = b"This is a fleet state checksum..".hex(
                    )
    yield _ursulas

    for ursula in _ursulas:
        del MOCK_KNOWN_URSULAS_CACHE[ursula.rest_interface.port]
예제 #2
0
def fleet_of_highperf_mocked_ursulas(ursula_federated_test_config, request):
    try:
        quantity = request.param
    except AttributeError:
        quantity = 5000  # Bigass fleet by default; that's kinda the point.
    with GlobalLoggerSettings.pause_all_logging_while():
        with mock_secret_source():
            with mock_cert_storage, mock_cert_loading, mock_rest_app_creation, mock_cert_generation, mock_remember_node, mock_message_verification:
                _ursulas = make_federated_ursulas(ursula_config=ursula_federated_test_config,
                                                  quantity=quantity, know_each_other=False)
                all_ursulas = {u.checksum_address: u for u in _ursulas}
                for ursula in _ursulas:
                    ursula.known_nodes._nodes = all_ursulas
                    ursula.known_nodes.checksum = b"This is a fleet state checksum..".hex()
    return _ursulas
def test_worker_failure_non_resilience():
    """
    abort on error is True for this one
    """

    # Control time
    clock = Clock()
    worktracker = WorkTrackerThatFailsHalfTheTime(clock, True)

    def advance_one_cycle(_):
        clock.advance(WorkTrackerThatFailsHalfTheTime.INTERVAL_CEIL)

    def checkworkstate(_):
        assert worktracker.workdone == 0

    def start():
        worktracker.start()

    d = threads.deferToThread(start)

    for i in range(10):
        d.addCallback(advance_one_cycle)
        d.addCallback(checkworkstate)

    critical = []

    def critical_trapper(event):
        if event['log_level'] == LogLevel.critical:
            critical.append(event)

    globalLogPublisher.addObserver(critical_trapper)

    with GlobalLoggerSettings.pause_all_logging_while(
    ):  # To suppress the traceback being displayed from the cricial error.
        globalLogPublisher.addObserver(critical_trapper)
        yield d
        globalLogPublisher.removeObserver(critical_trapper)

    assert len(critical) == 1
    assert critical[0]['failure'].getErrorMessage(
    ) == "zomg something went wrong"
예제 #4
0
def fleet_of_highperf_mocked_ursulas(ursula_federated_test_config, request):
    # good_serials = _determine_good_serials(10000, 50000)
    try:
        quantity = request.param
    except AttributeError:
        quantity = 5000  # Bigass fleet by default; that's kinda the point.
    with GlobalLoggerSettings.pause_all_logging_while():
        with mock_secret_source():
            with mock_cert_storage, mock_cert_loading, mock_rest_app_creation, mock_cert_generation, mock_remember_node, mock_message_verification:
                _ursulas = make_federated_ursulas(
                    ursula_config=ursula_federated_test_config,
                    quantity=quantity,
                    know_each_other=False)
                all_ursulas = {u.checksum_address: u for u in _ursulas}

                for ursula in _ursulas:
                    ursula.known_nodes.current_state._nodes = all_ursulas
                    ursula.known_nodes.current_state.checksum = b"This is a fleet state checksum..".hex(
                    )
    yield _ursulas

    for ursula in _ursulas:
        del MOCK_KNOWN_URSULAS_CACHE[ursula.rest_interface.port]
예제 #5
0
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
예제 #6
0
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]