Repository navigation
Db creds security #319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Db creds security #319
Changes from all commits
29df9bf
8c62212
453554f
9e1732b
7bdcac0
e119aed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,22 +41,22 @@ $ apic create --type datasource | |
| You can also add and modify data sources using the API Designer tool. | ||
| </div> | ||
|
|
||
| The tool will prompt you for the name of the new data source and the connector to use; for example, MySQL, Oracle, REST, and so on. | ||
| The tool will prompt you for the name of the new data source and the connector to use; for example, MongoDB, MySQL, Oracle, REST, and so on. | ||
| The tool will then add an entry such as the following to `datasources.json`: | ||
|
|
||
| {% include code-caption.html content="/server/datasources.json" %} | ||
| ```javascript | ||
| ... | ||
| "corp1": { | ||
| "name": "corp1", | ||
| "connector": "mysql" | ||
| "name": "mongo1", | ||
| "connector": "mongodb" | ||
| } | ||
| ... | ||
| ``` | ||
|
|
||
| This example creates a MySQL data source called "corp1". The identifier determines the name by which you refer to the data source and can be any string. | ||
| This example creates a MongoDB data source called "mongo1". The identifier determines the name by which you refer to the data source and can be any string. | ||
|
|
||
| ## Add data source credentials | ||
| ## Add database credentials | ||
|
|
||
| Edit `datasources.json` to add the necessary authentication credentials for the data source; typically hostname, username, password, and database name. | ||
|
|
||
|
|
@@ -66,14 +66,66 @@ For example: | |
| ```javascript | ||
| "corp1": { | ||
| "name": "corp1", | ||
| "connector": "mysql", | ||
| "host": "your-mysql-server.foo.com", | ||
| "connector": "mongodb", | ||
| "host": "your-mongodb-server.foo.com", | ||
| "user": "db-username", | ||
| "password": "db-password", | ||
| "database": "your-db-name" | ||
| } | ||
| ``` | ||
|
|
||
| {% include warning.html content="Putting production database credentials in a JSON file is not recommended for security reasons. | ||
| Instead, load the credentials from environment variables, as explained [below](#specifying-database-credentials-with-environment-variables). | ||
| "%} | ||
|
|
||
| ### Specifying database credentials with environment variables | ||
|
|
||
| Best practice is not to put production database credentials explicitly in JSON or JavaScript files, where they could be a security vulnerability. Instead, define the values in environment variables and reference them in the data source configuration file. | ||
|
|
||
| For example, assuming you have set a valid MongoDB username and password in the | ||
| environment variables MONGO_USER and MONGO_PASS, respectively: | ||
|
|
||
| {% include code-caption.html content="datasources.json" %} | ||
| ```javascript | ||
| ... | ||
| "accountDS": { | ||
| "name": "accountDS", | ||
| "connector": "mongodb", | ||
| "host": "demo.strongloop.com", | ||
| "port": 27017, | ||
| "database": "demo", | ||
| "username": "${MONGO_USER}", | ||
| "password": "${MONGO_PASS}" | ||
| } | ||
| ... | ||
| ``` | ||
|
|
||
| You can use different data source definitions (including database credentials) for development and production by using the NODE_ENV | ||
| environment variable, as explained in [Environment-specific configuration](Environment-specific-configuration.html#data-source-configuration), | ||
| for example `datasources.production.json` for production environment when NODE_ENV is 'production'. | ||
|
|
||
| ### Using multiple data source configurations | ||
|
|
||
| LoopBack merges environment-specific configurations (for example in `datasources.production.json`) with the baseline configuration in `datasources.json`. | ||
| Environment-specific configurations can override the top-level values with string and number values only. You cannot use objects or array values. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not true - AFAIK, one can override object values with object values, array values with array values too.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well this is copy/paste from the current doc |
||
|
|
||
| It can be difficult to clear extra datasource settings inherited from the top-level `datasources.json` file in environment-specific files. To avoid issues, use a single `datasources.json` file that includes both individual settings such as `host`, `port`, and `database` for local environment and a `url` connection string setting that overrides the other settings for staging and production. For example: | ||
|
|
||
| {% include code-caption.html content="datasources.json" %} | ||
| ```js | ||
| { | ||
| "db": { | ||
| "connector": "mongodb", | ||
| // The url setting will be used only when MONGODB_URL env var is defined | ||
| "url": "${MONGODB_URL}", | ||
| // configuration below is for development/staging environments only | ||
| "host": "localhost", | ||
| "database": "my-database-name", | ||
| ... | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Make the model use the data source | ||
|
|
||
| When you create a new model with the [model generator](Model-generator.html), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok for the warning but it stands that the problem expressed in this comment is still not covered by the doc.
The doc explains how to create a datasource programmatically but some info should be added on where to use this snippet, and how to connect models on these datasources (programmatically I suppose, but one could assume that they could reference datasources created programmatically in the model-config.json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened a new issue for this: #331