├── .gitignore ├── Dockerfile.test ├── README.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | -------------------------------------------------------------------------------- /Dockerfile.test: -------------------------------------------------------------------------------- 1 | FROM node:8-alpine 2 | 3 | RUN mkdir -p /usr/src \ 4 | && mkdir -p /run/secrets \ 5 | && echo "admin" > /run/secrets/db_user \ 6 | && echo "password" > /run/secrets/db_pass 7 | 8 | WORKDIR /usr/src 9 | 10 | COPY ./ /usr/src 11 | 12 | RUN npm install 13 | 14 | CMD [ "node" , "./test/index.js" ] 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker Secrets 2 | This NPM module loads Docker secrets from the `/run/secrets` directory created by Docker Swarm into a JS object for use within Node.js applications. 3 | 4 | ## Installation 5 | ```bash 6 | npm install @cloudreach/docker-secrets 7 | ``` 8 | 9 | ## Usage 10 | ```javascript 11 | const secrets = require('@cloudreach/docker-secrets'); 12 | console.log(secrets); 13 | ``` 14 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | 6 | const SECRETS_DIR = '/run/secrets'; 7 | const output = {}; 8 | 9 | if (fs.existsSync(SECRETS_DIR)) { 10 | const files = fs.readdirSync(SECRETS_DIR); 11 | 12 | files.forEach(function(file, index) { 13 | const fullPath = path.join(SECRETS_DIR, file); 14 | const key = file; 15 | const data = fs.readFileSync(fullPath, 'utf8').toString().trim(); 16 | 17 | output[key] = data; 18 | }); 19 | } 20 | 21 | module.exports = output; 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@cloudreach/docker-secrets", 3 | "version": "1.0.3", 4 | "description": "Converts Docker Secrets mounted into a container to an object.", 5 | "keywords": [ 6 | "docker", 7 | "swarm", 8 | "docker-secrets", 9 | "node-docker", 10 | "containers", 11 | "node.js", 12 | "secrets" 13 | ], 14 | "main": "index.js", 15 | "scripts": { 16 | "test": "echo \"Error: no test specified\" && exit 1" 17 | }, 18 | "author": "Alex Rhea ", 19 | "repository": { 20 | "type": "git", 21 | "url": "https://github.com/cloudreach/node-docker-secrets.git" 22 | }, 23 | "bugs": { 24 | "url": "https://github.com/cloudreach/node-docker-secrets/issues" 25 | }, 26 | "license": "Apache-2.0", 27 | "engines": { 28 | "node": ">= 6" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const secrets = require('../index.js'); 4 | 5 | console.log(secrets); 6 | --------------------------------------------------------------------------------