loopback-boot icon indicating copy to clipboard operation
loopback-boot copied to clipboard

Configure datasources via env variables

Open bajtos opened this issue 12 years ago • 28 comments

The Twelve-Factor App recommends to store configuration in environment variables - see the section III. Config.

loopback-boot can already read host and port from environment variables. We should extend this mechanism to cover datasource configuration too.

Proposed naming convention:

DATASOURCE_{ds-name}_{option-name}=VALUE

Example:

DATASOURCE_db_host=staging.mysql.com
DATASOURCE_db_user=webapp

DATASOURCE_email_type=SMTP
DATASOURCE_email_host=smtp.gmail.com

bajtos avatar May 29 '14 06:05 bajtos

It might be better to use the dot notion (the json way), such as:

DATASOURCE.email.type=SMTP

raymondfeng avatar May 29 '14 06:05 raymondfeng

It might be better to use the dot notion (the json way), such as:

DATASOURCE.email.type=SMTP

That's an option too.

I prefer to stay consistent with existing conventions, which use underscores (see index.js):

appConfig.host =
    process.env.npm_config_host ||
    process.env.OPENSHIFT_SLS_IP ||
    process.env.OPENSHIFT_NODEJS_IP ||
    process.env.HOST ||
    appConfig.host ||
    process.env.npm_package_config_host ||
    app.get('host');

bajtos avatar May 29 '14 07:05 bajtos

Underscores are definitely the convention on the environment variable side. You could take a cue from nconf and use the _'s to generate a hierarchical config tree like that of a JSON file.

rmg avatar Jun 05 '14 07:06 rmg

