Skip to content

Request example ssl usecase #621

Open
@inspiraller

Description

@inspiraller

In the documentation it describes providing a property in the configuration:
ss: {}

But there is no example of how to user it and what to put into that and any other necessary requirements.

Desired Behavior

I would like to see an example of what parameters are supplied to connect via ssl over slonik to my ssl protected postgress docker container.

Motivation

Implementation

I have created the following crt, key and pem on my windows host.
I have then copied them to the docker container and setup the necessary ssl configuration.
I have created an equivalent pg example and that connects over ssl

pg-server.ts

import dotEnv from "dotenv";
import pg from "pg";
import fs from "fs";
import path from "path";
dotEnv.config();

const { env } = process;

const PG_DB = env.PG_DB as string;
const PG_USER = env.PG_USER as string;
const PG_PWD = env.PG_PWD as string;
const PG_TABLE = env.PG_TABLE as string;

const pem = fs
  .readFileSync(path.join(__dirname, "./ssl/server.pem"))
  .toString();

const init = () => {

  const client = new pg.Client({
    user: PG_USER,
    password: PG_PWD,
    database: PG_DB,
    port: 5432,
    host: "localhost",
    ssl: {
      ca: pem, // Example - equivalent needed for slonik
    },
  });
  client.connect();

  client.query(`SELECT * FROM ${PG_TABLE} ORDER BY id ASC`, (result) => {
    console.log('pg query', result) // Works
  });
};

init();

slonik-server.ts

  const pool = createPool(
    `postgresql://${PG_USER}:${PG_PWD}@localhost:5432/${PG_DB}`, {
      ssl: {
        ca: pem,
        rejectUnauthorized: true,
      }
    }
  );
  pool.connect(async (connection) => {
    try {
      const resultExist = await connection.query(sql`SELECT EXISTS (SELECT FROM pg_tables WHERE tablename  = ${PG_TABLE});`);
      const isExist = resultExist.rows[0].exists;
      console.log('DB exists =', {isExist})

      // NOTE: Could need to use sql.identifier to reference dynamic table name.
      const resultRows = await connection.query(sql`SELECT * FROM ${sql.identifier([PG_TABLE])} ORDER BY id ASC`);
      console.log('TABLE ROWS', {resultRows})

    } catch (err) {
      console.log(err);
    }
  });

Both of these examples work. It would be useful to have a basic example like this in your documentation

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions