|
1 | 1 | import { exec, ExecOptions } from './shell';
|
2 | 2 | import { sleep } from './util';
|
| 3 | +import { readFileSync, writeFileSync } from 'fs'; |
| 4 | +import * as path from 'path'; |
3 | 5 |
|
4 | 6 |
|
5 | 7 | export class IssueCertificateParams {
|
@@ -68,6 +70,48 @@ export async function issueCertficate(werft, params: IssueCertificateParams, she
|
68 | 70 | }
|
69 | 71 | }
|
70 | 72 |
|
| 73 | +export async function newIssueCertficate(werft, params: IssueCertificateParams, shellOpts: ExecOptions) { |
| 74 | + var subdomains = []; |
| 75 | + werft.log("certificate", `Subdomains: ${params.additionalSubdomains}`) |
| 76 | + for (const sd of params.additionalSubdomains) { |
| 77 | + subdomains.push(sd); |
| 78 | + } |
| 79 | + |
| 80 | + // sanity: check if there is a "SAN short enough to fit into CN (63 characters max)" |
| 81 | + // source: https://community.letsencrypt.org/t/certbot-errors-with-obtaining-a-new-certificate-an-unexpected-error-occurred-the-csr-is-unacceptable-e-g-due-to-a-short-key-error-finalizing-order-issuing-precertificate-csr-doesnt-contain-a-san-short-enough-to-fit-in-cn/105513/2 |
| 82 | + if (!subdomains.some(sd => { |
| 83 | + const san = sd + params.domain; |
| 84 | + return san.length <= 63; |
| 85 | + })) { |
| 86 | + throw new Error(`there is no subdomain + '${params.domain}' shorter or equal to 63 characters, max. allowed length for CN. No HTTPS certs for you! Consider using a short branch name...`); |
| 87 | + } |
| 88 | + var cmd = `set -x \ |
| 89 | + && cd ${path.join(params.pathToTerraform, 'cert/templates')} \ |
| 90 | + && cp cert-manager_certificate.tpl cert.yaml \ |
| 91 | + && yq w -i cert.yaml metadata.name '${params.namespace}' \ |
| 92 | + && yq w -i cert.yaml spec.secretName '${params.namespace}' \ |
| 93 | + && yq w -i cert.yaml metadata.namespace '${params.certNamespace}' \ |
| 94 | + ${subdomains.map(s => `&& yq w -i cert.yaml spec.dnsNames[+] '${s+params.domain}'`).join(' ')} \ |
| 95 | + && kubectl apply -f cert.yaml`; |
| 96 | + |
| 97 | + werft.log("certificate", "Kubectl command for cert creation: " + cmd) |
| 98 | + exec(cmd, { ...shellOpts, slice: 'certificate' }); |
| 99 | + |
| 100 | + werft.log('certificate', `waiting until certificate ${params.certNamespace}/${params.namespace} is ready...`) |
| 101 | + let notReadyYet = true; |
| 102 | + for (let i = 0; i < 90 && notReadyYet; i++) { |
| 103 | + werft.log('certificate', `polling state of ${params.certNamespace}/${params.namespace}...`) |
| 104 | + const result = exec(`kubectl -n ${params.certNamespace} get certificate ${params.namespace} -o jsonpath="{.status.conditions[?(@.type == 'Ready')].status}"`, { ...shellOpts, silent: true, dontCheckRc: true, async: false }); |
| 105 | + if (result != undefined && result.code === 0 && result.stdout === "True") { |
| 106 | + notReadyYet = false; |
| 107 | + break; |
| 108 | + } |
| 109 | + |
| 110 | + await sleep(5000); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | + |
71 | 115 | export async function installCertficate(werft, params: InstallCertificateParams, shellOpts: ExecOptions) {
|
72 | 116 | let notReadyYet = true;
|
73 | 117 | werft.log('certificate', `copying certificate from "${params.certNamespace}/${params.certName}" to "${params.destinationNamespace}/${params.certSecretName}"`);
|
|
0 commit comments