Пример #1
0
def enrico(action, policy_encrypting_key, dry_run, http_port):
    """
    Start and manage an "Enrico" character control HTTP server
    """

    click.secho(ENRICO_BANNER.format(policy_encrypting_key))

    if action == 'run':  # Forest
        policy_encrypting_key = UmbralPublicKey.from_bytes(bytes.fromhex(policy_encrypting_key))
        ENRICO = Enrico(policy_encrypting_key=policy_encrypting_key)

        # Enrico Control
        enrico_control = ENRICO.make_wsgi_app()
        click.secho("Starting Enrico Character Control...")

        click.secho(f"Enrico Verifying Key {bytes(ENRICO.stamp).hex()}", fg="green", bold=True)

        # Run
        if dry_run:
            return

        hx_deployer = HendrixDeploy(action="start", options={"wsgi": enrico_control, "http_port": http_port})
        hx_deployer.run()  # <--- Blocking Call to Reactor

    else:
        raise click.BadArgumentUsage
Пример #2
0
    def start(self,
              host: str,
              port: int,
              web_services: bool = True,
              distribution: bool = True,
              crash_on_error: bool = False):

        self.crash_on_error = crash_on_error

        if self.start_time is not NOT_RUNNING:
            raise RuntimeError("Felix is already running.")

        self.start_time = maya.now()
        payload = {
            "wsgi": self.rest_app,
            "http_port": port,
            "resources": get_static_resources()
        }
        deployer = HendrixDeploy(action="start", options=payload)

        if distribution is True:
            self.start_distribution()

        if web_services is True:
            deployer.run()  # <-- Blocking call (Reactor)
Пример #3
0
    def start(self, http_port: int, dry_run: bool = False):

        self.log.info("Starting HTTP Character Control...")

        if dry_run:
            return

        # TODO: Make non-blocking web control startup
        hx_deployer = HendrixDeploy(action="start", options={"wsgi": self._web_app,
                                                             "http_port": http_port})
        hx_deployer.run()  # <--- Blocking Call to Reactor
Пример #4
0
    def start(self, eager: bool = False):
        """Start the crawler if not already running"""
        if not self.is_running:
            self.log.info('Starting Crawler...')
            if self._influx_client is None:
                self._influx_client = InfluxDBClient(
                    host=self._db_host,
                    port=self._db_port,
                    database=self.INFLUX_DB_NAME)
                self._initialize_influx()

            if self._crawler_client is None:
                from monitor.db import CrawlerStorageClient
                self._crawler_client = CrawlerStorageClient()

                # TODO: Maybe?
                # from monitor.db import CrawlerInfluxClient
                # self.crawler_influx_client = CrawlerInfluxClient()

            # start tasks
            node_learner_deferred = self._node_details_task.start(
                interval=random.randint(
                    int(self._refresh_rate * (1 - self.REFRESH_RATE_WINDOW)),
                    self._refresh_rate),
                now=eager)
            time.sleep(random.randint(2, 10))  # random stagger start of task
            collection_deferred = self._stats_collection_task.start(
                interval=random.randint(
                    self._refresh_rate,
                    int(self._refresh_rate * (1 + self.REFRESH_RATE_WINDOW))),
                now=eager)

            # get known last event block
            self.__events_from_block = self._get_last_known_blocknumber()
            time.sleep(random.randint(2, 10))  # random stagger start of task
            events_deferred = self._events_collection_task.start(
                interval=self._refresh_rate, now=eager)

            # hookup error callbacks
            node_learner_deferred.addErrback(self._handle_errors)
            collection_deferred.addErrback(self._handle_errors)
            events_deferred.addErrback(self._handle_errors)

            # Start up
            self.start_learning_loop(now=False)
            self.make_flask_server()
            hx_deployer = HendrixDeploy(action="start",
                                        options={
                                            "wsgi": self._flask,
                                            "http_port":
                                            self._crawler_http_port
                                        })
            hx_deployer.run()  # <--- Blocking Call to Reactor
