Support ssl

This commit is contained in:
Adria Navarro 2023-06-19 11:46:22 +01:00
parent e7026d4aed
commit dbcf7814a8
2 changed files with 33 additions and 4 deletions

View File

@ -1,3 +1,4 @@
import fs from "fs"
import { import {
Integration, Integration,
DatasourceFieldType, DatasourceFieldType,
@ -22,6 +23,7 @@ import { escapeDangerousCharacters } from "../utilities"
import { Client, ClientConfig, types } from "pg" import { Client, ClientConfig, types } from "pg"
import { exec } from "child_process" import { exec } from "child_process"
import { storeTempFile } from "../utilities/fileSystem"
// Return "date" and "timestamp" types as plain strings. // Return "date" and "timestamp" types as plain strings.
// This lets us reference the original stored timezone. // This lets us reference the original stored timezone.
@ -385,11 +387,36 @@ class PostgresIntegration extends Sql implements DatasourcePlus {
async getSchema() { async getSchema() {
const dumpCommandParts = [ const dumpCommandParts = [
`PGPASSWORD="${this.config.password}"`, `user=${this.config.user}`,
`pg_dump -U ${this.config.user} -h ${this.config.host} -p ${this.config.port} -d ${this.config.database} --schema-only`, `host=${this.config.host}`,
`port=${this.config.port}`,
`dbname=${this.config.database}`,
] ]
const dumpCommand = dumpCommandParts.join(" ") if (this.config.ssl) {
dumpCommandParts.push("sslmode=verify-ca")
if (this.config.ca) {
const caFilePath = storeTempFile(this.config.ca)
fs.chmodSync(caFilePath, "0600")
dumpCommandParts.push(`sslrootcert=${caFilePath}`)
}
if (this.config.clientCert) {
const clientCertFilePath = storeTempFile(this.config.clientCert)
fs.chmodSync(clientCertFilePath, "0600")
dumpCommandParts.push(`sslcert=${clientCertFilePath}`)
}
if (this.config.clientKey) {
const clientKeyFilePath = storeTempFile(this.config.clientKey)
fs.chmodSync(clientKeyFilePath, "0600")
dumpCommandParts.push(`sslkey=${clientKeyFilePath}`)
}
}
const dumpCommand = `PGPASSWORD="${
this.config.password
}" pg_dump --schema-only "${dumpCommandParts.join(" ")}"`
return new Promise<string>((res, rej) => { return new Promise<string>((res, rej) => {
exec(dumpCommand, (error, stdout, stderr) => { exec(dumpCommand, (error, stdout, stderr) => {

View File

@ -81,7 +81,9 @@ export const streamFile = (path: string) => {
* @param {string} fileContents contents which will be written to a temp file. * @param {string} fileContents contents which will be written to a temp file.
* @return {string} the path to the temp file. * @return {string} the path to the temp file.
*/ */
export const storeTempFile = (fileContents: any) => { export const storeTempFile = (
fileContents: string | NodeJS.ArrayBufferView
) => {
const path = join(budibaseTempDir(), uuid()) const path = join(budibaseTempDir(), uuid())
fs.writeFileSync(path, fileContents) fs.writeFileSync(path, fileContents)
return path return path