Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request example ssl usecase #621

Open
inspiraller opened this issue Jun 7, 2024 · 0 comments
Open

Request example ssl usecase #621

inspiraller opened this issue Jun 7, 2024 · 0 comments

Comments

@inspiraller
Copy link

inspiraller commented Jun 7, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant