• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

telefonicaid / iotagent-json / 15172033588

21 May 2025 08:34PM UTC coverage: 79.881% (-0.2%) from 80.121%
15172033588

push

github

web-flow
Merge pull request #877 from telefonicaid/task/add_headers_to_http_commands

add test about group command with extra headers

516 of 729 branches covered (70.78%)

Branch coverage included in aggregate %.

1100 of 1294 relevant lines covered (85.01%)

139.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.74
/lib/configService.js
1
/*
2
 * Copyright 2016 Telefonica Investigación y Desarrollo, S.A.U
3
 *
4
 * This file is part of iotagent-json
5
 *
6
 * iotagent-json is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU Affero General Public License as
8
 * published by the Free Software Foundation, either version 3 of the License,
9
 * or (at your option) any later version.
10
 *
11
 * iotagent-json is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 * See the GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public
17
 * License along with iotagent-json.
18
 * If not, seehttp://www.gnu.org/licenses/.
19
 *
20
 * For those usages not covered by the GNU Affero General Public License
21
 * please contact with::[contacto@tid.es]
22
 */
23

24
let config = {};
1✔
25
const fs = require('fs');
1✔
26
let logger = require('logops');
1✔
27
const iotAgentLib = require('iotagent-node-lib');
1✔
28

29
function anyIsSet(variableSet) {
30
    for (let i = 0; i < variableSet.length; i++) {
801✔
31
        if (process.env[variableSet[i]]) {
9,047✔
32
            return true;
3✔
33
        }
34
    }
35

36
    return false;
798✔
37
}
38

39
/**
40
 * For a parameter pointing to a file, check the file exists
41
 *
42
 * @param {string} path        Path to the file
43
 */
44
function fileExists(path) {
45
    try {
5✔
46
        fs.statSync(path);
5✔
47
        logger.debug(path + ' - File exists.');
5✔
48
    } catch (e) {
49
        logger.fatal(path + ' - File does not exist.');
×
50
        throw Error(path + ' - File does not exist.');
×
51
    }
52
}
53

