def initialize_services(controller, services_config):
    """
    Load keys for services listed in the config
    """

    # Load the keys and config for each onion service
    for service in services_config:
        try:
            service_key = util.key_decrypt_prompt(service.get("key"))
        except OSError as e:
            if e.errno == errno.ENOENT:
                logger.error("Private key file %s could not be found. "
                             "Relative paths in the config file are loaded "
                             "relative to the config file directory.",
                             service.get("key"))
                sys.exit(1)
            else:
                raise
        # Key file was read but a valid private key was not found.
        if not service_key:
            logger.error("Private key %s could not be loaded. It is a not "
                         "valid 1024 bit PEM encoded RSA private key",
                         service.get("key"))
            sys.exit(1)
        else:
            # Successfully imported the private key
            onion_address = util.calc_onion_address(service_key)
            logger.debug("Loaded private key for service %s.onion.",
                         onion_address)

        # Load all instances for the current onion service
        instance_config = service.get("instances", [])
        if not instance_config:
            logger.error("Could not load any instances for service "
                         "%s.onion.", onion_address)
            sys.exit(1)
        else:
            instances = []
            for instance in instance_config:
                instances.append(onionbalance.instance.Instance(
                    controller=controller,
                    onion_address=instance.get("address"),
                    authentication_cookie=instance.get("auth")
                ))

            logger.info("Loaded %d instances for service %s.onion.",
                        len(instances), onion_address)

        # Store service configuration in config.services global
        config.services.append(onionbalance.service.Service(
            controller=controller,
            service_key=service_key,
            instances=instances
        ))

        # Store a global reference to current controller connection
        config.controller = controller
Example #2
0
def initialize_services(controller, services_config):
    """
    Load keys for services listed in the config
    """

    # Load the keys and config for each onion service
    for service in services_config:
        try:
            service_key = util.key_decrypt_prompt(service.get("key"))
        except OSError as e:
            if e.errno == errno.ENOENT:
                logger.error(
                    "Private key file %s could not be found. "
                    "Relative paths in the config file are loaded "
                    "relative to the config file directory.",
                    service.get("key"))
                sys.exit(1)
            else:
                raise
        # Key file was read but a valid private key was not found.
        if not service_key:
            logger.error(
                "Private key %s could not be loaded. It is a not "
                "valid 1024 bit PEM encoded RSA private key",
                service.get("key"))
            sys.exit(1)
        else:
            # Successfully imported the private key
            onion_address = util.calc_onion_address(service_key)
            logger.debug("Loaded private key for service %s.onion.",
                         onion_address)

        # Load all instances for the current onion service
        instance_config = service.get("instances", [])
        if not instance_config:
            logger.error(
                "Could not load and instances for service "
                "%s.onion.", onion_address)
            sys.exit(1)
        else:
            instances = []
            for instance in instance_config:
                instances.append(
                    onionbalance.instance.Instance(
                        controller=controller,
                        onion_address=instance.get("address"),
                        authentication_cookie=instance.get("auth")))

            logger.info("Loaded %d instances for service %s.onion.",
                        len(instances), onion_address)

        # Store service configuration in config.services global
        config.services.append(
            onionbalance.service.Service(controller=controller,
                                         service_key=service_key,
                                         instances=instances))
Example #3
0
def parse_config_file(config_file):
    """
    Parse config file containing service information
    """
    config_path = os.path.abspath(config_file)
    if os.path.exists(config_path):
        with open(config_file, 'r') as handle:
            config_data = yaml.load(handle.read())
            logger.info("Loaded the config file '%s'.", config_path)
    else:
        logger.error("The specified config file '%s' does not exist.",
                     config_path)
        sys.exit(1)

    # Rewrite relative paths in the config to be relative to the config
    # file directory
    config_directory = os.path.dirname(config_path)
    for service in config_data.get('services'):
        if not os.path.isabs(service.get('key')):
            service['key'] = os.path.join(config_directory, service['key'])

    return config_data
Example #4
0
def parse_config_file(config_file):
    """
    Parse config file containing service information
    """
    config_path = os.path.abspath(config_file)
    if os.path.exists(config_path):
        with open(config_file, 'r') as handle:
            config_data = yaml.load(handle.read())
            logger.info("Loaded the config file '%s'.", config_path)
    else:
        logger.error("The specified config file '%s' does not exist.",
                     config_path)
        sys.exit(1)

    # Rewrite relative paths in the config to be relative to the config
    # file directory
    config_directory = os.path.dirname(config_path)
    for service in config_data.get('services'):
        if not os.path.isabs(service.get('key')):
            service['key'] = os.path.join(config_directory, service['key'])

    return config_data