-
Notifications
You must be signed in to change notification settings - Fork 109
/
schema.sql
664 lines (605 loc) · 30.7 KB
/
schema.sql
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
-- Copyright 2023 Specter Ops, Inc.
--
-- Licensed under the Apache License, Version 2.0
-- 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.
--
-- SPDX-License-Identifier: Apache-2.0
CREATE TABLE IF NOT EXISTS ad_data_quality_aggregations (
domains bigint,
users bigint,
groups bigint,
computers bigint,
ous bigint,
containers bigint,
gpos bigint,
acls bigint,
sessions bigint,
relationships bigint,
session_completeness numeric,
local_group_completeness numeric,
run_id text,
id integer NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS ad_data_quality_aggregations_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE ad_data_quality_aggregations_id_seq OWNED BY ad_data_quality_aggregations.id;
CREATE TABLE IF NOT EXISTS ad_data_quality_stats (
domain_sid text,
users bigint,
groups bigint,
computers bigint,
ous bigint,
containers bigint,
gpos bigint,
acls bigint,
sessions bigint,
relationships bigint,
session_completeness numeric,
local_group_completeness numeric,
run_id text,
id integer NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS ad_data_quality_stats_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE ad_data_quality_stats_id_seq OWNED BY ad_data_quality_stats.id;
CREATE TABLE IF NOT EXISTS asset_group_collection_entries (
asset_group_collection_id bigint,
object_id text,
node_label text,
properties jsonb,
id bigint NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS asset_group_collection_entries_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE asset_group_collection_entries_id_seq OWNED BY asset_group_collection_entries.id;
CREATE TABLE IF NOT EXISTS asset_group_collections (
asset_group_id integer,
id bigint NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS asset_group_collections_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE asset_group_collections_id_seq OWNED BY asset_group_collections.id;
CREATE TABLE IF NOT EXISTS asset_group_selectors (
asset_group_id integer,
name text,
selector text,
system_selector boolean,
id integer NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS asset_group_selectors_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE asset_group_selectors_id_seq OWNED BY asset_group_selectors.id;
CREATE TABLE IF NOT EXISTS asset_groups (
name text,
tag text,
system_group boolean,
id integer NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS asset_groups_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE asset_groups_id_seq OWNED BY asset_groups.id;
CREATE TABLE IF NOT EXISTS audit_logs (
id bigint NOT NULL,
created_at timestamp with time zone,
actor_id text,
actor_name text,
action text,
fields jsonb,
request_id text
);
CREATE SEQUENCE IF NOT EXISTS audit_logs_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE audit_logs_id_seq OWNED BY audit_logs.id;
CREATE TABLE IF NOT EXISTS auth_secrets (
user_id text,
digest text,
digest_method text,
expires_at timestamp with time zone,
totp_secret text,
totp_activated boolean,
id integer NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS auth_secrets_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE auth_secrets_id_seq OWNED BY auth_secrets.id;
CREATE TABLE IF NOT EXISTS auth_tokens (
user_id text,
client_id text,
name text,
key text,
hmac_method text,
last_access timestamp with time zone,
id text NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE TABLE IF NOT EXISTS azure_data_quality_aggregations (
tenants bigint,
users bigint,
groups bigint,
apps bigint,
service_principals bigint,
devices bigint,
management_groups bigint,
subscriptions bigint,
resource_groups bigint,
vms bigint,
key_vaults bigint,
relationships bigint,
run_id text,
id integer NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS azure_data_quality_aggregations_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE azure_data_quality_aggregations_id_seq OWNED BY azure_data_quality_aggregations.id;
CREATE TABLE IF NOT EXISTS azure_data_quality_stats (
tenant_id text,
users bigint,
groups bigint,
apps bigint,
service_principals bigint,
devices bigint,
management_groups bigint,
subscriptions bigint,
resource_groups bigint,
vms bigint,
key_vaults bigint,
relationships bigint,
run_id text,
id integer NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS azure_data_quality_stats_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE azure_data_quality_stats_id_seq OWNED BY azure_data_quality_stats.id;
CREATE TABLE IF NOT EXISTS domain_collection_results (
job_id bigint,
domain_name text,
success boolean,
message text,
user_count bigint,
group_count bigint,
computer_count bigint,
gpo_count bigint,
ou_count bigint,
container_count bigint,
deleted_count bigint,
id bigint NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS domain_collection_results_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE domain_collection_results_id_seq OWNED BY domain_collection_results.id;
CREATE TABLE IF NOT EXISTS feature_flags (
id integer NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone,
key text,
name text,
description text,
enabled boolean,
user_updatable boolean
);
CREATE SEQUENCE IF NOT EXISTS feature_flags_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE feature_flags_id_seq OWNED BY feature_flags.id;
CREATE TABLE IF NOT EXISTS file_upload_jobs (
user_id text,
user_email_address text,
status bigint,
status_message text,
start_time timestamp with time zone,
end_time timestamp with time zone,
last_ingest timestamp with time zone,
id bigint NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS file_upload_jobs_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE file_upload_jobs_id_seq OWNED BY file_upload_jobs.id;
CREATE TABLE IF NOT EXISTS ingest_tasks (
file_name text,
request_guid text,
task_id bigint,
id bigint NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS ingest_tasks_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE ingest_tasks_id_seq OWNED BY ingest_tasks.id;
CREATE TABLE IF NOT EXISTS installations (
id text NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE TABLE IF NOT EXISTS parameters (
key text,
name text,
description text,
value jsonb,
id integer NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS parameters_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE parameters_id_seq OWNED BY parameters.id;
CREATE TABLE IF NOT EXISTS permissions (
authority text,
name text,
id integer NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS permissions_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE permissions_id_seq OWNED BY permissions.id;
CREATE TABLE IF NOT EXISTS roles (
name text,
description text,
id integer NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS roles_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE roles_id_seq OWNED BY roles.id;
CREATE TABLE IF NOT EXISTS roles_permissions (
role_id integer NOT NULL,
permission_id integer NOT NULL
);
CREATE TABLE IF NOT EXISTS saml_providers (
name text,
display_name text,
issuer_uri text,
single_sign_on_uri text,
metadata_xml bytea,
ous text[],
id integer NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS saml_providers_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE saml_providers_id_seq OWNED BY saml_providers.id;
CREATE TABLE IF NOT EXISTS saved_queries (
user_id text,
name text,
query text,
id bigint NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS saved_queries_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE saved_queries_id_seq OWNED BY saved_queries.id;
CREATE TABLE IF NOT EXISTS user_sessions (
user_id text,
auth_provider_type bigint,
auth_provider_id integer,
expires_at timestamp with time zone,
id bigint NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE IF NOT EXISTS user_sessions_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE user_sessions_id_seq OWNED BY user_sessions.id;
CREATE TABLE IF NOT EXISTS users (
saml_provider_id integer,
first_name text,
last_name text,
email_address text,
principal_name text,
last_login timestamp with time zone,
is_disabled boolean,
eula_accepted boolean,
id text NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE TABLE IF NOT EXISTS users_roles (
user_id text NOT NULL,
role_id integer NOT NULL
);
ALTER TABLE ONLY ad_data_quality_aggregations ALTER COLUMN id SET DEFAULT nextval('ad_data_quality_aggregations_id_seq'::regclass);
ALTER TABLE ONLY ad_data_quality_stats ALTER COLUMN id SET DEFAULT nextval('ad_data_quality_stats_id_seq'::regclass);
ALTER TABLE ONLY asset_group_collection_entries ALTER COLUMN id SET DEFAULT nextval('asset_group_collection_entries_id_seq'::regclass);
ALTER TABLE ONLY asset_group_collections ALTER COLUMN id SET DEFAULT nextval('asset_group_collections_id_seq'::regclass);
ALTER TABLE ONLY asset_group_selectors ALTER COLUMN id SET DEFAULT nextval('asset_group_selectors_id_seq'::regclass);
ALTER TABLE ONLY asset_groups ALTER COLUMN id SET DEFAULT nextval('asset_groups_id_seq'::regclass);
ALTER TABLE ONLY audit_logs ALTER COLUMN id SET DEFAULT nextval('audit_logs_id_seq'::regclass);
ALTER TABLE ONLY auth_secrets ALTER COLUMN id SET DEFAULT nextval('auth_secrets_id_seq'::regclass);
ALTER TABLE ONLY azure_data_quality_aggregations ALTER COLUMN id SET DEFAULT nextval('azure_data_quality_aggregations_id_seq'::regclass);
ALTER TABLE ONLY azure_data_quality_stats ALTER COLUMN id SET DEFAULT nextval('azure_data_quality_stats_id_seq'::regclass);
ALTER TABLE ONLY domain_collection_results ALTER COLUMN id SET DEFAULT nextval('domain_collection_results_id_seq'::regclass);
ALTER TABLE ONLY feature_flags ALTER COLUMN id SET DEFAULT nextval('feature_flags_id_seq'::regclass);
ALTER TABLE ONLY file_upload_jobs ALTER COLUMN id SET DEFAULT nextval('file_upload_jobs_id_seq'::regclass);
ALTER TABLE ONLY ingest_tasks ALTER COLUMN id SET DEFAULT nextval('ingest_tasks_id_seq'::regclass);
ALTER TABLE ONLY parameters ALTER COLUMN id SET DEFAULT nextval('parameters_id_seq'::regclass);
ALTER TABLE ONLY permissions ALTER COLUMN id SET DEFAULT nextval('permissions_id_seq'::regclass);
ALTER TABLE ONLY roles ALTER COLUMN id SET DEFAULT nextval('roles_id_seq'::regclass);
ALTER TABLE ONLY saml_providers ALTER COLUMN id SET DEFAULT nextval('saml_providers_id_seq'::regclass);
ALTER TABLE ONLY saved_queries ALTER COLUMN id SET DEFAULT nextval('saved_queries_id_seq'::regclass);
ALTER TABLE ONLY user_sessions ALTER COLUMN id SET DEFAULT nextval('user_sessions_id_seq'::regclass);
ALTER TABLE ONLY ad_data_quality_aggregations
ADD CONSTRAINT ad_data_quality_aggregations_pkey PRIMARY KEY (id);
ALTER TABLE ONLY ad_data_quality_stats
ADD CONSTRAINT ad_data_quality_stats_pkey PRIMARY KEY (id);
ALTER TABLE ONLY asset_group_collection_entries
ADD CONSTRAINT asset_group_collection_entries_pkey PRIMARY KEY (id);
ALTER TABLE ONLY asset_group_collections
ADD CONSTRAINT asset_group_collections_pkey PRIMARY KEY (id);
ALTER TABLE ONLY asset_group_selectors
ADD CONSTRAINT asset_group_selectors_name_key UNIQUE (name);
ALTER TABLE ONLY asset_group_selectors
ADD CONSTRAINT asset_group_selectors_pkey PRIMARY KEY (id);
ALTER TABLE ONLY asset_groups
ADD CONSTRAINT asset_groups_pkey PRIMARY KEY (id);
ALTER TABLE ONLY audit_logs
ADD CONSTRAINT audit_logs_pkey PRIMARY KEY (id);
ALTER TABLE ONLY auth_secrets
ADD CONSTRAINT auth_secrets_pkey PRIMARY KEY (id);
ALTER TABLE ONLY auth_tokens
ADD CONSTRAINT auth_tokens_pkey PRIMARY KEY (id);
ALTER TABLE ONLY azure_data_quality_aggregations
ADD CONSTRAINT azure_data_quality_aggregations_pkey PRIMARY KEY (id);
ALTER TABLE ONLY azure_data_quality_stats
ADD CONSTRAINT azure_data_quality_stats_pkey PRIMARY KEY (id);
ALTER TABLE ONLY domain_collection_results
ADD CONSTRAINT domain_collection_results_pkey PRIMARY KEY (id);
ALTER TABLE ONLY feature_flags
ADD CONSTRAINT feature_flags_key_key UNIQUE (key);
ALTER TABLE ONLY feature_flags
ADD CONSTRAINT feature_flags_pkey PRIMARY KEY (id);
ALTER TABLE ONLY file_upload_jobs
ADD CONSTRAINT file_upload_jobs_pkey PRIMARY KEY (id);
ALTER TABLE ONLY asset_group_selectors
ADD CONSTRAINT idx_asset_group_selectors_name UNIQUE (name);
ALTER TABLE ONLY ingest_tasks
ADD CONSTRAINT ingest_tasks_pkey PRIMARY KEY (id);
ALTER TABLE ONLY installations
ADD CONSTRAINT installations_pkey PRIMARY KEY (id);
ALTER TABLE ONLY parameters
ADD CONSTRAINT parameters_key_key UNIQUE (key);
ALTER TABLE ONLY parameters
ADD CONSTRAINT parameters_pkey PRIMARY KEY (id);
ALTER TABLE ONLY permissions
ADD CONSTRAINT permissions_pkey PRIMARY KEY (id);
ALTER TABLE ONLY roles_permissions
ADD CONSTRAINT roles_permissions_pkey PRIMARY KEY (role_id, permission_id);
ALTER TABLE ONLY roles
ADD CONSTRAINT roles_pkey PRIMARY KEY (id);
ALTER TABLE ONLY saml_providers
ADD CONSTRAINT saml_providers_name_key UNIQUE (name);
ALTER TABLE ONLY saml_providers
ADD CONSTRAINT saml_providers_pkey PRIMARY KEY (id);
ALTER TABLE ONLY saved_queries
ADD CONSTRAINT saved_queries_pkey PRIMARY KEY (id);
ALTER TABLE ONLY user_sessions
ADD CONSTRAINT user_sessions_pkey PRIMARY KEY (id);
ALTER TABLE ONLY users
ADD CONSTRAINT users_pkey PRIMARY KEY (id);
ALTER TABLE ONLY users
ADD CONSTRAINT users_principal_name_key UNIQUE (principal_name);
ALTER TABLE ONLY users_roles
ADD CONSTRAINT users_roles_pkey PRIMARY KEY (user_id, role_id);
CREATE INDEX IF NOT EXISTS idx_ad_data_quality_aggregations_run_id ON ad_data_quality_aggregations USING btree (run_id);
CREATE INDEX IF NOT EXISTS idx_ad_data_quality_stats_run_id ON ad_data_quality_stats USING btree (run_id);
CREATE INDEX IF NOT EXISTS idx_audit_logs_action ON audit_logs USING btree (action);
CREATE INDEX IF NOT EXISTS idx_audit_logs_actor_id ON audit_logs USING btree (actor_id);
CREATE INDEX IF NOT EXISTS idx_audit_logs_created_at ON audit_logs USING btree (created_at);
CREATE INDEX IF NOT EXISTS idx_azure_data_quality_aggregations_run_id ON azure_data_quality_aggregations USING btree (run_id);
CREATE INDEX IF NOT EXISTS idx_azure_data_quality_stats_run_id ON azure_data_quality_stats USING btree (run_id);
CREATE INDEX IF NOT EXISTS idx_saml_providers_name ON saml_providers USING btree (name);
CREATE UNIQUE INDEX IF NOT EXISTS idx_saved_queries_composite_index ON saved_queries USING btree (user_id, name);
CREATE INDEX IF NOT EXISTS idx_users_principal_name ON users USING btree (principal_name);
ALTER TABLE ONLY asset_group_collection_entries
ADD CONSTRAINT fk_asset_group_collections_entries FOREIGN KEY (asset_group_collection_id) REFERENCES asset_group_collections(id) ON DELETE CASCADE;
ALTER TABLE ONLY asset_group_collections
ADD CONSTRAINT fk_asset_groups_collections FOREIGN KEY (asset_group_id) REFERENCES asset_groups(id) ON DELETE CASCADE;
ALTER TABLE ONLY asset_group_selectors
ADD CONSTRAINT fk_asset_groups_selectors FOREIGN KEY (asset_group_id) REFERENCES asset_groups(id) ON DELETE CASCADE;
ALTER TABLE ONLY file_upload_jobs
ADD CONSTRAINT fk_file_upload_jobs_user FOREIGN KEY (user_id) REFERENCES users(id);
ALTER TABLE ONLY roles_permissions
ADD CONSTRAINT fk_roles_permissions_permission FOREIGN KEY (permission_id) REFERENCES permissions(id);
ALTER TABLE ONLY roles_permissions
ADD CONSTRAINT fk_roles_permissions_role FOREIGN KEY (role_id) REFERENCES roles(id);
ALTER TABLE ONLY user_sessions
ADD CONSTRAINT fk_user_sessions_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
ALTER TABLE ONLY auth_secrets
ADD CONSTRAINT fk_users_auth_secret FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
ALTER TABLE ONLY auth_tokens
ADD CONSTRAINT fk_users_auth_tokens FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
ALTER TABLE ONLY users_roles
ADD CONSTRAINT fk_users_roles_role FOREIGN KEY (role_id) REFERENCES roles(id);
ALTER TABLE ONLY users_roles
ADD CONSTRAINT fk_users_roles_user FOREIGN KEY (user_id) REFERENCES users(id);
ALTER TABLE ONLY users
ADD CONSTRAINT fk_users_saml_provider FOREIGN KEY (saml_provider_id) REFERENCES saml_providers(id);
-- Populate asset group table
INSERT INTO asset_groups (name, tag, system_group, created_at, updated_at) VALUES ('Admin Tier Zero', 'admin_tier_0', true, current_timestamp, current_timestamp);
-- Populate permissions table
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('app', 'ReadAppConfig', current_timestamp, current_timestamp);
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('app', 'WriteAppConfig', current_timestamp, current_timestamp);
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('risks', 'GenerateReport', current_timestamp, current_timestamp);
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('risks', 'ManageRisks', current_timestamp, current_timestamp);
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('auth', 'CreateToken', current_timestamp, current_timestamp);
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('auth', 'ManageAppConfig', current_timestamp, current_timestamp);
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('auth', 'ManageProviders', current_timestamp, current_timestamp);
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('auth', 'ManageSelf', current_timestamp, current_timestamp);
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('auth', 'ManageUsers', current_timestamp, current_timestamp);
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('clients', 'Manage', current_timestamp, current_timestamp);
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('clients', 'Tasking', current_timestamp, current_timestamp);
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('collection', 'ManageJobs', current_timestamp, current_timestamp);
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('graphdb', 'Read', current_timestamp, current_timestamp);
INSERT INTO permissions (authority, name, created_at, updated_at) VALUES ('graphdb', 'Write', current_timestamp, current_timestamp);
-- Populate roles table
INSERT INTO roles (name, description, created_at, updated_at) VALUES ('Administrator', 'Can manage users, clients, and application configuration', current_timestamp, current_timestamp);
INSERT INTO roles (name, description, created_at, updated_at) VALUES ('User', 'Can read data, modify asset group memberships', current_timestamp, current_timestamp);
INSERT INTO roles (name, description, created_at, updated_at) VALUES ('Read-Only', 'Used for integrations', current_timestamp, current_timestamp);
INSERT INTO roles (name, description, created_at, updated_at) VALUES ('Upload-Only', 'Used for data collection clients, can post data but cannot read data', current_timestamp, current_timestamp);
-- Populate roles_permissions table
-- Administrator
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'app' and permissions.name = 'ReadAppConfig'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'app' and permissions.name = 'WriteAppConfig'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'risks' and permissions.name = 'GenerateReport'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'risks' and permissions.name = 'ManageRisks'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'auth' and permissions.name = 'CreateToken'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'auth' and permissions.name = 'ManageAppConfig'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'auth' and permissions.name = 'ManageProviders'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'auth' and permissions.name = 'ManageSelf'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'auth' and permissions.name = 'ManageUsers'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'clients' and permissions.name = 'Manage'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'clients' and permissions.name = 'Tasking'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'collection' and permissions.name = 'ManageJobs'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'graphdb' and permissions.name = 'Read'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Administrator'), (SELECT id FROM permissions WHERE permissions.authority = 'graphdb' and permissions.name = 'Write'));
-- User
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'User'), (SELECT id FROM permissions WHERE permissions.authority = 'app' and permissions.name = 'ReadAppConfig'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'User'), (SELECT id FROM permissions WHERE permissions.authority = 'risks' and permissions.name = 'GenerateReport'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'User'), (SELECT id FROM permissions WHERE permissions.authority = 'auth' and permissions.name = 'CreateToken'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'User'), (SELECT id FROM permissions WHERE permissions.authority = 'auth' and permissions.name = 'ManageSelf'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'User'), (SELECT id FROM permissions WHERE permissions.authority = 'clients' and permissions.name = 'Manage'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'User'), (SELECT id FROM permissions WHERE permissions.authority = 'graphdb' and permissions.name = 'Read'));
-- Read-Only
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Read-Only'), (SELECT id FROM permissions WHERE permissions.authority = 'app' and permissions.name = 'ReadAppConfig'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Read-Only'), (SELECT id FROM permissions WHERE permissions.authority = 'risks' and permissions.name = 'GenerateReport'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Read-Only'), (SELECT id FROM permissions WHERE permissions.authority = 'auth' and permissions.name = 'ManageSelf'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Read-Only'), (SELECT id FROM permissions WHERE permissions.authority = 'graphdb' and permissions.name = 'Read'));
-- Upload-Only
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Upload-Only'), (SELECT id FROM permissions WHERE permissions.authority = 'clients' and permissions.name = 'Tasking'));
INSERT INTO roles_permissions (role_id, permission_id) VALUES ((SELECT id FROM roles WHERE roles.name = 'Upload-Only'), (SELECT id FROM permissions WHERE permissions.authority = 'graphdb' and permissions.name = 'Write'));
-- Populate feature_flags table
INSERT INTO feature_flags (created_at, updated_at, key, name, description, enabled, user_updatable) VALUES (current_timestamp, current_timestamp, 'butterfly_analysis', 'Enhanced Asset Inbound-Outbound Exposure Analysis', 'Enables more extensive analysis of attack path findings that allows BloodHound to help the user prioritize remediation of the most exposed assets.', false, false);
INSERT INTO feature_flags (created_at, updated_at, key, name, description, enabled, user_updatable) VALUES (current_timestamp, current_timestamp, 'enable_saml_sso', 'SAML Single Sign-On Support', 'Enables SSO authentication flows and administration panels to third party SAML identity providers.', true, false);
INSERT INTO feature_flags (created_at, updated_at, key, name, description, enabled, user_updatable) VALUES (current_timestamp, current_timestamp, 'scope_collection_by_ou', 'Enable SharpHound OU Scoped Collections', 'Enables scoping SharpHound collections to specific lists of OUs.', true, false);
INSERT INTO feature_flags (created_at, updated_at, key, name, description, enabled, user_updatable) VALUES (current_timestamp, current_timestamp, 'azure_support', 'Enable Azure Support', 'Enables Azure support.', true, false);
INSERT INTO feature_flags (created_at, updated_at, key, name, description, enabled, user_updatable) VALUES (current_timestamp, current_timestamp, 'reconciliation', 'Reconciliation', 'Enables Reconciliation', true, false);
INSERT INTO feature_flags (created_at, updated_at, key, name, description, enabled, user_updatable) VALUES (current_timestamp, current_timestamp, 'entity_panel_cache', 'Enable application level caching', 'Enables the use of application level caching for entity panel queries', true, false);
INSERT INTO feature_flags (created_at, updated_at, key, name, description, enabled, user_updatable) VALUES (current_timestamp, current_timestamp, 'adcs', 'Enable collection and processing of Active Directory Certificate Services Data', 'Enables the ability to collect, analyze, and explore Active Directory Certificate Services data and previews new attack paths.', false, false );
-- Populate parameters table
INSERT INTO parameters (key, name, description, value, created_at, updated_at) VALUES ('auth.password_expiration_window', 'Local Auth Password Expiry Window', 'This configuration parameter sets the local auth password expiry window for users that have valid auth secrets. Values for this configuration must follow the duration specification of ISO-8601.', '{"duration": "P90D"}', current_timestamp, current_timestamp);
INSERT INTO parameters (key, name, description, value, created_at, updated_at) VALUES ('neo4j.configuration', 'Neo4j Configuration Parameters', 'This configuration parameter sets the BatchWriteSize and the BatchFlushSize for Neo4J.', '{"batch_write_size": 20000, "write_flush_size": 100000}', current_timestamp, current_timestamp);