54
function processEnvironmentVariables() {
55
    const environmentVariables = [
267✔
56
        'IOTA_MQTT_PROTOCOL',
57
        'IOTA_MQTT_HOST',
58
        'IOTA_MQTT_PORT',
59
        'IOTA_MQTT_CA',
60
        'IOTA_MQTT_CERT',
61
        'IOTA_MQTT_KEY',
62
        'IOTA_MQTT_REJECT_UNAUTHORIZED',
63
        'IOTA_MQTT_USERNAME',
64
        'IOTA_MQTT_PASSWORD',
65
        'IOTA_MQTT_QOS',
66
        'IOTA_MQTT_RETAIN',
67
        'IOTA_MQTT_RETRIES',
68
        'IOTA_MQTT_RETRY_TIME',
69
        'IOTA_MQTT_KEEPALIVE',
70
        'IOTA_MQTT_AVOID_LEADING_SLASH',
71
        'IOTA_MQTT_CLEAN',
72
        'IOTA_MQTT_CLIENT_ID',
73
        'IOTA_MQTT_DISABLED',
74
        'IOTA_MQTT_SUBSCRIBE_BATCH_SIZE',
75
        'IOTA_AMQP_HOST',
76
        'IOTA_AMQP_PORT',
77
        'IOTA_AMQP_USERNAME',
78
        'IOTA_AMQP_PASSWORD',
79
        'IOTA_AMQP_EXCHANGE',
80
        'IOTA_AMQP_QUEUE',
81
        'IOTA_AMQP_DURABLE',
82
        'IOTA_AMQP_RETRIES',
83
        'IOTA_AMQP_RETRY_TIME',
84
        'IOTA_AMQP_DISABLED',
85
        'IOTA_HTTP_HOST',
86
        'IOTA_HTTP_PORT',
87
        'IOTA_HTTP_TIMEOUT',
88
        'IOTA_HTTP_KEY',
89
        'IOTA_HTTP_CERT',
90
        'IOTA_CONFIG_RETRIEVAL',
91
        'IOTA_DEFAULT_KEY',
92
        'IOTA_DEFAULT_TRANSPORT'
93
    ];
94
    const mqttVariables = [
267✔
95
        'IOTA_MQTT_PROTOCOL',
96
        'IOTA_MQTT_HOST',
97
        'IOTA_MQTT_PORT',
98
        'IOTA_MQTT_CA',
99
        'IOTA_MQTT_CERT',
100
        'IOTA_MQTT_KEY',
101
        'IOTA_MQTT_REJECT_UNAUTHORIZED',
102
        'IOTA_MQTT_USERNAME',
103
        'IOTA_MQTT_PASSWORD',
104
        'IOTA_MQTT_QOS',
105
        'IOTA_MQTT_RETAIN',
106
        'IOTA_MQTT_RETRIES',
107
        'IOTA_MQTT_RETRY_TIME',
108
        'IOTA_MQTT_KEEPALIVE',
109
        'IOTA_MQTT_AVOID_LEADING_SLASH',
110
        'IOTA_MQTT_CLEAN',
111
        'IOTA_MQTT_CLIENT_ID',
112
        'IOTA_MQTT_DISABLED',
113
        'IOTA_MQTT_SUBSCRIBE_BATCH_SIZE'
114
    ];
115
    const amqpVariables = [
267✔
116
        'IOTA_AMQP_HOST',
117
        'IOTA_AMQP_PORT',
118
        'IOTA_AMQP_USERNAME',
119
        'IOTA_AMQP_PASSWORD',
120
        'IOTA_AMQP_EXCHANGE',
121
        'IOTA_AMQP_QUEUE',
122
        'IOTA_AMQP_DURABLE',
123
        'IOTA_AMQP_RETRIES',
124
        'IOTA_AMQP_RETRY_TIME',
125
        'IOTA_AMQP_DISABLED'
126
    ];
127
    const httpVariables = ['IOTA_HTTP_HOST', 'IOTA_HTTP_PORT', 'IOTA_HTTP_TIMEOUT', 'IOTA_HTTP_KEY', 'IOTA_HTTP_CERT'];
267✔
128

129
    const protectedVariables = [
267✔
130
        'IOTA_MQTT_KEY',
131
        'IOTA_MQTT_USERNAME',
132
        'IOTA_MQTT_PASSWORD',
133
        'IOTA_AMQP_USERNAME',
134
        'IOTA_AMQP_PASSWORD'
135
    ];
136
    // Substitute Docker Secret Variables where set.
137
    protectedVariables.forEach((key) => {
267✔
138
        iotAgentLib.configModule.getSecretData(key);
1,335✔
139
    });
140
    environmentVariables.forEach((key) => {
267✔
141
        let value = process.env[key];
9,879✔
142
        if (value) {
9,879✔
143
            if (key.endsWith('USERNAME') || key.endsWith('PASSWORD') || key.endsWith('KEY')) {
30✔
144
                value = '********';
6✔
145
            }
146
            logger.info('Setting %s to environment value: %s', key, value);
30✔
147
        }
148
    });
149

150
    if (process.env.IOTA_CONFIG_RETRIEVAL) {
267!
151
        config.configRetrieval = process.env.IOTA_CONFIG_RETRIEVAL;
×
152
    }
153
    if (process.env.IOTA_DEFAULT_KEY) {
267!
154
        config.defaultKey = process.env.IOTA_DEFAULT_KEY;
×
155
    }
156
    if (process.env.IOTA_DEFAULT_TRANSPORT) {
267!
157
        config.defaultTransport = process.env.IOTA_DEFAULT_TRANSPORT;
×
158
    }
159

160
    if (anyIsSet(mqttVariables)) {
267✔
161
        config.mqtt = config.mqtt || {};
1!
162
    }
163

164
    if (process.env.IOTA_MQTT_PROTOCOL) {
267✔
165
        config.mqtt.protocol = process.env.IOTA_MQTT_PROTOCOL;
1✔
166
    }
167

168
    if (process.env.IOTA_MQTT_HOST) {
267✔
169
        config.mqtt.host = process.env.IOTA_MQTT_HOST;
1✔
170
    }
171

172
    if (process.env.IOTA_MQTT_PORT) {
267✔
173
        config.mqtt.port = process.env.IOTA_MQTT_PORT;
1✔
174
    }
175

176
    if (process.env.IOTA_MQTT_CA) {
267✔
177
        fileExists(process.env.IOTA_MQTT_CA);
1✔
178
        config.mqtt.ca = process.env.IOTA_MQTT_CA;
1✔
179
    }
180

181
    if (process.env.IOTA_MQTT_CERT) {
267✔
182
        fileExists(process.env.IOTA_MQTT_CERT);
1✔
183
        config.mqtt.cert = process.env.IOTA_MQTT_CERT;
1✔
184
    }
185

186
    if (process.env.IOTA_MQTT_KEY) {
267✔
187
        fileExists(process.env.IOTA_MQTT_KEY);
1✔
188
        config.mqtt.key = process.env.IOTA_MQTT_KEY;
1✔
189
    }
190

191
    // Since default is true, need to be able accept "false" as
192
    // a valid Environment variable
193
    if (process.env.IOTA_MQTT_REJECT_UNAUTHORIZED !== undefined) {
267✔
194
        config.mqtt.rejectUnauthorized = process.env.IOTA_MQTT_REJECT_UNAUTHORIZED.trim().toLowerCase() === 'true';
1✔
195
    }
196

197
    if (process.env.IOTA_MQTT_USERNAME) {
267✔
198
        config.mqtt.username = process.env.IOTA_MQTT_USERNAME;
1✔
199
    }
200

201
    if (process.env.IOTA_MQTT_PASSWORD) {
267✔
202
        config.mqtt.password = process.env.IOTA_MQTT_PASSWORD;
1✔
203
    }
204

205
    if (process.env.IOTA_MQTT_QOS) {
267✔
206
        config.mqtt.qos = process.env.IOTA_MQTT_QOS;
1✔
207
    }
208

209
    if (process.env.IOTA_MQTT_RETAIN) {
267✔
210
        config.mqtt.retain = process.env.IOTA_MQTT_RETAIN.trim().toLowerCase() === 'true';
1✔
211
    }
212

213
    if (process.env.IOTA_MQTT_RETRIES) {
267✔
214
        config.mqtt.retries = process.env.IOTA_MQTT_RETRIES;
1✔
215
    }
216

217
    if (process.env.IOTA_MQTT_RETRY_TIME) {
267✔
218
        config.mqtt.retryTime = process.env.IOTA_MQTT_RETRY_TIME;
1✔
219
    }
220

221
    if (process.env.IOTA_MQTT_KEEPALIVE) {
267✔
222
        config.mqtt.keepalive = process.env.IOTA_MQTT_KEEPALIVE;
1✔
223
    }
224

225
    if (process.env.IOTA_MQTT_AVOID_LEADING_SLASH) {
267!
226
        config.mqtt.avoidLeadingSlash = process.env.IOTA_MQTT_AVOID_LEADING_SLASH;
×
227
    }
228

229
    if (process.env.IOTA_MQTT_CLEAN) {
267!
230
        config.mqtt.clean = process.env.IOTA_MQTT_CLEAN.trim().toLowerCase() === 'true';
×
231
    }
232

233
    if (process.env.IOTA_MQTT_CLIENT_ID) {
267!
234
        config.mqtt.clientId = process.env.IOTA_MQTT_CLIENT_ID;
×
235
    }
236

237
    if (process.env.IOTA_MQTT_DISABLED && process.env.IOTA_MQTT_DISABLED.trim().toLowerCase() === 'true') {
267✔
238
        config.mqtt.disabled = true;
1✔
239
    }
240

241
    if (process.env.IOTA_MQTT_SUBSCRIBE_BATCH_SIZE) {
267!
242
        config.mqtt.subscribeBatchSize = process.env.IOTA_MQTT_SUBSCRIBE_BATCH_SIZE;
×
243
    }
244

245
    if (anyIsSet(amqpVariables)) {
267✔
246
        config.amqp = config.amqp || {};
1!
247
    }
248

249
    if (process.env.IOTA_AMQP_HOST) {
267✔
250
        config.amqp.host = process.env.IOTA_AMQP_HOST;
1✔
251
    }
252

253
    if (process.env.IOTA_AMQP_PORT) {
267✔
254
        config.amqp.port = process.env.IOTA_AMQP_PORT;
1✔
255
    }
256

257
    if (process.env.IOTA_AMQP_USERNAME) {
267✔
258
        config.amqp.username = process.env.IOTA_AMQP_USERNAME;
1✔
259
    }
260

261
    if (process.env.IOTA_AMQP_PASSWORD) {
267✔
262
        config.amqp.password = process.env.IOTA_AMQP_PASSWORD;
1✔
263
    }
264

265
    if (process.env.IOTA_AMQP_EXCHANGE) {
267✔
266
        config.amqp.exchange = process.env.IOTA_AMQP_EXCHANGE;
1✔
267
    }
268

269
    if (process.env.IOTA_AMQP_QUEUE) {
267✔
270
        config.amqp.queue = process.env.IOTA_AMQP_QUEUE;
1✔
271
    }
272

273
    if (process.env.IOTA_AMQP_DURABLE) {
267✔
274
        config.amqp.options = {};
1✔
275
        config.amqp.options.durable = process.env.IOTA_AMQP_DURABLE.trim().toLowerCase() === 'true';
1✔
276
    }
277

278
    if (process.env.IOTA_AMQP_RETRIES) {
267✔
279
        config.amqp.retries = process.env.IOTA_AMQP_RETRIES;
1✔
280
    }
281

282
    if (process.env.IOTA_AMQP_RETRY_TIME) {
267✔
283
        config.amqp.retryTime = process.env.IOTA_AMQP_RETRY_TIME;
1✔
284
    }
285

286
    if (process.env.IOTA_AMQP_DISABLED && process.env.IOTA_AMQP_DISABLED.trim().toLowerCase() === 'true') {
267✔
287
        config.amqp.disabled = true;
1✔
288
    }
289

290
    if (anyIsSet(httpVariables)) {
267✔
291
        config.http = {};
1✔
292
    }
293

294
    if (process.env.IOTA_HTTP_HOST) {
267✔
295
        config.http.host = process.env.IOTA_HTTP_HOST;
1✔
296
    }
297

298
    if (process.env.IOTA_HTTP_PORT) {
267✔
299
        config.http.port = process.env.IOTA_HTTP_PORT;
1✔
300
    }
301

302
    if (process.env.IOTA_HTTP_TIMEOUT) {
267✔
303
        config.http.timeout = process.env.IOTA_HTTP_TIMEOUT;
1✔
304
    }
305

306
    if (process.env.IOTA_HTTP_KEY) {
267✔
307
        fileExists(process.env.IOTA_HTTP_KEY);
1✔
308
        config.http.key = process.env.IOTA_HTTP_KEY;
1✔
309
    }
310

311
    if (process.env.IOTA_HTTP_CERT) {
267✔
312
        fileExists(process.env.IOTA_HTTP_KEY);
1✔
313
        config.http.cert = process.env.IOTA_HTTP_CERT;
1✔
314
    }
315
}
316

317
function setConfig(newConfig) {
318
    config = newConfig;
267✔
319

320
    processEnvironmentVariables();
267✔
321
}
322

323
function getConfig() {
324
    return config;
5,956✔
325
}
326

327
function setLogger(newLogger) {
328
    logger = newLogger;
264✔
329
}
330

331
function getLogger() {
332
    return logger;
13,716✔
333
}
334

335
exports.setConfig = setConfig;
1✔
336
exports.getConfig = getConfig;
1✔
337
exports.setLogger = setLogger;
1✔
338
exports.getLogger = getLogger;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc