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

sec-env

v0.0.3

Published

Asynchronous environment processing and lookup

Downloads

6

Readme

In a Nutshell

This is a unified system to access process environment. This is most useful in serverless applications (e.g. AWS Lambda) in which some configuration information is passed to the system via environments.

Environments can be either literal strings (= traditional environments) or pointers to either plaintext or encrypted AWS SSM strings. Changing an environment value from the plaintext one to the protected one can be done by only changing the value of the environment variable. No changes to the code is needed.

Example

Environment is typically accessed asynchronously, because fetching and decrypting values stored to SSM can't be fetched synchronously. However after the initialization has been completed, lookups complete immediately.

const env = require('sec-env');

function xxx() {
    var home, shell, something;
    return (Promise.resolve()
            .then(function() {
                return Promise.all([ env.get('HOME'), env.get('SHELL'), env.get('SOMETHING') ]);
            })
            .then(function(ret) {
                home = ret[0];
                shell = ret[1];
                something = ret[2];
                console.log('got environments home:' , home, 'shell:', shell, 'something:', something);
            })
            .catch(function(e) {
                console.log(e.message);
                process.exit(1);
            }));
}

xxx();

Testing from command line:

$ node async-test.js
Environment variable "SOMETHING" is not set
$ SOMETHING=foobar node async-test.js
got environments home: /home/tri shell: /bin/bash something: foobar

Alternatively, values can be accessed using synchronous API, but in order to do that, the asynchronous module initialization must complete first.

const env = require('./node-sec-env/sec-env.js');

function xxx() {
        var home, shell, something;
        return (env.moduleInitWait()
                        .then(function(ret) {
                                home = env.getSync('HOME');
                                shell = env.getSync('SHELL');
                                something =  env.getSync('SOMETHING');
                                console.log('got environments home:' , home, 'shell:', shell, 'something:', something);
                        })
                        .catch(function(e) {
                                console.log(e.message);
                                process.exit(1);
                        }));
}

xxx();

Testing from command line:

$ node sync-test.js
got environments home: /home/tri shell: /bin/bash something: undefined
$ SOMETHING=foobar node sync-test.js
got environments home: /home/tri shell: /bin/bash something: foobar
$ SOMETHING='sec-env:string(barbar)' node sync-test.js
got environments home: /home/tri shell: /bin/bash something: barbar
$ SOMETHING='sec-env:base64-string(a3Vra3V1)' node sync-test.js
got environments home: /home/tri shell: /bin/bash something: kukkuu
$ SOMETHING='sec-env:base64(a3Vra3V1)' node sync-test.js
got environments home: /home/tri shell: /bin/bash something: <Buffer 6b 75 6b 6b 75 75>

The main difference being that the individual values doesn't have to be evaluated over promise steps and the undefined environment value doesn't throw error but instead returns undefined.

Reference

env.moduleInitWait()

Returns a promise that resolves once the env module is initialized and synchronous interfaces can be used. Using any synchronous interface before initializations are complete causes an error to be thrown. There is no need to call and wait this, if only asynchronous interfaces are used, since they individually wait the initialization to complete, before executing.

env.get(name)

Fetch the value of a variable asynchronously. Returns a promise resolving to the value. Promise rejects, if the name does not exist.

env.getFull(name)

Fetch the value object containing name, value ane type of the variable. Returns a promise resolving to the object. Promise rejects, if the name does not exist.

env.keys()

Fetch the list of available variable keys as an array. Returns a promise resolving to array of strings.

env.getSync(name)

Get a value of the variable synchronously. Throws an error, if the sec-env module has not been initialized. This interface can be used safely only after the code one way or another has waited env.moduleInitWait() to resolve.

env.getFullSync(name)

Get a value object (see env.getFull() for details) of the variable synchronously. Throws an error, if the sec-env module has not been initialized. This interface can be used safely only after the code one way or another has waited env.moduleInitWait() to resolve.

env.keysSync()

Fetch the list of available variable keys as an array synchronously. Throws an error, if the sec-env module has not been initialized. This interface can be used safely only after the code one way or another has waited env.moduleInitWait() to resolve.

env.isSet(name)

Return a promise resolving to true or false depending on whether the variable name is defined or not. This is an asynchronous interface and is safe to call even when the module initialization still in progress.

Value Formats

Values are encoded to the process environment as follows:

sec-env:type(value)

All environment values that do not match to the format above, are used as literals. This means that such variables are not processed at all, but can still be accessed via env.get() and other methods.

Types

string

A value is a non-processed string. For eaxmple setting variable x to sec-env:type(foobar) causes env.getSync('x') to return string foobar.

base64

A value is a base64 encoded data. The encoding is automatically removed and the data is returned as Buffer.

base64-string

A value is a base64 encoded data. The encoding is automatically removed and the data is returned as string.

aws-ssm-string and aws-ssm-secure-string

A value is a name of a variable stored in AWS SSM. In case of a secure string, it is automatically decrypted. A variable that has been stored as a secure (i.e. encrypted) string to SSM can be used only as a secure string. Data is returned as a string.

For example sec-env:aws-ssm-secure-string(/my/service/development/env/zap) causes the value to be fetched from AWS SSM path /my/service/development/env/zap and automatically decrypted using the AWS KMS key that was used in encrypting the value and returned as a string.

aws-ssm-base64 and aws-ssm-secure-base64

A value is a name of a variable stored in AWS SSM. In case of a secure string, it is automatically decrypted. A variable that has been stored as a secure (i.e. encrypted) string to SSM can be used only as a secure string. Data is automatically base64 decoded and returned as a Buffer.

aws-ssm-base64-string and aws-ssm-secure-base64-string

A value is a name of a variable stored in AWS SSM. In case of a secure string, it is automatically decrypted. A variable that has been stored as a secure (i.e. encrypted) string to SSM can be used only as a secure string. Data is automatically base64 decoded and returned as a string.

Author

Timo J. Rinne [email protected]

License

MIT License