Skip to content

Commit f007751

Browse files
authored
chore: cleanup e2e test certs (#8936)
1 parent b0c1179 commit f007751

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

packages/wrangler/e2e/cert.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ describe("cert", () => {
125125
const { certificate: caCert } = generateRootCaCert();
126126

127127
// Generate filenames for concurrent e2e test environment
128-
const mtlsCertName = `mtls_cert_${randomUUID()}`;
129-
const caCertName = `ca_cert_${randomUUID()}`;
128+
const mtlsCertName = `tmp-e2e-mtls-cert-${randomUUID()}`;
129+
const caCertName = `tmp-e2e-ca-cert-${randomUUID()}`;
130130

131131
it("upload mtls-certificate", async () => {
132132
// locally generated certs/key
@@ -137,8 +137,8 @@ describe("cert", () => {
137137
`wrangler cert upload mtls-certificate --name ${mtlsCertName} --cert mtls_client_cert_file.pem --key mtls_client_private_key_file.pem`
138138
);
139139
expect(normalize(output.stdout)).toMatchInlineSnapshot(`
140-
"Uploading mTLS Certificate mtls_cert_00000000-0000-0000-0000-000000000000...
141-
Success! Uploaded mTLS Certificate mtls_cert_00000000-0000-0000-0000-000000000000
140+
"Uploading mTLS Certificate tmp-e2e-mtls-cert-00000000-0000-0000-0000-000000000000...
141+
Success! Uploaded mTLS Certificate tmp-e2e-mtls-cert-00000000-0000-0000-0000-000000000000
142142
ID: 00000000-0000-0000-0000-000000000000
143143
Issuer: CN=Root CA,O=Localhost Root CA,ST=California,C=US
144144
Expires on 11/18/2034"
@@ -152,8 +152,8 @@ describe("cert", () => {
152152
`wrangler cert upload certificate-authority --name ${caCertName} --ca-cert ca_chain_cert.pem`
153153
);
154154
expect(normalize(output.stdout)).toMatchInlineSnapshot(`
155-
"Uploading CA Certificate ca_cert_00000000-0000-0000-0000-000000000000...
156-
Success! Uploaded CA Certificate ca_cert_00000000-0000-0000-0000-000000000000
155+
"Uploading CA Certificate tmp-e2e-ca-cert-00000000-0000-0000-0000-000000000000...
156+
Success! Uploaded CA Certificate tmp-e2e-ca-cert-00000000-0000-0000-0000-000000000000
157157
ID: 00000000-0000-0000-0000-000000000000
158158
Issuer: CN=Localhost CA,OU=SSL Department,O=Localhost,L=San Francisco,ST=California,C=US
159159
Expires on 11/18/2034"
@@ -164,10 +164,10 @@ describe("cert", () => {
164164
const output = await helper.run(`wrangler cert list`);
165165
const result = normalize(output.stdout);
166166
expect(result).toContain(
167-
`Name: mtls_cert_00000000-0000-0000-0000-000000000000`
167+
`Name: tmp-e2e-mtls-cert-00000000-0000-0000-0000-000000000000`
168168
);
169169
expect(result).toContain(
170-
`Name: ca_cert_00000000-0000-0000-0000-000000000000`
170+
`Name: tmp-e2e-ca-cert-00000000-0000-0000-0000-000000000000`
171171
);
172172
});
173173

@@ -177,9 +177,9 @@ describe("cert", () => {
177177
);
178178
expect(normalize(delete_mtls_cert_output.stdout)).toMatchInlineSnapshot(
179179
`
180-
"? Are you sure you want to delete certificate 00000000-0000-0000-0000-000000000000 (mtls_cert_00000000-0000-0000-0000-000000000000)?
180+
"? Are you sure you want to delete certificate 00000000-0000-0000-0000-000000000000 (tmp-e2e-mtls-cert-00000000-0000-0000-0000-000000000000)?
181181
🤖 Using fallback value in non-interactive context: yes
182-
Deleted certificate 00000000-0000-0000-0000-000000000000 (mtls_cert_00000000-0000-0000-0000-000000000000) successfully"
182+
Deleted certificate 00000000-0000-0000-0000-000000000000 (tmp-e2e-mtls-cert-00000000-0000-0000-0000-000000000000) successfully"
183183
`
184184
);
185185
});
@@ -190,9 +190,9 @@ describe("cert", () => {
190190
);
191191
expect(normalize(delete_ca_cert_output.stdout)).toMatchInlineSnapshot(
192192
`
193-
"? Are you sure you want to delete certificate 00000000-0000-0000-0000-000000000000 (ca_cert_00000000-0000-0000-0000-000000000000)?
193+
"? Are you sure you want to delete certificate 00000000-0000-0000-0000-000000000000 (tmp-e2e-ca-cert-00000000-0000-0000-0000-000000000000)?
194194
🤖 Using fallback value in non-interactive context: yes
195-
Deleted certificate 00000000-0000-0000-0000-000000000000 (ca_cert_00000000-0000-0000-0000-000000000000) successfully"
195+
Deleted certificate 00000000-0000-0000-0000-000000000000 (tmp-e2e-ca-cert-00000000-0000-0000-0000-000000000000) successfully"
196196
`
197197
);
198198
});

tools/e2e/common.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ export type HyperdriveConfig = {
3636
created_on: string;
3737
};
3838

39+
export type MTlsCertificateResponse = {
40+
id: string;
41+
name?: string;
42+
ca: boolean;
43+
certificates: string;
44+
expires_on: string;
45+
issuer: string;
46+
serial_number: string;
47+
signature: string;
48+
uploaded_on: string;
49+
};
50+
3951
class ApiError extends Error {
4052
constructor(
4153
readonly url: string,
@@ -265,3 +277,25 @@ export const deleteHyperdriveConfig = async (id: string) => {
265277
true
266278
);
267279
};
280+
281+
export const listCertificates = async () => {
282+
const results = (await apiFetch(`/mtls_certificates`, {
283+
method: "GET",
284+
})) as MTlsCertificateResponse[];
285+
286+
return results.filter(
287+
(cert) =>
288+
cert.name?.includes("tmp-e2e") && // Certs are more than an hour old
289+
Date.now() - new Date(cert.uploaded_on).valueOf() > 1000 * 60 * 60
290+
);
291+
};
292+
293+
export const deleteCertificate = async (id: string) => {
294+
await apiFetch(
295+
`/mtls_certificates/${id}`,
296+
{
297+
method: "DELETE",
298+
},
299+
true
300+
);
301+
};

tools/e2e/e2eCleanup.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {
2+
deleteCertificate,
23
deleteDatabase,
34
deleteKVNamespace,
45
deleteProject,
56
deleteWorker,
7+
listCertificates,
68
listHyperdriveConfigs,
79
listTmpDatabases,
810
listTmpE2EProjects,
@@ -94,4 +96,17 @@ async function run() {
9496
`Successfully deleted ${hyperdriveConfigsToDelete.length} hyperdrive configs`
9597
);
9698
}
99+
100+
const mtlsCertificates = await listCertificates();
101+
for (const certificate of mtlsCertificates) {
102+
console.log("Deleting mTLS certificate: " + certificate.id);
103+
await deleteCertificate(certificate.id);
104+
}
105+
if (mtlsCertificates.length === 0) {
106+
console.log(`No mTLS certificates to delete.`);
107+
} else {
108+
console.log(
109+
`Successfully deleted ${mtlsCertificates.length} mTLS certificates`
110+
);
111+
}
97112
}

0 commit comments

Comments
 (0)