Пример #5
0
    def start(self, http_port: int, dry_run: bool = False):

        self.log.info("Starting HTTP Character Control...")
        if dry_run:
            return

        # TODO #845: Make non-blocking web control startup
        hx_deployer = HendrixDeploy(action="start",
                                    options={
                                        "wsgi": self._transport,
                                        "http_port": http_port,
                                        "resources": get_static_resources()
                                    })
        hx_deployer.run()  # <--- Blocking Call to Reactor
Пример #6
0
    def start(self, ws_port: int, http_port: int, dry_run: bool = False):

        #
        # Websocket Service
        #

        def send_states(subscriber):
            message = ["states", self.known_nodes.abridged_states_dict()]
            subscriber.sendMessage(json.dumps(message).encode())

        def send_nodes(subscriber):
            message = ["nodes", self.known_nodes.abridged_nodes_dict()]
            subscriber.sendMessage(json.dumps(message).encode())

        websocket_service = hey_joe.WebSocketService("127.0.0.1", ws_port)
        websocket_service.register_followup("states", send_states)
        websocket_service.register_followup("nodes", send_nodes)

        #
        # WSGI Service
        #

        self.rest_app = Flask("fleet-monitor", template_folder=TEMPLATES_DIR)
        rest_app = self.rest_app

        @rest_app.route("/")
        def status():
            try:
                return render_template('monitor.html')
            except Exception as e:
                self.log.debug(str(e))

        #
        # Server
        #

        deployer = HendrixDeploy(action="start",
                                 options={
                                     "wsgi": rest_app,
                                     "http_port": http_port
                                 })
        deployer.add_non_tls_websocket_service(websocket_service)

        click.secho(f"Running Moe on 127.0.0.1:{http_port}")

        if not dry_run:
            deployer.run()
Пример #7
0
    def start(self,
              host: str,
              port: int,
              web_services: bool = True,
              distribution: bool = True,
              crash_on_error: bool = False):

        self.crash_on_error = crash_on_error

        if self.start_time is not NOT_RUNNING:
            raise RuntimeError("Felix is already running.")

        self.start_time = maya.now()
        payload = {"wsgi": self.rest_app, "http_port": port}
        deployer = HendrixDeploy(action="start", options=payload)
        click.secho(f"Running {self.__class__.__name__} on {host}:{port}")

        if distribution is True:
            self.start_distribution()

        if web_services is True:
            deployer.run()  # <-- Blocking call (Reactor)
Пример #8
0
from hendrix.deploy.base import HendrixDeploy
from hendrix.experience import hey_joe

deployer = HendrixDeploy(options={
    'wsgi': 'example_app.wsgi.application',
    'http_port': 7575
})

websocket_service = hey_joe.WebSocketService("127.0.0.1", 9000)
deployer.add_non_tls_websocket_service(websocket_service)

deployer.run()
Пример #9
0
tp = reactor.suggestThreadPoolSize(1000)

# begin chdir armor
up = os.path.dirname(os.path.abspath(__file__))
testdir = os.path.dirname(up)
hendrix_app_dir = os.path.dirname(testdir)
hendrix_package_dir = os.path.dirname(hendrix_app_dir)
sys.path.insert(0, hendrix_package_dir)
# end chdir armor

from hendrix.deploy.base import HendrixDeploy

import threading


def delay(request):
    delay_time = float(request.matchdict['seconds'])
    print "Thread %s sleeping for %s seconds" % (threading.current_thread(), delay_time)
    time.sleep(delay_time)
    return Response('')

if __name__ == '__main__':
    config = Configurator()
    config.add_route('delay', '/{seconds}')
    config.add_view(delay, route_name='delay')

    app = config.make_wsgi_app()
    tp = ThreadPool(name="Big and Slow.", maxthreads=1000)
    deployer = HendrixDeploy(options={'wsgi': app, 'http_port': 8010}, threadpool=tp)
    deployer.run()
