Exemplo n.º 1
0
def _setup_application():
    """Setup things that can be taken care of before io loop is started"""
    global io_loop, tornado_app, public_url, thrift_context, easy_client
    global server, client_ssl, request_scheduler, anonymous_principal

    # Tweak some config options
    config.web.url_prefix = normalize_url_prefix(config.web.url_prefix)
    if not config.auth.token.secret:
        config.auth.token.secret = os.urandom(20)
        if config.auth.enabled:
            logger.warning(
                "Brew-view was started with authentication enabled and no "
                "Secret. Generated tokens will not be valid across Brew-view "
                "restarts. To prevent this set the auth.token.secret config."
            )

    public_url = Url(scheme='https' if config.web.ssl.enabled else 'http',
                     host=config.web.public_fqdn,
                     port=config.web.port,
                     path=config.web.url_prefix).url

    # This is not super clean as we're pulling the config from different
    # 'sections,' but the scheduler is the only thing that uses this
    easy_client = EasyClient(
        host=config.web.public_fqdn,
        port=config.web.port,
        url_prefix=config.web.url_prefix,
        ssl_enabled=config.web.ssl.enabled,
        ca_cert=config.web.ssl.ca_cert,
        username=config.scheduler.auth.username,
        password=config.scheduler.auth.password,
    )

    thrift_context = _setup_thrift_context()
    tornado_app = _setup_tornado_app()
    server_ssl, client_ssl = _setup_ssl_context()
    anonymous_principal = load_anonymous()
    request_scheduler = _setup_scheduler()

    server = HTTPServer(tornado_app, ssl_options=server_ssl)
    io_loop = IOLoop.current()
Exemplo n.º 2
0
    def __init__(self,
                 bg_host=None,
                 bg_port=None,
                 ssl_enabled=False,
                 api_version=None,
                 logger=None,
                 ca_cert=None,
                 client_cert=None,
                 url_prefix=None,
                 ca_verify=True,
                 **kwargs):
        bg_host = bg_host or kwargs.get('host')
        if not bg_host:
            raise ValueError('Missing keyword argument "bg_host"')

        bg_port = bg_port or kwargs.get('port')
        if not bg_port:
            raise ValueError('Missing keyword argument "bg_port"')

        self.logger = logger or logging.getLogger(__name__)

        # Configure the session to use when making requests
        self.session = Session()
        timeout = kwargs.get('client_timeout', None)
        if timeout == -1:
            timeout = None

        # Having two is kind of strange to me, but this is what Requests does
        self.session.mount('https://', TimeoutAdapter(timeout=timeout))
        self.session.mount('http://', TimeoutAdapter(timeout=timeout))

        if not ca_verify:
            urllib3.disable_warnings()
            self.session.verify = False
        elif ca_cert:
            self.session.verify = ca_cert

        if client_cert:
            self.session.cert = client_cert

        self.username = kwargs.get('username', None)
        self.password = kwargs.get('password', None)
        self.access_token = kwargs.get('access_token', None)
        self.refresh_token = kwargs.get('refresh_token', None)

        # Configure the beer-garden URLs
        scheme = 'https' if ssl_enabled else 'http'
        self.base_url = (
            '%s://%s:%s%s' %
            (scheme, bg_host, bg_port, normalize_url_prefix(url_prefix)))
        self.version_url = self.base_url + 'version'
        self.config_url = self.base_url + 'config'

        api_version = api_version or self.LATEST_VERSION
        if api_version == 1:
            self.system_url = self.base_url + 'api/v1/systems/'
            self.instance_url = self.base_url + 'api/v1/instances/'
            self.command_url = self.base_url + 'api/v1/commands/'
            self.request_url = self.base_url + 'api/v1/requests/'
            self.queue_url = self.base_url + 'api/v1/queues/'
            self.logging_config_url = self.base_url + 'api/v1/config/logging/'
            self.job_url = self.base_url + 'api/v1/jobs/'
            self.token_url = self.base_url + 'api/v1/tokens/'
            self.user_url = self.base_url + 'api/v1/users/'

            self.event_url = self.base_url + 'api/vbeta/events/'
        else:
            raise ValueError("Invalid beer-garden API version: %s" %
                             api_version)
