-
Notifications
You must be signed in to change notification settings - Fork 29
/
perseo
executable file
·247 lines (229 loc) · 9.78 KB
/
perseo
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env node
/*
* Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of perseo-fe
*
* perseo-fe is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* perseo-fe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with perseo-fe.
* If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::[[email protected]]
*/
'use strict';
var app = require('../lib/perseo'),
logger = require('logops'),
constants = require('../lib/constants'),
config = require('../config'),
context = {op: 'perseo', comp: constants.COMPONENT_NAME},
URL = require('url').URL;
logger.format = logger.formatters.pipe;
process.on('uncaughtException', function(ex){
logger.fatal(context, 'uncaughtException: %s', ex.message || ex , ex.stack && ex.stack.split('\n').join(','));
});
const fs = require('fs');
const path = require('path');
const SECRETS_DIR = process.env.SECRETS_DIR || '/run/secrets';
const secrets = {};
if (fs.existsSync(SECRETS_DIR)) {
const files = fs.readdirSync(SECRETS_DIR);
files.forEach(function (file) {
const fullPath = path.join(SECRETS_DIR, file);
const key = file;
try {
const data = fs.readFileSync(fullPath, 'utf8').toString().trim();
secrets[key] = data;
} catch (e) {
logger.error(e.message);
}
});
}
/**
* If an ENV is a protected Docker Secret extract the value of the secret data
*/
function getSecretData(key) {
const filepath = process.env[key + '_FILE'];
if (filepath) {
process.env[key] = secrets[path.parse(filepath).base] || process.env[key];
}
}
function loadConfiguration() {
var environmentValues = [
'PERSEO_ENDPOINT_HOST',
'PERSEO_ENDPOINT_PORT',
'PERSEO_MONGO_ENDPOINT',
'PERSEO_MONGO_REPLICASET',
'PERSEO_MONGO_AUTH_SOURCE',
'PERSEO_MONGO_USER',
'PERSEO_MONGO_PASSWORD',
'PERSEO_IS_MASTER',
'PERSEO_SLAVE_DELAY',
'PERSEO_CORE_URL',
'PERSEO_NEXT_URL',
'PERSEO_ORION_URL',
'PERSEO_PEP_URL',
'PERSEO_LOG_LEVEL',
'PERSEO_SMTP_PORT',
'PERSEO_SMTP_HOST',
'PERSEO_SMTP_SECURE',
'PERSEO_SMTP_AUTH_USER',
'PERSEO_SMTP_AUTH_PASS',
'PERSEO_SMTP_TLS_REJECTUNAUTHORIZED',
'PERSEO_SMS_URL',
'PERSEO_SMS_API_KEY',
'PERSEO_SMS_API_SECRET',
'PERSEO_SMS_FROM',
'PERSEO_SMPP_HOST',
'PERSEO_SMPP_PORT',
'PERSEO_SMPP_SYSTEMID',
'PERSEO_SMPP_PASSWORD',
'PERSEO_SMPP_FROM',
'PERSEO_SMPP_ENABLED',
'PERSEO_NOTICES_PATH',
'PERSEO_RULES_PATH',
'PERSEO_CAST_TYPES',
'PERSEO_MAX_RULES_BY_CORR',
'PERSEO_CHECK_NON_SIGNAL_BY_API'
];
const protectedVariables = [
'PERSEO_MONGO_USER',
'PERSEO_MONGO_PASSWORD',
'PERSEO_SMTP_AUTH_USER',
'PERSEO_SMTP_AUTH_PASS',
'PERSEO_SMS_API_KEY',
'PERSEO_SMS_API_SECRET',
'PERSEO_SMPP_SYSTEMID',
'PERSEO_SMPP_PASSWORD',
'PERSEO_AUTHENTICATION_USER',
'PERSEO_AUTHENTICATION_PASSWORD'
];
// Substitute Docker Secret Variables where set.
protectedVariables.forEach((key) => {
getSecretData(key);
});
environmentValues.forEach((key) => {
let value = process.env[key];
if (value) {
if (
key.endsWith('USER') ||
key.endsWith('PASS') ||
key.endsWith('KEY') ||
key.endsWith('PASSWORD') ||
key.endsWith('SYSTEMID') ||
key.endsWith('SECRET')
) {
value = '********';
}
logger.info('Setting %s to environment value: %s', key, value);
}
});
if (process.env.PERSEO_ENDPOINT_HOST) {
config.endpoint.host = process.env.PERSEO_ENDPOINT_HOST;
}
if (process.env.PERSEO_ENDPOINT_PORT) {
config.endpoint.port = process.env.PERSEO_ENDPOINT_PORT;
}
if (process.env.PERSEO_MONGO_ENDPOINT) {
config.mongo.url = 'mongodb://'+ (
process.env.PERSEO_MONGO_USER && process.env.PERSEO_MONGO_PASSWORD ? process.env.PERSEO_MONGO_USER + ':' + process.env.PERSEO_MONGO_PASSWORD + '@' : '') +
process.env.PERSEO_MONGO_ENDPOINT + '/cep' + (
(process.env.PERSEO_MONGO_REPLICASET ? '?replicaSet=' + process.env.PERSEO_MONGO_REPLICASET : '') +
(process.env.PERSEO_MONGO_AUTH_SOURCE ? (process.env.PERSEO_MONGO_REPLICASET ? '&' : '?') + 'authSource=' + process.env.PERSEO_MONGO_AUTH_SOURCE : ''));
config.orionDb.url = 'mongodb://' + (
process.env.PERSEO_MONGO_USER && process.env.PERSEO_MONGO_PASSWORD ? process.env.PERSEO_MONGO_USER + ':' + process.env.PERSEO_MONGO_PASSWORD + '@' : '') +
process.env.PERSEO_MONGO_ENDPOINT + '/orion' + (
(process.env.PERSEO_MONGO_REPLICASET ? '?replicaSet=' + process.env.PERSEO_MONGO_REPLICASET : '') +
(process.env.PERSEO_MONGO_AUTH_SOURCE ? (process.env.PERSEO_MONGO_REPLICASET ? '&' : '?') + 'authSource=' + process.env.PERSEO_MONGO_AUTH_SOURCE : ''));
}
if (process.env.PERSEO_IS_MASTER === 'true') {
config.isMaster = true;
} else if (process.env.PERSEO_IS_MASTER === 'false') {
config.isMaster = false;
}
if (process.env.PERSEO_SLAVE_DELAY) {
config.slaveDelay = process.env.PERSEO_SLAVE_DELAY;
}
if (process.env.PERSEO_CORE_URL) {
config.perseoCore.rulesURL = process.env.PERSEO_CORE_URL + '/perseo-core/rules';
config.perseoCore.noticesURL = process.env.PERSEO_CORE_URL + '/perseo-core/events';
}
if (process.env.PERSEO_NEXT_URL) {
config.nextCore.rulesURL = process.env.PERSEO_NEXT_URL + '/perseo-core/rules';
config.nextCore.noticesURL = process.env.PERSEO_NEXT_URL + '/perseo-core/events';
}
if (process.env.PERSEO_ORION_URL) {
config.orion.URL = new URL(process.env.PERSEO_ORION_URL);
}
if (process.env.PERSEO_PEP_URL) {
config.pep.URL = new URL(process.env.PERSEO_PEP_URL);
}
if (process.env.PERSEO_LOG_LEVEL) {
config.logLevel = process.env.PERSEO_LOG_LEVEL;
}
config.smtp = config.smtp || {};
config.smtp.port = process.env.PERSEO_SMTP_PORT || config.smtp.port;
config.smtp.host = process.env.PERSEO_SMTP_HOST || config.smtp.host;
if (process.env.PERSEO_SMTP_SECURE === 'true') {
config.smtp.secure = true;
} else if (process.env.PERSEO_SMTP_SECURE === 'false') {
config.smtp.secure = false;
}
config.smtp.auth = config.smtp.auth || {};
config.smtp.auth.user = process.env.PERSEO_SMTP_AUTH_USER || config.smtp.auth.user;
config.smtp.auth.pass = process.env.PERSEO_SMTP_AUTH_PASS || config.smtp.auth.pass;
if (!config.smtp.auth.user && !config.smtp.auth.user) {
delete config.smtp.auth;
}
if (process.env.PERSEO_SMTP_TLS_REJECTUNAUTHORIZED === 'true') {
config.smtp.tls.rejectUnauthorized = true;
} else if (process.env.PERSEO_SMTP_TLS_REJECTUNAUTHORIZED === 'false') {
config.smtp.tls.rejectUnauthorized = false;
}
config.sms = config.sms || {};
config.sms.URL = process.env.PERSEO_SMS_URL || config.sms.URL;
config.sms.API_KEY = process.env.PERSEO_SMS_API_KEY || config.sms.API_KEY;
config.sms.API_SECRET = process.env.PERSEO_SMS_API_SECRET || config.sms.API_SECRET;
config.sms.from = process.env.PERSEO_SMS_FROM || config.sms.from;
config.smpp = config.smpp || {};
config.smpp.host = process.env.PERSEO_SMPP_HOST || config.smpp.host;
config.smpp.port = process.env.PERSEO_SMPP_PORT || config.smpp.port;
config.smpp.systemid = process.env.PERSEO_SMPP_SYSTEMID || config.smpp.systemid;
config.smpp.password = process.env.PERSEO_SMPP_PASSWORD || config.smpp.password;
config.smpp.from = process.env.PERSEO_SMPP_FROM || config.smpp.from;
config.smpp.enabled = process.env.PERSEO_SMPP_ENABLED || config.smpp.enabled;
config.endpoint.noticesPath = process.env.PERSEO_NOTICES_PATH || config.endpoint.noticesPath;
config.endpoint.rulesPath = process.env.PERSEO_RULES_PATH || config.endpoint.rulesPath;
config.castTypes = process.env.PERSEO_CAST_TYPES || config.castTypes;
config.authentication = config.authentication || {};
config.authentication.host = process.env.PERSEO_AUTHENTICATION_HOST || config.authentication.host;
config.authentication.port = process.env.PERSEO_AUTHENTICATION_PORT || config.authentication.port;
config.authentication.user = process.env.PERSEO_AUTHENTICATION_USER || config.authentication.user;
config.authentication.password = process.env.PERSEO_AUTHENTICATION_PASSWORD || config.authentication.password;
config.authentication.service = process.env.PERSEO_AUTHENTICATION_SERVICE || config.authentication.service;
if (process.env.PERSEO_MAX_RULES_BY_CORR) {
config.maxRulesByCorr = process.env.PERSEO_MAX_RULES_BY_CORR;
}
if (process.env.PERSEO_CHECK_NON_SIGNAL_BY_API) {
config.nonSignalByAPI = process.env.PERSEO_CHECK_NON_SIGNAL_BY_API;
}
}
loadConfiguration();
app.start(function (err) {
logger.info(context, 'perseo.bin: starting perseo');
if (err) {
logger.fatal(context, err.message);
process.exit(-1);
}
logger.info(context, 'perseo.bin: perseo started');
});