def _fetch_and_enable_new_certificate(domain, staging=False, no_checks=False): if not os.path.exists(ACCOUNT_KEY_FILE): _generate_account_key() # Make sure tmp folder exists logger.debug("Making sure tmp folders exists...") if not os.path.exists(WEBROOT_FOLDER): os.makedirs(WEBROOT_FOLDER) if not os.path.exists(TMP_FOLDER): os.makedirs(TMP_FOLDER) _set_permissions(WEBROOT_FOLDER, "root", "www-data", 0o650) _set_permissions(TMP_FOLDER, "root", "root", 0o640) # Regen conf for dnsmasq if needed _regen_dnsmasq_if_needed() # Prepare certificate signing request logger.debug("Prepare key and certificate signing request (CSR) for %s...", domain) domain_key_file = "%s/%s.pem" % (TMP_FOLDER, domain) _generate_key(domain_key_file) _set_permissions(domain_key_file, "root", "ssl-cert", 0o640) _prepare_certificate_signing_request(domain, domain_key_file, TMP_FOLDER) # Sign the certificate logger.debug("Now using ACME Tiny to sign the certificate...") domain_csr_file = "%s/%s.csr" % (TMP_FOLDER, domain) if staging: certification_authority = STAGING_CERTIFICATION_AUTHORITY else: certification_authority = PRODUCTION_CERTIFICATION_AUTHORITY try: signed_certificate = sign_certificate( ACCOUNT_KEY_FILE, domain_csr_file, WEBROOT_FOLDER, log=logger, disable_check=no_checks, CA=certification_authority, ) except ValueError as e: if "urn:acme:error:rateLimited" in str(e): raise YunohostError("certmanager_hit_rate_limit", domain=domain) else: logger.error(str(e)) raise YunohostError("certmanager_cert_signing_failed") except Exception as e: logger.error(str(e)) raise YunohostError("certmanager_cert_signing_failed") # Now save the key and signed certificate logger.debug("Saving the key and signed certificate...") # Create corresponding directory date_tag = datetime.utcnow().strftime("%Y%m%d.%H%M%S") if staging: folder_flag = "staging" else: folder_flag = "letsencrypt" new_cert_folder = "%s/%s-history/%s-%s" % ( CERT_FOLDER, domain, date_tag, folder_flag, ) os.makedirs(new_cert_folder) _set_permissions(new_cert_folder, "root", "root", 0o655) # Move the private key domain_key_file_finaldest = os.path.join(new_cert_folder, "key.pem") shutil.move(domain_key_file, domain_key_file_finaldest) _set_permissions(domain_key_file_finaldest, "root", "ssl-cert", 0o640) # Write the cert domain_cert_file = os.path.join(new_cert_folder, "crt.pem") with open(domain_cert_file, "w") as f: f.write(signed_certificate) _set_permissions(domain_cert_file, "root", "ssl-cert", 0o640) if staging: return _enable_certificate(domain, new_cert_folder) # Check the status of the certificate is now good status_summary = _get_status(domain)["summary"] if status_summary["code"] != "great": raise YunohostError( "certmanager_certificate_fetching_or_enabling_failed", domain=domain )
def _fetch_and_enable_new_certificate(domain, staging=False): # Make sure tmp folder exists logger.debug("Making sure tmp folders exists...") if not os.path.exists(WEBROOT_FOLDER): os.makedirs(WEBROOT_FOLDER) if not os.path.exists(TMP_FOLDER): os.makedirs(TMP_FOLDER) _set_permissions(WEBROOT_FOLDER, "root", "www-data", 0650) _set_permissions(TMP_FOLDER, "root", "root", 0640) # Prepare certificate signing request logger.info( "Prepare key and certificate signing request (CSR) for %s...", domain) domain_key_file = "%s/%s.pem" % (TMP_FOLDER, domain) _generate_key(domain_key_file) _set_permissions(domain_key_file, "root", "metronome", 0640) _prepare_certificate_signing_request(domain, domain_key_file, TMP_FOLDER) # Sign the certificate logger.info("Now using ACME Tiny to sign the certificate...") domain_csr_file = "%s/%s.csr" % (TMP_FOLDER, domain) if staging: certification_authority = STAGING_CERTIFICATION_AUTHORITY else: certification_authority = PRODUCTION_CERTIFICATION_AUTHORITY try: signed_certificate = sign_certificate(ACCOUNT_KEY_FILE, domain_csr_file, WEBROOT_FOLDER, log=logger, CA=certification_authority) except ValueError as e: if "urn:acme:error:rateLimited" in str(e): raise MoulinetteError(errno.EINVAL, m18n.n( 'certmanager_hit_rate_limit', domain=domain)) else: logger.error(str(e)) raise MoulinetteError(errno.EINVAL, m18n.n( 'certmanager_cert_signing_failed')) except Exception as e: logger.error(str(e)) raise MoulinetteError(errno.EINVAL, m18n.n( 'certmanager_cert_signing_failed')) try: intermediate_certificate = requests.get(INTERMEDIATE_CERTIFICATE_URL, timeout=30).text except Timeout as e: raise MoulinetteError(errno.EINVAL, m18n.n('certmanager_couldnt_fetch_intermediate_cert')) # Now save the key and signed certificate logger.info("Saving the key and signed certificate...") # Create corresponding directory date_tag = datetime.now().strftime("%Y%m%d.%H%M%S") if staging: folder_flag = "staging" else: folder_flag = "letsencrypt" new_cert_folder = "%s/%s-history/%s-%s" % ( CERT_FOLDER, domain, date_tag, folder_flag) os.makedirs(new_cert_folder) _set_permissions(new_cert_folder, "root", "root", 0655) # Move the private key domain_key_file_finaldest = os.path.join(new_cert_folder, "key.pem") shutil.move(domain_key_file, domain_key_file_finaldest) _set_permissions(domain_key_file_finaldest, "root", "metronome", 0640) # Write the cert domain_cert_file = os.path.join(new_cert_folder, "crt.pem") with open(domain_cert_file, "w") as f: f.write(signed_certificate) f.write(intermediate_certificate) _set_permissions(domain_cert_file, "root", "metronome", 0640) if staging: return _enable_certificate(domain, new_cert_folder) # Check the status of the certificate is now good status_summary = _get_status(domain)["summary"] if status_summary["code"] != "great": raise MoulinetteError(errno.EINVAL, m18n.n( 'certmanager_certificate_fetching_or_enabling_failed', domain=domain))