Compare commits

..

26 Commits

Author SHA1 Message Date
90add506e3 4.0.5 2019-09-13 11:20:48 +02:00
adea8d1d69 fix(core): update 2019-09-13 11:20:47 +02:00
1b2eb1d763 4.0.4 2019-08-29 14:29:16 +02:00
cbc974b3d1 fix(core): update 2019-08-29 14:29:16 +02:00
76e72e2a28 4.0.3 2019-08-29 14:19:16 +02:00
697eb83390 fix(core): update 2019-08-29 14:19:15 +02:00
00ec15ccf7 4.0.2 2019-08-06 17:41:46 +02:00
b54a5e2fef fix(core): update 2019-08-06 17:41:45 +02:00
a1aa1543a2 4.0.1 2019-08-06 17:37:07 +02:00
0e48cb8f89 fix(core): update 2019-08-06 17:37:07 +02:00
d76d1a263b 4.0.0 2019-01-15 23:54:29 +01:00
5f6f434998 BREAKING CHANGE(process.env): treat environment as immutable 2019-01-15 23:54:29 +01:00
8e7909e367 3.1.1 2019-01-14 22:42:25 +01:00
ff9302f745 fix(core): update 2019-01-14 22:42:24 +01:00
3577fa22e1 3.1.0 2019-01-14 01:54:05 +01:00
f5275adfbd feat(docker secret): docker secret.json can now also be named [anything].secret.json 2019-01-14 01:54:04 +01:00
31bf1b27a4 3.0.7 2019-01-13 22:33:19 +01:00
a77def5844 fix(core): update 2019-01-13 22:33:18 +01:00
2357699f3e 3.0.6 2019-01-13 22:04:20 +01:00
01f5784488 fix(core): update 2019-01-13 22:04:20 +01:00
80f35c39aa 3.0.5 2019-01-13 21:35:57 +01:00
87f55773bd fix(core): update 2019-01-13 21:35:57 +01:00
1a25341232 3.0.4 2019-01-13 18:25:09 +01:00
ccd41d86bf fix(core): update 2019-01-13 18:25:09 +01:00
0a3a7e480e 3.0.3 2019-01-13 00:00:32 +01:00
33a91c6ae7 fix(core): update 2019-01-13 00:00:32 +01:00
11 changed files with 1199 additions and 544 deletions

22
.gitignore vendored
View File

@ -1,4 +1,22 @@
node_modules/
.nogit/
# artifacts
coverage/
pages/
public/
pages/
# installs
node_modules/
# caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
# custom

View File

@ -1,5 +1,5 @@
# gitzone standard
image: hosttoday/ht-docker-node:npmci
# gitzone ci_default
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache:
paths:
@ -34,48 +34,11 @@ snyk:
- docker
- notpriv
sast:
stage: security
image: registry.gitlab.com/hosttoday/ht-docker-dbase:npmci
variables:
DOCKER_DRIVER: overlay2
allow_failure: true
services:
- docker:stable-dind
script:
- npmci npm prepare
- npmci npm install
- npmci command npm run build
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
- docker run
--env SAST_CONFIDENCE_LEVEL="${SAST_CONFIDENCE_LEVEL:-3}"
--volume "$PWD:/code"
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/sast:$SP_VERSION" /app/bin/run /code
artifacts:
reports:
sast: gl-sast-report.json
tags:
- docker
- priv
# ====================
# test stage
# ====================
testLTS:
stage: test
script:
- npmci npm prepare
- npmci node install lts
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
testSTABLE:
testStable:
stage: test
script:
- npmci npm prepare
@ -85,6 +48,18 @@ testSTABLE:
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- priv
testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
release:
@ -103,19 +78,11 @@ release:
# ====================
codequality:
stage: metadata
image: docker:stable
allow_failure: true
services:
- docker:stable-dind
script:
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
- docker run
--env SOURCE_CODE="$PWD"
--volume "$PWD":/code
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
artifacts:
paths: [codeclimate.json]
- npmci command npm install -g tslint typescript
- npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags:
- docker
- priv
@ -131,13 +98,15 @@ trigger:
- notpriv
pages:
image: hosttoday/ht-docker-node:npmci
image: hosttoday/ht-docker-dbase:npmci
services:
- docker:stable-dind
stage: metadata
script:
- npmci command npm install -g typedoc typescript
- npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare
- npmci npm install
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
- npmci command tsdoc
tags:
- docker
- notpriv

View File

