fix: use sslmode=preferred for pg and mysql

all hosted vendors require SSL and the existing ssl=none won’t work.
This commit is contained in:
Tianzhou Chen
2023-05-21 17:28:13 +08:00
parent ad31e18ea2
commit f2aefa8e95
3 changed files with 27 additions and 19 deletions

View File

@ -18,14 +18,14 @@ interface Props {
close: () => void;
}
type SSLType = "none" | "ca-only" | "full";
type SSLType = "preferred" | "ca-only" | "full";
type SSLFieldType = "ca" | "cert" | "key";
const SSLTypeOptions = [
{
label: "None",
value: "none",
label: "Preferred",
value: "preferred",
},
{
label: "CA Only",
@ -76,7 +76,7 @@ const CreateConnectionModal = (props: Props) => {
const connectionStore = useConnectionStore();
const [connection, setConnection] = useState<Connection>(defaultConnection);
const [showDeleteConnectionModal, setShowDeleteConnectionModal] = useState(false);
const [sslType, setSSLType] = useState<SSLType>("none");
const [sslType, setSSLType] = useState<SSLType>("preferred");
const [selectedSSLField, setSelectedSSLField] = useState<SSLFieldType>("ca");
const [isRequesting, setIsRequesting] = useState(false);
const showDatabaseField = connection.engineType === Engine.PostgreSQL;
@ -310,7 +310,7 @@ const CreateConnectionModal = (props: Props) => {
</label>
))}
</div>
{sslType !== "none" && (
{sslType !== "preferred" && (
<>
<div className="text-sm space-x-3 mb-2">
<span

View File

@ -19,6 +19,11 @@ const getMySQLConnection = async (connection: Connection): Promise<mysql.Connect
cert: connection.ssl?.cert,
key: connection.ssl?.key,
};
} else {
// rejectUnauthorized=false to infer sslmode=prefer since hosted MySQL venders have SSL enabled.
connectionOptions.ssl = {
rejectUnauthorized: false,
};
}
const conn = await mysql.createConnection(connectionOptions);
return conn;

View File

@ -2,13 +2,14 @@ import { Client, ClientConfig } from "pg";
import { Connection, ExecutionResult } from "@/types";
import { Connector } from "..";
const newPostgresClient = (connection: Connection) => {
const newPostgresClient = async (connection: Connection) => {
const clientConfig: ClientConfig = {
host: connection.host,
port: Number(connection.port),
user: connection.username,
password: connection.password,
database: connection.database,
application_name: "sqlchat",
};
if (connection.ssl) {
clientConfig.ssl = {
@ -16,21 +17,27 @@ const newPostgresClient = (connection: Connection) => {
cert: connection.ssl?.cert,
key: connection.ssl?.key,
};
} else {
// Use rejectUnauthorized to infer sslmode=prefer since hosted PG venders have SSL enabled.
clientConfig.ssl = {
rejectUnauthorized: false,
};
}
return new Client(clientConfig);
let client = new Client(clientConfig);
await client.connect();
return client;
};
const testConnection = async (connection: Connection): Promise<boolean> => {
const client = newPostgresClient(connection);
await client.connect();
const client = await newPostgresClient(connection);
await client.end();
return true;
};
const execute = async (connection: Connection, databaseName: string, statement: string): Promise<any> => {
connection.database = databaseName;
const client = newPostgresClient(connection);
await client.connect();
const client = await newPostgresClient(connection);
const { rows, rowCount } = await client.query(statement);
await client.end();
@ -46,8 +53,7 @@ const execute = async (connection: Connection, databaseName: string, statement:
};
const getDatabases = async (connection: Connection): Promise<string[]> => {
const client = newPostgresClient(connection);
await client.connect();
const client = await newPostgresClient(connection);
if (connection.database) {
await client.end();
return [connection.database];
@ -67,8 +73,7 @@ const getDatabases = async (connection: Connection): Promise<string[]> => {
const getTables = async (connection: Connection, databaseName: string): Promise<string[]> => {
connection.database = databaseName;
const client = newPostgresClient(connection);
await client.connect();
const client = await newPostgresClient(connection);
const { rows } = await client.query(
`SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE' AND table_catalog=$1;`,
[databaseName]
@ -90,8 +95,7 @@ const getTableStructure = async (
structureFetched: (tableName: string, structure: string) => void
): Promise<void> => {
connection.database = databaseName;
const client = newPostgresClient(connection);
await client.connect();
const client = await newPostgresClient(connection);
const { rows } = await client.query(
`SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_schema='public' AND table_name=$1;`,
[tableName]
@ -119,8 +123,7 @@ const getTableStructureBatch = async (
structureFetched: (tableName: string, structure: string) => void
): Promise<void> => {
connection.database = databaseName;
const client = newPostgresClient(connection);
await client.connect();
const client = await newPostgresClient(connection);
await Promise.all(
tableNameList.map(async (tableName) => {
const { rows } = await client.query(