Skip to content

Commit

Permalink
feat: environment variables support in configuration file (#2199)
Browse files Browse the repository at this point in the history
* feat:Environment variables support in configuration file

* add changeset

* remove fixes typo

Co-authored-by: amit <amit@enso.security>
  • Loading branch information
amitgilad3 and amit-enso committed Apr 25, 2021
1 parent 04931c9 commit 1810ed0
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 26 deletions.
11 changes: 11 additions & 0 deletions .changeset/healthy-poets-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@verdaccio/config': patch
---

Feature

- add option to set storage from environment variable VERDACCIO_STORAGE_PATH

#### Related tickets

https://github.com/verdaccio/verdaccio/issues/1681
4 changes: 4 additions & 0 deletions docs/env.variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ The default header to identify the protocol is `X-Forwarded-Proto`, but there ar
```
$ VERDACCIO_FORWARDED_PROTO=CloudFront-Forwarded-Proto verdaccio --listen 5000
```

#### VERDACCIO_STORAGE_PATH

By default, the storage is taken from config file, but using this variable allows to set it from environment variable.
2 changes: 1 addition & 1 deletion packages/config/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Config implements AppConfig {

public constructor(config: ConfigRuntime) {
const self = this;
this.storage = config.storage;
this.storage = process.env.VERDACCIO_STORAGE_PATH || config.storage;
this.config_path = config.config_path;
this.plugins = config.plugins;
this.security = _.merge(defaultSecurity, config.security);
Expand Down
9 changes: 1 addition & 8 deletions packages/config/test/config-parsing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import path from 'path';
import { parseConfigFile } from '../src';
import { parseConfigurationFile } from './utils';

describe('Package access utilities', () => {
const parseConfigurationFile = (conf) => {
const { name, ext } = path.parse(conf);
const format = ext.startsWith('.') ? ext.substring(1) : 'yaml';

return path.join(__dirname, `./partials/config/${format}/${name}.${format}`);
};

describe('JSON format', () => {
test('parse default.json', () => {
const config = parseConfigFile(parseConfigurationFile('default.json'));
Expand Down
27 changes: 27 additions & 0 deletions packages/config/test/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ROLES,
WEB_TITLE,
} from '../src';
import { parseConfigurationFile } from './utils';

const resolveConf = (conf) => {
const { name, ext } = path.parse(conf);
Expand Down Expand Up @@ -80,6 +81,32 @@ describe('check basic content parsed file', () => {
checkDefaultConfPackages(config);
});

test('should set storage to value set in VERDACCIO_STORAGE_PATH environment variable', () => {
const storageLocation = '/tmp/verdaccio';
process.env.VERDACCIO_STORAGE_PATH = storageLocation;
const config = new Config(parseConfigFile(resolveConf('default')));
expect(config.storage).toBe(storageLocation);
delete process.env.VERDACCIO_STORAGE_PATH;
});

test('should set storage path to VERDACCIO_STORAGE_PATH if both config and env are set', () => {
const storageLocation = '/tmp/verdaccio';
process.env.VERDACCIO_STORAGE_PATH = storageLocation;
const config = new Config(parseConfigFile(parseConfigurationFile('storage')));
expect(config.storage).toBe(storageLocation);
delete process.env.VERDACCIO_STORAGE_PATH;
});

test('should take storage from environment variable if not exists in configs', () => {
const storageLocation = '/tmp/verdaccio';
process.env.VERDACCIO_STORAGE_PATH = storageLocation;
const defaultConfig = parseConfigFile(resolveConf('default'));
delete defaultConfig.storage;
const config = new Config(defaultConfig);
expect(config.storage).toBe(storageLocation);
delete process.env.VERDACCIO_STORAGE_PATH;
});

test('parse docker.yaml', () => {
const config = new Config(parseConfigFile(resolveConf('docker')));
checkDefaultUplink(config);
Expand Down
9 changes: 1 addition & 8 deletions packages/config/test/package-access.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import path from 'path';
import _ from 'lodash';

import {
Expand All @@ -7,15 +6,9 @@ import {
PACKAGE_ACCESS,
} from '../src/package-access';
import { parseConfigFile } from '../src';
import { parseConfigurationFile } from './utils';

describe('Package access utilities', () => {
const parseConfigurationFile = (conf) => {
const { name, ext } = path.parse(conf);
const format = ext.startsWith('.') ? ext.substring(1) : 'yaml';

return path.join(__dirname, `./partials/config/${format}/${name}.${format}`);
};

describe('normalisePackageAccess', () => {
test('should test basic conversion', () => {
const { packages } = parseConfigFile(parseConfigurationFile('pkgs-basic'));
Expand Down
7 changes: 7 additions & 0 deletions packages/config/test/partials/config/yaml/storage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
storage: './storage_default_storage'

logs:
- type: stdout
format: pretty
level: warn
10 changes: 1 addition & 9 deletions packages/config/test/uplinks.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import path from 'path';

import { hasProxyTo, sanityCheckUplinksProps, uplinkSanityCheck } from '../src/uplinks';
import { normalisePackageAccess, parseConfigFile } from '../src';
import { parseConfigurationFile } from './utils';

describe('Uplinks Utilities', () => {
const parseConfigurationFile = (conf) => {
const { name, ext } = path.parse(conf);
const format = ext.startsWith('.') ? ext.substring(1) : 'yaml';

return path.join(__dirname, `./partials/config/${format}/${name}.${format}`);
};

describe('uplinkSanityCheck', () => {
test('should test basic conversion', () => {
const uplinks = uplinkSanityCheck(
Expand Down
1 change: 1 addition & 0 deletions packages/config/test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { parseConfigurationFile } from './parse-configuration-file';
8 changes: 8 additions & 0 deletions packages/config/test/utils/parse-configuration-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import path from 'path';

export const parseConfigurationFile = (conf: string) => {
const { name, ext } = path.parse(conf);
const format = ext.startsWith('.') ? ext.substring(1) : 'yaml';

return path.join(__dirname, `../partials/config/${format}/${name}.${format}`);
};

0 comments on commit 1810ed0

Please sign in to comment.