Пример #10
0
def moe(teacher_uri, min_stake, network, ws_port, dry_run, http_port,
        learn_on_launch):
    """
    "Moe" NuCypher node monitor CLI.
    """

    click.secho(MOE_BANNER)

    #
    # Teacher
    #

    teacher_nodes = list()
    if teacher_uri:
        teacher_node = Ursula.from_seed_and_stake_info(seed_uri=teacher_uri,
                                                       federated_only=True,
                                                       minimum_stake=min_stake)
        teacher_nodes.append(teacher_node)

    # Deserialize network domain name if override passed
    if network:
        domain_constant = getattr(constants, network.upper())
        domains = {domain_constant}
    else:
        domains = None

    monitor = Moe(
        domains=domains,
        network_middleware=RestMiddleware(),
        known_nodes=teacher_nodes,
        federated_only=True,
    )

    monitor.start_learning_loop(now=learn_on_launch)

    #
    # Websocket Service
    #

    def send_states(subscriber):
        message = ["states", monitor.known_nodes.abridged_states_dict()]
        subscriber.sendMessage(json.dumps(message).encode())

    def send_nodes(subscriber):
        message = ["nodes", monitor.known_nodes.abridged_nodes_dict()]
        subscriber.sendMessage(json.dumps(message).encode())

    websocket_service = hey_joe.WebSocketService("127.0.0.1", ws_port)
    websocket_service.register_followup("states", send_states)
    websocket_service.register_followup("nodes", send_nodes)

    #
    # Flask App
    #

    rest_app = Flask("fleet-monitor", root_path=os.path.dirname(__file__))

    @rest_app.route("/")
    def status():
        template_path = os.path.join('monitor.html')
        return render_template(template_path)

    #
    # Server
    #

    deployer = HendrixDeploy(action="start",
                             options={
                                 "wsgi": rest_app,
                                 "http_port": http_port
                             })
    deployer.add_non_tls_websocket_service(websocket_service)

    click.secho(f"Running Moe on 127.0.0.1:{http_port}")

    if not dry_run:
        deployer.run()
