aboutsummaryrefslogtreecommitdiffstats
path: root/lib/config.js
blob: f74faa90b031737017eea5656e6a005f21236321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Copyright(c) 2017 AT&T Intellectual Property. All rights reserved. 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License.

You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, 
software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and limitations under the License.
*/

/*
 * Dispatcher configuration
 * Configuration may come from environment variables, configuration file, or defaults,
 * in that order of precedence.
 * The configuration file is optional.
 * If present, the configuration file is a UTF-8 serialization of a JSON object.
 * 
 * --------------------------------------------------------------------------------------
 * | JSON property         | Environment variable   | Required? | Default               |
 * --------------------------------------------------------------------------------------
 * | foreground            | FOREGROUND             | Yes       | true                  |
 * --------------------------------------------------------------------------------------
 * | logLevel              | LOG_LEVEL              | Yes       | "INFO"                |
 * --------------------------------------------------------------------------------------
 * | listenHost            | LISTEN_HOST            | Yes       | "0.0.0.0"             |
 * --------------------------------------------------------------------------------------
 * | listenPort            | LISTEN_PORT            | Yes       | 8443                  |
 * --------------------------------------------------------------------------------------
 * | ssl.pfxFile           | SSL_PFX_FILE           | No        | none                  |
 * --------------------------------------------------------------------------------------
 * | ssl.pfxPassFile       | SSL_PFX_PASSFILE       | No        | none                  |
 * --------------------------------------------------------------------------------------
 * | cloudify.url          | CLOUDIFY_URL           | Yes       | none                  |
 * --------------------------------------------------------------------------------------
 * | cloudify.user         | CLOUDIFY_USER          | No        | none                  |
 * --------------------------------------------------------------------------------------
 * | cloudify.password     | CLOUDIFY_PASSWORD      | No        | none                  |
 * --------------------------------------------------------------------------------------
 * | inventory.url         | INVENTORY_URL          | Yes       | http://inventory:8080 |
 * --------------------------------------------------------------------------------------
 * | inventory.user        | INVENTORY_USER         | No        | none                  |
 * --------------------------------------------------------------------------------------
 * | inventory.password    | INVENTORY_PASSWORD     | No        | none                  |
 * --------------------------------------------------------------------------------------
 * 
 */
"use strict";

const fs = require("fs");
const utils = require("./utils");

const DEFAULT_FOREGROUND = true;
const DEFAULT_LISTEN_PORT = 8443;
const DEFAULT_LISTEN_HOST = "0.0.0.0";
const DEFAULT_LOG_LEVEL = "INFO";
const DEFAULT_INVENTORY_URL = "http://inventory:8080";

/* Check configuration for completeness */
const findMissingConfig = function(cfg) {
	const requiredProps = ['logLevel', 'listenHost', 'listenPort', 'cloudify.url', 'inventory.url'];
	return requiredProps.filter(function(p){return !utils.hasProperty(cfg,p);});	
};

/** Reads configuration-related files and returns a configuration object
 *  Sets some defaults
 *  Throws I/O errors, JSON parsing errors, and an error if required config elements are missing
 */
exports.configure = function(configFile) {
	var cfg = {};
    
	/* If there's a config file, read it */
	if (configFile) {
		cfg = JSON.parse(fs.readFileSync(configFile, 'utf8').trim());
	}
	
	/* Set config values */
	cfg.foreground = process.env['FOREGROUND'] || cfg.foreground || DEFAULT_FOREGROUND;
	cfg.logLevel = process.env['LOG_LEVEL'] || cfg.logLevel || DEFAULT_LOG_LEVEL;
	cfg.listenHost = process.env['LISTEN_HOST'] || cfg.listenHost || DEFAULT_LISTEN_HOST;
	cfg.listenPort = (process.env['LISTEN_PORT'] && parseInt(process.env['LISTEN_PORT'])) || cfg.listenPort || DEFAULT_LISTEN_PORT;
	
	if (process.env['SSL_PFX_FILE']) {
		if (!cfg.ssl) {
			cfg.ssl = {};
		}
		cfg.ssl.pfxFile = process.env['SSL_PFX_FILE'];
		cfg.ssl.pfxPassFile = process.env['SSL_PFX_PASS_FILE'] || cfg.ssl.pfxPassFile;
	}
	
	if (!cfg.cloudify) {
		cfg.cloudify = {};
	}
	cfg.cloudify.url = process.env['CLOUDIFY_URL'] || cfg.cloudify.url;
	cfg.cloudify.user = process.env['CLOUDIFY_USER'] || cfg.cloudify.user;
	cfg.cloudify.password = process.env['CLOUDIFY_PASSWORD'] || cfg.cloudify.password;
	
	if (!cfg.inventory) {
		cfg.inventory = {};
	}
	cfg.inventory.url = process.env['INVENTORY_URL'] || cfg.inventory.url || DEFAULT_INVENTORY_URL;
	cfg.inventory.user = process.env['INVENTORY_USER'] || cfg.inventory.user;
	cfg.inventory.password = process.env['INVENTORY_PASSWORD'] || cfg.inventory.password;
	cfg.locations = {};

	/* If https params are present, read in the cert/passphrase */
	if (cfg.ssl && cfg.ssl.pfxFile) {
		cfg.ssl.pfx = fs.readFileSync(cfg.ssl.pfxFile);
		if (cfg.ssl.pfxPassFile) {
			cfg.ssl.passphrase = fs.readFileSync(cfg.ssl.pfxPassFile,'utf8').trim();
		}
	}

	const missing = findMissingConfig(cfg);
	if (missing.length > 0) {
		throw new Error ("Required configuration elements missing: " + missing.join(','));
		cfg = null;
	}

	return cfg;   
};

/** Read locations file
*/
exports.getLocations = function(locationsFile) {
	let locations = {};

	try {
		locations = JSON.parse(fs.readFileSync(locationsFile));
	}
	catch (e) {
		locations = {};
	}
	return locations;
}