├── .eslintirgnore ├── .eslintrc.json ├── package.json ├── CHANGELOG.md ├── .gitignore ├── LICENSE ├── README.md └── index.js /.eslintirgnore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb-base", 3 | "env": { 4 | "node": true 5 | }, 6 | "rules": { 7 | "strict": "off", 8 | "import/no-unresolved": "off" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-secrets-plugin", 3 | "version": "0.1.0", 4 | "description": "A Serverless plugin to help managing credentials in encrypted files", 5 | "author": "", 6 | "license": "MIT", 7 | "scripts": { 8 | "lint": "eslint ." 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/serverless/serverless-secrets-plugin.git" 13 | }, 14 | "dependencies": { 15 | "bluebird": "^3.4.6" 16 | }, 17 | "devDependencies": { 18 | "eslint": "^3.11.0", 19 | "eslint-config-airbnb-base": "^10.0.1", 20 | "eslint-plugin-import": "^2.2.0" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 6 | 7 | ## [Unreleased] 8 | 9 | ## [0.1.0] - 2017-10-16 10 | 11 | ### Added 12 | - Add support for custom secrets files location using the config `secretsFilePathPrefix` 13 | 14 | ### Changed 15 | - Use `package` hook instead of legacy `before:deploy` 16 | - Improved wording of success messages. 17 | - Renamed class for improved error messages. 18 | 19 | ## [0.0.1] - 2016-11-24 20 | ### Added 21 | - Initial Release 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Serverless Inc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Serverless Credentials Plugin 2 | 3 | IMPORTANT NOTE: As pointed out in the [AWS documentation](http://docs.aws.amazon.com/lambda/latest/dg/env_variables.html) for storing sensible information Amazon recommends to use AWS KMS instead of environment variables like this plugin. 4 | 5 | [![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) 6 | [![npm version](https://badge.fury.io/js/serverless-secrets-plugin.svg)](https://badge.fury.io/js/serverless-secrets-plugin) 7 | [![license](https://img.shields.io/npm/l/serverless-secrets-plugin.svg)](https://www.npmjs.com/package/serverless-secrets-plugin) 8 | 9 | # Install 10 | 11 | ```bash 12 | npm install --save-dev serverless-secrets-plugin 13 | ``` 14 | 15 | After that you need to add the plugin to your `serverless.yml` of you service. 16 | 17 | Run the command `serverless --help` and verify the list of commands contain an `encrypt` and a `decrypt` command. 18 | 19 | # Usage 20 | 21 | Create a `secrets.{stage}.yml` file for each stage e.g. `secrets.dev.yml`. 22 | 23 | Store the keys in there, that you want to keep private e.g. 24 | ```yml 25 | EMAIL_SERVICE_API_KEY: DEV_API_EXAMPLE_KEY_12 26 | SESSION_KEY: DEV_SESSION_EXAMPLE_KEY_12 27 | ``` 28 | 29 | You can also provide a path prefix if you like to keep your secrets in a different directory e.g. 30 | ```yml 31 | custom: 32 | secretsFilePathPrefix: config 33 | ``` 34 | 35 | Encrypt the secrets file for the desired stage by running 36 | 37 | ```bash 38 | serverless encrypt --stage dev --password '{your super secure password}' 39 | ``` 40 | 41 | This will result in an encrypted file e.g. `secrets.dev.yml.encrypted`. You can check the encrypted file into your version control system e.g. Git. It's recommened to add your unencrypted file to `.gitignore` or similar so you and your colleagues can't check it in by accident. 42 | 43 | In your `serverless.yaml` you can use the file variable syntax to import the secrets and set them as environment variables. When you create or update Lambda functions that use environment variables, AWS Lambda encrypts them using the AWS Key Management Service. Read more about that in the AWS documentation [here](http://docs.aws.amazon.com/lambda/latest/dg/env_variables.html). 44 | 45 | Whenever you want to deploy there needs to be the unencrypted version of the secrets file available otherwise the plugin will prevent the deployment. 46 | 47 | # Example 48 | 49 | You can check out a full example in the Serverless Examples repository: [serverless/examples/aws-node-env-variables-encrypted-in-a-file](https://github.com/serverless/examples/tree/master/aws-node-env-variables-encrypted-in-a-file). 50 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | const crypto = require('crypto'); 6 | const BbPromise = require('bluebird'); 7 | 8 | const algorithm = 'aes-256-cbc'; 9 | 10 | class ServerlessSecretsPlugin { 11 | constructor(serverless, options) { 12 | this.serverless = serverless; 13 | this.options = options; 14 | if (this.serverless.service.custom && this.serverless.service.custom.secretsFilePathPrefix) { 15 | this.customPath = this.serverless.service.custom.secretsFilePathPrefix; 16 | } else { 17 | this.customPath = ''; 18 | } 19 | 20 | const commandOptions = { 21 | stage: { 22 | usage: 'Stage of the file to encrypt', 23 | shortcut: 's', 24 | required: true, 25 | }, 26 | password: { 27 | usage: 'Password to encrypt the file.', 28 | shortcut: 'p', 29 | required: true, 30 | }, 31 | }; 32 | 33 | this.commands = { 34 | encrypt: { 35 | usage: 'Encrypt a secrets file for a specific stage.', 36 | lifecycleEvents: [ 37 | 'encrypt', 38 | ], 39 | options: commandOptions, 40 | }, 41 | decrypt: { 42 | usage: 'Decrypt a secrets file for a specific stage.', 43 | lifecycleEvents: [ 44 | 'decrypt', 45 | ], 46 | options: commandOptions, 47 | }, 48 | }; 49 | 50 | this.hooks = { 51 | 'encrypt:encrypt': this.encrypt.bind(this), 52 | 'decrypt:decrypt': this.decrypt.bind(this), 53 | 'package:cleanup': this.checkFileExists.bind(this), 54 | }; 55 | } 56 | 57 | encrypt() { 58 | return new BbPromise((resolve, reject) => { 59 | const servicePath = this.serverless.config.servicePath; 60 | const customPath = this.customPath; 61 | const credentialFileName = `secrets.${this.options.stage}.yml`; 62 | const encryptedCredentialFileName = `${credentialFileName}.encrypted`; 63 | const secretsPath = path.join(servicePath, customPath, credentialFileName); 64 | const encryptedCredentialsPath = path.join(servicePath, customPath, encryptedCredentialFileName); 65 | 66 | fs.createReadStream(secretsPath) 67 | .on('error', reject) 68 | .pipe(crypto.createCipher(algorithm, this.options.password)) 69 | .on('error', reject) 70 | .pipe(fs.createWriteStream(encryptedCredentialsPath)) 71 | .on('error', reject) 72 | .on('close', () => { 73 | this.serverless.cli.log(`Successfully encrypted '${credentialFileName}' to '${encryptedCredentialFileName}'`); 74 | resolve(); 75 | }); 76 | }); 77 | } 78 | 79 | decrypt() { 80 | return new BbPromise((resolve, reject) => { 81 | const servicePath = this.serverless.config.servicePath; 82 | const customPath = this.customPath; 83 | const credentialFileName = `secrets.${this.options.stage}.yml`; 84 | const encryptedCredentialFileName = `${credentialFileName}.encrypted`; 85 | const secretsPath = path.join(servicePath, customPath, credentialFileName); 86 | const encryptedCredentialsPath = path.join(servicePath, customPath, encryptedCredentialFileName); 87 | 88 | fs.createReadStream(encryptedCredentialsPath) 89 | .on('error', reject) 90 | .pipe(crypto.createDecipher(algorithm, this.options.password)) 91 | .on('error', reject) 92 | .pipe(fs.createWriteStream(secretsPath)) 93 | .on('error', reject) 94 | .on('close', () => { 95 | this.serverless.cli.log(`Successfully decrypted '${encryptedCredentialFileName}' to '${credentialFileName}'`); 96 | resolve(); 97 | }); 98 | }); 99 | } 100 | 101 | checkFileExists() { 102 | return new BbPromise((resolve, reject) => { 103 | const servicePath = this.serverless.config.servicePath; 104 | const customPath = this.customPath; 105 | const credentialFileName = `secrets.${this.options.stage}.yml`; 106 | const secretsPath = path.join(servicePath, customPath, credentialFileName); 107 | fs.access(secretsPath, fs.F_OK, (err) => { 108 | if (err) { 109 | reject(`Couldn't find the secrets file for this stage: ${credentialFileName}`); 110 | } else { 111 | resolve(); 112 | } 113 | }); 114 | }); 115 | } 116 | } 117 | 118 | module.exports = ServerlessSecretsPlugin; 119 | --------------------------------------------------------------------------------