Пример #11
0
def alice(click_config, action, quiet, teacher_uri, min_stake, http_port,
          discovery_port, federated_only, network, config_root, config_file,
          provider_uri, registry_filepath, dev, force, dry_run,
          bob_encrypting_key, bob_verifying_key, label, m, n):
    """
    Start and manage an "Alice" character.
    """

    if not quiet:
        click.secho(ALICE_BANNER)

    if action == 'init':
        """Create a brand-new persistent Alice"""

        if dev and not quiet:
            click.secho("WARNING: Using temporary storage area", fg='yellow')

        if not config_root:  # Flag
            config_root = click_config.config_file  # Envvar

        alice_config = AliceConfiguration.generate(
            password=click_config.get_password(confirm=True),
            config_root=config_root,
            rest_host="localhost",
            domains={network} if network else None,
            federated_only=federated_only,
            no_registry=True,  # Yes we have no registry,
            registry_filepath=registry_filepath,
            provider_uri=provider_uri,
        )

        if not quiet:
            click.secho("Generated keyring {}".format(
                alice_config.keyring_dir),
                        fg='green')
            click.secho("Saved configuration file {}".format(
                alice_config.config_file_location),
                        fg='green')

            # Give the use a suggestion as to what to do next...
            how_to_run_message = "\nTo run an Alice node from the default configuration filepath run: \n\n'{}'\n"
            suggested_command = 'nucypher alice run'
            if config_root is not None:
                config_file_location = os.path.join(
                    config_root, config_file
                    or AliceConfiguration.CONFIG_FILENAME)
                suggested_command += ' --config-file {}'.format(
                    config_file_location)
            click.secho(how_to_run_message.format(suggested_command),
                        fg='green')
            return  # FIN

        else:
            click.secho("OK")

    elif action == "destroy":
        """Delete all configuration files from the disk"""

        if dev:
            message = "'nucypher ursula destroy' cannot be used in --dev mode"
            raise click.BadOptionUsage(option_name='--dev', message=message)

        destroy_system_configuration(config_class=AliceConfiguration,
                                     config_file=config_file,
                                     network=network,
                                     config_root=config_root,
                                     force=force)
        if not quiet:
            click.secho("Destroyed {}".format(config_root))
        return

    #
    # Get Alice Configuration
    #

    if dev:
        alice_config = AliceConfiguration(
            dev_mode=True,
            domains={network},
            provider_uri=provider_uri,
            federated_only=True,
        )
    else:
        alice_config = AliceConfiguration.from_configuration_file(
            filepath=config_file,
            domains={network or GLOBAL_DOMAIN},
            rest_port=discovery_port,
            provider_uri=provider_uri)

    # Teacher
    teacher_nodes = list()
    if teacher_uri:
        teacher_node = Ursula.from_teacher_uri(
            teacher_uri=teacher_uri,
            min_stake=min_stake,
            federated_only=alice_config.federated_only)
        teacher_nodes.append(teacher_node)

    if not dev:
        # Keyring
        try:
            click.secho("Decrypting keyring...", fg='blue')
            alice_config.keyring.unlock(password=click_config.get_password())
        except CryptoError:
            raise alice_config.keyring.AuthenticationFailed
        finally:
            click_config.alice_config = alice_config

    # Produce
    ALICE = alice_config(known_nodes=teacher_nodes)

    if action == "run":

        # Alice Control
        alice_control = ALICE.make_wsgi_app()
        click.secho("Starting Alice Character Control...")

        click.secho(f"Alice Verifying Key {bytes(ALICE.stamp).hex()}",
                    fg="green",
                    bold=True)

        # Run
        if dry_run:
            return

        hx_deployer = HendrixDeploy(action="start",
                                    options={
                                        "wsgi": alice_control,
                                        "http_port": http_port
                                    })
        hx_deployer.run()  # <--- Blocking Call to Reactor

    elif action == "view":
        """Paint an existing configuration to the console"""
        paint_configuration(
            config_filepath=config_file or alice_config.config_file_location)
        return

    elif action == "create-policy":
        if not all((bob_verifying_key, bob_encrypting_key, label)):
            raise click.BadArgumentUsage(
                message=
                "--bob-verifying-key, --bob-encrypting-key, and --label are "
                "required options to create a new policy.")

        request_data = {
            'bob_encrypting_key': bob_encrypting_key,
            'bob_signing_key': bob_verifying_key,
            'label': b64encode(bytes(label, encoding='utf-8')).decode(),
            'm': m,
            'n': n,
        }

        response = requests.put(f'http://localhost:{http_port}/create_policy',
                                data=json.dumps(request_data))
        click.secho(response.json())
        return

    elif action == "derive-policy":
        response = requests.post(
            f'http://localhost:{http_port}/derive_policy_pubkey/{label}')

        response_data = response.json()
        policy_encrypting_key = response_data['result'][
            'policy_encrypting_pubkey']
        click.secho(
            f"Created new Policy with label {label} | {policy_encrypting_key}",
            fg='green')

    elif action == "grant":
        request_data = {
            'bob_encrypting_key': bob_encrypting_key,
            'bob_signing_key': bob_verifying_key,
            'label': b64encode(bytes(label, encoding='utf-8')).decode(),
            'm': m,
            'n': n,
            'expiration_time':
            (maya.now() + datetime.timedelta(days=3)).iso8601(),  # TODO
        }

        response = requests.put(f'http://localhost:{http_port}/grant',
                                data=json.dumps(request_data))
        click.secho(response)
        return

    elif action == "revoke":
        raise NotImplementedError  # TODO

    else:
        raise click.BadArgumentUsage(f"No such argument {action}")
