Beispiel #1
0
def test_dict_resolution():
    dict_object = {'delegated': 'TEST1', 'cloudflare': {'auth_token': 'TEST2'}}

    config = ConfigResolver().with_dict(dict_object)

    assert config.resolve('lexicon:delegated') == 'TEST1'
    assert config.resolve('lexicon:cloudflare:auth_token') == 'TEST2'
    assert config.resolve('lexicon:nonexistent') is None
Beispiel #2
0
def test_dict_resolution():
    dict_object = {"delegated": "TEST1", "cloudflare": {"auth_token": "TEST2"}}

    config = ConfigResolver().with_dict(dict_object)

    assert config.resolve("lexicon:delegated") == "TEST1"
    assert config.resolve("lexicon:cloudflare:auth_token") == "TEST2"
    assert config.resolve("lexicon:nonexistent") is None
Beispiel #3
0
def test_provider_config_lexicon_file_resolution(tmpdir):
    provider_file = tmpdir.join('lexicon_cloudflare.yml')
    provider_file.write('auth_token: TEST2')

    config = ConfigResolver().with_provider_config_file(
        'cloudflare', str(provider_file))

    assert config.resolve('lexicon:cloudflare:auth_token') == 'TEST2'
    assert config.resolve('lexicon:nonexistent') is None
Beispiel #4
0
def test_config_lexicon_file_resolution(tmpdir):
    lexicon_file = tmpdir.join('lexicon.yml')
    lexicon_file.write('delegated: TEST1\ncloudflare:\n  auth_token: TEST2')

    config = ConfigResolver().with_config_file(str(lexicon_file))

    assert config.resolve('lexicon:delegated') == 'TEST1'
    assert config.resolve('lexicon:cloudflare:auth_token') == 'TEST2'
    assert config.resolve('lexicon:nonexistent') is None
Beispiel #5
0
def test_provider_config_lexicon_file_resolution(tmpdir):
    provider_file = tmpdir.join("lexicon_cloudflare.yml")
    provider_file.write("auth_token: TEST2")

    config = ConfigResolver().with_provider_config_file(
        "cloudflare", str(provider_file))

    assert config.resolve("lexicon:cloudflare:auth_token") == "TEST2"
    assert config.resolve("lexicon:nonexistent") is None
Beispiel #6
0
def test_generic_config_feeder_resolution():
    class GenericConfigSource(ConfigSource):
        def resolve(self, config_key):
            return 'TEST1'

    config = ConfigResolver().with_config_source(GenericConfigSource())

    assert config.resolve('lexicon:cloudflare:auth_username') == 'TEST1'
    assert config.resolve('lexicon:nonexistent') == 'TEST1'
Beispiel #7
0
def test_config_lexicon_file_resolution(tmpdir):
    lexicon_file = tmpdir.join("lexicon.yml")
    lexicon_file.write("delegated: TEST1\ncloudflare:\n  auth_token: TEST2")

    config = ConfigResolver().with_config_file(str(lexicon_file))

    assert config.resolve("lexicon:delegated") == "TEST1"
    assert config.resolve("lexicon:cloudflare:auth_token") == "TEST2"
    assert config.resolve("lexicon:nonexistent") is None
Beispiel #8
0
def test_environment_resolution(monkeypatch):
    monkeypatch.setenv("LEXICON_DELEGATED", "TEST1")
    monkeypatch.setenv("LEXICON_CLOUDFLARE_TOKEN", "TEST2")
    monkeypatch.setenv("LEXICON_CLOUDFLARE_AUTH_USERNAME", "TEST3")

    config = ConfigResolver().with_env()

    assert config.resolve("lexicon:delegated") == "TEST1"
    assert config.resolve("lexicon:cloudflare:auth_token") == "TEST2"
    assert config.resolve("lexicon:cloudflare:auth_username") == "TEST3"
    assert config.resolve("lexicon:nonexistent") is None
Beispiel #9
0
def test_environment_resolution(monkeypatch):
    monkeypatch.setenv('LEXICON_DELEGATED', 'TEST1')
    monkeypatch.setenv('LEXICON_CLOUDFLARE_TOKEN', 'TEST2')
    monkeypatch.setenv('LEXICON_CLOUDFLARE_AUTH_USERNAME', 'TEST3')

    config = ConfigResolver().with_env()

    assert config.resolve('lexicon:delegated') == 'TEST1'
    assert config.resolve('lexicon:cloudflare:auth_token') == 'TEST2'
    assert config.resolve('lexicon:cloudflare:auth_username') == 'TEST3'
    assert config.resolve('lexicon:nonexistent') is None
Beispiel #10
0
def test_provider_config_dir_resolution(tmpdir):
    lexicon_file = tmpdir.join('lexicon.yml')
    provider_file = tmpdir.join('lexicon_cloudflare.yml')
    lexicon_file.write('delegated: TEST1\ncloudflare:\n  auth_token: TEST2')
    provider_file.write('auth_username: TEST3')

    config = ConfigResolver().with_config_dir(str(tmpdir))

    assert config.resolve('lexicon:delegated') == 'TEST1'
    assert config.resolve('lexicon:cloudflare:auth_token') == 'TEST2'
    assert config.resolve('lexicon:cloudflare:auth_username') == 'TEST3'
    assert config.resolve('lexicon:nonexistent') is None
Beispiel #11
0
def test_argparse_resolution():
    parser = generate_cli_main_parser()
    data = parser.parse_args([
        '--delegated', 'TEST1', 'cloudflare', 'create', 'example.com', 'TXT',
        '--auth-token', 'TEST2'
    ])

    config = ConfigResolver().with_args(data)

    assert config.resolve('lexicon:delegated') == 'TEST1'
    assert config.resolve('lexicon:cloudflare:auth_token') == 'TEST2'
    assert config.resolve('lexicon:nonexistent') is None
