npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

secret-manager-loader-2-env

v1.0.13

Published

Open source to load secret manager secrets to environment variables using config file

Downloads

4

Readme

Secret Manager Loader 2 Environment Variables

Secret Manager Loader

Description

This project provides a set of utilities for loading secrets from a secret manager to the environment variable in local environment.

Installation

This package is designed to be used in your development environment. You can install this package as a development dependency in your project by running the following command in your terminal:

npm i --save-dev secret-manager-loader-2-env

test

Usage

1. Secret Clients

This package exports several types and classes related to secret management:

  • AbstractSecretClient: An abstract class representing a generic secret client, you can extend it to implement your own client.
  • AWSSecretClient: A class for interacting with AWS Secrets Manager, you need to use aws sso login with default to use it.
  • SecretClientFactory: A factory class for creating instances of secret clients based on configuration.
  • SecretLoader: A utility function for loading secrets using a secret client.

2. Importing the Package

You can import the necessary classes and functions from the package as follows:

import {
  AbstractSecretClient,
  AWSSecretClient,
  SecretClientFactory,
  SecretLoader,
} from 'secret-manager-loader-2-env';

3. Usage Examples

Create src/config directory with 3 files inside

src/config/local-development.ts

import { ConfigManager } from 'secret-manager-loader-2-env';

export const Config: ConfigManager = {
  secretManagers: [
    {
      secretName: "SECRET_NAME_1",
      values: [
        { envName: "MONGO_PASS", secretKey: "MONGO_PASS_SECRET_ROW_NAME" },
        { envName: "AWS_KMS_SECRET_ACCESS_KEY", secretKey: "AWS_KMS_SECRET_ACCESS_KEY_SECRET_ROW_NAME" },
      ],
    },
    {
      secretName: "SECRET_NAME_2",
      values: [
        { envName: "REDIS_HOST", secretKey: "localhost" },
        { envName: "REDIS_PORT", secretKey: "6379" },
      ],
    },
  ],
  environmentVariables: [
    { envName: "ENV_1", envValue: "value1" },
    { envName: "ENV_2", envValue: "value2" },
  ],
};

src/config/staging.ts

import { ConfigManager } from 'secret-manager-loader-2-env';

export const Config: ConfigManager = {
  secretManagers: [
    {
      secretName: "SECRET_NAME_1",
      values: [
        { envName: "MONGO_PASS", secretKey: "MONGO_PASS_SECRET_ROW_NAME" },
        { envName: "AWS_KMS_SECRET_ACCESS_KEY", secretKey: "AWS_KMS_SECRET_ACCESS_KEY_SECRET_ROW_NAME" },
      ],
    },
    {
      secretName: "SECRET_NAME_2",
      values: [
        { envName: "ENV_NAME", secretKey: "SECRET_ROW_NAME" },
      ],
    },
  ],
  environmentVariables: [
    { envName: "REDIS_HOST", envValue: "localhost" },
    { envName: "REDIS_PORT", envValue: "6379" },
  ],
};

src/config/secrets-loader.ts

import { Config as LocalConfig } from './local-development';
import { Config as StagingConfig } from './staging';
import { loadSecretsAWS } from 'secret-manager-loader-2-env';

const loadSecrets = async () => {
  if (
    process.env.NODE_ENV === 'local-development'
  ) {
    const config = getConfigByEnv();

    console.log(`Loading keys for ${process.env.NODE_ENV}`);
    await loadSecretsAWS(config);
  }
};

const getConfigByEnv = () => {
  let Config;
  if (process.env.NODE_ENV === 'local-development') {
    Config = LocalConfig;
  } 
  else if (process.env.NODE_ENV === 'local-staging') {
    Config = StagingConfig;
  } 
  else {
    throw new Error('NODE_ENV is not set');
  }
  return Config;
};
loadSecrets();

Create webpack file in the root dir

webpack.config.js

/* eslint-disable @typescript-eslint/no-var-requires */
const nodeExternals = require('webpack-node-externals');
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
require('ts-node').register();
//Load all secrets to env variables
require('./src/config/secrets-loader.ts');  //This line loading all the secrets from the secret manager

module.exports = function (options, webpack) {
  return {
    ...options,
    entry: [options.entry],
    externals: [nodeExternals()],
    plugins: [
      ...options.plugins,
      new webpack.WatchIgnorePlugin({
        paths: [/\.js$/, /\.d\.ts$/],
      }),
      new RunScriptWebpackPlugin({
        name: options.output.filename,
      }),
    ],
    devtool: 'source-map',
  };
};

Use this command to run the service in package.json

These are example for commands to nest services, but you can changes your conmmands to adjust you service if you are not using nestjs.


"dev": "NODE_ENV=local-development nest build --webpack --webpackPath webpack.config.js --watch",
"dev-stg": "NODE_ENV=local-staging nest build --webpack --webpackPath webpack.config.js --watch",

Contributing

Contributions are welcome! If you'd like to contribute to this project, please open a PR.

License

[License Name] - [License Description]