Example #1
0
def enrico(click_config, action, policy_encrypting_key, dry_run, http_port,
           message):
    """
    "Enrico the Encryptor" management commands.
    """

    #
    # Validate
    #

    if not policy_encrypting_key:
        raise click.BadArgumentUsage(
            '--policy-encrypting-key is required to start Enrico.')

    # Banner
    emitter = click_config.emitter
    emitter.clear()
    emitter.banner(ENRICO_BANNER.format(policy_encrypting_key))

    #
    # Make Enrico
    #

    policy_encrypting_key = UmbralPublicKey.from_bytes(
        bytes.fromhex(policy_encrypting_key))
    ENRICO = Enrico(policy_encrypting_key=policy_encrypting_key)
    ENRICO.controller.emitter = emitter  # TODO: set it on object creation? Or not set at all?

    #
    # Actions
    #

    if action == 'run':

        # RPC
        if click_config.json_ipc:
            rpc_controller = ENRICO.make_rpc_controller()
            _transport = rpc_controller.make_control_transport()
            rpc_controller.start()
            return

        ENRICO.log.info('Starting HTTP Character Web Controller')
        controller = ENRICO.make_web_controller()
        return controller.start(http_port=http_port, dry_run=dry_run)

    elif action == 'encrypt':

        # Validate
        if not message:
            raise click.BadArgumentUsage(
                '--message is a required flag to encrypt.')

        # Request
        encryption_request = {'message': message}
        response = ENRICO.controller.encrypt_message(
            request=encryption_request)
        return response

    else:
        raise click.BadArgumentUsage
Example #2
0
def enrico(click_config, action, policy_encrypting_key, dry_run, http_port, message):
    """
    Start and manage an "Enrico" character control HTTP server
    """

    #
    # Validate
    #

    if not policy_encrypting_key:
        raise click.BadArgumentUsage('--policy-encrypting-key is required to start Enrico.')

    # Banner
    click.clear()
    if not click_config.json_ipc and not click_config.quiet:
        click.secho(ENRICO_BANNER)

    #
    # Make Enrico
    #

    policy_encrypting_key = UmbralPublicKey.from_bytes(bytes.fromhex(policy_encrypting_key))
    ENRICO = Enrico(policy_encrypting_key=policy_encrypting_key)
    if click_config.json_ipc:
        ENRICO.controller.emitter = JSONRPCStdoutEmitter(quiet=click_config.quiet)

    #
    # Actions
    #

    if action == 'run':

        # RPC
        if click_config.json_ipc:
            rpc_controller = ENRICO.make_rpc_controller()
            _transport = rpc_controller.make_control_transport()
            rpc_controller.start()
            return

        ENRICO.log.info('Starting HTTP Character Web Controller')
        controller = ENRICO.make_web_controller()
        return controller.start(http_port=http_port, dry_run=dry_run)

    elif action == 'encrypt':

        # Validate
        if not message:
            raise click.BadArgumentUsage('--message is a required flag to encrypt.')

        # Request
        encryption_request = {'message': message}
        response = ENRICO.controller.encrypt_message(request=encryption_request)
        return response

    else:
        raise click.BadArgumentUsage