Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions packages/types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ export type ConnectionOptions = {
options?: string;
password?: string;
port?: number;
ssl?: {
ca?: string;
cert?: string;
key?: string;
rejectUnauthorized: boolean;
};
sslMode?: 'disable' | 'no-verify' | 'require';
ssl?:
| boolean
| {
ca?: string;
cert?: string;
key?: string;
rejectUnauthorized: boolean;
};
sslMode?:
| 'allow'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here

| 'disable'
| 'no-verify'
| 'prefer'
| 'require'
| 'verify-ca'
| 'verify-full';
username?: string;
};

Expand Down
113 changes: 79 additions & 34 deletions packages/utilities/src/utilities/parseDsn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { type ConnectionOptions } from '@slonik/types';
import { readFileSync } from 'node:fs';
import { z } from 'zod';

// eslint-disable-next-line complexity
export const parseDsn = (dsn: string): ConnectionOptions => {
if (dsn.trim() === '') {
return {};
Expand Down Expand Up @@ -56,7 +57,17 @@ export const parseDsn = (dsn: string): ConnectionOptions => {
.describe(
'Specifies the location for the secret key used for the client certificate.',
),
sslmode: z.enum(['disable', 'no-verify', 'require']).optional(),
sslmode: z
.enum([
'allow',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allow is not listed in pg documentation so I don't think we should support it.

Also, it is not used in the switch line 147.

'disable',
'no-verify',
'prefer',
'require',
'verify-ca',
'verify-full',
])
.optional(),
sslrootcert: z
.string()
.optional()
Expand All @@ -78,49 +89,83 @@ export const parseDsn = (dsn: string): ConnectionOptions => {
connectionOptions.sslMode = searchParameters.sslmode;
}

let sslCert: string | undefined;
let sslKey: string | undefined;
let sslRootCert: string | undefined;
/**
* Refer to https://github.com/brianc/node-postgres/pull/2709
*/
if (
searchParameters.sslcert ||
searchParameters.sslkey ||
searchParameters.sslrootcert ||
searchParameters.sslmode
) {
let sslCert: string | undefined;
let sslKey: string | undefined;
let sslRootCert: string | undefined;

if (searchParameters.sslcert) {
try {
sslCert = readFileSync(searchParameters.sslcert, 'utf8');
} catch {
throw new UnexpectedStateError('Failed to read SSL certificate file.');
}
}

if (searchParameters.sslcert) {
try {
sslCert = readFileSync(searchParameters.sslcert, 'utf8');
} catch {
throw new UnexpectedStateError('Failed to read SSL certificate file.');
if (searchParameters.sslkey) {
try {
sslKey = readFileSync(searchParameters.sslkey, 'utf8');
} catch {
throw new UnexpectedStateError('Failed to read SSL key file.');
}
}
}

if (searchParameters.sslkey) {
try {
sslKey = readFileSync(searchParameters.sslkey, 'utf8');
} catch {
throw new UnexpectedStateError('Failed to read SSL key file.');
if (searchParameters.sslrootcert) {
try {
sslRootCert = readFileSync(searchParameters.sslrootcert, 'utf8');
} catch {
throw new UnexpectedStateError(
'Failed to read SSL root certificate file.',
);
}
}
}

if (searchParameters.sslrootcert) {
try {
sslRootCert = readFileSync(searchParameters.sslrootcert, 'utf8');
} catch {
throw new UnexpectedStateError(
'Failed to read SSL root certificate file.',
);
if (sslCert || sslKey || sslRootCert) {
if ((sslCert && !sslKey) || (!sslCert && sslKey)) {
throw new UnexpectedStateError(
'Both sslcert and sslkey must be provided together.',
);
}

connectionOptions.ssl = {
ca: sslRootCert,
cert: sslCert,
key: sslKey,
rejectUnauthorized: searchParameters.sslmode !== 'no-verify',
};
}
}

if (sslCert || sslKey || sslRootCert) {
if ((sslCert && !sslKey) || (!sslCert && sslKey)) {
throw new UnexpectedStateError(
'Both sslcert and sslkey must be provided together.',
);
switch (connectionOptions.sslMode) {
case 'disable': {
connectionOptions.ssl = false;
break;
}

connectionOptions.ssl = {
ca: sslRootCert,
cert: sslCert,
key: sslKey,
rejectUnauthorized: searchParameters.sslmode !== 'no-verify',
};
case 'no-verify': {
const ssl = connectionOptions.ssl ?? {};

connectionOptions.ssl = {
...ssl,
rejectUnauthorized: false,
};
break;
}

case 'prefer':
case 'require':
case 'verify-ca':
case 'verify-full': {
break;
}
}

return connectionOptions;
Expand Down
Loading