-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathviews.py
More file actions
1382 lines (1166 loc) · 41.7 KB
/
views.py
File metadata and controls
1382 lines (1166 loc) · 41.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
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
"""This module defines the API endpoints for the FastAPI application."""
# Standard Python Libraries
from datetime import datetime, timezone
import os
from typing import List, Optional
from uuid import UUID
# Third-Party Libraries
from fastapi import APIRouter, Body, Depends, HTTPException, Query, Request, status
from fastapi.responses import RedirectResponse
from redis import asyncio as aioredis
# from .schemas import Cpe
from .api_methods import api_key as api_key_methods
from .api_methods import notification as notification_methods
from .api_methods import organization, proxy, scan, scan_tasks, user
from .api_methods.blocklist import handle_check_ip
from .api_methods.cpe import get_cpes_by_id
from .api_methods.cve import get_cves_by_id, get_cves_by_name
from .api_methods.domain import export_domains, get_domain_by_id, search_domains
from .api_methods.saved_search import (
create_saved_search,
delete_saved_search,
get_saved_search,
list_saved_searches,
update_saved_search,
)
from .api_methods.search import search_export, search_post
from .api_methods.stats import (
get_by_org_stats,
get_num_vulns,
get_severity_stats,
get_stats,
get_user_ports_count,
get_user_services_count,
stats_latest_vulns,
stats_most_common_vulns,
)
from .api_methods.sync import sync_post
from .api_methods.user import (
accept_terms,
delete_user,
get_me,
get_users,
get_users_by_region_id,
get_users_by_state,
get_users_v2,
update_user_v2,
)
from .api_methods.user_log_search import search_logs
from .api_methods.vulnerability import (
export_vulnerabilities,
get_vulnerability_by_id,
search_vulnerabilities,
update_vulnerability,
)
from .auth import get_current_active_user, handle_okta_callback
from .login_gov import callback
from .models import User
from .schema_models import organization_schema as OrganizationSchema
from .schema_models import scan as scanSchema
from .schema_models import scan_tasks as scanTaskSchema
from .schema_models import stat_schema
from .schema_models.api_key import ApiKey as ApiKeySchema
from .schema_models.blocklist import BlocklistCheckResponse
from .schema_models.cpe import Cpe as CpeSchema
from .schema_models.cve import Cve as CveSchema
from .schema_models.domain import DomainSearch, DomainSearchResponse, GetDomainResponse
from .schema_models.notification import CreateNotificationSchema
from .schema_models.notification import Notification as NotificationSchema
from .schema_models.saved_search import (
SavedSearchCreate,
SavedSearchList,
SavedSearchUpdate,
)
from .schema_models.saved_search import SavedSearch as SavedSearchSchema
from .schema_models.search import DomainSearchBody, SearchResponse
from .schema_models.sync import SyncBody, SyncResponse
from .schema_models.user import (
NewUser,
NewUserResponseModel,
RegisterUserResponse,
UpdateUserV2,
)
from .schema_models.user import User as UserSchema
from .schema_models.user import UserResponseV2, VersionModel
from .schema_models.user_log_schema import LogSearch, LogSearchResponse
from .schema_models.vulnerability import (
VulnerabilitySearch,
VulnerabilitySearchResponse,
)
from .schema_models.vulnerability import GetVulnerabilityResponse
from .schema_models.vulnerability import Vulnerability as VulnerabilitySchema
from .tools.serializers import serialize_organization, serialize_user
from .tools.user_logger_decorator import (
get_organization_sync,
get_user_sync,
log_action,
)
# Define API router
api_router = APIRouter()
async def get_redis_client(request: Request):
"""Get the Redis client from the application state."""
return request.app.state.redis
# ========================================
# Analytic Endpoints
# ========================================
# Matomo Proxy
@api_router.api_route(
"/matomo/{path:path}",
dependencies=[Depends(get_current_active_user)],
tags=["Analytics"],
)
async def matomo_proxy(
path: str, request: Request, current_user: User = Depends(get_current_active_user)
):
"""Proxy requests to the Matomo analytics instance."""
# Public paths -- directly allowed
allowed_paths = ["/matomo.php", "/matomo.js"]
if any(
[request.url.path.startswith(allowed_path) for allowed_path in allowed_paths]
):
return await proxy.proxy_request(path, request, os.getenv("MATOMO_URL"))
# Redirects for specific font files
if request.url.path in [
"/plugins/Morpheus/fonts/matomo.woff2",
"/plugins/Morpheus/fonts/matomo.woff",
"/plugins/Morpheus/fonts/matomo.ttf",
]:
return RedirectResponse(
url="https://cdn.jsdelivr.net/gh/matomo-org/matomo@5.2.1{}".format(
request.url.path
)
)
# Ensure only global admin can access other paths
if current_user.userType != "globalAdmin":
raise HTTPException(status_code=403, detail="Unauthorized")
# Handle the proxy request to Matomo
return await proxy.proxy_request(
request, os.getenv("MATOMO_URL", ""), path, cookie_name="MATOMO_SESSID"
)
# P&E Proxy
@api_router.api_route(
"/pe/{path:path}",
methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
dependencies=[Depends(get_current_active_user)],
tags=["Analytics"],
)
async def pe_proxy(
path: str, request: Request, current_user: User = Depends(get_current_active_user)
):
"""Proxy requests to the P&E Django application."""
# Ensure only Global Admin and Global View users can access
if current_user.userType not in ["globalView", "globalAdmin"]:
raise HTTPException(status_code=403, detail="Unauthorized")
# Handle the proxy request to the P&E Django application
return await proxy.proxy_request(request, os.getenv("PE_API_URL", ""), path)
# ========================================
# API Key Endpoints
# ========================================
# POST
@api_router.post("/api-keys", response_model=ApiKeySchema, tags=["API Keys"])
async def create_api_key(current_user: User = Depends(get_current_active_user)):
"""Create api key."""
return api_key_methods.post(current_user)
# DELETE
@api_router.delete("/api-keys/{api_key_id}", tags=["API Keys"])
async def delete_api_key(
api_key_id: str, current_user: User = Depends(get_current_active_user)
):
"""Delete api key by id."""
return api_key_methods.delete(api_key_id, current_user)
# GET ALL
@api_router.get("/api-keys", response_model=List[ApiKeySchema], tags=["API Keys"])
async def get_all_api_keys(current_user: User = Depends(get_current_active_user)):
"""Get all api keys."""
return api_key_methods.get_all(current_user)
# GET BY ID
@api_router.get(
"/api-keys/{api_key_id}", response_model=ApiKeySchema, tags=["API Keys"]
)
async def get_api_key(
api_key_id: str, current_user: User = Depends(get_current_active_user)
):
"""Get api key by id."""
return api_key_methods.get_by_id(api_key_id, current_user)
# ========================================
# Auth Endpoints
# ========================================
# Okta Callback
@api_router.post("/auth/okta-callback", tags=["Auth"])
async def okta_callback(request: Request):
"""Handle Okta Callback."""
return await handle_okta_callback(request)
# V1 Callback
@api_router.post("/auth/callback", tags=["Auth"])
async def callback_route(request: Request):
"""Handle V1 Callback."""
body = await request.json()
try:
user_info = callback(body)
return user_info
except Exception as error:
raise HTTPException(status_code=400, detail=str(error))
# ========================================
# CPE Endpoints
# ========================================
@api_router.get(
"/cpes/{cpe_id}",
dependencies=[Depends(get_current_active_user)],
response_model=CpeSchema,
tags=["CPEs"],
)
async def call_get_cpes_by_id(cpe_id):
"""Get Cpe by id."""
return get_cpes_by_id(cpe_id)
# ========================================
# CVE Endpoints
# ========================================
@api_router.get(
"/cves/{cve_id}",
dependencies=[Depends(get_current_active_user)],
response_model=CveSchema,
tags=["CVEs"],
)
async def call_get_cves_by_id(cve_id):
"""Get Cve by id."""
return get_cves_by_id(cve_id)
@api_router.get(
"/cves/name/{cve_name}",
dependencies=[Depends(get_current_active_user)],
response_model=CveSchema,
tags=["CVEs"],
)
async def call_get_cves_by_name(cve_name):
"""Get Cve by name."""
return get_cves_by_name(cve_name)
# ========================================
# Domain Endpoints
# ========================================
@api_router.post(
"/domain/search",
dependencies=[Depends(get_current_active_user)],
response_model=DomainSearchResponse,
tags=["Domains"],
)
async def call_search_domains(
domain_search: DomainSearch, current_user: User = Depends(get_current_active_user)
):
"""Call search domains."""
domains, count = search_domains(domain_search, current_user)
return DomainSearchResponse(result=domains, count=count)
@api_router.post(
"/domain/export",
dependencies=[Depends(get_current_active_user)],
tags=["Domains"],
)
async def call_export_domains(
domain_search: DomainSearch, current_user: User = Depends(get_current_active_user)
):
"""Call export domains."""
try:
return export_domains(domain_search, current_user)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@api_router.get(
"/domain/{domain_id}",
dependencies=[Depends(get_current_active_user)],
response_model=GetDomainResponse,
tags=["Domains"],
)
async def call_get_domain_by_id(domain_id: str):
"""Get domain by id."""
return get_domain_by_id(domain_id)
# ========================================
# Log Endpoints
# ========================================
@api_router.post(
"/logs/search",
dependencies=[Depends(get_current_active_user)],
response_model=LogSearchResponse,
tags=["Logs"],
)
async def call_search_logs(
log_search: LogSearch, current_user: User = Depends(get_current_active_user)
):
"""Search log table."""
log_data, count = search_logs(log_search, current_user)
return LogSearchResponse(result=log_data, count=count)
# ========================================
# Notification Endpoints
# ========================================
# POST
@api_router.post(
"/notifications",
dependencies=[Depends(get_current_active_user)],
response_model=NotificationSchema,
tags=["Notifications"],
)
async def create_notification(
notification_data: CreateNotificationSchema,
current_user: User = Depends(get_current_active_user),
):
"""Create notification key."""
return notification_methods.post(notification_data, current_user)
# DELETE
@api_router.delete(
"/notifications/{notification_id}",
dependencies=[Depends(get_current_active_user)],
tags=["Notifications"],
)
async def delete_notification(
notification_id: str, current_user: User = Depends(get_current_active_user)
):
"""Delete notification by id."""
return notification_methods.delete(notification_id, current_user)
# GET ALL: Doesn't require authentication
@api_router.get(
"/notifications", response_model=List[NotificationSchema], tags=["Notifications"]
)
async def get_all_notifications():
"""Get all notifications."""
return notification_methods.get_all()
# GET BY ID
@api_router.get(
"/notifications/{notification_id}",
dependencies=[Depends(get_current_active_user)],
response_model=NotificationSchema,
tags=["Notifications"],
)
async def get_notification(
notification_id: str, current_user: User = Depends(get_current_active_user)
):
"""Get notification by id."""
return notification_methods.get_by_id(notification_id, current_user)
# UPDATE BY ID
@api_router.put(
"/notifications/{notification_id}",
dependencies=[Depends(get_current_active_user)],
response_model=NotificationSchema,
tags=["Notifications"],
)
async def update_notification(
notification_id: str,
notification_data: CreateNotificationSchema,
current_user: User = Depends(get_current_active_user),
):
"""Update notification key by id."""
return notification_methods.put(notification_id, notification_data, current_user)
# TODO: Adding placeholder until we determine if we still need this.
# GET 508 Banner: Doesn't require authentication
# @api_router.get("/notifications/508-banner", tags=["Notifications"])
# async def get_508_banner():
# """Get notification by id."""
# return notification_methods.get_508_banner()
# ========================================
# Organization Endpoints
# ========================================
@api_router.get(
"/organizations",
dependencies=[Depends(get_current_active_user)],
response_model=List[OrganizationSchema.GetOrganizationSchema],
tags=["Organizations"],
)
async def list_organizations(current_user: User = Depends(get_current_active_user)):
"""Retrieve a list of all organizations."""
return organization.list_organizations(current_user)
@api_router.get(
"/organizations/tags",
dependencies=[Depends(get_current_active_user)],
response_model=List[OrganizationSchema.GetTagSchema],
tags=["Organizations"],
)
async def get_organization_tags(current_user: User = Depends(get_current_active_user)):
"""Retrieve a list of organization tags."""
return organization.get_tags(current_user)
@api_router.get(
"/organizations/{organization_id}",
dependencies=[Depends(get_current_active_user)],
response_model=OrganizationSchema.GetSingleOrganizationSchema,
tags=["Organizations"],
)
async def get_organization(
organization_id: str, current_user: User = Depends(get_current_active_user)
):
"""Retrieve an organization by its ID."""
return organization.get_organization(organization_id, current_user)
@api_router.get(
"/organizations/state/{state}",
dependencies=[Depends(get_current_active_user)],
response_model=List[OrganizationSchema.GetOrganizationSchema],
tags=["Organizations"],
)
async def get_organizations_by_state(
state: str, current_user: User = Depends(get_current_active_user)
):
"""Retrieve organizations by state."""
return organization.get_by_state(state, current_user)
@api_router.get(
"/organizations/regionId/{region_id}",
dependencies=[Depends(get_current_active_user)],
response_model=List[OrganizationSchema.GetOrganizationSchema],
tags=["Organizations"],
)
async def get_organizations_by_region(
region_id: str, current_user: User = Depends(get_current_active_user)
):
"""Retrieve organizations by region ID."""
return organization.get_by_region(region_id, current_user)
@api_router.post(
"/organizations",
dependencies=[Depends(get_current_active_user)],
response_model=OrganizationSchema.GetSingleOrganizationSchema,
tags=["Organizations"],
)
async def create_organization(
organization_data: OrganizationSchema.NewOrganization,
current_user: User = Depends(get_current_active_user),
):
"""Create a new organization."""
return organization.create_organization(organization_data, current_user)
@api_router.post(
"/organizations_upsert",
dependencies=[Depends(get_current_active_user)],
response_model=OrganizationSchema.GetSingleOrganizationSchema,
tags=["Organizations"],
)
async def upsert_organization(
organization_data: OrganizationSchema.NewOrganization,
current_user: User = Depends(get_current_active_user),
):
"""Upsert an organization."""
return organization.upsert_organization(organization_data, current_user)
@api_router.put(
"/organizations/{organization_id}",
dependencies=[Depends(get_current_active_user)],
response_model=OrganizationSchema.GetSingleOrganizationSchema,
tags=["Organizations"],
)
async def update_organization(
organization_id: str,
org_data: OrganizationSchema.NewOrganization,
current_user: User = Depends(get_current_active_user),
):
"""Update an organization by its ID."""
return organization.update_organization(organization_id, org_data, current_user)
@api_router.delete(
"/organizations/{organization_id}",
dependencies=[Depends(get_current_active_user)],
response_model=OrganizationSchema.GenericMessageResponseModel,
tags=["Organizations"],
)
async def delete_organization(
organization_id: str, current_user: User = Depends(get_current_active_user)
):
"""Delete an organization by its ID."""
return organization.delete_organization(organization_id, current_user)
@api_router.post(
"/v2/organizations/{organization_id}/users",
dependencies=[Depends(get_current_active_user)],
tags=["Organizations"],
)
@log_action(
action="USER ASSIGNED",
message_or_cb=lambda current_user, response, organization_id, user_data, **kwargs: {
"timestamp": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
"userPerformedAssignment": serialize_user(current_user),
"organization": serialize_organization(get_organization_sync(organization_id)),
"role": user_data.role,
"user": serialize_user(get_user_sync(user_data.userId))
if user_data.userId
else None,
},
)
async def add_user_to_organization_v2(
organization_id: str,
user_data: OrganizationSchema.NewOrgUser,
current_user: User = Depends(get_current_active_user),
):
"""Add a user to an organization."""
return organization.add_user_to_org_v2(organization_id, user_data, current_user)
@api_router.post(
"/organizations/{organization_id}/roles/{role_id}/approve",
dependencies=[Depends(get_current_active_user)],
response_model=OrganizationSchema.GenericMessageResponseModel,
tags=["Organizations"],
)
async def approve_role(
organization_id: str,
role_id: str,
current_user: User = Depends(get_current_active_user),
):
"""Approve a role within an organization."""
return organization.approve_role(organization_id, role_id, current_user)
@api_router.post(
"/organizations/{organization_id}/roles/{role_id}/remove",
dependencies=[Depends(get_current_active_user)],
response_model=OrganizationSchema.RemoveRoleResponseModel,
tags=["Organizations"],
)
@log_action(
action="USER ROLE REMOVED",
message_or_cb=lambda current_user, response, organization_id, role_id, **kwargs: {
"timestamp": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
"userPerformedRemoval": serialize_user(current_user),
"fromOrganization": serialize_organization(
get_organization_sync(organization_id)
),
"roleId": role_id,
"removalResult": response,
},
)
async def remove_role(
organization_id: str,
role_id: str,
current_user: User = Depends(get_current_active_user),
):
"""Remove a role from an organization."""
return organization.remove_role(organization_id, role_id, current_user)
@api_router.post(
"/organizations/{organization_id}/granularScans/{scan_id}/update",
dependencies=[Depends(get_current_active_user)],
response_model=OrganizationSchema.GetSingleOrganizationSchema,
tags=["Organizations"],
)
async def update_granular_scan(
organization_id: str,
scan_id: str,
scan_data: OrganizationSchema.NewOrgScan,
current_user: User = Depends(get_current_active_user),
):
"""Update a granular scan for an organization."""
return organization.update_org_scan(
organization_id, scan_id, scan_data, current_user
)
@api_router.get(
"/v2/organizations",
dependencies=[Depends(get_current_active_user)],
response_model=List[OrganizationSchema.GetOrganizationSchema],
tags=["Organizations"],
)
async def list_organizations_v2(
state: Optional[List[str]] = Query(None),
regionId: Optional[List[str]] = Query(None),
current_user: User = Depends(get_current_active_user),
):
"""Retrieve a list of all organizations (version 2)."""
return organization.list_organizations_v2(state, regionId, current_user)
@api_router.post(
"/search/organizations",
dependencies=[Depends(get_current_active_user)],
tags=["Organizations"],
)
async def search_organizations(
search_body: OrganizationSchema.OrganizationSearchBody,
current_user: User = Depends(get_current_active_user),
):
"""Search for organizations in Elasticsearch."""
return organization.search_organizations_task(search_body, current_user)
# ========================================
# Region Endpoints
# ========================================
@api_router.get(
"/regions",
dependencies=[Depends(get_current_active_user)],
response_model=List[OrganizationSchema.RegionSchema],
tags=["Regions"],
)
async def list_regions(current_user: User = Depends(get_current_active_user)):
"""Retrieve a list of all regions."""
return organization.get_all_regions(current_user)
# ========================================
# Saved Search Endpoints
# ========================================
# Create a new saved search
@api_router.post(
"/saved-searches",
dependencies=[Depends(get_current_active_user)],
response_model=SavedSearchSchema,
tags=["Saved Searches"],
)
async def call_create_saved_search(
saved_search: SavedSearchCreate,
current_user: User = Depends(get_current_active_user),
):
"""Create a new saved search."""
request = {
"name": saved_search.name,
"count": saved_search.count,
"sortDirection": saved_search.sortDirection,
"sortField": saved_search.sortField,
"searchTerm": saved_search.searchTerm,
"searchPath": saved_search.searchPath,
"filters": saved_search.filters,
"createdById": current_user,
}
return create_saved_search(request)
# Get all existing saved searches
@api_router.get(
"/saved-searches",
dependencies=[Depends(get_current_active_user)],
response_model=SavedSearchList,
tags=["Saved Searches"],
)
async def call_list_saved_searches(user: User = Depends(get_current_active_user)):
"""Retrieve a list of all saved searches."""
return list_saved_searches(user)
# Get individual saved search by ID
@api_router.get(
"/saved-searches/{saved_search_id}",
dependencies=[Depends(get_current_active_user)],
response_model=SavedSearchSchema,
tags=["Saved Searches"],
)
async def call_get_saved_search(
saved_search_id: str, current_user: User = Depends(get_current_active_user)
):
"""Retrieve a saved search by its ID."""
return get_saved_search(saved_search_id, current_user)
# Update saved search by ID
@api_router.put(
"/saved-searches/{saved_search_id}",
dependencies=[Depends(get_current_active_user)],
response_model=SavedSearchUpdate,
tags=["Saved Searches"],
)
async def call_update_saved_search(
saved_search: SavedSearchUpdate,
saved_search_id: str,
current_user: User = Depends(get_current_active_user),
):
"""Update a saved search by its ID."""
request = {
"saved_search_id": saved_search_id,
"name": saved_search.name,
"count": saved_search.count,
"searchTerm": saved_search.searchTerm,
"sortDirection": saved_search.sortDirection,
"sortField": saved_search.sortField,
"searchPath": saved_search.searchPath,
"filters": saved_search.filters,
}
return update_saved_search(request, current_user)
# Delete saved search by ID
@api_router.delete(
"/saved-searches/{saved_search_id}",
dependencies=[Depends(get_current_active_user)],
tags=["Saved Searches"],
)
async def call_delete_saved_search(
saved_search_id: str, current_user: User = Depends(get_current_active_user)
):
"""Delete a saved search by its ID."""
return delete_saved_search(saved_search_id, current_user)
# ========================================
# Scan Endpoints
# ========================================
@api_router.get(
"/scans",
dependencies=[Depends(get_current_active_user)],
response_model=scanSchema.GetScansResponseModel,
tags=["Scans"],
)
async def list_scans(current_user: User = Depends(get_current_active_user)):
"""Retrieve a list of all scans."""
return scan.list_scans(current_user)
@api_router.get(
"/granularScans",
dependencies=[Depends(get_current_active_user)],
response_model=scanSchema.GetGranularScansResponseModel,
tags=["Scans"],
)
async def list_granular_scans(current_user: User = Depends(get_current_active_user)):
"""Retrieve a list of granular scans. User must be authenticated."""
return scan.list_granular_scans(current_user)
@api_router.post(
"/scans",
dependencies=[Depends(get_current_active_user)],
response_model=scanSchema.CreateScanResponseModel,
tags=["Scans"],
)
async def create_scan(
scan_data: scanSchema.NewScan, current_user: User = Depends(get_current_active_user)
):
"""Create a new scan."""
return scan.create_scan(scan_data, current_user)
@api_router.get(
"/scans/{scan_id}",
dependencies=[Depends(get_current_active_user)],
response_model=scanSchema.GetScanResponseModel,
tags=["Scans"],
)
async def get_scan(scan_id: str, current_user: User = Depends(get_current_active_user)):
"""Get a scan by its ID. User must be authenticated."""
return scan.get_scan(scan_id, current_user)
@api_router.put(
"/scans/{scan_id}",
dependencies=[Depends(get_current_active_user)],
response_model=scanSchema.CreateScanResponseModel,
tags=["Scans"],
)
async def update_scan(
scan_id: str,
scan_data: scanSchema.NewScan,
current_user: User = Depends(get_current_active_user),
):
"""Update a scan by its ID."""
return scan.update_scan(scan_id, scan_data, current_user)
@api_router.delete(
"/scans/{scan_id}",
dependencies=[Depends(get_current_active_user)],
response_model=scanSchema.GenericMessageResponseModel,
tags=["Scans"],
)
async def delete_scan(
scan_id: str, current_user: User = Depends(get_current_active_user)
):
"""Delete a scan by its ID."""
return scan.delete_scan(scan_id, current_user)
@api_router.post(
"/scans/{scan_id}/run",
dependencies=[Depends(get_current_active_user)],
response_model=scanSchema.GenericMessageResponseModel,
tags=["Scans"],
)
async def run_scan(scan_id: str, current_user: User = Depends(get_current_active_user)):
"""Manually run a scan by its ID."""
return scan.run_scan(scan_id, current_user)
@api_router.post(
"/scheduler/invoke", dependencies=[Depends(get_current_active_user)], tags=["Scans"]
)
async def invoke_scheduler(current_user: User = Depends(get_current_active_user)):
"""Manually invoke the scan scheduler."""
response = await scan.invoke_scheduler(current_user)
return response
# ========================================
# Scan Task Endpoints
# ========================================
@api_router.post(
"/scan-tasks/search",
dependencies=[Depends(get_current_active_user)],
response_model=scanTaskSchema.ScanTaskListResponse,
tags=["Scan Tasks"],
)
async def list_scan_tasks(
search_data: Optional[scanTaskSchema.ScanTaskSearch] = Body(None),
current_user: User = Depends(get_current_active_user),
):
"""List scan tasks based on filters."""
return scan_tasks.list_scan_tasks(search_data, current_user)
@api_router.post(
"/scan-tasks/{scan_task_id}/kill",
dependencies=[Depends(get_current_active_user)],
tags=["Scan Tasks"],
)
async def kill_scan_tasks(
scan_task_id: UUID, current_user: User = Depends(get_current_active_user)
):
"""Kill a scan task."""
return scan_tasks.kill_scan_task(scan_task_id, current_user)
@api_router.get(
"/scan-tasks/{scan_task_id}/logs",
dependencies=[Depends(get_current_active_user)],
tags=["Scan Tasks"],
)
async def get_scan_task_logs(
scan_task_id: UUID, current_user: User = Depends(get_current_active_user)
):
"""Get logs from a particular scan task."""
return scan_tasks.get_scan_task_logs(scan_task_id, current_user)
# ========================================
# Search Endpoints
# ========================================
@api_router.post(
"/sync",
dependencies=[Depends(get_current_active_user)],
response_model=SyncResponse,
tags=["Sync"],
)
async def sync(
sync_body: SyncBody,
request: Request,
current_user: User = Depends(get_current_active_user),
):
"""Post organizations for datalake sync."""
try:
return await sync_post(sync_body, request)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
)
@api_router.post(
"/search",
dependencies=[Depends(get_current_active_user)],
response_model=SearchResponse,
tags=["Search"],
)
async def search(
search_body: DomainSearchBody, current_user: User = Depends(get_current_active_user)
):
"""Get domains index from elastic search."""
try:
return await search_post(search_body, current_user)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
)
@api_router.post(
"/search/export", dependencies=[Depends(get_current_active_user)], tags=["Search"]
)
async def export_endpoint(
search_body: DomainSearchBody, current_user: User = Depends(get_current_active_user)
):
"""Search export endpoint."""
try:
result = await search_export(search_body, current_user)
return result
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ========================================
# Stat Endpoints
# ========================================
@api_router.post(
"/stats",
dependencies=[Depends(get_current_active_user)],
response_model=stat_schema.StatsResponse,
tags=["Stats"],
)
async def get_stats_combined(
filter_data: OrganizationSchema.StatsPayloadSchema,
request: Request,
current_user: User = Depends(get_current_active_user),
redis_client=Depends(get_redis_client),
):
"""Retrieve all stats from Elasticache filtered by user."""
return await get_stats(filter_data, current_user, redis_client, request)
@api_router.post(
"/services",
response_model=List[stat_schema.ServiceStat],
dependencies=[Depends(get_current_active_user)],
tags=["Stats"],
)
async def post(
filter_data: OrganizationSchema.StatsPayloadSchema,
current_user: User = Depends(get_current_active_user),