Beyond the 12-factor app is a preference for the configuration to be stored in a separate service as a way of mitigating the risks of local-file-injection vulnerabilities (if someone tricked loopback into serving up /proc/self/environment and exposes all of the app's secrets, for example). In that scenario, the preference would probably be module.exports = remoteConfig(process.env.CONFIG_SERVER || 'localhost').

This is something I would like to use for our own servers, putting development defaults in a config.js of some kind, and having the production runtime configuration stored in Redis. To simplify even further, each VM could have a slave instance of Redis. Every app on every server queries localhost, removing the need for any production configuration being packaged for deployment.

rmg avatar Jun 05 '14 07:06 rmg

For mongodb data-sources, it would be nice to support Heroku env variables MONGOLAB_URI and MONGOHQ_URL too.

bajtos avatar Jun 27 '14 17:06 bajtos

Consider using URLs where possible to specify config, it reduces the number of variables, and also emphasizes that apps should be self-contained in terms of behaviour , but will need to know externalities: mostly where dependent services are, and where to offer it's own services (for example, PORT).

Opening example:

DATASOURCE_db_host=staging.mysql.com
DATASOURCE_db_user=webapp

DATASOURCE_email_type=SMTP
DATASOURCE_email_host=smtp.gmail.com

could be:

LOOPBACK_CONNECTOR_DB=mysql://[email protected]
LOOPBACK_CONNECTOR_EMAIL=smtp://smtp.gmail.com

I added namespacing under LOOPBACK_.

Also, @raymondfeng brought up more things like connection pool size, and more complex config than just endpoints, this is also URL representable:

DATASOURCE_DB=mysql://user:[email protected]:9886?poolSize=20&retryDelay=120

@raymondfeng also brought up the pain of managing configs, and modifying when you move your app around to different environments. Obivously, having values in your app.js that change depending on the deploy/run enviornment is awful.

Moving it out into files that contain nothing bug config (either json or .env environment specifications) is better, but managing them can be painful, this is true of env vars as well as .yml/json files. Some things we found help in StrongOps"

  • all config should have a default, and the default should be development, so that for developers, things run as easily as possible from git clone
  • the idea of having a grunt task that writes a .env (or other config) based on interactive questions is interesting, it could help devs or even devops during initial setup, a README that docs the env keys and their meaning isn't terrible, either!
  • slc run now loads a .env if it finds one: strongloop/strong-supervisor#20 - I considered adding a --env=CFG and a --no-env argument options, but punted for now. Might do on request.
  • since slc run is more of a deploy tool, than a local dev-time tool (when some kind of restart on file-change runner is probably being used), its loading of .env favours the "defaults are for developers, so they can run without env vars" approach.

sam-github avatar Jul 08 '14 16:07 sam-github

DATASOURCE_DB=mysql://user:[email protected]:9886?poolSize=20&retryDelay=120

This makes a lot of sense, is easy to implement and we are already using for connector config. +1

ritch avatar Aug 15 '14 17:08 ritch

DATASOURCE_DB=mysql://user:[email protected]:9886?poolSize=20&retryDelay=120

This makes a lot of sense, is easy to implement and we are already using for connector config. +1

I agree it makes sense to pass the full config as a connection string.

However, not all connectors support connections strings - e.g. e-mail transport.

Therefore I'd prefer to implement a generic mechanism that would work for any datasource config option, not only the connection string.

Assuming the mysq connector expects the connection string in url property, the ENV variable would be

DATASOURCE_db_url=mysql://user:[email protected]:9886?poolSize=20&retryDelay=120

bajtos avatar Aug 15 '14 17:08 bajtos

I'm not active on the project but is it a bad idea to convert all json configs to javascript files? That way we can inject our own environment variables where we need.

gregpardo avatar Oct 06 '14 14:10 gregpardo

@gregpardo We need to keep the main config file in JSON to allow tools like yo loopback and StrongLoop Studio edit them programatically.

You can provide another JS file to customise these initial settings - see Environment-specific configuration.

The goal of this enhancement is to make the environment-specific files redundant for people using ENV variables.

bajtos avatar Oct 06 '14 15:10 bajtos

After discussion with @bajtos in #84 I took some time to look into the implementation of this feature. A few questions have emerged which I'd like to discuss. First though, a summary of my understanding of the objectives and approach to implementation. As I understand it from this thread,

  1. The objective is to merge environment variables into the config produced by ConfigLoader in order to allow the environment to override directives. Ideally, it should be possible to override any config value using the environment.
  2. Loopback environment variables should be marked with a magic prefix, likely corresponding to the config file it targets (main config, datasource, middleware, or component). For example, LB_DATASOURCE_db_host.
  3. Environment directives targeting nested values would indicate this using an underscore delimiter. For example, the env-var LB_DATASOURCE_db_host would override the values for the datasource config's db.host.
  4. Per @bajtos in #84, environment would be merged after local, dynamic, and master config.

After thinking about implementation from these conditions I have the following questions and comments,

  1. Underscore as a delimiter will prevent the override of config directives that use underscores in the key name. While camelcase is typical by convention, using . as a delimiter (as @raymondfeng suggested) may avoid future conflicts.
  2. Configuration may contain nested arrays. A common example is the email transports config. I propose that array indexes be supported in the following manner: LB_DATASOURCE_mail.transports.0.auth.pass.
  3. Env-vars are always strings. I propose that they be coerced into the types of the values they are overriding.
  4. @bajtos, you suggested that env vars be merged last, meaning they are processed after the dynamic config. This makes it possible for dynamic config to inject a non-primitive type which we cannot coerce the env-var value in to. I propose hard failure under any case where the target value is non-primitive. This will also prevents env-var config from clobbering non-leaf config nodes.
  5. I propose that env-vars targeting non-existent configuration keys be dropped, not inserted.

Please help me refine this informal spec to meet your standards and expectations. Afterwards, I will put together a pull request including tests that enforce these conditions.

Thanks! :)

doublemarked avatar Jan 17 '15 22:01 doublemarked

@doublemarked thank you for thinking this through. The objectives 1-4 are correct.

Underscore as a delimiter will prevent the override of config directives that use underscores in the key name. While camelcase is typical by convention, using . as a delimiter (as @raymondfeng suggested) may avoid future conflicts.

