5.13.5 • Published 4 years ago

config_api v5.13.5

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
4 years ago
 _______                     __    __      __                 .__     __   .__
 \      \    ____    _______/  |_ /  \    /  \  ____  _____   |  |  _/  |_ |  |__
 /   |   \ _/ __ \  /  ___/\   __\\   \/\/   /_/ __ \ \__  \  |  |  \   __\|  |  \
/    |    \\  ___/  \___ \  |  |   \        / \  ___/  / __ \_|  |__ |  |  |   Y  \
\____|__  / \___  >/____  > |__|    \__/\  /   \___  >(____  /|____/ |__|  |___|  /
        \/      \/      \/               \/        \/      \/                   \/

Config API

Used to get configuration from a central location so we don't have to keep updating config files. Configuration for the server itself is under the config directory.

Vault Setup

Using NPM Recommended

  • Import Vault Environment variables
export VAULT_ADDR=<VAULT_ADDR>
export VAULT_TOKEN=<VAULT_TOKEN>
  • Read/write the secrets as follows:
NODE_ENV=uat TENANT=nw npm run vault:read
NODE_ENV=dev TENANT=nbfwork npm run vault:patch new-secrets.json

Note: NODE_ENV=test to play with the vault.

Using UI

Using CLI

export VAULT_ADDR=<VAULT_ADDR>
export VAULT_TOKEN=<VAULT_TOKEN>
  • Read/write the secrets as follows, where <env> can be dev, uat or production:
vault read secrets/<env>/<tenant>/env
vault write secrets/<env>/<tenant>/env foo=bar
  • For more advanced documentation, follow https://learn.hashicorp.com/vault/
  • While writing secrets to vault, please make sure that they are stored as key=value pair, NO JSON OBJECT ALLOWED. Though vault supports JSON but it is not supported by a number of other services that we want to use with vault.

Usage

Adding new configurations to a tenant

If the configuration is independent of environment and tenants, then add it in scripts/default/config-default.json else in scripts/default/config-<env>.json.

scripts/default
├── config-default.json
├── config-dev.json
├── config-local.json
├── config-production.json
└── config-uat.json

Within a tenant, if a configuration is common in all the environments, then add it in scripts/<tenant>/config-default.json else in scripts/<tenant>/config-<env>.json

scripts/<tenant>
├── config-default.json
├── config-dev.json
├── config-local.json
├── config-production.json
└── config-uat.json

All 4 files (listed below) will be merged(in order) to create the final configuration of a particular tenant<tenant> in a specific environment<env>.

scripts/default/config-default.json
scripts/default/config-<env>.json
scripts/<tenant>/config-default.json
scripts/<tenant>/config-<env>.json

In addition, you must add a new entry for the new configuration to the following files, follow the example of existing configurations in those files:

config_api/models/src/organization/index.js
config_api/models/src/organization/src/static/create-organization.js

For an example adding a new tenant and environment-dependent configuration, see this pull request

Adding configuration to the mongo databse:

NODE_ENV=<env> npm run add-config -- --file scripts/<tenant>/config-<env>.json
  • where <env> & <tenant> are environment and tenant respectively.

For example,

On local environment

// to add all scripts
node scripts/add-all-local.js

// to add individual scripts
NODE_ENV=local npm run add-config -- --file scripts/nw/config-local.json

On dev, uat or production environment

// nestwork tenant
NODE_ENV=production npm run add-config -- --file scripts/nestwork/config-production.json

// nw tenant
NODE_ENV=production npm run add-config -- --file scripts/nw/config-production.json

Running the application Default Environment:

NODE_ENV=<env> npm start

or

NODE_ENV=<env> node .

The config package will pick up the correct configuration.

Running the application Specific Environment:

To run this in a new environment (e.g.: side-production for NWaW), we need to tell config_api to connect to the new database.

// Create `config/local.json`
{
  "database": {
    "uri": "mongodb://[RR1]:27017,[RR2]:27017,[MASTER]:27017/config_api"
  }
}

Where RR1, RR2, and MASTER are the nestwork mongo IPs.

Running Tests

npm test

Note

There is an issue with winston where if using a custom formatter and outputting large objects. Do no log very large objects (e.g.: mongoose documents or models). If you must do so, call toObject() on it first!