Skip to content

Commit d76750f

Browse files
committed
remove Chilkat dependency and fix test issie
1 parent 80f7218 commit d76750f

12 files changed

+128
-426
lines changed

AKS.WP.code-workspace

-20
This file was deleted.

Certificate/index.ts

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import * as tls from '@pulumi/tls';
2+
import fs from 'fs';
3+
import * as pem from './p12';
24
import { KeyVaultInfo } from '../types';
35
import { addCustomSecret } from '../KeyVault/CustomHelper';
6+
import forge from 'node-forge';
47

58
export const defaultAllowedUses = [
9+
'data_encipherment',
10+
'digital_signature',
611
'cert_signing',
712
'client_auth',
813
'key_agreement',
914
'key_encipherment',
1015
'server_auth',
16+
'timestamping',
1117
];
1218

1319
export const defaultCodeSignUses = [
@@ -45,7 +51,7 @@ export const createSelfSignCert = ({
4551

4652
const privateKey = new tls.PrivateKey(`${dnsName}-privateKey`, {
4753
algorithm: 'RSA',
48-
rsaBits: 4096,
54+
rsaBits: 2048,
4955
});
5056

5157
const cert = new tls.SelfSignedCert(`${dnsName}-selfSignedCert`, {
@@ -78,13 +84,41 @@ export const createSelfSignCert = ({
7884
addCustomSecret({
7985
name: vaultPrivateKeyName,
8086
vaultInfo,
81-
value: cert.privateKeyPem,
87+
value: privateKey.privateKeyPem,
8288
contentType: `${dnsName} self sign private key.`,
8389
});
8490
}
8591

8692
return {
8793
cert: cert.certPem,
88-
privateKey: cert.privateKeyPem,
94+
privateKey: privateKey.privateKeyPem,
8995
};
9096
};
97+
98+
export const convertPfxFileToPem = async ({
99+
certPath,
100+
password,
101+
}: {
102+
certPath: string;
103+
password?: string;
104+
}) => {
105+
const p12File = await fs.promises.readFile(certPath, { encoding: 'binary' });
106+
const cert = pem.convertToPem(p12File, password);
107+
108+
console.log('Loaded P12 file', certPath);
109+
return { cert: cert.pemCertificate, privateKey: cert.pemKey };
110+
};
111+
112+
export const convertPfxToPem = ({
113+
base64Cert,
114+
password,
115+
}: {
116+
base64Cert: string;
117+
password?: string;
118+
}) => {
119+
const byteArray = Buffer.from(base64Cert, 'base64');
120+
const cert = pem.convertToPem(byteArray.toString('binary'), password);
121+
122+
console.log('Loaded P12 base64');
123+
return { cert: cert.pemCertificate, privateKey: cert.pemKey };
124+
};

Certificate/p12.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as forge from 'node-forge';
2+
import { util } from 'node-forge';
3+
4+
export function convertToPem(
5+
p12base64: string | util.ByteBuffer,
6+
password: string | undefined
7+
) {
8+
const p12Asn1 = forge.asn1.fromDer(p12base64);
9+
const p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, password);
10+
11+
const pemKey = getKeyFromP12(p12, password);
12+
const cert = getCertificateFromP12(p12);
13+
14+
return { pemKey, pemCertificate: cert.pemCertificate };
15+
}
16+
17+
function getKeyFromP12(p12: any, password: string | undefined) {
18+
const keyData = p12.getBags(
19+
{ bagType: forge.pki.oids.pkcs8ShroudedKeyBag },
20+
password
21+
);
22+
23+
let pkcs8Key = keyData[forge.pki.oids.pkcs8ShroudedKeyBag]![0];
24+
if (typeof pkcs8Key === 'undefined') {
25+
pkcs8Key = keyData[forge.pki.oids.keyBag]![0];
26+
}
27+
28+
if (typeof pkcs8Key === 'undefined') {
29+
throw new Error('Unable to get private key.');
30+
}
31+
32+
let pemKey = forge.pki.privateKeyToPem(pkcs8Key.key!);
33+
pemKey = pemKey.replace(/\r\n/g, '');
34+
35+
return pemKey;
36+
}
37+
38+
function getCertificateFromP12(p12: forge.pkcs12.Pkcs12Pfx) {
39+
const certData = p12.getBags({ bagType: forge.pki.oids.certBag });
40+
const certificate = certData[forge.pki.oids.certBag]![0];
41+
42+
let pemCertificate = forge.pki.certificateToPem(certificate.cert!);
43+
pemCertificate = pemCertificate.replace(/\r\n/g, '');
44+
//const commonName = certificate.cert.subject.attributes[0].value;
45+
return { pemCertificate };
46+
}

KubeX/CertHelper.ts

-109
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,5 @@
1-
import * as forge from 'node-forge';
2-
import * as os from 'os';
31
import { replaceAll } from '../Common/Helpers';
42

5-
const getChilkatTool = () => {
6-
const v = process.version.split('.')[0].replace('v', '');
7-
const node = v ? `node${v}` : 'node14';
8-
const platform = os.platform();
9-
const arch = os.arch();
10-
11-
let name = '';
12-
13-
if (platform == 'win32') {
14-
if (arch == 'ia32') name = 'win-ia32';
15-
else name = 'win64';
16-
} else if (platform == 'linux') {
17-
if (arch == 'arm') name = 'arm';
18-
else if (arch == 'x86') name = 'linux32';
19-
else name = 'linux64';
20-
} else if (platform == 'darwin') {
21-
if (arch == 'arm64') name = 'mac-m1';
22-
else name = 'macosx';
23-
}
24-
25-
return require(`@chilkat/ck-${node}-${name}`);
26-
};
27-
28-
interface Props {
29-
pfxBase64: string;
30-
password: string | undefined;
31-
includeAll?: boolean;
32-
}
33-
34-
export function convertPfxToPem({ pfxBase64, password, includeAll }: Props) {
35-
const pfx = getChilkatTool().Pfx();
36-
37-
const success = pfx.LoadPfxEncoded(pfxBase64, 'Base64', password || '');
38-
if (success !== true) {
39-
console.log(pfx.LastErrorText);
40-
return undefined;
41-
}
42-
43-
const keyCount: number = pfx.NumPrivateKeys;
44-
const certCount: number = pfx.NumCerts;
45-
46-
const keys = new Array<string>();
47-
const clientCerts = new Array<string>();
48-
const serverCerts = new Array<string>();
49-
const caCerts = new Array<string>();
50-
51-
const originalKeys = new Array<any>();
52-
const originalClientCerts = new Array<any>();
53-
const originalServerCerts = new Array<any>();
54-
const originalCaCerts = new Array<any>();
55-
56-
//Keys
57-
for (let i = 0; i < keyCount; i++) {
58-
keys.push(pfx.GetPrivateKey(i).GetPkcs8Pem());
59-
}
60-
61-
for (let i = 0; i < certCount; i++) {
62-
const c = pfx.GetCert(i);
63-
64-
if (includeAll) {
65-
clientCerts.push(c.ExportCertPem());
66-
originalClientCerts.push(c);
67-
continue;
68-
}
69-
70-
if (
71-
c.ForClientAuthentication &&
72-
!(c.SubjectCN.includes('CA') || c.SubjectCN.includes('Validation'))
73-
) {
74-
clientCerts.push(c.ExportCertPem());
75-
originalClientCerts.push(c);
76-
}
77-
if (c.ForServerAuthentication) {
78-
//console.log(c);
79-
serverCerts.push(c.ExportCertPem());
80-
originalServerCerts.push(c);
81-
}
82-
83-
if (
84-
(!c.ForClientAuthentication && !c.ForServerAuthentication) ||
85-
c.SubjectCN.includes('CA') ||
86-
c.SubjectCN.includes('Validation')
87-
) {
88-
caCerts.push(c.ExportCertPem());
89-
originalCaCerts.push(c);
90-
}
91-
}
92-
93-
return {
94-
key: keys.join(''),
95-
cert: clientCerts.join(''),
96-
ca: caCerts.join(''),
97-
server: serverCerts.join(''),
98-
clientCerts,
99-
serverCerts,
100-
keys,
101-
caCerts,
102-
originalCaCerts,
103-
originalClientCerts,
104-
originalKeys,
105-
originalServerCerts,
106-
};
107-
}
108-
109-
export const DecodeBase64Cert = (pfxBase64: string) =>
110-
forge.util.decode64(pfxBase64);
111-
1123
export const getTlsName = (domain: string, enableCertIssuer: boolean) =>
1134
enableCertIssuer
1145
? `tls-${replaceAll(domain, '.', '-')}-lets`

KubeX/CertImports.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import * as k8s from '@pulumi/kubernetes';
2-
import { Input } from '@pulumi/pulumi';
31
import { getKubeDomainCert } from './Helpers';
4-
import { convertPfxToPem, getTlsName } from './CertHelper';
2+
import { getTlsName } from './CertHelper';
53
import fs from 'fs';
64
import { KeyVaultInfo } from '../types';
75
import { getSecret } from '../KeyVault/Helper';
86
import { K8sArgs } from './types';
97
import ksCertSecret from './Core/KsCertSecret';
8+
import { convertPfxToPem } from '../Certificate';
109

1110
export interface FromCertOrderProps extends K8sArgs {
1211
namespaces: string[];
@@ -28,9 +27,7 @@ export const certImportFromCertOrder = async ({
2827
ksCertSecret({
2928
name: `${name}-${i}`,
3029
namespace: n,
31-
cert: cert.cert,
32-
ca: cert.ca,
33-
privateKey: cert.key,
30+
...cert,
3431
...others,
3532
})
3633
);
@@ -90,19 +87,16 @@ export const certImportFromVault = async ({
9087

9188
const pems = cert?.value
9289
? convertPfxToPem({
93-
pfxBase64: cert.value,
90+
base64Cert: cert.value,
9491
password: '',
95-
includeAll: false,
9692
})
9793
: undefined;
9894

9995
if (pems) {
10096
ksCertSecret({
10197
name: `${c}-${i}`,
10298
namespace,
103-
cert: pems.cert,
104-
ca: pems.ca,
105-
privateKey: pems.key,
99+
...pems,
106100
...others,
107101
});
108102
}

KubeX/Helpers.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import { getCertificateForDomain } from '../Web/Helpers';
2-
import { convertPfxToPem } from '../KubeX/CertHelper';
2+
import { convertPfxToPem } from '../Certificate';
33

44
export const getKubeDomainCert = async (domain: string) => {
55
//Get cert from CertOrder.
66
const cert = await getCertificateForDomain(domain);
77
//Convert to K8s cert
88
return cert
99
? convertPfxToPem({
10-
pfxBase64: cert.base64CertData,
11-
password: '',
12-
includeAll: false,
10+
base64Cert: cert.base64CertData,
1311
})
1412
: undefined;
1513
};
File renamed without changes.

0 commit comments

Comments
 (0)