secrets-manager-pg-cache

0.0.14 • Public • Published

Secrets Manager PG Cache

Secrets Manager PG Cache is a cacheable Postgresql database client that supports retrieving credentials using AWS Secrets Manager.

Usage

Secrets manager provides the ability to retrieve credentials using AWS Secrets Manager. Notwithstanding this support, AWS Secrets Manager is not the only retrieval mechanism. Sample usage can be found in test/integration.js for how to retrieve credentials using alternative locations.

Using External Credentials

const SecretsManager = require('secrets-manager-pg-cache').SecretsManager;
const CacheableClient = require('secrets-manager-pg-cache').CacheableClient;
const NodeCache = require('node-cache');
const Pool = require('pg-pool');

const cache = new NodeCache({ stdTTL: 3600, checkperiod: 600 });
const secretId = 'integration/postgres/smpc/integration_user';
const secretsManager = new SecretsManager({
    cache: cache,
    client: {
        getSecretValue: function (secretId) {
            return {
                promise: function () {
                return Promise.resolve(JSON.stringify({
                    engine: 'postgres',
                    username: 'test',
                    password: 'test',
                    host: 'localhost',
                    port: 5432,
                    database: 'test'
                }));
                }
            };
        }
    },
    logger: console
});

const pool = new Pool({
    secretsManager: secretsManager,
    secretId: secretId
}, CacheableClient);

pool.on('error', async function (err, client) {
    if (err.routine !== undefined && err.routine === 'auth_failed') {
        await secretsManager.refresh(secretId);
    }
});

Using AWS Secrets Manager

const AWS = require('aws-sdk');
const SecretsManager = require('secrets-manager-pg-cache').SecretsManager;
const CacheableClient = require('secrets-manager-pg-cache').CacheableClient;
const NodeCache = require('node-cache');
const Pool = require('pg-pool');

const cache = new NodeCache({ stdTTL: 3600, checkperiod: 600 });
const secretId = 'integration/postgres/smpc/integration_user';
const secretsManager = new SecretsManager({
    cache: cache,
    client: new AWS.SecretsManager({
        region: "us-east-1"
    }),
    logger: console
});

const pool = new Pool({
    secretsManager: secretsManager,
    secretId: secretId
}, CacheableClient);

pool.on('error', async function (err, client) {
    if (err.routine !== undefined && err.routine === 'auth_failed') {
      await secretsManager.refresh(secretId);
    }
});

Testing

Both integration and unit tests can be run via separate commands, namely:

  1. npm test
  2. npm run integration

In order for integration tests to be run, a postgres instance should be running locally. This can be done via docker using the following command:

docker run --name postgres -e POSTGRES_USER=test -e POSTGRES_DB=test -e POSTGRES_PASSWORD=test -p 5432:5432 -d postgres

Package Sidebar

Install

npm i secrets-manager-pg-cache

Weekly Downloads

2

Version

0.0.14

License

MIT

Unpacked Size

17.9 kB

Total Files

12

Last publish

Collaborators

  • alexandermj