@ -1,25 +1,20 @@
# qenv
# @pushrocks/qenv
easy promised environments
## Availabililty
[![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/qenv)
[![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/qenv)
[![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/qenv)
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/qenv/)
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/qenv)
* [gitlab.com (source)](https://gitlab.com/pushrocks/qenv)
* [github.com (source mirror)](https://github.com/pushrocks/qenv)
* [docs (typedoc)](https://pushrocks.gitlab.io/qenv/)
## Status for master
[![build status](https://GitLab.com/pushrocks/qenv/badges/master/build.svg)](https://GitLab.com/pushrocks/qenv/commits/master)
[![coverage report](https://GitLab.com/pushrocks/qenv/badges/master/coverage.svg)](https://GitLab.com/pushrocks/qenv/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/qenv.svg)](https://www.npmjs.com/package/qenv)
[![Dependency Status](https://david-dm.org/pushrocks/qenv.svg)](https://david-dm.org/pushrocks/qenv)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/qenv/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/qenv/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/pushrocks/qenv/badges/code.svg)](https://www.bithound.io/github/pushrocks/qenv)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
[![build status](https://gitlab.com/pushrocks/qenv/badges/master/build.svg)](https://gitlab.com/pushrocks/qenv/commits/master)
[![coverage report](https://gitlab.com/pushrocks/qenv/badges/master/coverage.svg)](https://gitlab.com/pushrocks/qenv/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/qenv.svg)](https://www.npmjs.com/package/@pushrocks/qenv)
[![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/qenv/badge.svg)](https://snyk.io/test/npm/@pushrocks/qenv)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
## Usage
@ -40,7 +35,9 @@ import { Qenv } from 'qenv';
const myQenv = new Qenv('path/to/dir/where/qenv.yml/is/', 'path/to/dir/where/env.yml/is(');
```
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
For further information read the linked docs at the top of this readme.
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks)
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

View File

@ -2,5 +2,15 @@
"npmci": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
},
"gitzone": {
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"gitrepo": "qenv",
"shortDescription": "easy promised environments",
"npmPackagename": "@pushrocks/qenv",
"license": "MIT"
}
}
}

1353
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/qenv",
"version": "3.0.2",
"version": "4.0.5",
"private": false,
"description": "easy promised environments",
"main": "dist/index.js",
@ -25,16 +25,27 @@
},
"homepage": "https://gitlab.com/pushrocks/qenv#README",
"devDependencies": {
"@gitzone/tsbuild": "^2.0.22",
"@gitzone/tsrun": "^1.1.12",
"@gitzone/tstest": "^1.0.13",
"@pushrocks/tapbundle": "^3.0.5",
"@types/node": "^10.5.8",
"tslint": "^5.12.0",
"tslint-config-prettier": "^1.17.0"
"@gitzone/tsbuild": "^2.1.17",
"@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.24",
"@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^12.7.5",
"tslint": "^5.20.0",
"tslint-config-prettier": "^1.18.0"
},
"dependencies": {
"@pushrocks/smartfile": "^6.0.6",
"@pushrocks/smartlog": "^2.0.9"
}
"@pushrocks/smartfile": "^7.0.4",
"@pushrocks/smartlog": "^2.0.19"
},
"files": [
"ts/*",
"ts_web/*",
"dist/*",
"dist_web/*",
"dist_ts_web/*",
"assets/*",
"cli.js",
"npmextra.json",
"readme.md"
]
}

4
test/assets/env.json Normal file
View File

@ -0,0 +1,4 @@
{
"key1": "fromJson",
"key2" :"fromJson"
}

View File

@ -1,2 +0,0 @@
key1: fromYml
key2: fromYml

View File

@ -4,28 +4,26 @@ import * as qenv from '../ts/index';
process.env['key1'] = 'original';
let qenvTestObject: qenv.Qenv;
let testQenv: qenv.Qenv;
tap.test('should create a new class', async () => {
qenvTestObject = new qenv.Qenv(
path.join(__dirname, 'assets'),
path.join(__dirname, 'assets'),
false
);
expect(qenvTestObject).to.be.instanceof(qenv.Qenv);
testQenv = new qenv.Qenv(path.join(__dirname, 'assets'), path.join(__dirname, 'assets'), false);
expect(testQenv).to.be.instanceof(qenv.Qenv);
});
tap.test('key1 should be not be overwritten since it is already present', async () => {
expect(process.env.key1).to.equal('original');
expect(testQenv.getEnvVarRequired('key1')).to.equal('original');
expect(testQenv.getEnvVarOnDemand('key1')).to.equal('original');
});
tap.test('key2 should be read from Yml', async () => {
expect(process.env.key2).to.equal('fromYml');
expect(testQenv.getEnvVarRequired('key2')).to.equal('fromJson');
expect(testQenv.getEnvVarOnDemand('key2')).to.equal('fromJson');
});
tap.test('keyValueObjectArray should hold all retrieved values', async () => {
expect(qenvTestObject.keyValueObjectArray[0].value).to.equal('original');
expect(qenvTestObject.keyValueObjectArray[1].value).to.equal('fromYml');
expect(testQenv.keyValueObject.key1).to.equal('original');
expect(testQenv.keyValueObject.key2).to.equal('fromJson');
});
tap.start();

View File

@ -1,15 +1,15 @@
import * as plugins from './qenv.plugins';
plugins.smartlog.defaultLogger.enableConsole();
export interface IKeyValueObject {
key: string;
value: string;
}
/**
* class Qenv
* allows to make assertions about the environments while being more flexibel in how to meet them
*/
export class Qenv {
public requiredEnvVars: string[] = [];
public availableEnvVars: string[] = [];
public missingEnvVars: string[] = [];
public keyValueObjectArray: IKeyValueObject[] = [];
public keyValueObject: { [key: string]: any } = {};
public logger: plugins.smartlog.Smartlog;
// filePaths
@ -25,8 +25,14 @@ export class Qenv {
this.logger = loggerArg;
// lets make sure paths are absolute
this.qenvFilePathAbsolute = plugins.path.join(plugins.path.resolve(qenvFileBasePathArg), 'qenv.yml');
this.envFilePathAbsolute = plugins.path.join(plugins.path.resolve(envFileBasePathArg), 'env.yml');
this.qenvFilePathAbsolute = plugins.path.join(
plugins.path.resolve(qenvFileBasePathArg),
'qenv.yml'
);
this.envFilePathAbsolute = plugins.path.join(
plugins.path.resolve(envFileBasePathArg),
'env.json'
);
this.getRequiredEnvVars();
this.getAvailableEnvVars();
@ -40,100 +46,147 @@ export class Qenv {
console.error('However some Env variables could not be resolved:');
console.log(this.missingEnvVars);
if (failOnMissing) {
console.error('Exiting!');
this.logger.log('error', 'Exiting!');
process.exit(1);
} else {
this.logger.log('warn', 'qenv is not set to fail on missing environment variables');
}
}
}
public getEnvVar(envVarName): string {
return process.env[envVarName];
/**
* only gets an environment variable if it is required within a read qenv.yml file
* @param envVarName
*/
public getEnvVarRequired(envVarName): string {
return this.keyValueObject[envVarName];
}
public getEnvVarOnDemand(requiredEnvVar: string): string {
/**
* tries to get any env var even if it is not required
* @param wantedEnvVar
*/
public getEnvVarOnDemand(wantedEnvVar: string): string {
let envVarFromEnvironmentVariable: string;
let envVarFromEnvJsonFile: string;
let envVarFromDockerSecret: string;
let dockerSecretJson: string;
// env var check
if (process.env[wantedEnvVar]) {
this.availableEnvVars.push(wantedEnvVar);
envVarFromEnvironmentVariable = process.env[wantedEnvVar];
}
// env file check
// lets determine the actual env yml
let envYml;
let envJsonFileAsObject;
try {
envYml = plugins.smartfile.fs.toObjectSync(this.envFilePathAbsolute);
envJsonFileAsObject = plugins.smartfile.fs.toObjectSync(this.envFilePathAbsolute);
} catch (err) {
console.log("env file couldn't be found at " + this.envFilePathAbsolute);
envYml = {};
envJsonFileAsObject = {};
}
if (envJsonFileAsObject.hasOwnProperty(wantedEnvVar)) {
envVarFromEnvJsonFile = envJsonFileAsObject[wantedEnvVar];
}
let envVar: string;
let envFileVar: string;
let dockerSecret: string;
// env var check
if (process.env[requiredEnvVar]) {
this.availableEnvVars.push(requiredEnvVar);
envVar = process.env[requiredEnvVar];
}
// docker secret check
if (
plugins.smartfile.fs.isDirectory('/run') &&
plugins.smartfile.fs.isDirectory('/run/secrets') &&
plugins.smartfile.fs.fileExistsSync(`/run/secrets/${wantedEnvVar}`)
) {
envVarFromDockerSecret = plugins.smartfile.fs.toStringSync(`/run/secrets/${wantedEnvVar}`);
}
// env file check
if (envYml.hasOwnProperty(requiredEnvVar)) {
envFileVar = envYml[requiredEnvVar];
this.availableEnvVars.push(requiredEnvVar);
}
if (
plugins.smartfile.fs.isDirectory('/run') &&
plugins.smartfile.fs.isDirectory('/run/secrets') &&
plugins.smartfile.fs.fileExists(`/run/secrets/${requiredEnvVar}`)
) {
dockerSecret = plugins.smartfile.fs.toStringSync(`/run/secrets/${requiredEnvVar}`);
// docker secret.json
if (
plugins.smartfile.fs.isDirectory('/run') &&
plugins.smartfile.fs.isDirectory('/run/secrets')
) {
const availableSecrets = plugins.smartfile.fs.listAllItemsSync('/run/secrets');
for (const secret of availableSecrets) {
if (secret.includes('secret.json') && !envVarFromDockerSecret) {
const secretObject = plugins.smartfile.fs.toObjectSync(`/run/secrets/${secret}`);
envVarFromDockerSecret = secretObject[wantedEnvVar];
}
}
}
if ((envVar && envFileVar) || (envVar && dockerSecret) || (envFileVar && dockerSecret)) {
// warn if there is more than one candidate
const availableCcandidates: any[] = [];
[
envVarFromEnvironmentVariable,
envVarFromEnvJsonFile,
envVarFromDockerSecret,
dockerSecretJson
].forEach(candidate => {
if (candidate) {
availableCcandidates.push(candidate);
}
});
if (availableCcandidates.length > 1) {
this.logger.log(
'warn',
`found multiple candidates for ${wantedEnvVar} Choosing in the order of envVar, envFileVar, dockerSecret, dockerSecretJson`
);
console.log(availableCcandidates);
}
switch (true) {
case !!envVarFromEnvironmentVariable:
this.logger.log('ok', `found ${wantedEnvVar} as environment variable`);
return envVarFromEnvironmentVariable;
case !!envVarFromEnvJsonFile:
this.logger.log('ok', `found ${wantedEnvVar} as env.json variable`);
return envVarFromEnvJsonFile;
case !!envVarFromDockerSecret:
this.logger.log('ok', `found ${wantedEnvVar} as docker secret`);
return envVarFromDockerSecret;
case !!dockerSecretJson:
this.logger.log('ok', `found ${wantedEnvVar} as docker secret.json`);
return dockerSecretJson;
default:
this.logger.log(
'warn',
`found multiple candidates for ${requiredEnvVar} Choosing in the order of envVar, envFileVar, dockerSecret`
`could not find the wanted environment variable ${wantedEnvVar} anywhere`
);
}
let chosenVar: string = null;
if (envVar) {
chosenVar = envVar;
} else if (envFileVar) {
chosenVar = envFileVar;
} else if (dockerSecret) {
chosenVar = dockerSecret;
}
return chosenVar;
return;
}
}
/**
* gets the required env values
*/
private getRequiredEnvVars = () => {
let qenvFile: any = {};
if(plugins.smartfile.fs.fileExistsSync(this.qenvFilePathAbsolute)) {
let qenvFile: any = {};
if (plugins.smartfile.fs.fileExistsSync(this.qenvFilePathAbsolute)) {
qenvFile = plugins.smartfile.fs.toObjectSync(this.qenvFilePathAbsolute);
}
if (!qenvFile || !qenvFile.required || !Array.isArray(qenvFile.required)) {
this.logger.log('warn', `env File does not contain a 'required' Array!`);
this.logger.log(
'warn',
`qenv (promised environment): ./env.yml File does not contain a 'required' Array!`
);
} else {
for (const keyArg of Object.keys(qenvFile.required)) {
this.requiredEnvVars.push(qenvFile.required[keyArg]);
}
}
}
};
/**
* gets the available env vars
*/
private getAvailableEnvVars = () => {
for (const requiredEnvVar of this.requiredEnvVars) {
const chosenVar = this.getEnvVarOnDemand(requiredEnvVar);
if (chosenVar) {
this.availableEnvVars.push(requiredEnvVar);
process.env[requiredEnvVar] = chosenVar;
this.keyValueObjectArray.push({
key: requiredEnvVar,
value: chosenVar
});
this.keyValueObject[requiredEnvVar] = chosenVar;
}
}
}
};
/**
* gets missing env vars
@ -146,5 +199,5 @@ export class Qenv {
}
}
return missingEnvVars;
}
};
}

View File

@ -1,16 +1,10 @@
// native
import * as path from 'path';
export {
path
}
export { path };
// @pushrocks scope
import * as smartfile from '@pushrocks/smartfile';
import * as smartlog from '@pushrocks/smartlog';
export {
smartfile,
smartlog
}
export { smartfile, smartlog };