forked from lbr38/repomanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.php
1203 lines (1087 loc) · 41.2 KB
/
Connection.php
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Models;
use Controllers\Common;
use SQLite3;
use Exception;
class Connection extends SQLite3
{
public function __construct(string $database, int|null $databaseId = null, bool $check = true)
{
/**
* Open database from its name
* If database does not exist it is automatically created
*/
try {
if (!is_dir(DB_DIR)) {
if (!mkdir(DB_DIR, 0770, true)) {
throw new Exception('Unable to create database directory');
}
}
/**
* Open database
*
* Case where database is 'main', it is the main database 'repomanager.db'
*/
if ($database == 'main') {
$this->open(DB);
$this->busyTimeout(30000);
$this->enableExceptions(true);
$this->enableWAL();
/**
* If check is true, check if tables are missing before generating them
* This avoid to execute all CREATE tables queries each time the class is instanciated, and should speed up page loading
*/
if ($check) {
$this->checkMainTables();
} else {
$this->generateMainTables();
}
/**
* Case where database is 'stats', it is the stats database 'repomanager-stats.db'
*/
} elseif ($database == 'stats') {
$this->open(STATS_DB);
$this->busyTimeout(30000);
$this->enableExceptions(true);
$this->enableWAL();
/**
* If check is true, check if tables are missing before generating them
* This avoid to execute all CREATE tables queries each time the class is instanciated, and should speed up page loading
*/
if ($check) {
$this->checkStatsTables();
} else {
$this->generateStatsTables();
}
/**
* Case where database is 'hosts', it is the hosts database 'repomanager-hosts.db'
*/
} elseif ($database == 'hosts') {
$this->open(HOSTS_DB);
$this->busyTimeout(30000);
$this->enableExceptions(true);
$this->enableWAL();
/**
* If check is true, check if tables are missing before generating them
* This avoid to execute all CREATE tables queries each time the class is instanciated, and should speed up page loading
*/
if ($check) {
$this->checkHostsTables();
} else {
$this->generateHostsTables();
}
/**
* Case where database is 'host', it is a host database 'properties.db', databaseId must be set
*/
} elseif ($database == 'host' and isset($databaseId)) {
$this->open(HOSTS_DIR . '/' . $databaseId . '/properties.db');
$this->busyTimeout(30000);
$this->enableExceptions(true);
$this->enableWAL();
$this->generateHostTables();
/**
* Case where database is 'ws', it is the websockets database 'repomanager-ws.db'
*/
} elseif ($database == 'ws') {
$this->open(WS_DB);
$this->busyTimeout(30000);
$this->enableExceptions(true);
$this->enableWAL();
$this->generateWsTables();
/**
* Case where database is 'task-log', it is a task log database 'task-<databaseId>-log.db'
*/
} elseif ($database == 'task-log' and isset($databaseId)) {
$this->open(MAIN_LOGS_DIR . '/repomanager-task-' . $databaseId . '-log.db');
$this->busyTimeout(30000);
$this->enableExceptions(true);
$this->enableWAL();
$this->generateTaskLogTables();
/**
* Case where database is not 'main', 'stats', 'hosts' or 'host'
*/
} else {
throw new Exception("unknown database: $database");
}
} catch (\Exception $e) {
die('Error while opening database: ' . $e->getMessage());
}
}
/**
* Enable WAL mode
*/
private function enableWAL()
{
$this->exec('pragma journal_mode = WAL; pragma synchronous = normal; pragma temp_store = memory; pragma mmap_size = 30000000000;');
}
/**
* Disable WAL mode
*/
private function disableWAL()
{
$this->exec('pragma journal_mode = DELETE;');
}
/**
*
* Functions to check if all tables are present
*
*/
/**
* Count the number of tables in the main database
*/
public function countMainTables()
{
$result = $this->query("SELECT name FROM sqlite_master WHERE type='table'
AND name='repos'
OR name='repos_env'
OR name='repos_snap'
OR name='env'
OR name='sources'
OR name='groups'
OR name='group_members'
OR name='tasks'
OR name='profile'
OR name='profile_settings'
OR name='profile_repo_members'
OR name='profile_package'
OR name='profile_service'
OR name='users'
OR name='user_role'
OR name='history'
OR name='notifications'
OR name='logs'
OR name='settings'
OR name='layout_container_state'
OR name='cve'
OR name='cve_cpe'
OR name='cve_reference'
OR name='cve_affected_hosts'
OR name='cve_import'
OR name='cve_affected_hosts_import'");
return $this->count($result);
}
/**
* Count the number of tables in the stats database
*/
public function countStatsTables()
{
$result = $this->query("SELECT name FROM sqlite_master WHERE type='table'
and name='stats'
OR name='access_deb'
OR name='access_rpm'
OR name='access_queue'");
return $this->count($result);
}
/**
* Count the number of tables in the hosts database
*/
public function countHostsTables()
{
$result = $this->query("SELECT name FROM sqlite_master WHERE type='table'
and name='requests'
OR name='hosts'
OR name='groups'
OR name='group_members'
OR name='settings'");
return $this->count($result);
}
/**
* Check if all tables are present in the main database
*/
public function checkMainTables()
{
$required = 26;
/**
* If the number of tables != $required then we try to regenerate the tables
*/
if ($this->countMainTables() != $required) {
$this->generateMainTables();
/**
* Count again the number of tables after the regeneration attempt, return false if it's still not good
*/
if ($this->countMainTables() != $required) {
return false;
}
}
return true;
}
/**
* Check if all tables are present in the stats database
*/
public function checkStatsTables()
{
$required = 4;
/**
* If the number of tables != $required then we try to regenerate the tables
*/
if ($this->countStatsTables() != $required) {
$this->generateStatsTables();
/**
* Count again the number of tables after the regeneration attempt, return false if it's still not good
*/
if ($this->countStatsTables() != $required) {
return false;
}
}
return true;
}
/**
* Check if all tables are present in the hosts database
*/
public function checkHostsTables()
{
$required = 5;
/**
* If the number of tables != $required then we try to regenerate the tables
*/
if ($this->countHostsTables() != $required) {
$this->generateHostsTables();
/**
* Count again the number of tables after the regeneration attempt, return false if it's still not good
*/
if ($this->countHostsTables() != $required) {
return false;
}
}
return true;
}
/**
*
* Functions to generate tables if not exists
*
*/
/**
* Generate tables in the main database
*/
private function generateMainTables()
{
/**
* repos table
*/
$this->exec("CREATE TABLE IF NOT EXISTS repos (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Name VARCHAR(255) NOT NULL,
Releasever VARCHAR(255),
Dist VARCHAR(255),
Section VARCHAR(255),
Source VARCHAR(255) NOT NULL,
Package_type VARCHAR(10) NOT NULL)");
/**
* repos_snap table
*/
$this->exec("CREATE TABLE IF NOT EXISTS repos_snap (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Date DATE NOT NULL,
Time TIME NOT NULL,
Signed CHAR(5) NOT NULL, /* true, false */
Arch VARCHAR(255),
Pkg_translation VARCHAR(255),
Pkg_included VARCHAR(255),
Pkg_excluded VARCHAR(255),
Type CHAR(6) NOT NULL,
Reconstruct CHAR(8), /* needed, running, failed */
Status CHAR(8) NOT NULL,
Id_repo INTEGER NOT NULL)");
/**
* repos_env table
*/
$this->exec("CREATE TABLE IF NOT EXISTS repos_env (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Env VARCHAR(255),
Description VARCHAR(255),
Id_snap INTEGER NOT NULL)");
/**
* env table
*/
$this->exec("CREATE TABLE IF NOT EXISTS env (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Name VARCHAR(255) NOT NULL,
Color VARCHAR(255))");
/**
* Insert default env if table is empty
*/
$result = $this->query("SELECT Id FROM env");
if ($this->isempty($result) === true) {
$this->exec("INSERT INTO env ('Name', 'Color') VALUES ('preprod', '#ffffff')");
$this->exec("INSERT INTO env ('Name', 'Color') VALUES ('prod', '#F32F63')");
}
/**
* sources table
*/
$this->exec("CREATE TABLE IF NOT EXISTS sources (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Definition TEXT,
Method VARCHAR(255))");
/**
* users table
*/
$this->exec("CREATE TABLE IF NOT EXISTS users (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Username VARCHAR(255) NOT NULL,
Password CHAR(60),
Api_key CHAR(32),
First_name VARCHAR(50),
Last_name VARCHAR(50),
Email VARCHAR(100),
Role INTEGER NOT NULL,
Type CHAR(5) NOT NULL,
State CHAR(7) NOT NULL)"); /* active / deleted */
/**
* user_role table
*/
$this->exec("CREATE TABLE IF NOT EXISTS user_role (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Name CHAR(15) NOT NULL UNIQUE)");
/**
* If user_role table is empty (just created) then we create default roles
*/
$result = $this->query("SELECT Id FROM user_role");
if ($this->isempty($result) === true) {
/**
* super-administrator role: all permissions
*/
$this->exec("INSERT INTO user_role ('Name') VALUES ('super-administrator')");
/**
* administrator role: all permissions except user management (only super-administrator can manage users)
*/
$this->exec("INSERT INTO user_role ('Name') VALUES ('administrator')");
/**
* usage role: read-only permissions
*/
$this->exec("INSERT INTO user_role ('Name') VALUES ('usage')");
}
/**
* If users table is empty (just created) then we create admin user (default password 'repomanager' and role n°1 (super-administrator))
*/
$result = $this->query("SELECT Id FROM users");
if ($this->isempty($result) === true) {
$password_hashed = '$2y$10$FD6/70o2nXPf76SAPYIGSutauQ96LqKie5PLanoYBNbCWen492cX6';
try {
$stmt = $this->prepare("INSERT INTO users ('Username', 'Password', 'First_name', 'Role', 'State', 'Type') VALUES ('admin', :password_hashed, 'Administrator', '1', 'active', 'local')");
$stmt->bindValue(':password_hashed', $password_hashed);
$stmt->execute();
} catch (\Exception $e) {
$this->logError($e);
}
}
/**
* history table
*/
$this->exec("CREATE TABLE IF NOT EXISTS history (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Date DATE NOT NULL,
Time TIME NOT NULL,
Id_user INTEGER NOT NULL,
Username VARCHAR(255),
Ip VARCHAR(255),
Ip_forwarded VARCHAR(255),
User_agent VARCHAR(255),
Action VARCHAR(255) NOT NULL,
State CHAR(7))"); /* success or error */
/**
* groups table
*/
$this->exec("CREATE TABLE IF NOT EXISTS groups (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Name VARCHAR(255) UNIQUE NOT NULL)");
/**
* group_members table
*/
$this->exec("CREATE TABLE IF NOT EXISTS group_members (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Id_repo INTEGER NOT NULL,
Id_group INTEGER NOT NULL);");
$this->exec("CREATE TABLE IF NOT EXISTS tasks (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Type CHAR(9), /* immediate, scheduled */
Date DATE,
Time TIME,
Raw_params TEXT NOT NULL,
Pid INTEGER,
Logfile VARCHAR(255),
Duration INTEGER,
Status CHAR(9))"); /* new, scheduled, running, done, stopped */
/**
* Create indexes
*/
$this->exec("CREATE INDEX IF NOT EXISTS tasks_rawparams_status ON tasks (Raw_params, Status)");
/**
* profile_settings table
*/
$this->exec("CREATE TABLE IF NOT EXISTS profile_settings (
Package_type VARCHAR(255))");
/**
* If profile_settings table is empty (just created) then we populate it
*/
$result = $this->query("SELECT * FROM profile_settings");
if ($this->isempty($result) === true) {
$this->exec("INSERT INTO profile_settings (Package_type) VALUES ('deb,rpm')");
}
/**
* profile table
*/
$this->exec("CREATE TABLE IF NOT EXISTS profile (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Name VARCHAR(255) NOT NULL,
Package_exclude VARCHAR(255),
Package_exclude_major VARCHAR(255),
Service_reload VARCHAR(255),
Service_restart VARCHAR(255),
Notes VARCHAR(255))");
/**
* profile_repo_members table
*/
$this->exec("CREATE TABLE IF NOT EXISTS profile_repo_members (
Id_profile INTEGER NOT NULL,
Id_repo INTEGER NOT NULL)");
/**
* profile_package table
*/
$this->exec("CREATE TABLE IF NOT EXISTS profile_package (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Name VARCHAR(255) UNIQUE NOT NULL)");
/**
* If profile_package table is empty (just created) then we populate it
*/
$result = $this->query("SELECT Id FROM profile_package");
if ($this->isempty($result) === true) {
$this->exec("INSERT INTO profile_package (Name) VALUES ('apache'), ('httpd'), ('php'), ('php-fpm'), ('mysql'), ('fail2ban'), ('nrpe'), ('munin-node'), ('node'), ('newrelic'), ('nginx'), ('haproxy'), ('netdata'), ('nfs'), ('rsnapshot'), ('kernel'), ('java'), ('redis'), ('varnish'), ('mongo'), ('rabbit'), ('clamav'), ('clam'), ('gpg'), ('gnupg')");
}
/**
* profile_service table
*/
$this->exec("CREATE TABLE IF NOT EXISTS profile_service (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Name VARCHAR(255) UNIQUE NOT NULL)");
/**
* If profile_service table is empty (just created) then we populate it
*/
$result = $this->query("SELECT Id FROM profile_service");
if ($this->isempty($result) === true) {
$this->exec("INSERT INTO profile_service (Name) VALUES ('apache'), ('httpd'), ('php-fpm'), ('mysqld'), ('fail2ban'), ('nrpe'), ('munin-node'), ('nginx'), ('haproxy'), ('netdata'), ('nfsd'), ('redis'), ('varnish'), ('mongod'), ('clamd')");
}
/**
* notifications table
*/
$this->exec("CREATE TABLE IF NOT EXISTS notifications (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Id_notification CHAR(5) NOT NULL,
Title VARCHAR(255) NOT NULL,
Message VARCHAR(255) NOT NULL,
Status CHAR(9) NOT NULL)"); /* new, acquitted */
/**
* settings table
*/
$this->exec("CREATE TABLE IF NOT EXISTS settings (
/* General settings */
DEBUG_MODE CHAR(5),
TIMEZONE VARCHAR(255),
EMAIL_RECIPIENT VARCHAR(255),
PROXY VARCHAR(255),
TASK_EXECUTION_MEMORY_LIMIT INTEGER,
TASK_QUEUING CHAR(5),
TASK_QUEUING_MAX_SIMULTANEOUS INTEGER,
TASK_CLEAN_OLDER_THAN INTEGER,
/* Repo settings */
RETENTION INTEGER,
REPO_CONF_FILES_PREFIX VARCHAR(255),
/* Mirroring */
MIRRORING_PACKAGE_DOWNLOAD_TIMEOUT INTEGER,
/* RPM */
RPM_REPO CHAR(5),
RPM_SIGN_PACKAGES CHAR(5),
RELEASEVER CHAR(5),
RPM_DEFAULT_ARCH VARCHAR(255),
RPM_MISSING_SIGNATURE VARCHAR(255), /* download, ignore, error */
RPM_INVALID_SIGNATURE VARCHAR(255), /* download, ignore, error */
/* DEB */
DEB_REPO CHAR(5),
DEB_SIGN_REPO CHAR(5),
DEB_DEFAULT_ARCH VARCHAR(255),
DEB_DEFAULT_TRANSLATION VARCHAR(255),
DEB_ALLOW_EMPTY_REPO CHAR(5),
DEB_INVALID_SIGNATURE VARCHAR(255), /* ignore, error */
/* GPG signing key */
GPG_SIGNING_KEYID VARCHAR(255),
/* Scheduled tasks settings */
SCHEDULED_TASKS_REMINDERS CHAR(5),
/* Statistics & metrics settings */
STATS_ENABLED CHAR(5),
/* Hosts and profiles settings */
MANAGE_HOSTS CHAR(5),
/* CVE settings */
CVE_IMPORT CHAR(5),
CVE_IMPORT_TIME TIME,
CVE_SCAN_HOSTS CHAR(5),
/* OIDC settings */
OIDC_ENABLED CHAR(5),
SSO_OIDC_ONLY CHAR(5),
OIDC_PROVIDER_URL VARCHAR(255),
OIDC_AUTHORIZATION_ENDPOINT VARCHAR(255),
OIDC_TOKEN_ENDPOINT VARCHAR(255),
OIDC_USERINFO_ENDPOINT VARCHAR(255),
OIDC_SCOPES VARCHAR(255),
OIDC_CLIENT_ID VARCHAR(255),
OIDC_CLIENT_SECRET VARCHAR(255),
OIDC_USERNAME VARCHAR(255),
OIDC_FIRST_NAME VARCHAR(255),
OIDC_LAST_NAME VARCHAR(255),
OIDC_EMAIL VARCHAR(255),
OIDC_GROUPS VARCHAR(255),
OIDC_GROUP_ADMINISTRATOR VARCHAR(255),
OIDC_GROUP_SUPER_ADMINISTRATOR VARCHAR(255),
OIDC_HTTP_PROXY VARCHAR(255),
OIDC_CERT_PATH VARCHAR(255))");
/**
* If settings table is empty then populate it
*/
$result = $this->query("SELECT * FROM settings");
if ($this->isempty($result) === true) {
/**
* Set default values
*/
$fqdn = 'localhost';
/**
* FQDN file is created on container startup (entrypoint)
*/
if (file_exists(ROOT . '/.fqdn')) {
$fqdn = trim(file_get_contents(ROOT . '/.fqdn'));
}
/**
* GPG key Id
*/
$gpgKeyId = 'repomanager@' . $fqdn;
/**
* For each OIDC setting, use the value from custom settings file (app.yaml) if defined, otherwise use default value
* (if a value was not defined, it means that there was no app.yaml file or the value was not defined in it)
*/
$oidcEnabled = defined('OIDC_ENABLED') ? OIDC_ENABLED : 'false';
$ssoOidcOnly = defined('SSO_OIDC_ONLY') ? SSO_OIDC_ONLY : 'false';
$oidcProviderUrl = defined('OIDC_PROVIDER_URL') ? OIDC_PROVIDER_URL : '';
$oidcAuthorizationEndpoint = defined('OIDC_AUTHORIZATION_ENDPOINT') ? OIDC_AUTHORIZATION_ENDPOINT : '';
$oidcTokenEndpoint = defined('OIDC_TOKEN_ENDPOINT') ? OIDC_TOKEN_ENDPOINT : '';
$oidcUserinfoEndpoint = defined('OIDC_USERINFO_ENDPOINT') ? OIDC_USERINFO_ENDPOINT : '';
$oidcScopes = defined('OIDC_SCOPES') ? OIDC_SCOPES : 'groups,email,profile';
$oidcClientId = defined('OIDC_CLIENT_ID') ? OIDC_CLIENT_ID : '';
$oidcClientSecret = defined('OIDC_CLIENT_SECRET') ? OIDC_CLIENT_SECRET : '';
$oidcUsername = defined('OIDC_USERNAME') ? OIDC_USERNAME : 'preferred_username';
$oidcFirstName = defined('OIDC_FIRST_NAME') ? OIDC_FIRST_NAME : 'given_name';
$oidcLastName = defined('OIDC_LAST_NAME') ? OIDC_LAST_NAME : 'family_name';
$oidcEmail = defined('OIDC_EMAIL') ? OIDC_EMAIL : 'email';
$oidcGroups = defined('OIDC_GROUPS') ? OIDC_GROUPS : 'groups';
$oidcGroupAdministrator = defined('OIDC_GROUP_ADMINISTRATOR') ? OIDC_GROUP_ADMINISTRATOR : 'administrator';
$oidcGroupSuperAdministrator = defined('OIDC_GROUP_SUPER_ADMINISTRATOR') ? OIDC_GROUP_SUPER_ADMINISTRATOR : 'super-administrator';
$oidcHttpProxy = defined('OIDC_HTTP_PROXY') ? OIDC_HTTP_PROXY : '';
$oidcCertPath = defined('OIDC_CERT_PATH') ? OIDC_CERT_PATH : '';
$this->exec("INSERT INTO settings (
EMAIL_RECIPIENT,
DEBUG_MODE,
REPO_CONF_FILES_PREFIX,
TIMEZONE,
TASK_EXECUTION_MEMORY_LIMIT,
TASK_QUEUING,
TASK_QUEUING_MAX_SIMULTANEOUS,
TASK_CLEAN_OLDER_THAN,
MIRRORING_PACKAGE_DOWNLOAD_TIMEOUT,
RPM_REPO,
RPM_SIGN_PACKAGES,
RELEASEVER,
RPM_DEFAULT_ARCH,
RPM_MISSING_SIGNATURE,
RPM_INVALID_SIGNATURE,
DEB_REPO,
DEB_SIGN_REPO,
DEB_DEFAULT_ARCH,
DEB_DEFAULT_TRANSLATION,
DEB_ALLOW_EMPTY_REPO,
DEB_INVALID_SIGNATURE,
GPG_SIGNING_KEYID,
SCHEDULED_TASKS_REMINDERS,
RETENTION,
STATS_ENABLED,
MANAGE_HOSTS,
CVE_IMPORT,
CVE_IMPORT_TIME,
CVE_SCAN_HOSTS,
OIDC_ENABLED,
SSO_OIDC_ONLY,
OIDC_PROVIDER_URL,
OIDC_AUTHORIZATION_ENDPOINT,
OIDC_TOKEN_ENDPOINT,
OIDC_USERINFO_ENDPOINT,
OIDC_SCOPES,
OIDC_CLIENT_ID,
OIDC_CLIENT_SECRET,
OIDC_USERNAME,
OIDC_FIRST_NAME,
OIDC_LAST_NAME,
OIDC_EMAIL,
OIDC_GROUPS,
OIDC_GROUP_ADMINISTRATOR,
OIDC_GROUP_SUPER_ADMINISTRATOR,
OIDC_HTTP_PROXY,
OIDC_CERT_PATH
)
VALUES (
'',
'false',
'repomanager-',
'Europe/Paris',
'1024',
'false',
'3',
'730',
'300',
'true',
'true',
'8',
'noarch,x86_64',
'error',
'error',
'true',
'true',
'amd64',
'',
'false',
'error',
'$gpgKeyId',
'false',
'3',
'false',
'false',
'false',
'00:00',
'false',
'$oidcEnabled',
'$ssoOidcOnly',
'$oidcProviderUrl',
'$oidcAuthorizationEndpoint',
'$oidcTokenEndpoint',
'$oidcUserinfoEndpoint',
'$oidcScopes',
'$oidcClientId',
'$oidcClientSecret',
'$oidcUsername',
'$oidcFirstName',
'$oidcLastName',
'$oidcEmail',
'$oidcGroups',
'$oidcGroupAdministrator',
'$oidcGroupSuperAdministrator',
'$oidcHttpProxy',
'$oidcCertPath'
)");
}
/**
* Generate cve table if not exists
* CVEs table
*/
$this->exec("CREATE TABLE IF NOT EXISTS cve (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Name VARCHAR(255) NOT NULL,
Date DATE,
Time TIME,
Updated_date DATE,
Updated_time TIME,
Cpe23Uri VARCHAR(255),
Parts VARCHAR(255),
Description VARCHAR(255),
Cvss2_score CHAR(3),
Cvss3_score CHAR(3))");
$this->exec("CREATE TABLE IF NOT EXISTS cve_cpe (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Part CHAR(1) NOT NULL,
Vendor VARCHAR(255) NOT NULL COLLATE NOCASE,
Product VARCHAR(255) NOT NULL COLLATE NOCASE,
Version VARCHAR(255) NOT NULL COLLATE NOCASE,
Id_cve INTEGER NOT NULL)");
$this->exec("CREATE TABLE IF NOT EXISTS cve_reference (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Name VARCHAR(255),
Url VARCHAR(255),
Source VARCHAR(255),
Tags VARCHAR(255),
Id_cve INTEGER NOT NULL)");
$this->exec("CREATE TABLE IF NOT EXISTS cve_affected_hosts (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Host_id INTEGER NOT NULL,
Product VARCHAR(255) NOT NULL,
Version VARCHAR(255) NOT NULL,
Status CHAR(8) NOT NULL, /* possible / affected */
Id_cve INTEGER NOT NULL)");
$this->exec("CREATE TABLE IF NOT EXISTS cve_import (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Date DATE NOT NULL,
Time TIME NOT NULL,
Duration INTEGER,
Status CHAR(7) NOT NULL)"); /* running, error, done */
$this->exec("CREATE TABLE IF NOT EXISTS cve_affected_hosts_import (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Date DATE NOT NULL,
Time TIME NOT NULL,
Duration INTEGER,
Status CHAR(7) NOT NULL)"); /* running, error, done */
/**
* Create indexes
*/
$this->exec("CREATE INDEX IF NOT EXISTS cve_index ON cve (Name, Updated_date, Updated_time)");
$this->exec("CREATE INDEX IF NOT EXISTS cve_cpe_index ON cve_cpe (Part, Vendor, Product, Version, Id_cve)");
$this->exec("CREATE INDEX IF NOT EXISTS cve_affected_hosts_index ON cve_affected_hosts (Status, Id_cve)");
/**
* Generate logs table if not exists
*/
$this->exec("CREATE TABLE IF NOT EXISTS logs (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Date DATE NOT NULL,
Time TIME NOT NULL,
Type CHAR(5) NOT NULL, /* info, error */
Component VARCHAR(255),
Message VARCHAR(255) NOT NULL,
Details TEXT,
Status CHAR(9) NOT NULL)"); /* new, acquitted */
/**
* Generate layout_container_state table if not exists
*/
$this->exec("CREATE TABLE IF NOT EXISTS layout_container_state (
Container VARCHAR(255) NOT NULL)");
}
/**
* Generate tables in the stats database
*/
private function generateStatsTables()
{
/**
* stats table
*/
$this->exec("CREATE TABLE IF NOT EXISTS stats (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Date DATE NOT NULL,
Time TIME NOT NULL,
Size INTEGER NOT NULL,
Packages_count INTEGER NOT NULL,
Id_env INTEGER NOT NULL)");
/**
* access_deb table
*/
$this->exec("CREATE TABLE IF NOT EXISTS access_deb (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Date DATE NOT NULL,
Time TIME NOT NULL,
Name VARCHAR(255) NOT NULL,
Dist VARCHAR(255) NOT NULL,
Section VARCHAR(255) NOT NULL,
Env VARCHAR(255) NOT NULL,
Source VARCHAR(255) NOT NULL,
IP VARCHAR(16) NOT NULL,
Request VARCHAR(255) NOT NULL,
Request_result VARCHAR(8) NOT NULL)");
/**
* access_rpm table
*/
$this->exec("CREATE TABLE IF NOT EXISTS access_rpm (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Date DATE NOT NULL,
Time TIME NOT NULL,
Name VARCHAR(255) NOT NULL,
Env VARCHAR(255) NOT NULL,
Source VARCHAR(255) NOT NULL,
IP VARCHAR(16) NOT NULL,
Request VARCHAR(255) NOT NULL,
Request_result VARCHAR(8) NOT NULL)");
/**
* access_queue table
*/
$this->exec("CREATE TABLE IF NOT EXISTS access_queue (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Request VARCHAR(255) NOT NULL)");
/**
* Create indexes
*/
// Indexes for access_deb:
$this->exec("CREATE INDEX IF NOT EXISTS access_deb_index ON access_deb (Date, Time, Name, Dist, Section, Env, Source, IP, Request, Request_result)");
$this->exec("CREATE INDEX IF NOT EXISTS access_deb_name_env_index ON access_deb (Name, Dist, Section, Env)"); // To optimize SELECT COUNT(*)
// Indexes for access_rpm:
$this->exec("CREATE INDEX IF NOT EXISTS access_rpm_index ON access_rpm (Date, Time, Name, Env, Source, IP, Request, Request_result)");
$this->exec("CREATE INDEX IF NOT EXISTS access_rpm_name_env_index ON access_rpm (Name, Env)"); // To optimize SELECT COUNT(*)
// Index for stats:
$this->exec("CREATE INDEX IF NOT EXISTS stats_index ON stats (Date, Time, Size, Packages_count, Id_env)");
}
/**
* Generate tables in the hosts database
*/
private function generateHostsTables()
{
/**
* requests table
*/
$this->exec("CREATE TABLE IF NOT EXISTS requests (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Date DATE NOT NULL,
Time TIME NOT NULL,
Request VARCHAR(255) NOT NULL,
Status VARCHAR(255) NOT NULL, /* new, sent, received, failed, completed */
Info VARCHAR(255), /* error or info message */
Response VARCHAR(255),
Response_json VARCHAR(255),
Retry INTEGER NOT NULL,
Next_retry VARCHAR(255),
Id_host INTEGER NOT NULL)");
/**
* hosts table
*/
$this->exec("CREATE TABLE IF NOT EXISTS hosts (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Ip VARCHAR(15) NOT NULL,
Hostname VARCHAR(255) NOT NULL,
Os VARCHAR(255),
Os_version VARCHAR(255),
Os_family VARCHAR(255),
Kernel VARCHAR(255),
Arch CHAR(10),
Type VARCHAR(255),
Profile VARCHAR(255),
Env VARCHAR(255),
AuthId VARCHAR(255),
Token VARCHAR(255),
Online_status CHAR(8), /* online / unreachable */
Online_status_date DATE,
Online_status_time TIME,
Reboot_required CHAR(5),
Linupdate_version VARCHAR(255),
Status VARCHAR(8) NOT NULL)"); /* active / disabled / deleted */
/**
* groups table
*/
$this->exec("CREATE TABLE IF NOT EXISTS groups (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Name VARCHAR(255) UNIQUE NOT NULL)");
/**
* group_members table
*/
$this->exec("CREATE TABLE IF NOT EXISTS group_members (
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Id_host INTEGER NOT NULL,
Id_group INTEGER NOT NULL)");
/**
* settings table
*/
$this->exec("CREATE TABLE IF NOT EXISTS settings (
pkgs_count_considered_outdated INTEGER NOT NULL,
pkgs_count_considered_critical INTEGER NOT NULL)");
/**
* If settings table is empty then populate it
*/
$result = $this->query("SELECT pkgs_count_considered_outdated FROM settings");
if ($this->isempty($result) === true) {
$this->exec("INSERT INTO settings ('pkgs_count_considered_outdated', 'pkgs_count_considered_critical') VALUES ('1', '10')");
}
/**
* Create indexes
*/
// hosts table indexes:
$this->exec("CREATE INDEX IF NOT EXISTS hosts_index ON hosts (Ip, Hostname, Os, Os_version, Os_family, Kernel, Arch, Type, Profile, Env, AuthId, Token, Online_status, Online_status_date, Online_status_time, Reboot_required, Linupdate_version, Status)");
$this->exec("CREATE INDEX IF NOT EXISTS hosts_authid_index ON hosts (AuthId)");
$this->exec("CREATE INDEX IF NOT EXISTS hosts_token_index ON hosts (Token)");
$this->exec("CREATE INDEX IF NOT EXISTS hosts_authid_token_status_index ON hosts (AuthId, Token, Status)");
$this->exec("CREATE INDEX IF NOT EXISTS hosts_hostname_index ON hosts (Hostname)");
$this->exec("CREATE INDEX IF NOT EXISTS hosts_kernel_index ON hosts (Kernel)");
$this->exec("CREATE INDEX IF NOT EXISTS hosts_profile_index ON hosts (Profile)");
$this->exec("CREATE INDEX IF NOT EXISTS hosts_status_online_status_date_time ON hosts (Status, Online_status, Online_status_date, Online_status_time)");
// groups table indexes:
$this->exec("CREATE INDEX IF NOT EXISTS groups_index ON groups (Name)");
// group_members table indexes:
$this->exec("CREATE INDEX IF NOT EXISTS group_members_index ON group_members (Id_host, Id_group)");
$this->exec("CREATE INDEX IF NOT EXISTS group_members_id_host_index ON group_members (Id_host)");
$this->exec("CREATE INDEX IF NOT EXISTS group_members_id_group_index ON group_members (Id_group)");
// requests table indexes:
$this->exec("CREATE INDEX IF NOT EXISTS requests_id_host ON requests (Id_host)");
$this->exec("CREATE INDEX IF NOT EXISTS requests_status ON requests (Status)");
$this->exec("CREATE INDEX IF NOT EXISTS requests_date_time ON requests (Date, Time)");
}
/**
* Generate tables in the database dedicated to a host
* This function is public because it can be called when resetting a host
*/
public function generateHostTables()
{
/**
* packages table
* Inventory of all packages installed on the host
*/