forked from interledger-deprecated/ilp-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.js
132 lines (108 loc) · 4.63 KB
/
env.js
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
const fs = require('fs')
const crypto = require('crypto')
const _ = require('lodash')
const Config = require('five-bells-shared').Config
const envVars = {}
const generateSecret = (secret, name) => {
return crypto.createHmac('sha256', secret).update(name).digest('base64')
}
const getVar = (name, def = '') => {
return process.env[name] || envVars[name] || def
}
function getConfigFilePath () {
let file = ''
// was an alternative path to the config file specified?
if (getVar('API_CONFIG_FILE')) {
// use alternative file path
file = getVar('API_CONFIG_FILE')
} else {
// use default file path
file = process.env.NODE_ENV === 'test' ? '../test.env.list' : '../env.list'
}
return file
}
function normalizeEnv (noExit) {
// get env vars from the env file
try {
const file = getConfigFilePath()
const envFile = fs.readFileSync(file)
const fileLines = envFile.toString().split('\n')
fileLines.forEach(line => {
if (!line) return
const keyPairArray = line.split('=')
envVars[keyPairArray[0]] = keyPairArray.slice(1).join('=').trim()
})
} catch (err) {
// Both env.list and environment variables are missing
if (!process.env.API_DB_URI && !process.env.DB_URI) {
console.log('DB_URI is not set. Did you configure ilp-kit? \n'
+ 'To configure ilp-kit, run: npm run configure')
noExit || process.exit(1)
}
}
// make sure versioning matches between file and this kit
const version = getVar('ILP_KIT_CLI_VERSION', '0.0.0')
const supportedVersion = require('./package.json')
.dependencies['ilp-kit-cli']
.replace(/^[\^\~]/, '')
if (+supportedVersion.split('.')[0] !== +version.split('.')[0]) {
console.log('`env.list` version (' + version
+ ') is older than supported version (' + supportedVersion
+ '). Back up your env.list and run `npm run configure` to update.')
noExit || process.exit(1)
}
// backwards compatibility
if (!envVars.DB_URI) {
envVars.DB_URI = getVar('API_DB_URI')
}
// Add API env vars
if (!envVars.API_DB_URI) {
envVars.API_DB_URI = getVar('DB_URI')
}
// Add this so it can be announced in webfinger
envVars.ILP_KIT_VERSION = require('./package.json').version
// Secrets
const secret = getVar('API_SECRET', 'secret') // 'secret' for tests
envVars.API_ED25519_SECRET_KEY = generateSecret(secret, 'API_ED25519')
envVars.CONNECTOR_ED25519_SECRET_KEY = generateSecret(secret, 'CONNECTOR_ED25519')
// Add ledger env vars
if (!getVar('API_LEDGER_URI')) {
const clientPublicPort = getVar('CLIENT_PUBLIC_PORT') || getVar('CLIENT_PORT', '80')
const ledgerPublicPort = (clientPublicPort !== '80' && clientPublicPort !== '443') ? ':' + clientPublicPort : ''
envVars.LEDGER_DB_URI = getVar('LEDGER_DB_URI') || getVar('DB_URI')
envVars.LEDGER_HOSTNAME = getVar('LEDGER_HOSTNAME') || getVar('API_HOSTNAME', 'localhost')
envVars.LEDGER_PORT = getVar('LEDGER_PORT') || Number(getVar('API_PORT')) + 1
envVars.LEDGER_PUBLIC_PORT = getVar('LEDGER_PUBLIC_PORT', clientPublicPort)
envVars.LEDGER_PUBLIC_PATH = getVar('LEDGER_PUBLIC_PATH', 'ledger')
envVars.API_LEDGER_ADMIN_USER = getVar('LEDGER_ADMIN_USER') || getVar('LEDGER_ADMIN_USER', 'admin')
envVars.API_LEDGER_ADMIN_PASS = getVar('LEDGER_ADMIN_PASS') || getVar('LEDGER_ADMIN_PASS', 'admin')
envVars.LEDGER_SECRET = generateSecret(secret, 'LEDGER_SECRET')
envVars.LEDGER_ENABLE = true
// is the API/LEDGER callable via http or https?
const API_PUBLIC_HTTPS = Config.castBool(getVar('API_PUBLIC_HTTPS'), true)
const LEDGER_PUBLIC_HTTPS = Config.castBool(getVar('LEDGER_PUBLIC_HTTPS'), API_PUBLIC_HTTPS)
envVars.LEDGER_PUBLIC_HTTPS = LEDGER_PUBLIC_HTTPS
envVars.API_PROTOCOL = API_PUBLIC_HTTPS ? 'https:' : 'http:'
envVars.API_LEDGER_URI = 'http://' + (getVar('API_PRIVATE_HOSTNAME') || getVar('LEDGER_HOSTNAME')) + ':' + getVar('LEDGER_PORT')
envVars.API_LEDGER_PUBLIC_URI = envVars.API_PROTOCOL + '//' + getVar('LEDGER_HOSTNAME') + ledgerPublicPort + '/' + getVar('LEDGER_PUBLIC_PATH')
}
// Set envVars in environment
_.each(envVars, (envVar, index) => {
if (!process.env[index]) process.env[index] = envVar
})
}
function changeAdminPass (newPassword) {
const file = getConfigFilePath()
const envFile = fs.readFileSync(file, 'utf-8')
// Replace the password
const newEnvFile = envFile.replace(/^LEDGER_ADMIN_PASS=.*$/m, `LEDGER_ADMIN_PASS=${newPassword}`)
// Save the file
fs.writeFileSync(file, newEnvFile, 'utf-8')
// Set env
process.env['LEDGER_ADMIN_PASS'] = newPassword
process.env['API_LEDGER_ADMIN_PASS'] = newPassword
}
module.exports = {
normalizeEnv,
changeAdminPass
}