1 | "use strict";
|
2 |
|
3 | var fs = require("fs");
|
4 | var traverse = require("traverse");
|
5 |
|
6 | var generalLogger = require('./logger')();
|
7 |
|
8 | var envVarRegex = /\$\{([^}]+)\}/;
|
9 |
|
10 | module.exports = function(file) {
|
11 | var globalAuth = {};
|
12 | try {
|
13 | globalAuth = JSON.parse(fs.readFileSync(file));
|
14 |
|
15 | traverse(globalAuth).forEach(function(val) {
|
16 | if ("string" === typeof val) {
|
17 | var match, modified;
|
18 | while ((match = envVarRegex.exec(val)) !== null) {
|
19 | var envName = match[1];
|
20 | var envVal = process.env[envName];
|
21 | if (envVal === undefined) {
|
22 | generalLogger.warn("-- Authentication file referenced var ${" + envName + "}, which was not present in environment");
|
23 | envVal = "";
|
24 | }
|
25 | val = val.substring(0, match.index) + envVal + val.substring(match.index + match[0].length);
|
26 | modified = true;
|
27 | }
|
28 |
|
29 | if (modified === true) {
|
30 | this.update(val);
|
31 | }
|
32 | }
|
33 | });
|
34 | }
|
35 | catch(e){
|
36 | generalLogger.warn("-- Authentication file not found in " + file +
|
37 | ". You may want to create your own. You can also define the place where the credential file will be located " +
|
38 | " by editing the general configuration property 'authentication-file-path' -- ");
|
39 | }
|
40 |
|
41 | return globalAuth;
|
42 | };
|