Ejemplo n.º 1
0
 def __init__(self, config: Dict[str, Any], scope: str = "default"):
     super(File, self).__init__(config, scope)
     try:
         self.path = Loadable.from_legacy_fmt(config["path"])
     except KeyError:
         try:
             self.path = Loadable(**config["spec"])
         except KeyError:
             raise KeyError('File source needs to specify "spec" within config')
Ejemplo n.º 2
0
 def load_tags(cls, v: Dict[str, Union[Loadable, str]]) -> Dict[str, Any]:
     ret = dict()
     for key, value in v.items():
         if isinstance(value, dict):
             ret[key] = Loadable(**value).load()
         elif isinstance(value, str):
             ret[key] = Loadable.from_legacy_fmt(value).load()
         else:
             raise ValueError(
                 f"Received an invalid tag for statsd: {value}")
     return ret
Ejemplo n.º 3
0
def test_loading_a_file():
    # --- setup
    config = yaml.safe_load("""
    sources:
      - type: service_broker
        config:
          brokers:
            - https://hello
    """)
    with open("test_file.yaml", "w+") as f:
        yaml.dump(config, f)
    # --- load
    data = Loadable.from_legacy_fmt("file://./test_file.yaml").load()
    # --- cleanup
    os.remove("test_file.yaml")
    # --- test
    expected = {
        "sources": [{
            "config": {
                "brokers": ["https://hello"]
            },
            "type": "service_broker"
        }]
    }
    assert data == expected
Ejemplo n.º 4
0
def test_loading_a_file_over_http_with_json():
    data = Loadable.from_legacy_fmt(
        "https+json://bitbucket.org"
        "/!api/2.0/snippets/vsyrakis/qebL6z/"
        "52450800bf05434831f9f702aedaeca0a1b42122/files/controlplane_test.json"
    ).load()
    expected = {"sources": [{"config": {}, "type": "service_broker"}]}
    assert data == expected
Ejemplo n.º 5
0
    def from_legacy_config(other: SovereignConfig) -> "SovereignConfigv2":
        new_templates = dict()
        for version, templates in other.templates.items():
            specs = list()
            for type, path in templates.items():
                if isinstance(path, str):
                    specs.append(
                        TemplateSpecification(
                            type=type, spec=Loadable.from_legacy_fmt(path)))
                else:
                    # Just in case? Although this shouldn't happen
                    specs.append(TemplateSpecification(type=type, spec=path))
            new_templates[str(version)] = specs

        return SovereignConfigv2(
            sources=other.sources,
            templates=new_templates,
            source_config=SourcesConfiguration(
                refresh_rate=other.sources_refresh_rate,
                cache_strategy=other.cache_strategy,
            ),
            modifiers=other.modifiers,
            global_modifiers=other.global_modifiers,
            template_context=ContextConfiguration(
                context=ContextConfiguration.context_from_legacy(
                    other.template_context),
                refresh=other.refresh_context,
                refresh_rate=other.context_refresh_rate,
            ),
            matching=NodeMatching(
                enabled=other.node_matching,
                source_key=other.source_match_key,
                node_key=other.node_match_key,
            ),
            authentication=AuthConfiguration(
                enabled=other.auth_enabled,
                auth_passwords=SecretStr(other.auth_passwords),
                encryption_key=SecretStr(other.encryption_key),
            ),
            logging=LoggingConfiguration(
                application_logs=ApplicationLogConfiguration(
                    enabled=other.enable_application_logs, ),
                access_logs=AccessLogConfiguration(
                    enabled=other.enable_access_logs,
                    log_fmt=other.log_fmt,
                    ignore_empty_fields=other.ignore_empty_log_fields,
                ),
            ),
            statsd=other.statsd,
            sentry_dsn=SecretStr(other.sentry_dsn),
            debug=other.debug_enabled,
            legacy_fields=LegacyConfig(
                regions=other.regions,
                eds_priority_matrix=other.eds_priority_matrix,
                dns_hard_fail=other.dns_hard_fail,
                environment=other.environment,
            ),
        )
Ejemplo n.º 6
0
 def load_context_variables(self) -> Dict[str, Any]:
     ret = dict()
     for k, v in self.configured_context.items():
         if isinstance(v, Loadable):
             ret[k] = v.load()
         elif isinstance(v, str):
             ret[k] = Loadable.from_legacy_fmt(v).load()
     if "crypto" not in ret:
         ret["crypto"] = self.crypto
     return ret
Ejemplo n.º 7
0
 def __init__(self, path: Union[str, Loadable]) -> None:
     if isinstance(path, str):
         self.loadable: Loadable = Loadable.from_legacy_fmt(path)
     elif isinstance(path, Loadable):
         self.loadable = path
     self.is_python_source = self.loadable.protocol == Protocol.python
     self.source = self.load_source()
     self.checksum = zlib.adler32(self.source.encode())
     template_ast = jinja_env.parse(self.source)
     self.jinja_variables = meta.find_undeclared_variables(
         template_ast)  # type: ignore
Ejemplo n.º 8
0
def test_loading_a_file_over_http():
    data = Loadable.from_legacy_fmt(
        "https://bitbucket.org"
        "/!api/2.0/snippets/vsyrakis/Ee9yjo/"
        "54ae1495ab113cc669623e538691106c7de313c9/files/controlplane_test.yaml"
    ).load()
    expected = {
        "sources": [{
            "config": {
                "brokers": ["https://google.com/"]
            },
            "type": "service_broker"
        }]
    }
    assert data == expected
Ejemplo n.º 9
0
class File(Source):
    def __init__(self, config: Dict[str, Any], scope: str = "default"):
        super(File, self).__init__(config, scope)
        try:
            self.path = Loadable.from_legacy_fmt(config["path"])
        except KeyError:
            try:
                self.path = Loadable(**config["spec"])
            except KeyError:
                raise KeyError('File source needs to specify "spec" within config')

    def get(self) -> Any:
        """
        Uses the file config loader to load the given path
        """
        return self.path.load()
Ejemplo n.º 10
0
 def context_from_legacy(context: Dict[str, str]) -> Dict[str, Loadable]:
     ret = dict()
     for key, value in context.items():
         ret[key] = Loadable.from_legacy_fmt(value)
     return ret
Ejemplo n.º 11
0
def test_loading_environment_variable():
    data = Loadable.from_legacy_fmt("env://CONFIG_LOADER_TEST").load()
    assert data == {"hello": "world"}
Ejemplo n.º 12
0
def test_loading_python_packaged_resources():
    data = Loadable.from_legacy_fmt(
        "pkgdata+string://sovereign:static/style.css").load()
    assert "font-family:" in data
Ejemplo n.º 13
0
def test_loading_a_non_parseable_line_returns_a_string():
    data = Loadable.from_legacy_fmt("helloworld").load()
    assert data == "helloworld"