-
Notifications
You must be signed in to change notification settings - Fork 6
Add initial support for docker secrets #5
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
base: master
Are you sure you want to change the base?
Changes from 9 commits
778f2e1
66055b6
2bd48e6
64b3d0d
5559c83
9031409
3164837
19ca246
47e8624
79d12b5
5afcc3b
f98262b
6fe8c84
aa6ebbd
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,6 +41,26 @@ console.log(config); | |
| - It is possible to change `%` to any other character. Just use `replaceEnv` configuration option. | ||
| - It is possible to use default values when environmental variable is not set. | ||
|
|
||
| ### Docker secret replacement | ||
|
|
||
| /tmp/config.json: | ||
| ``` javascript | ||
| { docks1: "%{DOCKER_SECRET1}", docks2: "%{DOCKER_SECRET2|def}" } | ||
| ``` | ||
| index.js: | ||
| ``` javascript | ||
| var readConfig = require('read-config'), | ||
| config = readConfig('/tmp/config.json'); | ||
|
|
||
| console.log(config); | ||
| // If secret file content DOCKER_SECRET1 is 'abc' and DOCKER_SECRET2 is not defined | ||
| // $ node index.js | ||
| // { docks1: 'abc', env2: 'def' } | ||
| ``` | ||
|
|
||
| - It is possible to change `#` to any other character. Just use `replaceDockerSecret` configuration option. | ||
| - It is possible to use default values when docker secret is not present. | ||
|
|
||
| ### Configuration overriding with system variables | ||
|
|
||
| /tmp/config.json: | ||
|
|
@@ -225,7 +245,8 @@ All json files are loaded using [JSON5](https://www.npmjs.com/package/json5) lib | |
| - **optional** - (String/Array, default: []) list of configuration paths that are optional. If any configuration path is not resolved and is not optional it's treated as empty file and no exception is raised. | ||
| - **basedir** - (String/Array, default: []) base directory (or directories) used for searching configuration files. Mind that `basedir` has lower priority than a configuration directory, process basedir, and absolute paths. | ||
| - **replaceEnv** - (Boolean/String, default: false, constraint: A string value must be different than `replaceLocal`) if specified enables environment variable replacement. Expected string value e.g. `%` that will be used to replace all occurrences of `%{...}` with environment variables. You can use default values like: %{a.b.c|some-default-value}. | ||
| - **replaceLocal** - (Boolean/String, default: '@', constraint: A string value must be different than `replaceEnv`) if specified enables configuration variable replacement. Expected string value e.g. `@` that will be used to replace all occurrences of `@{...}` with configuration variables. You can use default values like: @{a.b.c|some-default-value}. | ||
| - **replaceDockerSecret** - (Boolean/String, default: false, constraint: A string value must be different than `replaceLocal` and `replaceEnv`) if specified enables docker secret file replacement. Expected string value e.g. `#` that will be used to replace all occurrences of `#{...}` with docker secret file content. You can use default values like: #{DOCKER_SECRET|some-default-value}. | ||
|
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. A link to a doc explaining docker secrets would be handy
Author
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. Ok, next revision. |
||
| - **replaceLocal** - (Boolean/String, default: '@', constraint: A string value must be different than `replaceEnv` and `replaceDockerSecret`) if specified enables configuration variable replacement. Expected string value e.g. `@` that will be used to replace all occurrences of `@{...}` with configuration variables. You can use default values like: @{a.b.c|some-default-value}. | ||
| - **override** - (Boolean/String, default: false) If specified enables configuration overriding with environmental variables like `CONFIG_<propertyName>`. | ||
| - **skipUnresolved** - (Boolean, default: false) `true` blocks error throwing on unresolved variables. | ||
|
|
||
|
|
@@ -237,6 +258,7 @@ Default **opts** values: | |
| basedir: null, | ||
| replaceEnv: "%", | ||
| replaceLocal: "@", | ||
| replaceDockerSecret: "#", | ||
| skipUnresolved: false | ||
| } | ||
| ``` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,13 +3,15 @@ | |
| const replaceVariables = require('./replace-variables'), | ||
| override = require('./override'), | ||
| moduleName = '[read-config]', | ||
| ReadConfigError = require('../read-config-error'); | ||
| ReadConfigError = require('../read-config-error'), | ||
| secrets = require('./secrets'); | ||
|
|
||
| module.exports = function(config, opts) { | ||
| opts = opts || {}; | ||
| config = override(opts.override, config, process.env); | ||
| config = replaceEnvVariables(config, opts.replaceEnv, opts); | ||
| config = replaceLocalVariables(config, opts.replaceLocal, opts); | ||
| config = replaceDockerSecrets(config, opts.replaceDockerSecret, opts); | ||
|
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. Test it |
||
| return config; | ||
| }; | ||
|
|
||
|
|
@@ -36,3 +38,15 @@ function replaceLocalVariables(config, marker, opts) { | |
| return config; | ||
| } | ||
| } | ||
|
|
||
| function replaceDockerSecrets(config, marker, opts) { | ||
| if (marker) { | ||
| try { | ||
| return replaceVariables(marker, config, secrets, opts); | ||
| } catch (e) { | ||
| throw new ReadConfigError(`${moduleName} Could not resolve docker secret file. ${e.message}`); | ||
| } | ||
| } else { | ||
| return config; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| 'use strict'; | ||
|
|
||
| const fs = require('fs'), | ||
| path = require('path'), | ||
| SECRETS_DIR = '/run/secrets', | ||
|
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. It would be nice to make it more generic. Make a parameter out of this value.
Author
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. Is not possible, docker always search for secrets in this path. |
||
| output = {}; | ||
|
|
||
| if (fs.existsSync(SECRETS_DIR)) { | ||
|
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. It would be nice to wrap it in a function. This way it would be easier to test.
Author
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. Working on it. |
||
| const files = fs.readdirSync(SECRETS_DIR); | ||
|
|
||
| files.forEach((file) => { | ||
| const fullPath = path.join(SECRETS_DIR, file), | ||
| key = file, | ||
| data = fs.readFileSync(fullPath, 'utf8').toString().trim(); | ||
| output[key] = data; | ||
| }); | ||
| } | ||
|
|
||
| module.exports = output; | ||
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.
there should be probably '#' instead of '%' in this example ;)
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.
Thanks!, will correct it.