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

telefonicaid / iotagent-json / 12864697155

20 Jan 2025 09:15AM UTC coverage: 79.494% (-0.2%) from 79.696%
12864697155

push

github

web-flow
Merge pull request #853 from telefonicaid/fix/use_default_transport_config

Use config.defaultTransport instead of 'HTTP'

493 of 699 branches covered (70.53%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

7 existing lines in 2 files now uncovered.

1077 of 1276 relevant lines covered (84.4%)

134.23 hits per line

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

92.52
/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++) {
765✔
31
        if (process.env[variableSet[i]]) {
8,385✔
32
            return true;
3✔
33
        }
34
    }
35

36
    return false;
762✔
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 = [
255✔
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_AMQP_HOST',
75
        'IOTA_AMQP_PORT',
76
        'IOTA_AMQP_USERNAME',
77
        'IOTA_AMQP_PASSWORD',
78
        'IOTA_AMQP_EXCHANGE',
79
        'IOTA_AMQP_QUEUE',
80
        'IOTA_AMQP_DURABLE',
81
        'IOTA_AMQP_RETRIES',
82
        'IOTA_AMQP_RETRY_TIME',
83
        'IOTA_AMQP_DISABLED',
84
        'IOTA_HTTP_HOST',
85
        'IOTA_HTTP_PORT',
86
        'IOTA_HTTP_TIMEOUT',
87
        'IOTA_HTTP_KEY',
88
        'IOTA_HTTP_CERT',
89
        'IOTA_CONFIG_RETRIEVAL',
90
        'IOTA_DEFAULT_KEY',
91
        'IOTA_DEFAULT_TRANSPORT'
92
    ];
93
    const mqttVariables = [
255✔
94
        'IOTA_MQTT_PROTOCOL',
95
        'IOTA_MQTT_HOST',
96
        'IOTA_MQTT_PORT',
97
        'IOTA_MQTT_CA',
98
        'IOTA_MQTT_CERT',
99
        'IOTA_MQTT_KEY',
100
        'IOTA_MQTT_REJECT_UNAUTHORIZED',
101
        'IOTA_MQTT_USERNAME',
102
        'IOTA_MQTT_PASSWORD',
103
        'IOTA_MQTT_QOS',
104
        'IOTA_MQTT_RETAIN',
105
        'IOTA_MQTT_RETRIES',
106
        'IOTA_MQTT_RETRY_TIME',
107
        'IOTA_MQTT_KEEPALIVE',
108
        'IOTA_MQTT_AVOID_LEADING_SLASH',
109
        'IOTA_MQTT_CLEAN',
110
        'IOTA_MQTT_CLIENT_ID',
111
        'IOTA_MQTT_DISABLED'
112
    ];
113
    const amqpVariables = [
255✔
114
        'IOTA_AMQP_HOST',
115
        'IOTA_AMQP_PORT',
116
        'IOTA_AMQP_USERNAME',
117
        'IOTA_AMQP_PASSWORD',
118
        'IOTA_AMQP_EXCHANGE',
119
        'IOTA_AMQP_QUEUE',
120
        'IOTA_AMQP_DURABLE',
121
        'IOTA_AMQP_RETRIES',
122
        'IOTA_AMQP_RETRY_TIME',
123
        'IOTA_AMQP_DISABLED'
124
    ];
125
    const httpVariables = ['IOTA_HTTP_HOST', 'IOTA_HTTP_PORT', 'IOTA_HTTP_TIMEOUT', 'IOTA_HTTP_KEY', 'IOTA_HTTP_CERT'];
255✔
126

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

148
    if (process.env.IOTA_CONFIG_RETRIEVAL) {
255!
149
        config.configRetrieval = process.env.IOTA_CONFIG_RETRIEVAL;
×
150
    }
151
    if (process.env.IOTA_DEFAULT_KEY) {
255!
152
        config.defaultKey = process.env.IOTA_DEFAULT_KEY;
×
153
    }
