def test_write_tables(tmpdir): failed_domains = [] store_in_new_files = False key_directory = str(tmpdir) known_good_key_table_content = """\ test_1\texample_1.com:2000-01-01:%s/test_1.2000-01-01.key test_2\texample_2.com:2000-01-01:%s/test_2.2000-01-01.key """ % (key_directory, key_directory) known_good_signing_table_content = """\ *@example_1.com\ttest_1 *@example_2.com\ttest_2 """ genkeys_module = genkeys.Genkeys() genkeys_module.domain_data = { "example_1.com" : { "key" : "test_1" }, "example_2.com" : { "key" : "test_2" } } genkeys_module.config.update( { "key_table_owner" : os.getuid(), "key_table_group" : os.getgid(), "signing_table_owner" : os.getuid(), "signing_table_group" : os.getgid() }) selector = "2000-01-01" key_table_name = "key.table" signing_table_name = "signing.table" test_key_domain_table = { 1 : { "domain" : "example_1.com", "key_name" : "test_1", "selector" : selector, "path_to_keyfile" : "test_123.key" }, 2 : { "domain" : "example_2.com", "key_name" : "test_2", "selector" : selector, "path_to_keyfile" : "test_456.key" } } os.chdir(key_directory) assert genkeys_module.write_tables( test_key_domain_table, selector, failed_domains, store_in_new_files, key_directory) assert os.path.exists("key.table") assert os.path.exists("signing.table") key_table_content = open(key_table_name, "r", encoding="utf-8").read() signing_table_content = open(signing_table_name, "r", encoding="utf-8").read() assert key_table_content == known_good_key_table_content assert signing_table_content == known_good_signing_table_content
def test_save_dns_api_module_extra_data(tmpdir): genkeys_module = genkeys.Genkeys() valid_file_data = { "test1" : { "a" : "b", "c" : "d" }, "test2" : { "ab123" : "456" } } dnsapi_extra_file = "%s/dnsapi_extra_data.yml" % str(tmpdir) genkeys_module.dns_api_extra = valid_file_data genkeys_module.save_dns_api_module_extra_data(dnsapi_extra_file) new_file_data = yaml.safe_load(open(dnsapi_extra_file, "r")) assert new_file_data == valid_file_data assert genkeys_module.load_dns_api_module_extra_data(dnsapi_extra_file) assert genkeys_module.dns_api_extra == valid_file_data
def test_find_dns_api_modules(tmpdir): src_path = "%s/../src" % os.path.dirname(__file__) print(__file__) genkeys_module = genkeys.Genkeys() genkeys_module.config.update({"opendkim_dir" : src_path, "dnsapi_directory" : src_path}) genkeys_module.dns_api_data = { "null": [], "freedns" : [ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ], "linode" : [ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ], "route53" :[ "xxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ], "cloudflare" : [ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "*****@*****.**" ], "cloudflareapi" : [ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "*****@*****.**" ] } to_find = ["null", "freedns", "linode", "route53", "cloudflare", "cloudeflareapi"] print(src_path) os.chdir(str(tmpdir)) print(os.listdir(src_path)) modules, should_update_dns = genkeys_module.find_dns_api_modules() print(modules) assert should_update_dns found = [] for module in modules: assert module in to_find found.append(module) assert to_find.sort() == found.sort()
def test_load_dns_api_module_extra_data(tmpdir): genkeys_module = genkeys.Genkeys() valid_file_contents = """ - test1 a: b c: d - test2 ab123: 456 """ valid_file_data = { "test1" : { "a" : "b", "c" : "d" }, "test2" : { "ab123" : "456" } } dnsapi_extra_file = "%s/dnsapi_extra_data.yml" % str(tmpdir) with open(dnsapi_extra_file, "w") as file: yaml.safe_dump(valid_file_data, file) assert genkeys_module.load_dns_api_module_extra_data(dnsapi_extra_file) assert genkeys_module.dns_api_extra == valid_file_data
def test_generate_keys(tmpdir): class FakeArg(): next_month = False genkeys_module = genkeys.Genkeys() genkeys_module.args = FakeArg() selector = genkeys_module.generate_selector() # predefined private_keys test_keys = { "test_1" : { "new" : False, "selector" : selector, "private_key" : """\ -----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEAtSM6zxmpP5c/uoM7JZPHhYwerwXAcvNHsX/lDAOIYmMHrSzK /VWmH2L7wMd0XXNpT+iHxf3GPzHSXp21PpofVYSeQHVyjGAnXnrHjat3OExL+RYw F4INaETRePr5c9sWYIGv5vLwiePHV4fZJr6QnNht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JYIEom1OzDWaTB7zoLKof3UIP"\ "skXLspTmTpIEY3+9507BjMBpBO08gWgnfF 97M4F0mseyUkCJm"\ "ZZMyVKnPAHAkYX90aoANPXUYJ8qoBPjT3I5yT92yEwtKIQn4N xwIDAQAB", "filename" : "%s/test_1.%s.key" % (str(tmpdir), selector) } } key_names = [ "test_2" ] # write private keys os.chdir(str(tmpdir)) for key_name, key_data in test_keys.items(): key_names.append(key_name) path = "%s" % key_data["filename"] with open(path, "w") as file: file.write(key_data["private_key"]) genkeys_module.config["new_key_owner"] = os.getuid() genkeys_module.config["new_key_group"] = os.getgid() generated_key_data = genkeys_module.generate_keys(selector, str(tmpdir), key_names) # check if all generated files exist and if they are valid keys if not generated_key_data: print("Failed to generate keys.", file=sys.stderr) sys.exit(1) found = [] for key_name, key_data in generated_key_data.items(): # iterate over selector and key data predefined = test_keys.get(key_name) assert isinstance(key_data, dict) assert isinstance(key_data["new"], bool) file_name = key_data.get("filename") assert os.path.exists(file_name) #key_contents = open(file_name, "r", encoding="utf-8").read() if predefined: assert predefined["selector"] == selector assert not key_data["new"], "Key %s is declared as new but is old!" % key_name assert key_data.get("selector") == predefined["selector"] assert key_data.get("selector") == selector assert key_data["new"] == predefined["new"] # get pubkey print(key_data["plain"]) print(predefined["plain"]) assert key_data["plain"] == predefined["plain"] assert key_data["chunked"] == predefined["chunked"] else: assert key_data["new"] found.append(key_name) # check if selector and key_data are predefined # check if they're equal # add to found # check if key_names == found for item in key_names: assert item in found
def test_make_dkim_record_content(): genkeys_module = genkeys.Genkeys() test_publickey = "test_abc" valid_dkim_record_content = [("v", "DKIM1"), ("h", "sha256"), ("k", "rsa"), ("s", "email"), ("p", test_publickey)] generated_dkim_record_content = genkeys_module.make_dkim_record_content(test_publickey) assert valid_dkim_record_content == generated_dkim_record_content
def test_make_dkim_record_name(): genkeys_module = genkeys.Genkeys() generated_record_name = genkeys_module.make_dkim_record_name("test_selector", "example.com") valid_test_record_name = "test_selector._domainkey.example.com" assert generated_record_name == valid_test_record_name
def test_make_key_name(): genkeys_module = genkeys.Genkeys() assert genkeys_module.make_key_name("example.com") == "example-com"
def test_generate_key_file_name(): genkeys_module = genkeys.Genkeys() assert genkeys_module.generate_key_file_name("foo", "bar") == "foo.bar.key"