2016-04-14 20:04:23 -04:00
|
|
|
"""Utility functions for Certbot plugin tests."""
|
2015-07-16 19:57:47 -04:00
|
|
|
import argparse
|
2015-07-16 02:00:18 -04:00
|
|
|
import copy
|
2015-07-10 14:42:10 -04:00
|
|
|
import os
|
2015-07-16 02:00:18 -04:00
|
|
|
import re
|
2015-07-14 21:04:43 -04:00
|
|
|
import shutil
|
2015-07-10 14:42:10 -04:00
|
|
|
import tarfile
|
|
|
|
|
|
2021-12-13 20:14:11 -05:00
|
|
|
from certbot_compatibility_test import errors
|
2017-12-11 14:25:09 -05:00
|
|
|
import josepy as jose
|
|
|
|
|
|
2019-11-11 18:41:40 -05:00
|
|
|
from certbot._internal import constants
|
2019-12-09 15:50:20 -05:00
|
|
|
from certbot.tests import util as test_util
|
2015-07-10 14:42:10 -04:00
|
|
|
|
2019-11-01 13:06:32 -04:00
|
|
|
_KEY_BASE = "rsa2048_key.pem"
|
2015-07-21 21:14:57 -04:00
|
|
|
KEY_PATH = test_util.vector_path(_KEY_BASE)
|
|
|
|
|
KEY = test_util.load_pyopenssl_private_key(_KEY_BASE)
|
|
|
|
|
JWK = jose.JWKRSA(key=test_util.load_rsa_private_key(_KEY_BASE))
|
2015-07-16 02:00:18 -04:00
|
|
|
IP_REGEX = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
|
2015-07-10 14:42:10 -04:00
|
|
|
|
|
|
|
|
|
2021-12-13 20:14:11 -05:00
|
|
|
def create_le_config(parent_dir: str) -> argparse.Namespace:
|
2015-07-16 02:00:18 -04:00
|
|
|
"""Sets up LE dirs in parent_dir and returns the config dict"""
|
|
|
|
|
config = copy.deepcopy(constants.CLI_DEFAULTS)
|
|
|
|
|
|
2016-04-13 19:58:21 -04:00
|
|
|
le_dir = os.path.join(parent_dir, "certbot")
|
2018-05-21 23:23:21 -04:00
|
|
|
os.mkdir(le_dir)
|
|
|
|
|
for dir_name in ("config", "logs", "work"):
|
|
|
|
|
full_path = os.path.join(le_dir, dir_name)
|
|
|
|
|
os.mkdir(full_path)
|
|
|
|
|
full_name = dir_name + "_dir"
|
|
|
|
|
config[full_name] = full_path
|
2015-07-16 02:00:18 -04:00
|
|
|
|
2015-11-20 19:15:50 -05:00
|
|
|
config["domains"] = None
|
|
|
|
|
|
2018-05-14 12:33:30 -04:00
|
|
|
return argparse.Namespace(**config)
|
2015-07-16 02:00:18 -04:00
|
|
|
|
2015-07-22 22:51:05 -04:00
|
|
|
|
2021-12-13 20:14:11 -05:00
|
|
|
def extract_configs(configs: str, parent_dir: str) -> str:
|
2015-07-16 02:00:18 -04:00
|
|
|
"""Extracts configs to a new dir under parent_dir and returns it"""
|
2015-10-11 20:33:00 -04:00
|
|
|
config_dir = os.path.join(parent_dir, "configs")
|
2015-07-10 14:42:10 -04:00
|
|
|
|
2015-07-14 21:04:43 -04:00
|
|
|
if os.path.isdir(configs):
|
|
|
|
|
shutil.copytree(configs, config_dir, symlinks=True)
|
|
|
|
|
elif tarfile.is_tarfile(configs):
|
2015-07-16 02:00:18 -04:00
|
|
|
with tarfile.open(configs, "r") as tar:
|
2015-07-14 21:04:43 -04:00
|
|
|
tar.extractall(config_dir)
|
|
|
|
|
else:
|
2015-07-16 02:00:18 -04:00
|
|
|
raise errors.Error("Unknown configurations file type")
|
2015-07-14 21:04:43 -04:00
|
|
|
|
2015-07-16 02:00:18 -04:00
|
|
|
return config_dir
|