154
    if (process.env.IOTA_DEFAULT_TRANSPORT) {
255!
UNCOV
155
        config.defaultTransport = process.env.IOTA_DEFAULT_TRANSPORT;
×
156
    }
157

158
    if (anyIsSet(mqttVariables)) {
255✔
159
        config.mqtt = config.mqtt || {};
1!
160
    }
161

162
    if (process.env.IOTA_MQTT_PROTOCOL) {
255✔
163
        config.mqtt.protocol = process.env.IOTA_MQTT_PROTOCOL;
1✔
164
    }
165

166
    if (process.env.IOTA_MQTT_HOST) {
255✔
167
        config.mqtt.host = process.env.IOTA_MQTT_HOST;
1✔
168
    }
169

170
    if (process.env.IOTA_MQTT_PORT) {
255✔
171
        config.mqtt.port = process.env.IOTA_MQTT_PORT;
1✔
172
    }
173

174
    if (process.env.IOTA_MQTT_CA) {
255✔
175
        fileExists(process.env.IOTA_MQTT_CA);
1✔
176
        config.mqtt.ca = process.env.IOTA_MQTT_CA;
1✔
177
    }
178

179
    if (process.env.IOTA_MQTT_CERT) {
255✔
180
        fileExists(process.env.IOTA_MQTT_CERT);
1✔
181
        config.mqtt.cert = process.env.IOTA_MQTT_CERT;
1✔
182
    }
183

184
    if (process.env.IOTA_MQTT_KEY) {
255✔
185
        fileExists(process.env.IOTA_MQTT_KEY);
1✔
186
        config.mqtt.key = process.env.IOTA_MQTT_KEY;
1✔
187
    }
188

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

195
    if (process.env.IOTA_MQTT_USERNAME) {
255✔
196
        config.mqtt.username = process.env.IOTA_MQTT_USERNAME;
1✔
197
    }
198

199
    if (process.env.IOTA_MQTT_PASSWORD) {
255✔
200
        config.mqtt.password = process.env.IOTA_MQTT_PASSWORD;
1✔
201
    }
202

203
    if (process.env.IOTA_MQTT_QOS) {
255✔
204
        config.mqtt.qos = process.env.IOTA_MQTT_QOS;
1✔
205
    }
206

207
    if (process.env.IOTA_MQTT_RETAIN) {
255✔
208
        config.mqtt.retain = process.env.IOTA_MQTT_RETAIN.trim().toLowerCase() === 'true';
1✔
209
    }
210

211
    if (process.env.IOTA_MQTT_RETRIES) {
255✔
212
        config.mqtt.retries = process.env.IOTA_MQTT_RETRIES;
1✔
213
    }
214

215
    if (process.env.IOTA_MQTT_RETRY_TIME) {
255✔
216
        config.mqtt.retryTime = process.env.IOTA_MQTT_RETRY_TIME;
1✔
217
    }
218

219
    if (process.env.IOTA_MQTT_KEEPALIVE) {
255✔
220
        config.mqtt.keepalive = process.env.IOTA_MQTT_KEEPALIVE;
1✔
221
    }
222

223
    if (process.env.IOTA_MQTT_AVOID_LEADING_SLASH) {
255!
UNCOV
224
        config.mqtt.avoidLeadingSlash = process.env.IOTA_MQTT_AVOID_LEADING_SLASH;
×
225
    }
226

227
    if (process.env.IOTA_MQTT_CLEAN) {
255!
UNCOV
228
        config.mqtt.clean = process.env.IOTA_MQTT_CLEAN.trim().toLowerCase() === 'true';
×
229
    }
230

231
    if (process.env.IOTA_MQTT_CLIENT_ID) {
255!
UNCOV
232
        config.mqtt.clientId = process.env.IOTA_MQTT_CLIENT_ID;
×
233
    }
234

235
    if (process.env.IOTA_MQTT_DISABLED && process.env.IOTA_MQTT_DISABLED.trim().toLowerCase() === 'true') {
255✔
236
        config.mqtt.disabled = true;
1✔
237
    }
