Repository navigation
Expand file tree
/
Copy pathconfig.cpp
More file actions
564 lines (492 loc) · 16.7 KB
/
Copy pathconfig.cpp
File metadata and controls
564 lines (492 loc) · 16.7 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
* Copyright 2018 The Polycube Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include <getopt.h>
#include <linux/limits.h>
#include <string.h>
#include <sys/stat.h>
#include <algorithm>
#include <exception>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include "version.h"
namespace configuration {
#define LOGLEVEL spdlog::level::level_enum::info
#define DAEMON false
#define CUBESDUMPCLEANINIT false
//#define CUBESNODUMP false
#define CUBESDUMPENABLED false
#define CUBESDUMPFILEFLAG false
#define PIDFILE "/var/run/polycube.pid"
#define SERVER_PORT 9000
#define SERVER_IP "localhost"
#define LOGFILE "/var/log/polycube/polycubed.log"
#define CONFIGFILEDIR "/etc/polycube"
#define CONFIGFILENAME "polycubed.conf"
#define CUBESDUMPFILENAME "cubes.yaml"
#define CONFIGFILE (CONFIGFILEDIR "/" CONFIGFILENAME)
#define CUBESDUMPFILEPATH (CONFIGFILEDIR "/" CUBESDUMPFILENAME)
static void show_usage(const std::string &name) {
std::cout << std::boolalpha;
std::cout << "Usage: " << name << " [OPTIONS]" << std::endl;
std::cout << "-p, --port PORT: port where the rest server listens (default: "
<< SERVER_PORT << ")" << std::endl;
std::cout << "-a, --addr: addr where polycubed listens (default: "
<< SERVER_IP << ")" << std::endl;
std::cout << "-l, --loglevel: set log level (trace, debug, info, warn, err, "
"critical, off, default: info)"
<< std::endl;
std::cout << "-c, --cert: path to ssl certificate" << std::endl;
std::cout << "-k, --key: path to ssl private key" << std::endl;
std::cout << "--cacert: path to certification authority certificate used to "
"validate clients"
<< std::endl;
std::cout << "-d, --daemon: run as daemon (default: " << DAEMON << ")"
<< std::endl;
std::cout << "-v, --version: show version and exit" << std::endl;
std::cout << "--logfile: file to save polycube logs (default: " << LOGFILE
<< ")" << std::endl;
std::cout << "--pidfile: file to save polycubed pid (default: " << PIDFILE
<< ")" << std::endl;
std::cout << "--configfile: configuration file (default: " << CONFIGFILE
<< ")" << std::endl;
std::cout << "--cubes-dump-file: file that keeps the last topology, "
"including the configuration of all cubes (default: "
<< CUBESDUMPFILEPATH << ")" << std::endl;
std::cout << "--cubes-dump-clean-init: starts the daemon with an empty "
"topology" << std::endl;
//std::cout << "--cubes-nodump: starts the daemon without dumping updates to file" << std::endl;
std::cout << "--cubes-dump-enable: enables dumping updates to file"
<< std::endl;
std::cout << "--cert-blacklist: path to black listed certificates"
<< std::endl;
std::cout << "--cert-whitelist: path to white listed certificates"
<< std::endl;
std::cout << "-h, --help: print this message" << std::endl;
}
static void show_version(void) {
std::cout << "polycubed version " << VERSION << std::endl;
}
bool parse_bool(std::string str) {
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
if (str == "true") {
return true;
} else if (str == "false") {
return false;
} else {
throw std::runtime_error(str + " is not a boolean");
}
}
Config config;
Config::Config()
: loglevel(LOGLEVEL),
daemon(DAEMON),
pidfile(PIDFILE),
server_port(SERVER_PORT),
server_ip(SERVER_IP),
logfile(LOGFILE),
configfile(CONFIGFILE),
cubes_dump_file(CUBESDUMPFILEPATH),
cubes_dump_clean_init(CUBESDUMPCLEANINIT),
//cubes_nodump(CUBESNODUMP)
cubes_dump_enabled(CUBESDUMPENABLED),
cubes_dump_file_flag(CUBESDUMPFILEFLAG) {}
Config::~Config() {}
spdlog::level::level_enum Config::getLogLevel() const {
return loglevel;
}
#define CHECK_OVERWRITE(PARAMETER, NEW_VALUE, CURRENT_VALUE, DEFAULT) \
do { \
if (NEW_VALUE == CURRENT_VALUE) { \
return; \
} \
if (CURRENT_VALUE != DEFAULT) { \
logger->warn("'--" PARAMETER \
"' also present on configuration file, overwritting it"); \
} \
} while (0)
void Config::setLogLevel(const std::string &value_) {
std::string value(value_);
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
spdlog::level::level_enum level_ = spdlog::level::from_str(value);
// from_str returns off if the level is not valid, be more agressive on that
// check
if (level_ == spdlog::level::level_enum::off && value != "off") {
throw std::runtime_error(value + " is not a valid logging level");
}
CHECK_OVERWRITE("logevel", level_, loglevel, LOGLEVEL);
loglevel = level_;
}
bool Config::getDaemon() const {
return daemon;
}
void Config::setDaemon(const std::string &value) {
bool daemon_ = parse_bool(value);
CHECK_OVERWRITE("daemon", daemon_, daemon, DAEMON);
daemon = daemon_;
}
std::string Config::getPidFile() const {
return pidfile;
}
void Config::setPidFile(const std::string &value) {
CHECK_OVERWRITE("pidfile", value, pidfile, PIDFILE);
pidfile = value;
}
uint16_t Config::getServerPort() const {
return server_port;
}
void Config::setServerPort(const std::string &value) {
unsigned long port_ = std::atol(optarg);
if (port_ > UINT16_MAX) {
throw std::runtime_error("port value" + std::string(optarg) +
" is not not valid");
}
CHECK_OVERWRITE("port", port_, server_port, SERVER_PORT);
server_port = port_;
}
std::string Config::getServerIP() const {
return server_ip;
}
void Config::setServerIP(const std::string &value) {
CHECK_OVERWRITE("addr", value, server_ip, SERVER_IP);
server_ip = value;
}
std::string Config::getLogFile() const {
return logfile;
}
void Config::setLogFile(const std::string &value) {
CHECK_OVERWRITE("logfile", value, logfile, LOGFILE);
logfile = value;
}
std::string Config::getCubesDumpFile() const {
return cubes_dump_file;
}
void Config::setCubesDumpFile(const std::string &value) {
cubes_dump_file_flag = true;
CHECK_OVERWRITE("cubes-dump-file", value, cubes_dump_file, CUBESDUMPFILEPATH);
cubes_dump_file = value;
}
bool Config::getCubesDumpCleanInit() const {
return cubes_dump_clean_init;
}
void Config::setCubesDumpCleanInit() {
cubes_dump_clean_init = true;
}
//bool Config::getCubesNoDump() const {
// return cubes_nodump;
//}
//void Config::setCubesNoDump() {
// cubes_nodump = true;
//}
bool Config::getCubesDumpEnabled() const {
return cubes_dump_enabled;
}
void Config::setCubesDumpEnabled() {
cubes_dump_enabled = true;
}
std::string Config::getCertPath() const {
return cert_path;
}
void Config::setCertPath(const std::string &value) {
CHECK_OVERWRITE("cert", value, cert_path, "");
cert_path = value;
}
std::string Config::getKeyPath() const {
return key_path;
}
void Config::setKeyPath(const std::string &value) {
CHECK_OVERWRITE("key", value, key_path, "");
key_path = value;
}
std::string Config::getCACertPath() const {
return cacert_path;
}
void Config::setCACertPath(const std::string &value) {
CHECK_OVERWRITE("cacert", value, cacert_path, "");
cacert_path = value;
}
std::string Config::getCertWhitelistPath() const {
return cert_whitelist_path;
}
void Config::setCertWhitelistPath(const std::string &value) {
CHECK_OVERWRITE("cert-whitelist", value, cert_whitelist_path, "");
cert_whitelist_path = value;
}
std::string Config::getCertBlacklistPath() const {
return cert_blacklist_path;
}
void Config::setCertBlacklistPath(const std::string &value) {
CHECK_OVERWRITE("cert-blacklist", value, cert_blacklist_path, "");
cert_blacklist_path = value;
}
void Config::create_configuration_file(const std::string &path) {
mkdir(CONFIGFILEDIR, 0600);
std::ofstream file(path);
if (!file.good()) {
throw std::runtime_error("error creating configuration file");
}
file << std::boolalpha; // print booleans as "true"/"false" instead of "1/0"
file << "# polycubed configuration file" << std::endl << std::endl;
file << "# logging level (trace, debug, info, warn, err, critical, off)"
<< std::endl;
file << "loglevel: " << spdlog::level::to_c_str(loglevel) << std::endl;
file << "# run as daemon" << std::endl;
file << "daemon: " << daemon << std::endl;
file << "# file to save polycubed pid" << std::endl;
file << "pidfile: " << pidfile << std::endl;
file << "# port where the rest server listens" << std::endl;
file << "port: " << server_port << std::endl;
file << "# addr where polycubed listens" << std::endl;
file << "addr: " << server_ip << std::endl;
file << "# file to save polycube logs" << std::endl;
file << "logfile: " << logfile << std::endl;
// uncomment when cubes dump is enabled as default behavior
//file << "# file to save last topology" << std::endl;
//file << "cubes-dump-file: " << cubes_dump_file << std::endl;
file << "# Security related:" << std::endl;
file << "# server certificate " << std::endl;
file << "#cert: path_to_certificate_file" << std::endl;
file << "# server private key " << std::endl;
file << "#key: path_to_key_fule" << std::endl;
file << "# certification authority certificate " << std::endl;
file << "#cacert: path_to_certificate_file" << std::endl;
file << "# path to black list certificates folder" << std::endl;
file << "#cert-blacklist: path to folder containing black listed certificates"
<< std::endl;
file << "# path to white list certificates folder" << std::endl;
file << "#cert-whitelist: path to folder containing white listed certificates"
<< std::endl;
}
void Config::dump() {
logger = spdlog::get("polycubed");
logger->info("configuration parameters: ");
logger->info(" loglevel: {}", spdlog::level::to_c_str(loglevel));
logger->info(" daemon: {}", daemon);
logger->info(" pidfile: {}", pidfile);
logger->info(" port: {}", server_port);
logger->info(" addr: {}", server_ip);
logger->info(" logfile: {}", logfile);
logger->info(" cubes-dump-file: {}", cubes_dump_file);
logger->info(" cubes-dump-clean-init: {}", cubes_dump_clean_init);
//logger->info(" cubes-nodump: {}", cubes_nodump);
logger->info(" cubes-dump-enable: {}", cubes_dump_enabled);
if (!cert_path.empty()) {
logger->info(" cert: {}", cert_path);
}
if (!key_path.empty()) {
logger->info(" key: {}", key_path);
}
if (!cacert_path.empty()) {
logger->info(" cacert: {}", cacert_path);
}
if (!cert_blacklist_path.empty()) {
logger->info(" blacklist: {}", cert_blacklist_path);
}
if (!cert_whitelist_path.empty()) {
logger->info(" whitelist: {}", cert_whitelist_path);
}
}
void Config::load_from_file(const std::string &path) {
logger->info("loading configuration from {}", path);
std::ifstream file(path);
if (!file.good()) {
if (path == CONFIGFILE) {
logger->warn(
"default configfile ({}) not found, creating a new with default "
"parameters",
CONFIGFILE);
create_configuration_file(CONFIGFILE);
} else {
throw std::runtime_error("error opening " + path);
}
}
std::string str;
std::string value;
size_t pos;
char *argv[50];
argv[0] = strndup("", PATH_MAX);
int argc = 1;
std::smatch match;
std::regex rule("(\\S+):(\\S+)");
int i = 0;
while (std::getline(file, str)) {
i++;
// remove all spaces
str.erase(remove_if(str.begin(), str.end(), isspace), str.end());
// remove comments
size_t pos = str.find('#');
if (pos != std::string::npos) {
str.erase(pos);
}
if (str.empty()) {
continue;
}
if (!std::regex_match(str, match, rule)) {
std::stringstream err;
err << "bad line in configuration file: " << path << ":" << i << " -> \""
<< str << "\"" << std::endl;
throw std::runtime_error(err.str().c_str());
}
std::replace(str.begin(), str.end(), ':', '=');
value = "--" + str;
argv[argc++] = strndup(value.c_str(), PATH_MAX);
}
load_from_cli(argc, argv);
for (int i = 0; i < argc; i++) {
free(argv[i]);
}
}
static struct option options[] = {
{"loglevel", required_argument, NULL, 'l'},
{"port", required_argument, NULL, 'p'},
{"daemon", optional_argument, NULL, 'd'},
{"addr", required_argument, NULL, 'a'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{"cert", required_argument, NULL, 'c'},
{"key", required_argument, NULL, 'k'},
{"logfile", required_argument, NULL, 1},
{"pidfile", required_argument, NULL, 2},
{"configfile", required_argument, NULL, 4},
{"cacert", required_argument, NULL, 5},
{"cert-whitelist", required_argument, NULL, 6},
{"cert-blacklist", required_argument, NULL, 7},
{"cubes-dump-file", required_argument, NULL, 8},
{"cubes-dump-clean-init", no_argument, NULL, 9},
//{"cubes-nodump", no_argument, NULL, 10},
{"cubes-dump-enable", no_argument, NULL, 10},
{NULL, 0, NULL, 0},
};
void Config::load_from_cli(int argc, char *argv[]) {
int option_index = 0;
char ch;
optind = 0;
while ((ch = getopt_long(argc, argv, "l:p:a:dhv", options, &option_index)) !=
-1) {
switch (ch) {
case 'l':
setLogLevel(optarg);
break;
case 'p':
setServerPort(optarg);
break;
case 'd':
setDaemon(optarg ? std::string(optarg) : "true");
break;
case 'a':
setServerIP(optarg);
break;
case 'c':
setCertPath(optarg);
break;
case 'k':
setKeyPath(optarg);
break;
case '?':
throw std::runtime_error("Missing argument, see stderr");
case 1:
setLogFile(optarg);
break;
case 2:
setPidFile(optarg);
break;
case 5:
setCACertPath(optarg);
break;
case 6:
setCertWhitelistPath(optarg);
break;
case 7:
setCertBlacklistPath(optarg);
break;
case 8:
setCubesDumpFile(optarg);
break;
case 9:
setCubesDumpCleanInit();
break;
case 10:
//setCubesNoDump();
setCubesDumpEnabled();
break;
}
}
}
bool Config::load(int argc, char *argv[]) {
logger = spdlog::get("polycubed");
int option_index = 0;
char ch;
// do a first pass looking for "configfile", "-h", "-v"
while ((ch = getopt_long(argc, argv, "l:p:a:dhv", options, &option_index)) !=
-1) {
switch (ch) {
case 'v':
show_version();
return false;
case 'h':
show_usage(argv[0]);
return false;
case 4:
configfile = optarg;
break;
}
}
load_from_file(configfile);
load_from_cli(argc, argv);
check();
if (cubes_dump_clean_init) {
std::ofstream output(cubes_dump_file);
if (output.is_open()) {
output << "{}";
output.close();
}
}
return true;
}
void Config::check() {
// cert and key has to both to be passed
if (cert_path != "" && key_path == "") {
throw std::runtime_error("-c, --cert requires -k, --key");
}
if (cert_path == "" && key_path != "") {
throw std::runtime_error("-k, --key requires -c, --cert");
}
if (cert_path != "" && cacert_path == "" && cert_whitelist_path == "") {
throw std::runtime_error(
"--cacert or --cert-whitelist are needed when using --cert");
}
if (cacert_path != "" && cert_path == "") {
throw std::runtime_error("--cacert requires -c, --cert and -k, --key");
}
if (cacert_path != "" && cert_whitelist_path != "") {
throw std::runtime_error(
"--cacert and --cert-whitelist cannot be used together");
}
if (cert_whitelist_path != "" && cert_blacklist_path != "") {
throw std::runtime_error(
"--cert-blacklist & --cert-whitelist cannot be used together");
}
if (cert_blacklist_path != "" && cacert_path == "") {
throw std::runtime_error("--cert-blacklist requires --cacert");
}
if ((cubes_dump_clean_init || cubes_dump_file_flag) && !cubes_dump_enabled) {
throw std::runtime_error("--cubes-dump-file and --cubes-dump-clean-init "
"require --cubes-dump-enable");
}
}
}