Underscores are definitely the convention on the environment variable side. For example nconf uses a double-underscore and allows the user to provide a custom delimiter. Can we do the same here? Default to __ and allow the user to provide a different value like . if he prefers so?

Env-vars are always strings. I propose that they be coerced into the types of the values they are overriding.

That may be difficult to implement, as the target type is now always know. Can we leave this out of the first iteration and implement it as a second step?

I propose hard failure under any case where the target value is non-primitive. This will also prevents env-var config from clobbering non-leaf config nodes.

+1

I propose that env-vars targeting non-existent configuration keys be dropped, not inserted.

I suppose that's an acceptable limitation for the first iteration. In the long term, we should IMO allow users to add extra configuration via env. E.g. production settings of a db-backed datasource may add extra options to tweak pooling size or reconnect behaviour, while the development configuration does not include any of that.

Most importantly, while implementing the type coercion proposed above, we must not assume that the target type can be inferred from an existing value.

bajtos avatar Jan 19 '15 09:01 bajtos

Hello! Thank you for your feedback. A few responses below.

Underscores are definitely the convention on the environment variable side. For example nconf uses a double-underscore and allows the user to provide a custom delimiter. Can we do the same here? Default to __ and allow the user to provide a different value like . if he prefers so?

Great, yes, agreed - __ to escape underscores, and potentially custom delimiters.

That may be difficult to implement, as the target type is now always know. [...] Most importantly, while implementing the type coercion proposed above, we must not assume that the target type can be inferred from an existing value.

Can you please elaborate on why this is the case? This would only apply to JSON primitive types (string, number, bool, null).

doublemarked avatar Jan 19 '15 11:01 doublemarked

Can you please elaborate on why this is the case? This would only apply to JSON primitive types (string, number, bool, null).

Here is a sample datasources.json file used in development:

{
  "db": {
    "database": "products"
  }
}

And the production ENV config:

DATASOURCE__db__url=mongodb://products:[email protected],backup.example.com/products?replicaSet=test&connectTimeoutMS=300000

As you can see, the configuration does not have any url property.

Does my previous comment make sense now?

bajtos avatar Jan 19 '15 17:01 bajtos

Ah yes, I understand. Actually, this is specifically why I suggested that unknown keys be dropped instead of inserted. But this does not apply to existing values, only to keys that do not already exist in the configuration.

Would always inserting new values as strings be reasonable?

doublemarked avatar Jan 19 '15 17:01 doublemarked

Would always inserting new values as strings be reasonable?

Definitely! Let's start with that, we can always fine tune this initial implementation later.

bajtos avatar Jan 19 '15 18:01 bajtos

It would be handy if a connector configuration could give the name of an environment variable to use if present, including a convention for specifying that value via an environment variable:

datasource.json:

{
  "db": {
    "env": "POSTGRES_URL"
  }
}

environment:

DATASOURCE_db_env=POSTGRES_URL

This would be useful for environments like Heroku where your environment will have injected into it:

HEROKU_POSTGRESQL_BROWN_URL=postgres://xxxx:yyyyy@some-host:5432/zzzz

Which could then be used without changing your config file:

$ heroku config:set DATASOURCE_db_env=HEROKU_POSTGRESQL_BROWN_URL

Also, just so it doesn't go unsaid: for maximum portability environment variable names should probably contain only letters, digits, and underscores http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html

rmg avatar Jan 19 '15 20:01 rmg

Hi,

To use the values from the environment variables, we do have support for configuration inside JS files. But, still it made sense to me to have the same support through the json files as well for easy maintanence even by a JS-agnostic guy, probably a devop.

I have come up with first draft of my changes, which can be reviewed here.

Working:

  • It looks for the pattern ${SOME_ENV_VAR} on the strigified configuration object and does the replacement with the process.env[SOME_ENV_VAR] if possible, else left intact. Please let me know if we should follow a different pattern like ${LB_SOME_ENV_VAR} etc.
  • The variable types are intact during strigifying and parsing it again to the JSON. :+1:
  • The ENV_VARS are alwasy string. So, post replacement the data-type is always going to be string. Probably a limitation for the first draft. (We can think if the code should be intelligent enough to parse it to a number/boolean if needed/possible. e.g. "3306", "5432" or "true")

