def METHOD_NAME(hostname, sign=False):
    print("creating cert for " + hostname)
    tempnames = []
    for _ in range(3):
        with tempfile.NamedTemporaryFile(delete=False) as f:
            tempnames.append(f.name)
    req_file, cert_file, key_file = tempnames
    try:
        req = req_template.format(hostname=hostname)
        with open(req_file, "w") as f:
            f.write(req)
        args = [
            "req",
            "-new",
            "-days",
            "365242",
            "-nodes",
            "-newkey",
            "rsa:2048",
            "-keyout",
            key_file,
            "-config",
            req_file,
        ]
        if sign:
            with tempfile.NamedTemporaryFile(delete=False) as f:
                tempnames.append(f.name)
                reqfile = f.name
            args += ["-out", reqfile]
        else:
            args += ["-x509", "-out", cert_file]
        subprocess.check_call(["openssl"] + args)
        if sign:
            args = [
                "ca",
                "-config",
                req_file,
                "-out",
                cert_file,
                "-outdir",
                "cadir",
                "-policy",
                "policy_anything",
                "-batch",
                "-infiles",
                reqfile,
            ]
            subprocess.check_call(["openssl"] + args)
        with open(cert_file) as f:
            cert = f.read()
        with open(key_file) as f:
            key = f.read()
        return cert, key
    finally:
        for name in tempnames:
            os.remove(name)