Beispiel #12
0
def test_provider_config_dir_resolution(tmpdir):
    lexicon_file = tmpdir.join("lexicon.yml")
    provider_file = tmpdir.join("lexicon_cloudflare.yml")
    lexicon_file.write("delegated: TEST1\ncloudflare:\n  auth_token: TEST2")
    provider_file.write("auth_username: TEST3")

    config = ConfigResolver().with_config_dir(str(tmpdir))

    assert config.resolve("lexicon:delegated") == "TEST1"
    assert config.resolve("lexicon:cloudflare:auth_token") == "TEST2"
    assert config.resolve("lexicon:cloudflare:auth_username") == "TEST3"
    assert config.resolve("lexicon:nonexistent") is None
Beispiel #13
0
def test_legacy_dict_config_resolution():
    legacy_config = {
        "delegated": "TEST1",
        "auth_token": "TEST2",
        "provider_name": "cloudflare",
    }

    config = ConfigResolver().with_legacy_dict(legacy_config)

    assert config.resolve("lexicon:delegated") == "TEST1"
    assert config.resolve("lexicon:cloudflare:auth_token") == "TEST2"
    assert config.resolve("lexicon:auth_token") is None
    assert config.resolve("lexicon:nonexistent") is None
Beispiel #14
0
def test_legacy_dict_config_resolution():
    legacy_config = {
        'delegated': 'TEST1',
        'auth_token': 'TEST2',
        'provider_name': 'cloudflare'
    }

    config = ConfigResolver().with_legacy_dict(legacy_config)

    assert config.resolve('lexicon:delegated') == 'TEST1'
    assert config.resolve('lexicon:cloudflare:auth_token') == 'TEST2'
    assert config.resolve('lexicon:auth_token') is None
    assert config.resolve('lexicon:nonexistent') is None
Beispiel #15
0
def main() -> None:
    """Main function of Lexicon."""
    # Dynamically determine all the providers available and gather command line arguments.
    parsed_args = generate_cli_main_parser().parse_args()

    log_level = logging.getLevelName(parsed_args.log_level)
    logging.basicConfig(stream=sys.stdout, level=log_level, format="%(message)s")
    logger.debug("Arguments: %s", parsed_args)

    # In the CLI context, will get configuration interactively:
    #   * from the command line
    #   * from the environment variables
    #   * from lexicon configuration files found in given --config-dir (default is current dir)
    config = ConfigResolver()
    config.with_args(parsed_args).with_env().with_config_dir(parsed_args.config_dir)

    client = Client(config)

    results = client.execute()

    action = config.resolve("lexicon:action")
    if not action:
        raise ValueError("Parameter action is not set.")

    handle_output(results, parsed_args.output, action)
Beispiel #16
0
def test_argparse_resolution():
    parser = generate_cli_main_parser()
    data = parser.parse_args([
        "--delegated",
        "TEST1",
        "cloudflare",
        "create",
        "example.com",
        "TXT",
        "--auth-token",
        "TEST2",
    ])

    config = ConfigResolver().with_args(data)

    assert config.resolve("lexicon:delegated") == "TEST1"
    assert config.resolve("lexicon:cloudflare:auth_token") == "TEST2"
    assert config.resolve("lexicon:nonexistent") is None
Beispiel #17
0
def main():
    """Main function of Lexicon."""
    # Dynamically determine all the providers available and gather command line arguments.
    parsed_args = generate_cli_main_parser().parse_args()

    log_level = logging.getLevelName(parsed_args.log_level)
    logging.basicConfig(stream=sys.stdout,
                        level=log_level,
                        format='%(message)s')
    logger.debug('Arguments: %s', parsed_args)

    # In the CLI context, will get configuration interactively:
    #   * from the command line
    #   * from the environment variables
    #   * from lexicon configuration files in working directory
    config = ConfigResolver()
    config.with_args(parsed_args).with_env().with_config_dir(os.getcwd())

    client = Client(config)

    results = client.execute()

    handle_output(results, parsed_args.output,
                  config.resolve('lexicon:action'))
Beispiel #18
0
class LexiconClient:
    def __init__(self, provider_name, action, domain, name, type, content):

        self.lexicon_config = {
            "provider_name": provider_name,
            "action": action,
            "domain": domain,
            "name": name,
            "type": type,
            "content": content,
        }
        # print(self.lexicon_config)
        self.config = LexiconConfigResolver()
        self.config.with_env().with_dict(dict_object=self.lexicon_config)

        self.client = LexClient(self.config)

        self.auth_token = self.config.resolve("lexicon:{}:auth_token".format(
            self.lexicon_config["provider_name"]))

    def execute(self):
        # Check provider config before doing stuff
        results = ""
        if self.auth_token:
            results = self.client.execute()
            # print(results)
            if results:
                logger.info("✓ {}: {} Record {} -> {}".format(
                    self.config.resolve("lexicon:provider_name"),
                    self.config.resolve("lexicon:action").upper(),
                    self.config.resolve("lexicon:name") + "." +
                    self.config.resolve("lexicon:domain"),
                    self.config.resolve("lexicon:content"),
                ))
            else:
                logger.error("Couldn't create Record: {}".format(results))
        else:
            logger.error(
                "✗ {}: Missing auth_token. {} Record {} -> {} failed".format(
                    self.config.resolve("lexicon:provider_name"),
                    self.config.resolve("lexicon:action").upper(),
                    self.config.resolve("lexicon:name") + "." +
                    self.config.resolve("lexicon:domain"),
                    self.config.resolve("lexicon:content"),
                ))
            results = False
        return results