Please let me know if the approach/direction is good to proceed with. I shall go ahead incorporate the feedback, if any And write the test cases before I generate a PR.

Cheers :-)

ashwinik001 avatar Feb 03 '16 20:02 ashwinik001

Hi @dopeddude, your approach looks reasonable. I have two concerns though:

  1. From my experience, there are usually two configs needed: a) configuration for development environment (e.g. running everything on a laptop) b) configuration for staging/production. The dev config is usually static and same for all users (i.e. everybody is running MySQL on localhost), only the staging/production config needs to pick values from ENV. With your proposal, one will either have to maintain two config files or else force all developers to set env variables, which may not work well when developing multiple apps on the same machine.

I suppose a possible solution is to replace values not found in ENV with empty value:

// server/datasources.json
{
  "db": {
    "connector": "postgresql",

    // staging/production
    "url": "${HEROKU_POSTGRESQL_BROWN_URL}",

    // local development
    "host": "localhost",
    "user": "admin"
   }
}

Then we need to check connector implementation and verify that that url always overrides individual config options.

  1. I am wondering what is the likelihood that somebody would actually want to use a string like ${FOO} in the configuration? If we implement string replacement based on environment variables, there won't be any reliable way for specifying such string in the config.

Thoughts?

bajtos avatar Feb 12 '16 09:02 bajtos

Hi @bajtos ,

Thanks for the response.

  1. May be I could not understand your comment well, But I can not imagine why

one will either have to maintain two config files or else force all developers to set env variables, which may not work well when developing multiple apps on the same machine.

Let me try with a sample configuration.

//datasources.json
{
    "db": {
        "name": "db",
        "connector": "memory"
    },
    "myDb": {
        "host": "localhost",
        "database": "some-db-name",
        "password": "some-secret-key",
        "name": "myDb",
        "connector": "postgresql",
        "user": "some-user"
    }
}

//datasources.staging.json
{
    "myDb": {
        "host": "pg.pool.com",
        "password": "some-staging-secret-key",
        "user": "some-staging-user"
    }
}

//datasources.production.json
{
    "myDb": {
        "host": "${ENV_PROD_PG_URL}",
        "password": "some-super-secret-key-for-prod",
        "user": "some-prod-user"
    }
}   

At runtime

Development Machine:
    host === 'localhost'
Staging:
    host === 'pg.pool.com'
Production:
    host === 'prod.pg.pool.com'     //If the env variable `ENV_PROD_PG_URL` is set and is equal to 'prod.pg.pool.com'.
    host === '${ENV_PROD_PG_URL}'   //If the env varaible `ENV_PROD_PG_URL` is not set.

Then we need to check connector implementation and verify that that url always overrides individual config options.

This should be an existing problem, if it is not implemented so. Reason: I assume at present anyways we are merging the configurations from datasources.json, datasources.local.json, datasources.staging.js || datasources.staging.json. Now, if after merging if the final configuration object has url and individual configs like host, port etc. The connector has to be intelligent to pick the url and not the individual configs.

  1. I also doubt the likelihood

that somebody would actually want to use a string like ${FOO} in the configuration

But let's say if some use case requires this exact string, then I assume the user should not be setting this FOO variable on the environment. And in that case the string will be left intact and no substitution. Good for him and his use case.

To add another concern, We should also be aware of the fact that there is another dynamic-configuration-params substituion in case of (Code Ref). In that case the placeholders patterns being similar can cause confusion. Like, if the user specifies a pattern like ${FOO}, We do not know if he wants it replace it with the ENV-VARS or he want to replace it with the APP-Config defined as part of the config.json to say.

ashwinik001 avatar Feb 13 '16 10:02 ashwinik001