238

239
    if (anyIsSet(amqpVariables)) {
255✔
240
        config.amqp = config.amqp || {};
1!
241
    }
242

243
    if (process.env.IOTA_AMQP_HOST) {
255✔
244
        config.amqp.host = process.env.IOTA_AMQP_HOST;
1✔
245
    }
246

247
    if (process.env.IOTA_AMQP_PORT) {
255✔
248
        config.amqp.port = process.env.IOTA_AMQP_PORT;
1✔
249
    }
250

251
    if (process.env.IOTA_AMQP_USERNAME) {
255✔
252
        config.amqp.username = process.env.IOTA_AMQP_USERNAME;
1✔
253
    }
254

255
    if (process.env.IOTA_AMQP_PASSWORD) {
255✔
256
        config.amqp.password = process.env.IOTA_AMQP_PASSWORD;
1✔
257
    }
258

259
    if (process.env.IOTA_AMQP_EXCHANGE) {
255✔
260
        config.amqp.exchange = process.env.IOTA_AMQP_EXCHANGE;
1✔
261
    }
262

263
    if (process.env.IOTA_AMQP_QUEUE) {
255✔
264
        config.amqp.queue = process.env.IOTA_AMQP_QUEUE;
1✔
265
    }
266

267
    if (process.env.IOTA_AMQP_DURABLE) {
255✔
268
        config.amqp.options = {};
1✔
269
        config.amqp.options.durable = process.env.IOTA_AMQP_DURABLE.trim().toLowerCase() === 'true';
1✔
270
    }
271

272
    if (process.env.IOTA_AMQP_RETRIES) {
255✔
273
        config.amqp.retries = process.env.IOTA_AMQP_RETRIES;
1✔
274
    }
275

276
    if (process.env.IOTA_AMQP_RETRY_TIME) {
255✔
277
        config.amqp.retryTime = process.env.IOTA_AMQP_RETRY_TIME;
1✔
278
    }
279

280
    if (process.env.IOTA_AMQP_DISABLED && process.env.IOTA_AMQP_DISABLED.trim().toLowerCase() === 'true') {
255✔
281
        config.amqp.disabled = true;
1✔
282
    }
283

284
    if (anyIsSet(httpVariables)) {
255✔
285
        config.http = {};
1✔
286
    }
287

288
    if (process.env.IOTA_HTTP_HOST) {
255✔
289
        config.http.host = process.env.IOTA_HTTP_HOST;
1✔
290
    }
291

292
    if (process.env.IOTA_HTTP_PORT) {
255✔
293
        config.http.port = process.env.IOTA_HTTP_PORT;
1✔
294
    }
295

296
    if (process.env.IOTA_HTTP_TIMEOUT) {
255✔
297
        config.http.timeout = process.env.IOTA_HTTP_TIMEOUT;
1✔
298
    }
299

300
    if (process.env.IOTA_HTTP_KEY) {
255✔
301
        fileExists(process.env.IOTA_HTTP_KEY);
1✔
302
        config.http.key = process.env.IOTA_HTTP_KEY;
1✔
303
    }
304

305
    if (process.env.IOTA_HTTP_CERT) {
255✔
306
        fileExists(process.env.IOTA_HTTP_KEY);
1✔
307
        config.http.cert = process.env.IOTA_HTTP_CERT;
1✔
308
    }
309
}
310

311
function setConfig(newConfig) {
312
    config = newConfig;
255✔
313

314
    processEnvironmentVariables();
255✔
315
}
316

317
function getConfig() {
318
    return config;
5,677✔
319
}
320

321
function setLogger(newLogger) {
322
    logger = newLogger;
252✔
323
}
324

325
function getLogger() {
326
    return logger;
13,105✔
327
}
328

329
exports.setConfig = setConfig;
1✔
330
exports.getConfig = getConfig;
1✔
331
exports.setLogger = setLogger;
1✔
332
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