1- import { randomUUID } from "node:crypto" ;
2- import * as forge from "node-forge" ;
31import { describe , expect , it } from "vitest" ;
2+ import {
3+ generateCaCertName ,
4+ generateLeafCertificate ,
5+ generateMtlsCertName ,
6+ generateRootCaCert ,
7+ generateRootCertificate ,
8+ } from "./helpers/cert" ;
49import { WranglerE2ETestHelper } from "./helpers/e2e-wrangler-test" ;
510import { normalizeOutput } from "./helpers/normalize" ;
611
7- // Generate X509 self signed root key pair and certificate
8- function generateRootCertificate ( ) {
9- const rootKeys = forge . pki . rsa . generateKeyPair ( 2048 ) ;
10- const rootCert = forge . pki . createCertificate ( ) ;
11- rootCert . publicKey = rootKeys . publicKey ;
12- rootCert . serialNumber = "01" ;
13- rootCert . validity . notBefore = new Date ( ) ;
14- rootCert . validity . notAfter = new Date ( ) ;
15- rootCert . validity . notAfter . setFullYear (
16- rootCert . validity . notBefore . getFullYear ( ) + 10
17- ) ; // 10 years validity
18-
19- const rootAttrs = [
20- { name : "commonName" , value : "Root CA" } ,
21- { name : "countryName" , value : "US" } ,
22- { shortName : "ST" , value : "California" } ,
23- { name : "organizationName" , value : "Localhost Root CA" } ,
24- ] ;
25- rootCert . setSubject ( rootAttrs ) ;
26- rootCert . setIssuer ( rootAttrs ) ; // Self-signed
27-
28- rootCert . sign ( rootKeys . privateKey , forge . md . sha256 . create ( ) ) ;
29-
30- return { certificate : rootCert , privateKey : rootKeys . privateKey } ;
31- }
32-
33- // Generate X509 leaf certificate signed by the root
34- function generateLeafCertificate (
35- rootCert : forge . pki . Certificate ,
36- rootKey : forge . pki . PrivateKey
37- ) {
38- const leafKeys = forge . pki . rsa . generateKeyPair ( 2048 ) ;
39- const leafCert = forge . pki . createCertificate ( ) ;
40- leafCert . publicKey = leafKeys . publicKey ;
41- leafCert . serialNumber = "02" ;
42- leafCert . validity . notBefore = new Date ( ) ;
43- leafCert . validity . notAfter = new Date ( ) ;
44- leafCert . validity . notAfter . setFullYear ( 2034 , 10 , 18 ) ;
45-
46- const leafAttrs = [
47- { name : "commonName" , value : "example.org" } ,
48- { name : "countryName" , value : "US" } ,
49- { shortName : "ST" , value : "California" } ,
50- { name : "organizationName" , value : "Example Inc" } ,
51- ] ;
52- leafCert . setSubject ( leafAttrs ) ;
53- leafCert . setIssuer ( rootCert . subject . attributes ) ; // Signed by root
54-
55- leafCert . sign ( rootKey , forge . md . sha256 . create ( ) ) ; // Signed using root's private key
56-
57- const pemLeafCert = forge . pki . certificateToPem ( leafCert ) ;
58- const pemLeafKey = forge . pki . privateKeyToPem ( leafKeys . privateKey ) ;
59-
60- return { certificate : pemLeafCert , privateKey : pemLeafKey } ;
61- }
62-
63- // Generate self signed X509 CA root certificate
64- function generateRootCaCert ( ) {
65- // Create a key pair (private and public keys)
66- const keyPair = forge . pki . rsa . generateKeyPair ( 2048 ) ;
67-
68- // Create a new X.509 certificate
69- const cert = forge . pki . createCertificate ( ) ;
70-
71- // Set certificate fields
72- cert . publicKey = keyPair . publicKey ;
73- cert . serialNumber = "01" ;
74- cert . validity . notBefore = new Date ( ) ;
75- cert . validity . notAfter = new Date ( ) ;
76- cert . validity . notAfter . setFullYear ( 2034 , 10 , 18 ) ;
77-
78- // Add issuer and subject fields (for a root CA, they are the same)
79- const attrs = [
80- { name : "commonName" , value : "Localhost CA" } ,
81- { name : "countryName" , value : "US" } ,
82- { shortName : "ST" , value : "California" } ,
83- { name : "localityName" , value : "San Francisco" } ,
84- { name : "organizationName" , value : "Localhost" } ,
85- { shortName : "OU" , value : "SSL Department" } ,
86- ] ;
87- cert . setSubject ( attrs ) ;
88- cert . setIssuer ( attrs ) ;
89-
90- // Add basic constraints and key usage extensions
91- cert . setExtensions ( [
92- {
93- name : "basicConstraints" ,
94- cA : true ,
95- } ,
96- {
97- name : "keyUsage" ,
98- keyCertSign : true ,
99- digitalSignature : true ,
100- cRLSign : true ,
101- } ,
102- ] ) ;
103-
104- // Self-sign the certificate with the private key
105- cert . sign ( keyPair . privateKey , forge . md . sha256 . create ( ) ) ;
106-
107- // Convert the certificate and private key to PEM format
108- const pemCert = forge . pki . certificateToPem ( cert ) ;
109- const pemPrivateKey = forge . pki . privateKeyToPem ( keyPair . privateKey ) ;
110-
111- return { certificate : pemCert , privateKey : pemPrivateKey } ;
112- }
113-
11412describe ( "cert" , ( ) => {
11513 const normalize = ( str : string ) =>
11614 normalizeOutput ( str , {
@@ -125,8 +23,8 @@ describe("cert", () => {
12523 const { certificate : caCert } = generateRootCaCert ( ) ;
12624
12725 // Generate filenames for concurrent e2e test environment
128- const mtlsCertName = `tmp-e2e-mtls-cert- ${ randomUUID ( ) } ` ;
129- const caCertName = `tmp-e2e-ca-cert- ${ randomUUID ( ) } ` ;
26+ const mtlsCertName = generateMtlsCertName ( ) ;
27+ const caCertName = generateCaCertName ( ) ;
13028
13129 it ( "upload mtls-certificate" , async ( ) => {
13230 // locally generated certs/key
0 commit comments