-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
280 lines (246 loc) · 9.79 KB
/
Copy pathmain.cpp
File metadata and controls
280 lines (246 loc) · 9.79 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
/**
* @file main.cpp
* @brief Haupteinstiegspunkt für OpenSylab LIMS
*
* OpenSylab - Open Source Laboratory Information Management System
* Version: siehe include/version.h (SSOT: CMakeLists.txt)
*
* Dieses Programm bietet LIMS-Funktionalität für kleine
* medizinische Diagnostiklabore:
* - Probenverwaltung (CRUD-Operationen)
* - Auftragsverwaltung
* - Ergebniseingabe mit Plausibilitätsprüfung
* - Gerätedatenschnittstelle (CSV/HL7)
* - Audit-Trail
* - Benutzerauthentifizierung
* - SQLite-Datenbank
* - Command-Line Interface
* - Config-Datei (opensylab.conf) mit INI-Format
*/
#include "version.h"
#include "api/ApiServer.h"
#include "db/Database.h"
#include "db/IDatabase.h"
#include "utils/CliInterface.h"
#include "utils/Config.h"
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
// Reject paths containing directory-traversal sequences.
static bool isSafeConfigPath(const std::string& path) {
// Look for ".." as a path component (handles /../, leading ../, trailing /..)
if (path.find("..") != std::string::npos) {
std::cerr << "FEHLER: Konfigurationspfad enthält unerlaubte Sequenzen (..): "
<< path << "\n";
return false;
}
return true;
}
struct ConfigResolution {
std::string path;
bool isExplicit; // true = --config flag or OPENSYLAB_CONFIG env var
};
/**
* @brief Resolve config-file path using the standard search order:
* 1. CLI flag --config <path> (explicit — missing file is a hard error)
* 2. Environment variable OPENSYLAB_CONFIG (explicit — same)
* 3. ./opensylab.conf (auto-discovered — silently skipped if absent)
* 4. /etc/opensylab/opensylab.conf (auto-discovered — silently skipped)
*/
static ConfigResolution resolveConfigPath(int argc, char* argv[]) {
// 1. CLI flag (peek before full parse)
for (int i = 1; i < argc - 1; ++i) {
if (std::string(argv[i]) == "--config") {
return {std::string(argv[i + 1]), true};
}
}
// 2. Environment variable
const char* envCfg = std::getenv("OPENSYLAB_CONFIG");
if (envCfg && *envCfg) {
return {std::string(envCfg), true};
}
// 3. Current directory (auto-discovered)
if (opensylab::utils::Config::fileExists("./opensylab.conf")) {
return {"./opensylab.conf", false};
}
// 4. System-wide path (auto-discovered)
if (opensylab::utils::Config::fileExists("/etc/opensylab/opensylab.conf")) {
return {"/etc/opensylab/opensylab.conf", false};
}
return {{}, false};
}
/**
* @brief Hauptfunktion
*/
int main(int argc, char* argv[]) {
std::cout << OPENSYLAB_VERSION_STRING << " wird gestartet...\n" << std::endl;
// ------------------------------------------------------------------
// 1. Load config file (CLI flag / env var / default paths)
// ------------------------------------------------------------------
const ConfigResolution cfgResolution = resolveConfigPath(argc, argv);
const std::string& cfgPath = cfgResolution.path;
if (!cfgPath.empty() && !isSafeConfigPath(cfgPath)) {
return 1;
}
if (cfgResolution.isExplicit && !cfgPath.empty() &&
!opensylab::utils::Config::fileExists(cfgPath)) {
std::cerr << "FEHLER: Konfigurationsdatei nicht gefunden: " << cfgPath << "\n";
return 1;
}
opensylab::utils::AppConfig config =
cfgPath.empty()
? opensylab::utils::Config::defaults()
: opensylab::utils::Config::loadFromFile(cfgPath);
if (!cfgPath.empty() && opensylab::utils::Config::fileExists(cfgPath)) {
std::cout << "Konfiguration geladen: " << cfgPath << "\n";
}
// Apply env-var overrides for TLS (kept for backward-compatibility)
{
const char* envCert = std::getenv("OPENSYLAB_TLS_CERT");
const char* envKey = std::getenv("OPENSYLAB_TLS_KEY");
if (envCert && *envCert) { config.tlsCert = envCert; }
if (envKey && *envKey) { config.tlsKey = envKey; }
}
{
const char* envDb = std::getenv("OPENSYLAB_DB_PATH");
if (envDb && *envDb) { config.dbPath = envDb; }
}
// ------------------------------------------------------------------
// 2. Parse CLI flags — override config values where flags are given
// ------------------------------------------------------------------
bool runApi = false;
for (int i = 1; i < argc; ++i) {
const std::string arg = argv[i];
if (arg == "--config" && i + 1 < argc) {
++i; // already consumed in resolveConfigPath — skip value
continue;
}
if (arg == "--api") {
runApi = true;
continue;
}
if (arg == "--api-port" && i + 1 < argc) {
try {
const int p = std::stoi(argv[++i]);
config.apiPort = (p >= 1 && p <= 65535) ? p : 8080;
} catch (...) {
config.apiPort = 8080;
}
continue;
}
if (arg == "--db" && i + 1 < argc) {
config.dbPath = argv[++i];
continue;
}
if (arg == "--tls-cert" && i + 1 < argc) {
config.tlsCert = argv[++i];
continue;
}
if (arg == "--tls-key" && i + 1 < argc) {
config.tlsKey = argv[++i];
continue;
}
if (arg == "--force-https") {
config.forceHttps = true;
continue;
}
// Positional argument: treat as db path if not yet set
if (config.dbPath.empty() && arg[0] != '-') {
config.dbPath = arg;
}
}
// ------------------------------------------------------------------
// 3. Apply final fallback for db path
// ------------------------------------------------------------------
if (config.dbPath.empty()) {
config.dbPath = "opensylab.db";
}
std::cout << "Verwende Datenbank: " << config.dbPath << "\n" << std::endl;
// ------------------------------------------------------------------
// 4. Open database
// ------------------------------------------------------------------
std::shared_ptr<opensylab::db::IDatabase> database;
// Audit HMAC key — must be set before open() to ensure persistent chain.
const char* auditKeyEnv = std::getenv("OPENSYLAB_AUDIT_HMAC_KEY");
const std::string auditHmacKey = (auditKeyEnv && *auditKeyEnv)
? std::string(auditKeyEnv) : "";
if (auditHmacKey.empty()) {
std::cerr << "FEHLER: OPENSYLAB_AUDIT_HMAC_KEY ist nicht gesetzt.\n"
<< " Setzen Sie: export OPENSYLAB_AUDIT_HMAC_KEY=<sicherer-key-min-32-zeichen>\n"
<< " Generieren mit: openssl rand -hex 32\n";
return 1;
}
if (auditHmacKey.size() < 32) {
std::cerr << "FEHLER: OPENSYLAB_AUDIT_HMAC_KEY ist zu kurz (min. 32 Zeichen erforderlich).\n"
<< " Generieren mit: openssl rand -hex 32\n";
return 1;
}
if (config.dbBackend == "postgresql") {
std::cerr << "FEHLER: PostgreSQL-Backend unterstuetzt noch kein Audit-HMAC (geplant fuer v1.1).\n"
<< " Verwenden Sie das SQLite-Backend (Standard) bis zur vollstaendigen Implementierung.\n";
return 1;
} else {
database = std::make_shared<opensylab::db::Database>(config.dbPath);
}
database->setAuditHmacKey(auditHmacKey);
if (!database->open()) {
std::cerr << "FEHLER: Kann Datenbank nicht öffnen!\n";
std::cerr << "Details: " << database->getLastError() << std::endl;
return 1;
}
if (!database->initializeSchema()) {
std::cerr << "FEHLER: Kann Datenbankschema nicht initialisieren!\n";
std::cerr << "Details: " << database->getLastError() << std::endl;
database->close();
return 1;
}
std::cout << "Datenbank erfolgreich initialisiert.\n" << std::endl;
// ------------------------------------------------------------------
// 5. Run API server or CLI
// ------------------------------------------------------------------
if (runApi) {
if (config.forceHttps && (config.tlsCert.empty() || config.tlsKey.empty())) {
std::cerr << "FEHLER: --force-https requires TLS configuration.\n"
<< " Provide --tls-cert and --tls-key or set\n"
<< " OPENSYLAB_TLS_CERT and OPENSYLAB_TLS_KEY.\n";
database->close();
return 1;
}
std::cout << "API-Server wird auf Port " << config.apiPort
<< " gestartet...\n"
<< std::endl;
opensylab::api::ApiServer server(database, config.apiPort);
if (!config.tlsCert.empty() && !config.tlsKey.empty()) {
if (!server.enableTls(config.tlsCert, config.tlsKey)) {
std::cerr << "WARNUNG: TLS konnte nicht aktiviert werden. Starte ohne TLS.\n";
if (config.forceHttps) {
std::cerr << "FEHLER: --force-https erfordert funktionierendes TLS.\n";
database->close();
return 1;
}
} else {
std::cout << "TLS aktiviert.\n" << std::endl;
}
}
if (!server.run()) {
std::cerr << "FEHLER: API-Server konnte nicht gestartet werden.\n";
database->close();
return 1;
}
} else {
try {
auto cli = std::make_unique<opensylab::utils::CliInterface>(database);
cli->run();
} catch (const std::exception& e) {
std::cerr << "FEHLER: Unerwarteter Fehler im CLI: " << e.what()
<< std::endl;
database->close();
return 1;
}
}
database->close();
std::cout << "\nDatenbank geschlossen. OpenSylab wurde beendet.\n"
<< std::endl;
return 0;
}