Exemplo n.º 3
0
def load_config(cli_args=None, argument_parser=None, **kwargs):
    """Load configuration using Yapconf

    Configuration will be loaded from these sources, with earlier sources having
    higher priority:

        1. ``**kwargs`` passed to this method
        2. ``cli_args`` passed to this method
        3. Environment variables using the ``BG_`` prefix
        4. Default values in the brewtils specification

    Args:
        cli_args (list, optional): List of command line arguments for
            configuration loading
        argument_parser (ArgumentParser, optional): Argument parser to use when
            parsing cli_args. Supplying this allows adding additional arguments
            prior to loading the configuration. This can be useful if your
            startup script takes additional arguments. See get_argument_parser
            for additional information.
        **kwargs: Additional configuration overrides

    Returns:
        :obj:`box.Box`: The resolved configuration object
    """
    spec = YapconfSpec(SPECIFICATION, env_prefix="BG_")

    sources = []

    if kwargs:
        # Do a little kwarg massaging for backwards compatibility
        if "bg_host" not in kwargs and "host" in kwargs:
            warnings.warn(
                "brewtils.load_config called with 'host' keyword "
                "argument. This name will be removed in version 3.0, "
                "please use 'bg_host' instead.",
                DeprecationWarning,
                stacklevel=2,
            )
            kwargs["bg_host"] = kwargs.pop("host")
        if "bg_port" not in kwargs and "port" in kwargs:
            warnings.warn(
                "brewtils.load_config called with 'port' keyword "
                "argument. This name will be removed in version 3.0, "
                "please use 'bg_port' instead.",
                DeprecationWarning,
                stacklevel=2,
            )
            kwargs["bg_port"] = kwargs.pop("port")

        sources.append(("kwargs", kwargs))

    if cli_args:
        if not argument_parser:
            argument_parser = ArgumentParser()
            spec.add_arguments(argument_parser)

        parsed_args = argument_parser.parse_args(cli_args)
        sources.append(("cli_args", vars(parsed_args)))

    sources.append("ENVIRONMENT")

    try:
        config = spec.load_config(*sources)
    except YapconfItemNotFound as ex:
        if ex.item.name == "bg_host":
            raise ValidationError(
                "Unable to create a plugin without a "
                "beer-garden host. Please specify one on the "
                "command line (--bg-host), in the "
                "environment (BG_HOST), or in kwargs "
                "(bg_host)"
            )
        raise

    # Make sure the url_prefix is normal
    config.url_prefix = normalize_url_prefix(config.url_prefix)

    return config
Exemplo n.º 4
0
def test_normalize_url_prefix(normalized, initial):
    assert normalized == normalize_url_prefix(initial)
Exemplo n.º 5
0
def load_config(cli_args=True,
                environment=True,
                argument_parser=None,
                bootstrap=False,
                **kwargs):
    """Load configuration using Yapconf

    Configuration will be loaded from these sources, with earlier sources having
    higher priority:

        1. ``**kwargs`` passed to this method
        2. Command line arguments (if ``cli_args`` argument is not False)
        3. Environment variables using the ``BG_`` prefix (if ``environment`` argument
            is not False)
        4. Default values in the brewtils specification

    Args:
        cli_args (Union[bool, list], optional): Specifies whether command line should be
            used as a configuration source
            - True: Argparse will use the standard sys.argv[1:]
            - False: Command line arguments will be ignored when loading configuration
            - List of strings: Will be parsed as CLI args (instead of using sys.argv)
        environment (bool): Specifies whether environment variables (with the ``BG_``
            prefix) should be used when loading configuration
        argument_parser (ArgumentParser, optional, deprecated): Argument parser to use
            when parsing cli_args. Supplying this allows adding additional arguments
            prior to loading the configuration. This can be useful if your
            startup script takes additional arguments. See get_argument_parser
            for additional information.
        **kwargs: Additional configuration overrides

    Returns:
        box.Box: The resolved configuration object
    """
    spec = YapconfSpec(SPECIFICATION, env_prefix="BG_")

    sources = []

    if kwargs:
        # First deprecate / translate items with multiple names
        mangled_kwargs = _translate_kwargs(**kwargs)

        # Metadata is a little weird because yapconf doesn't support raw dicts, so we
        # need to make it a json string in that case
        metadata = kwargs.get("metadata")
        if isinstance(metadata, dict):
            mangled_kwargs["metadata"] = json.dumps(metadata)

        sources.append(("kwargs", mangled_kwargs))

    if cli_args:
        if cli_args is True:
            sources.append("CLI")
        else:
            if not argument_parser:
                argument_parser = ArgumentParser()
                spec.add_arguments(argument_parser)

            parsed_args, unknown = argument_parser.parse_known_args(cli_args)
            sources.append(("cli_args", vars(parsed_args)))

    if environment:
        sources.append("ENVIRONMENT")

    try:
        config = spec.load_config(*sources, bootstrap=bootstrap)
    except YapconfItemNotFound as ex:
        if ex.item.name == "bg_host":
            raise ValidationError(
                "Unable to create a plugin without a Beer-garden host. Please specify "
                "one on the command line (--bg-host), in the environment (BG_HOST), or "
                "in kwargs (bg_host).")
        raise

    # Make sure the url_prefix is normal
    if "bg_url_prefix" in config:
        config.bg_url_prefix = normalize_url_prefix(config.bg_url_prefix)

    return config
