Example #1
0
    def create_character(self, emitter, config_file, debug):

        felix_config = self.config_options.create_config(emitter, config_file)

        try:
            # Authenticate
            unlock_nucypher_keyring(
                emitter,
                character_configuration=felix_config,
                password=get_nucypher_password(confirm=False))

            client_password = get_client_password(
                checksum_address=felix_config.checksum_address,
                envvar=NUCYPHER_ENVVAR_WORKER_ETH_PASSWORD)

            # Produce Felix
            FELIX = felix_config.produce(domain=self.config_options.domain,
                                         client_password=client_password)
            FELIX.make_web_app(
            )  # attach web application, but dont start service

            return FELIX
        except Exception as e:
            if debug:
                raise
            else:
                emitter.echo(str(e), color='red', bold=True)
                raise click.Abort
Example #2
0
def make_cli_character(
    character_config,
    emitter,
    unlock_keyring: bool = True,
    teacher_uri: str = None,
    min_stake: int = 0,  # We not using this anymore?  Where is it hooked up?
    **config_args
) -> Character:

    #
    # Pre-Init
    #

    # Handle Keyring

    if unlock_keyring:
        unlock_nucypher_keyring(emitter,
                                character_configuration=character_config,
                                password=get_nucypher_password(confirm=False))

    # Handle Teachers
    # TODO: Is this still relevant?  Is it better to DRY this up by doing it later?
    sage_nodes = list()

    #
    # Character Init
    #

    # Produce Character
    if teacher_uri:
        maybe_sage_node = character_config.known_node_class.from_teacher_uri(
            teacher_uri=teacher_uri,
            min_stake=0,  # TODO: Where to get this?
            federated_only=character_config.federated_only,
            network_middleware=character_config.network_middleware,
            registry=character_config.registry)
        sage_nodes.append(maybe_sage_node)

    CHARACTER = character_config(
        known_nodes=sage_nodes,
        network_middleware=character_config.network_middleware,
        **config_args)

    #
    # Post-Init
    #

    if CHARACTER.controller is not NO_CONTROL_PROTOCOL:
        CHARACTER.controller.emitter = emitter  # TODO: set it on object creation? Or not set at all?

    # Federated
    if character_config.federated_only:
        emitter.message(FEDERATED_WARNING, color='yellow')

    emitter.message(
        f"Loaded {CHARACTER.__class__.__name__} {CHARACTER.checksum_address} ({CHARACTER.domain})",
        color='green')
    return CHARACTER
Example #3
0
def make_cli_character(character_config,
                       emitter,
                       unlock_keyring: bool = True,
                       teacher_uri: str = None,
                       min_stake: int = 0,
                       load_preferred_teachers: bool = True,
                       **config_args) -> Character:

    #
    # Pre-Init
    #

    # Handle Keyring

    if unlock_keyring:
        unlock_nucypher_keyring(emitter,
                                character_configuration=character_config,
                                password=get_nucypher_password(confirm=False))

    # Handle Teachers
    teacher_nodes = list()
    if load_preferred_teachers:
        teacher_nodes = load_seednodes(emitter,
                                       teacher_uris=[teacher_uri] if teacher_uri else None,
                                       min_stake=min_stake,
                                       federated_only=character_config.federated_only,
                                       network_domains=character_config.domains,
                                       network_middleware=character_config.network_middleware,
                                       registry=character_config.registry)

    #
    # Character Init
    #

    # Produce Character
    CHARACTER = character_config(known_nodes=teacher_nodes,
                                 network_middleware=character_config.network_middleware,
                                 **config_args)

    #
    # Post-Init
    #

    if CHARACTER.controller is not NO_CONTROL_PROTOCOL:
        CHARACTER.controller.emitter = emitter  # TODO: set it on object creation? Or not set at all?

    # Federated
    if character_config.federated_only:
        emitter.message(FEDERATED_WARNING, color='yellow')

    return CHARACTER
def test_unlock_nucypher_keyring(mocker, test_emitter, capsys,
                                 alice_blockchain_test_config, patch_keystore,
                                 tmpdir):

    # Setup
    # Do not test "real" unlocking here, just the plumbing
    unlock_spy = mocker.patch.object(NucypherKeyring,
                                     'unlock',
                                     return_value=True)
    attach_spy = mocker.spy(CharacterConfiguration, 'attach_keyring')
    mocker.patch.object(CharacterConfiguration,
                        'dev_mode',
                        return_value=False,
                        new_callable=mocker.PropertyMock)
    # Test
    result = unlock_nucypher_keyring(
        emitter=test_emitter,
        password=INSECURE_DEVELOPMENT_PASSWORD,
        character_configuration=alice_blockchain_test_config)

    assert result
    captured = capsys.readouterr()
    message = DECRYPTING_CHARACTER_KEYRING.format(
        name=alice_blockchain_test_config.NAME)
    assert message in captured.out

    unlock_spy.assert_called_once_with(password=INSECURE_DEVELOPMENT_PASSWORD)
    attach_spy.assert_called_once()
def test_unlock_nucypher_keyring_invalid_password(mocker, test_emitter,
                                                  alice_blockchain_test_config,
                                                  capsys):

    # Setup
    keyring_attach_spy = mocker.spy(CharacterConfiguration, 'attach_keyring')
    mocker.patch.object(NucypherKeyring, 'unlock', side_effect=CryptoError)
    mocker.patch.object(CharacterConfiguration,
                        'dev_mode',
                        return_value=False,
                        new_callable=mocker.PropertyMock)

    # Test
    with pytest.raises(NucypherKeyring.AuthenticationFailed):
        unlock_nucypher_keyring(
            emitter=test_emitter,
            password=INSECURE_DEVELOPMENT_PASSWORD + 'typo',
            character_configuration=alice_blockchain_test_config)
    keyring_attach_spy.assert_called_once()

    captured = capsys.readouterr()
    assert DECRYPTING_CHARACTER_KEYRING.format(name='alice') in captured.out
def test_unlock_nucypher_keyring_dev_mode(mocker, test_emitter, capsys,
                                          alice_blockchain_test_config):

    # Setup
    unlock_spy = mocker.spy(NucypherKeyring, 'unlock')
    attach_spy = mocker.spy(CharacterConfiguration, 'attach_keyring')
    mocker.patch.object(CharacterConfiguration,
                        'dev_mode',
                        return_value=True,
                        new_callable=mocker.PropertyMock)
    # Test
    result = unlock_nucypher_keyring(
        emitter=test_emitter,
        password=INSECURE_DEVELOPMENT_PASSWORD,
        character_configuration=alice_blockchain_test_config)

    assert result
    output = capsys.readouterr().out
    message = DECRYPTING_CHARACTER_KEYRING.format(
        name=alice_blockchain_test_config.NAME)
    assert message in output

    unlock_spy.assert_not_called()
    attach_spy.assert_not_called()