Пример #12
0
def bob(click_config, action, quiet, teacher_uri, min_stake, http_port,
        discovery_port, federated_only, network, config_root, config_file,
        provider_uri, registry_filepath, dev, force, dry_run, label,
        policy_encrypting_key, alice_encrypting_key):
    """
    Start and manage a "Bob" character.
    """

    if not quiet:
        click.secho(BOB_BANNER)

    if action == 'init':
        """Create a brand-new persistent Bob"""

        if dev and not quiet:
            click.secho("WARNING: Using temporary storage area", fg='yellow')

        if not config_root:  # Flag
            config_root = click_config.config_file  # Envvar

        bob_config = BobConfiguration.generate(
            password=click_config.get_password(confirm=True),
            config_root=config_root,
            rest_host="localhost",
            domains={network} if network else None,
            federated_only=federated_only,
            no_registry=True,  # Yes we have no registry,
            registry_filepath=registry_filepath,
            provider_uri=provider_uri,
        )

        if not quiet:
            click.secho("Generated keyring {}".format(bob_config.keyring_dir),
                        fg='green')
            click.secho("Saved configuration file {}".format(
                bob_config.config_file_location),
                        fg='green')

            # Give the use a suggestion as to what to do next...
            how_to_run_message = "\nTo run an Bob node from the default configuration filepath run: \n\n'{}'\n"
            suggested_command = 'nucypher bob run'
            if config_root is not None:
                config_file_location = os.path.join(
                    config_root, config_file
                    or BobConfiguration.CONFIG_FILENAME)
                suggested_command += ' --config-file {}'.format(
                    config_file_location)
            click.secho(how_to_run_message.format(suggested_command),
                        fg='green')
            return  # FIN

        else:
            click.secho("OK")

    elif action == "destroy":
        """Delete all configuration files from the disk"""

        if dev:
            message = "'nucypher ursula destroy' cannot be used in --dev mode"
            raise click.BadOptionUsage(option_name='--dev', message=message)

        destroy_system_configuration(config_class=BobConfiguration,
                                     config_file=config_file,
                                     network=network,
                                     config_root=config_root,
                                     force=force)
        if not quiet:
            click.secho("Destroyed {}".format(config_root))
        return

    #
    # Get Bob Configuration
    #

    if dev:
        bob_config = BobConfiguration(
            dev_mode=True,
            domains={network},
            provider_uri=provider_uri,
            federated_only=True,
        )
    else:
        bob_config = BobConfiguration.from_configuration_file(
            filepath=config_file,
            domains={network or GLOBAL_DOMAIN},
            rest_port=discovery_port,
            provider_uri=provider_uri)

    # Teacher
    teacher_nodes = list()
    if teacher_uri:
        teacher_node = Ursula.from_teacher_uri(
            teacher_uri=teacher_uri,
            min_stake=min_stake,
            federated_only=bob_config.federated_only)
        teacher_nodes.append(teacher_node)

    # Produce
    BOB = bob_config(known_nodes=teacher_nodes)

    if action == "run":

        if not dev:
            # Keyring
            try:
                click.secho("Decrypting keyring...", fg='blue')
                bob_config.keyring.unlock(password=click_config.get_password())
            except CryptoError:
                raise bob_config.keyring.AuthenticationFailed
            finally:
                click_config.bob_config = bob_config

        # Bob Control
        bob_control = BOB.make_wsgi_app()
        click.secho("Starting Bob Character Control...")

        click.secho(f"Bob Verifying Key {bytes(BOB.stamp).hex()}",
                    fg="green",
                    bold=True)
        click.secho(
            f"Bob Encrypting Key {bytes(BOB.public_keys(DecryptingPower)).hex()}",
            fg="blue",
            bold=True)

        # Run
        if dry_run:
            return

        hx_deployer = HendrixDeploy(action="start",
                                    options={
                                        "wsgi": bob_control,
                                        "http_port": http_port
                                    })
        hx_deployer.run()  # <--- Blocking Call to Reactor

    elif action == "view":
        """Paint an existing configuration to the console"""
        paint_configuration(
            config_filepath=config_file or bob_config.config_file_location)
        return

    elif action == "retrieve":
        bob_request_data = {
            'label': b64encode(label).decode(),
            'policy_encrypting_pubkey': policy_encrypting_key,
            'alice_signing_pubkey': alice_encrypting_key,
            # 'message_kit': b64encode(bob_message_kit.to_bytes()).decode(),  # TODO
        }

        response = requests.post('/retrieve',
                                 data=json.dumps(bob_request_data))
        click.secho(response)
        return

    else:
        raise click.BadArgumentUsage(f"No such argument {action}")