-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af202f6
commit 694bcfc
Showing
4 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,4 @@ typings/ | |
# dotenv environment variables file | ||
.env | ||
|
||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,30 @@ | ||
# njs-tfso-secret | ||
Reads secret from files in a directory | ||
Get secret values from file | ||
|
||
Try to read file and returns content as value. | ||
Name of file is name of key, file location is determined by config. | ||
|
||
Note: Reading is sync. Use only on startup. | ||
|
||
## install | ||
`npm install tfso/njs-tfso-secret` | ||
|
||
## usage | ||
```javascript | ||
const secret = require('njs-tfso-secret'); | ||
|
||
let value = secret.get('some-secret', 'somedefaultvalue'); | ||
|
||
|
||
//custom config | ||
secret.config({ | ||
location: '/run/secrets', // default | ||
silent: false // if false, log to console if error reading keys from file. Default is "true" | ||
}); | ||
|
||
|
||
// use together with getenv | ||
const env = require('getenv'); | ||
|
||
let valueWithDefaultFromEnv = secrets.get('PASSWORD',env.string('PASSWORD', '')) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
class Secret { | ||
constructor(config) { | ||
this._secrets = {}; | ||
this.config(config); | ||
} | ||
|
||
config(config) { | ||
this._config = Object.assign(this._config || {},config); | ||
} | ||
|
||
read(key) { | ||
let fullPath = path.join(this._config.location,key), | ||
value = null; | ||
try { | ||
this._secrets[key] = value = fs.readFileSync(fullPath,'utf8'); | ||
} catch(ex) { | ||
if ( !this._config.silent ) | ||
console.log(`secret: unable to read key ${key} from file ${fullPath}`); | ||
} | ||
return value; | ||
} | ||
|
||
get(key, defaultValue=null) { | ||
let value; | ||
if ( !!(value = this._secrets[key])) | ||
return value; | ||
value = this.read(key); | ||
return !!value ? value : defaultValue; | ||
} | ||
} | ||
|
||
|
||
module.exports = (config=>{ | ||
let secret = new Secret(config); | ||
return { | ||
config: secret.config.bind(secret), | ||
get: secret.get.bind(secret) | ||
}; | ||
})({location:'/run/secrets', silent: true}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "njs-tfso-secret", | ||
"version": "1.0.0", | ||
"description": "Get secret values by reading from file", | ||
"main": "index.js", | ||
"scripts": { | ||
|
||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/tfso/njs-tfso-secret.git" | ||
}, | ||
"keywords": [ | ||
"tfso", | ||
"secret" | ||
], | ||
"author": "Richard B", | ||
"license": "UNLICENSED", | ||
"bugs": { | ||
"url": "https://github.com/tfso/njs-tfso-secret/issues" | ||
}, | ||
"homepage": "https://github.com/tfso/njs-tfso-secret#readme" | ||
} |