Exemplo n.º 6
0
    def __init__(self, *args, **kwargs):
        self._config = self._load_config(args, kwargs)

        self.bg_host = self._config.bg_host
        self.bg_port = self._config.bg_port
        self.bg_prefix = self._config.bg_url_prefix
        self.api_version = self._config.api_version
        self.username = self._config.username
        self.password = self._config.password
        self.access_token = self._config.access_token
        self.refresh_token = self._config.refresh_token
        self.client_cert = self._config.client_cert
        self.client_key = self._config.client_key

        # Configure the session to use when making requests
        self.session = Session()

        if self._config.proxy:
            if self._config.ssl_enabled:
                self.session.proxies.update({"https": self._config.proxy})
            else:
                self.session.proxies.update({"http": self._config.proxy})

        # This is what Requests is expecting
        if self._config.client_key:
            self.session.cert = (self._config.client_cert,
                                 self._config.client_key)
        else:
            self.session.cert = self._config.client_cert

        if not self._config.ca_verify:
            urllib3.disable_warnings()
            self.session.verify = False
        elif self._config.ca_cert:
            self.session.verify = self._config.ca_cert

        client_timeout = self._config.client_timeout
        if client_timeout == -1:
            client_timeout = None

        # Having two is kind of strange to me, but this is what Requests does
        self.session.mount("https://", TimeoutAdapter(timeout=client_timeout))
        self.session.mount("http://", TimeoutAdapter(timeout=client_timeout))

        # Configure the beer-garden URLs
        self.base_url = "%s://%s:%s%s" % (
            "https" if self._config.ssl_enabled else "http",
            self.bg_host,
            self.bg_port,
            normalize_url_prefix(self.bg_prefix),
        )
        self.version_url = self.base_url + "version"
        self.config_url = self.base_url + "config"

        if self.api_version == 1:
            self.garden_url = self.base_url + "api/v1/gardens/"
            self.system_url = self.base_url + "api/v1/systems/"
            self.instance_url = self.base_url + "api/v1/instances/"
            self.command_url = self.base_url + "api/v1/commands/"
            self.request_url = self.base_url + "api/v1/requests/"
            self.queue_url = self.base_url + "api/v1/queues/"
            self.logging_url = self.base_url + "api/v1/logging/"
            self.job_url = self.base_url + "api/v1/jobs/"
            self.job_export_url = self.base_url + "api/v1/export/jobs/"
            self.job_import_url = self.base_url + "api/v1/import/jobs/"
            self.token_url = self.base_url + "api/v1/token/"
            self.user_url = self.base_url + "api/v1/users/"
            self.admin_url = self.base_url + "api/v1/admin/"
            self.forward_url = self.base_url + "api/v1/forward"

            # Deprecated
            self.logging_config_url = self.base_url + "api/v1/config/logging/"

            # Beta
            self.event_url = self.base_url + "api/vbeta/events/"
            self.chunk_url = self.base_url + "api/vbeta/chunks/"
            self.file_url = self.base_url + "api/vbeta/file/"
        else:
            raise ValueError("Invalid Beer-garden API version: %s" %
                             self.api_version)