Skip to content

Commit

Permalink
feat(first): commit
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinG3 committed Aug 17, 2020
0 parents commit 0a2c581
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 0 deletions.
76 changes: 76 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
version: 2

defaults: &defaults
working_directory: ~/repo
docker:
- image: circleci/node:12

jobs:
install:
<<: *defaults
steps:
- checkout
- restore_cache:
keys:
- node-modules
- run: npm install
- save_cache:
paths:
- node_modules
key: node-modules
- persist_to_workspace:
root: ~/repo
paths: .
test:
<<: *defaults
steps:
- attach_workspace:
at: ~/repo
- run:
name: Setup environment variables
command: |
echo 'export COVERALLS_SERVICE_JOB_ID="$CIRCLE_BUILD_NUM"' >> $BASH_ENV
- run:
name: Run lint
command: npm run test:lint
- run:
name: Run tests & coverage
command: npm run test:coveralls
publish:
<<: *defaults
steps:
- attach_workspace:
at: ~/repo
- run:
name: Authenticate on npm registry
command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
- run:
name: Publish on npm registry
command: npm publish --access=public

workflows:
version: 2
main-flow:
jobs:
- install:
filters:
tags:
only:
- /.*/
- test:
requires:
- install
filters:
tags:
only:
- /.*/
- publish:
context: Npm
requires:
- test
filters:
branches:
ignore:
- /.*/
tags:
only: /v[0-9]+(\.[0-9]+)*/
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
.nyc_output
coverage
node_modules
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!index.js
!src/**/*
!README.md
!LICENSE
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Sagacify

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Hush

[![CircleCI](https://circleci.com/gh/Sagacify/hush.svg?style=svg)](https://circleci.com/gh/Sagacify/hush)
[![Coverage Status](https://coveralls.io/repos/github/Sagacify/hush/badge.svg?branch=master)](https://coveralls.io/github/Sagacify/hush?branch=master)
[![npm version](https://img.shields.io/npm/v/@sagacify/hush.svg)](https://www.npmjs.com/package/@sagacify/hush)
[![Dependency Status](https://img.shields.io/david/Sagacify/hush.svg?style=flat-square)](https://david-dm.org/Sagacify/hush)

## Description

Hush is a package meant to simplify secrets managements in application.
It reads secrets injected via docker inside the container.

## Installation

```sh
$ npm install @sagacify/hush
```

## Usage

### Import in your project
```js
const { readSecret } = require('@sagacify/hush');

// Read secret AWS_ACCESS_KEY in the default docker secret injection folder
const secret = readSecret('AWS_ACCESS_KEY');

// Read secret AWS_ACCESS_KEY in a custom secret folder
const secret = readSecret('AWS_ACCESS_KEY', 'path/to/my/custom/secret/folder/');
```

### API

**readSecret(fileName, secretsPath = '/run/secrets/')**

*Arguments*
- fileName: the name of the file inside the secretsPath
- secretsPath: path to the folder containing the secrets

*Returns*
- If the secret is empty or the file existing: returns null
- Else: returns the secret

## Npm scripts

### Running code formating

```sh
$ npm run format
```

### Running tests

```sh
$ npm test
```

### Running lint tests

```sh
$ npm run test:lint
```

### Running coverage tests

```sh
$ npm run test:cover
```

This will create a coverage folder with all the report in `coverage/index.html`

### Running all tests

```sh
$ npm run test:all
```

*Note: that's the one you want to use most of the time*

## Reporting bugs and contributing

If you want to report a bug or request a feature, please open an issue.
If want to help us improve hush, fork and make a pull request.
Please use commit format as described [here](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines).
And don't forget to run `npm run format` before pushing commit.

## Repository

- [https://github.com/sagacify/hush](https://github.com/sagacify/hush)

## License

The MIT License (MIT)

Copyright (c) 2020 Sagacify

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@sagacify/hush",
"version": "1.0.0",
"description": "Secrets manager",
"main": "src/hush.js",
"directories": {
"test": "test"
},
"keywords": [
"docker",
"secret"
],
"author": "@quentinG3",
"license": "MIT",
"scripts": {
"format": "semistandard --fix",
"test:spec": "mocha --recursive",
"test:lint": "semistandard --verbose | snazzy",
"test:cover": "nyc --reporter=text-summary --reporter=html npm run test:spec",
"test:coveralls": "nyc npm run test:spec && nyc report --reporter=text-lcov | coveralls",
"test": "npm run test:lint && npm run test:cover"
},
"devDependencies": {
"chai": "^4.2.0",
"coveralls": "^3.1.0",
"mocha": "^8.1.1",
"nyc": "^15.1.0",
"semistandard": "^14.2.3",
"snazzy": "^8.0.0"
},
"nyc": {
"include": [
"src/**/*.js"
]
},
"semistandard": {
"env": {
"node": true,
"mocha": true
}
}
}
20 changes: 20 additions & 0 deletions src/hush.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('fs');
const _ = require('lodash');

module.exports.readSecret = (fileName, secretsPath = '/run/secrets/') => {
let secret = null;
try {
secret = fs.readFileSync(
`${secretsPath}${fileName}`,
{ encoding: 'utf8', flag: 'r' }
);
} catch (err) {
if (err.code === 'ENOENT') {
return null;
} else {
throw err;
}
}

return !_.isNil(secret) && !_.isEmpty(secret) ? secret : null;
};
Empty file added test/mocks/EMPTY_SECRET
Empty file.
1 change: 1 addition & 0 deletions test/mocks/SECRET_ENV
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SECRETENVVALUE
22 changes: 22 additions & 0 deletions test/src/hush.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const expect = require('chai').expect;
const { readSecret } = require('../../src/hush');

describe('hush', () => {
describe('readSecret', () => {
it('should read the secret in a secret file', async () => {
const secret = readSecret('SECRET_ENV', './test/mocks/');

expect(secret).to.equal('SECRETENVVALUE\n');
});
});
it('should return null in an empty secret file', async () => {
const secret = readSecret('EMPTY_SECRET', './test/mocks/');

expect(secret).to.equal(null);
});
it('should return null in an non existing secret file', async () => {
const secret = readSecret('NON_EXISTING_FILE', './test/mocks/');

expect(secret).to.equal(null);
});
});

0 comments on commit 0a2c581

Please sign in to comment.