Skip to content

Commit a96a0d9

Browse files
committed
use kubectl to create cert
1 parent d38a64d commit a96a0d9

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

.werft/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as path from 'path';
44
import { exec, ExecOptions } from './util/shell';
55
import { Werft } from './util/werft';
66
import { waitForDeploymentToSucceed, wipeAndRecreateNamespace, setKubectlContextNamespace, deleteNonNamespaceObjects, findFreeHostPorts, createNamespace, helmInstallName } from './util/kubectl';
7-
import { issueCertficate, installCertficate, IssueCertificateParams, InstallCertificateParams } from './util/certs';
7+
import { newIssueCertficate, installCertficate, IssueCertificateParams, InstallCertificateParams } from './util/certs';
88
import { reportBuildFailureInSlack } from './util/slack';
99
import * as semver from 'semver';
1010
import * as util from 'util';
@@ -874,7 +874,7 @@ export async function issueMetaCerts(namespace: string, domain: string) {
874874
metaClusterCertParams.ip = getCoreDevIngressIP();
875875
metaClusterCertParams.bucketPrefixTail = ""
876876
metaClusterCertParams.additionalSubdomains = additionalSubdomains
877-
await issueCertficate(werft, metaClusterCertParams, metaEnv());
877+
await newIssueCertficate(werft, metaClusterCertParams, metaEnv());
878878
}
879879

880880
async function installMetaCertificates(namespace: string) {

.werft/util/certs.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { exec, ExecOptions } from './shell';
22
import { sleep } from './util';
3+
import { readFileSync, writeFileSync } from 'fs';
4+
import * as path from 'path';
35

46

57
export class IssueCertificateParams {
@@ -68,6 +70,48 @@ export async function issueCertficate(werft, params: IssueCertificateParams, she
6870
}
6971
}
7072

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+
71115
export async function installCertficate(werft, params: InstallCertificateParams, shellOpts: ExecOptions) {
72116
let notReadyYet = true;
73117
werft.log('certificate', `copying certificate from "${params.certNamespace}/${params.certName}" to "${params.destinationNamespace}/${params.certSecretName}"`);

0 commit comments

Comments
 (0)