Skip to content

Commit 138e0dd

Browse files
committed
feat: Improve key generation and certificate parsing with formatted expiration date
1 parent a7fd771 commit 138e0dd

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

src/crypto.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ import {X509Certificate} from '@peculiar/x509';
2020
import {Convert} from 'pvtsutils';
2121

2222
export async function generateKeys(): Promise<RSAKeys> {
23-
// Check for Web Crypto API support
24-
if (!crypto?.subtle) {
25-
throw new CloudSQLConnectorError({
26-
message: 'Web Crypto API is not available in this environment',
27-
code: 'ENOCRYPTO',
28-
});
29-
}
30-
3123
const keyPair = await crypto.subtle.generateKey(
3224
{
3325
name: 'RSA-PSS',
@@ -50,7 +42,7 @@ export async function generateKeys(): Promise<RSAKeys> {
5042
);
5143

5244
// Convert to base64 and format as PEM
53-
const privateKeyPem = `-----BEGIN PRIVATE KEY-----\n${arrayBufferToBase64(privateKeyExport)}\n-----END PRIVATE KEY-----`;
45+
const privateKeyPem = `-----BEGIN RSA PRIVATE KEY-----\n${arrayBufferToBase64(privateKeyExport)}\n-----END RSA PRIVATE KEY-----`;
5446
const publicKeyPem = `-----BEGIN PUBLIC KEY-----\n${arrayBufferToBase64(publicKeyExport)}\n-----END PUBLIC KEY-----`;
5547

5648
return {
@@ -78,9 +70,13 @@ export async function parseCert(cert: string): Promise<SslCert> {
7870
});
7971
}
8072

73+
const date = x509.notAfter;
74+
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
75+
const formattedDate = `${months[date.getUTCMonth()]} ${String(date.getUTCDate()).padStart(2)} ${String(date.getUTCHours()).padStart(2, '0')}:${String(date.getUTCMinutes()).padStart(2, '0')}:${String(date.getUTCSeconds()).padStart(2, '0')} ${date.getUTCFullYear()} GMT`;
76+
8177
return {
8278
cert,
83-
expirationTime: x509.notAfter.toISOString(),
79+
expirationTime: formattedDate,
8480
};
8581
} catch (err: unknown) {
8682
throw new CloudSQLConnectorError({

test/crypto.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,8 @@ t.test('parseCert failure', async t => {
5050

5151
t.test('parseCert missing expiration time', async t => {
5252
const {parseCert} = t.mockRequire('../src/crypto', {
53-
'../src/node-crypto': {
54-
async cryptoModule() {
55-
return {
56-
X509Certificate: class {},
57-
};
58-
},
53+
'@peculiar/x509': {
54+
X509Certificate: class {},
5955
},
6056
});
6157
return t.rejects(

0 commit comments

Comments
 (0)