forked from kernelci/kernelci-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1133 lines (974 loc) · 39.3 KB
/
main.py
File metadata and controls
1133 lines (974 loc) · 39.3 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
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# Copyright (C) 2021-2023 Collabora Limited
# Author: Guillaume Tucker <guillaume.tucker@collabora.com>
# Author: Jeny Sadadia <jeny.sadadia@collabora.com>
# pylint: disable=unused-argument,global-statement,too-many-lines
"""KernelCI API main module"""
import os
import re
import asyncio
from typing import List, Union, Optional
from datetime import datetime
from contextlib import asynccontextmanager
from fastapi import (
Depends,
FastAPI,
HTTPException,
status,
Request,
Form,
Header,
Query,
Body,
)
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse, PlainTextResponse, FileResponse
from fastapi.security import OAuth2PasswordRequestForm
from fastapi_pagination import add_pagination, pagination_ctx
from fastapi_versioning import VersionedFastAPI
from bson import ObjectId, errors
from pymongo.errors import DuplicateKeyError
from fastapi_users import FastAPIUsers
from beanie import PydanticObjectId
from pydantic import BaseModel
from kernelci.api.models import (
Node,
Hierarchy,
PublishEvent,
parse_node_obj,
KernelVersion,
EventHistory,
)
from .auth import Authentication
from .db import Database
from .pubsub import PubSub
from .user_manager import get_user_manager, create_user_manager
from .models import (
PageModel,
Subscription,
SubscriptionStats,
User,
UserRead,
UserCreate,
UserCreateRequest,
UserUpdate,
UserUpdateRequest,
UserGroup,
)
from .metrics import Metrics
@asynccontextmanager
async def lifespan(app: FastAPI): # pylint: disable=redefined-outer-name
"""Lifespan functions for startup and shutdown events"""
await pubsub_startup()
await create_indexes()
await initialize_beanie()
yield
# List of all the supported API versions. This is a placeholder until the API
# actually supports multiple versions with different sets of endpoints and
# models etc.
API_VERSIONS = ['v0']
metrics = Metrics()
app = FastAPI(lifespan=lifespan, debug=True)
db = Database(service=(os.getenv('MONGO_SERVICE') or 'mongodb://db:27017'))
auth = Authentication(token_url="user/login")
pubsub = None # pylint: disable=invalid-name
auth_backend = auth.get_user_authentication_backend()
fastapi_users_instance = FastAPIUsers[User, PydanticObjectId](
get_user_manager,
[auth_backend],
)
user_manager = create_user_manager()
async def pubsub_startup():
"""Startup event handler to create Pub/Sub object"""
global pubsub # pylint: disable=invalid-name
pubsub = await PubSub.create()
async def create_indexes():
"""Startup event handler to create database indexes"""
await db.create_indexes()
async def initialize_beanie():
"""Startup event handler to initialize Beanie"""
await db.initialize_beanie()
@app.exception_handler(ValueError)
async def value_error_exception_handler(request: Request, exc: ValueError):
"""Global exception handler for 'ValueError'"""
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"detail": str(exc)},
)
@app.exception_handler(errors.InvalidId)
async def invalid_id_exception_handler(
request: Request,
exc: errors.InvalidId):
"""Global exception handler for `errors.InvalidId`
The exception is raised from Database when invalid ObjectId is received"""
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"detail": str(exc)},
)
@app.get('/')
async def root():
"""Root endpoint handler"""
metrics.add('http_requests_total', 1)
return {"message": "KernelCI API"}
# -----------------------------------------------------------------------------
# Users
def get_current_user(user: User = Depends(
fastapi_users_instance.current_user(active=True))):
"""Get current active user"""
return user
def get_current_superuser(user: User = Depends(
fastapi_users_instance.current_user(active=True, superuser=True))):
"""Get current active superuser"""
return user
app.include_router(
fastapi_users_instance.get_auth_router(auth_backend,
requires_verification=True),
prefix="/user",
tags=["user"]
)
register_router = fastapi_users_instance.get_register_router(
UserRead, UserCreate)
@app.post("/user/register", response_model=UserRead, tags=["user"],
response_model_by_alias=False)
async def register(request: Request, user: UserCreateRequest,
current_user: User = Depends(get_current_superuser)):
"""User registration route
Custom user registration router to ensure unique username.
`user` from request has a list of user group name strings.
This handler will convert them to `UserGroup` objects and
insert user object to database.
"""
metrics.add('http_requests_total', 1)
existing_user = await db.find_one(User, username=user.username)
if existing_user:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Username already exists",
)
groups = []
if user.groups:
for group_name in user.groups:
group = await db.find_one(UserGroup, name=group_name)
if not group:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"User group does not exist with name: \
{group_name}")
groups.append(group)
user_create = UserCreate(**(user.model_dump(
exclude={'groups'}, exclude_none=True)))
user_create.groups = groups
created_user = await register_router.routes[0].endpoint(
request, user_create, user_manager)
# Update user to be an admin user explicitly if requested as
# `fastapi-users` register route does not allow it
if user.is_superuser:
user_from_id = await db.find_by_id(User, created_user.id)
user_from_id.is_superuser = True
created_user = await db.update(user_from_id)
return created_user
app.include_router(
fastapi_users_instance.get_reset_password_router(),
prefix="/user",
tags=["user"],
)
app.include_router(
fastapi_users_instance.get_verify_router(UserRead),
prefix="/user",
tags=["user"],
)
users_router = fastapi_users_instance.get_users_router(
UserRead, UserUpdate, requires_verification=True)
app.add_api_route(
path="/whoami",
tags=["user"],
methods=["GET"],
description="Get current user information",
endpoint=users_router.routes[0].endpoint)
app.add_api_route(
path="/user/{id}",
tags=["user"],
methods=["GET"],
description="Get user information by ID",
dependencies=[Depends(get_current_user)],
endpoint=users_router.routes[2].endpoint)
app.add_api_route(
path="/user/{id}",
tags=["user"],
methods=["DELETE"],
description="Delete user by ID",
dependencies=[Depends(get_current_superuser)],
endpoint=users_router.routes[4].endpoint)
@app.patch("/user/me", response_model=UserRead, tags=["user"],
response_model_by_alias=False)
async def update_me(request: Request, user: UserUpdateRequest,
current_user: User = Depends(get_current_user)):
"""User update route
Custom user update router handler will only allow users to update
its own profile.
"""
metrics.add('http_requests_total', 1)
if user.username and user.username != current_user.username:
existing_user = await db.find_one(User, username=user.username)
if existing_user:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Username already exists: {user.username}",
)
groups = []
if user.groups:
for group_name in user.groups:
group = await db.find_one(UserGroup, name=group_name)
if not group:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"User group does not exist with name: \
{group_name}")
groups.append(group)
user_update = UserUpdate(**(user.model_dump(
exclude={'groups'}, exclude_none=True)))
if groups:
user_update.groups = groups
return await users_router.routes[1].endpoint(
request, user_update, current_user, user_manager)
@app.patch("/user/{user_id}", response_model=UserRead, tags=["user"],
response_model_by_alias=False)
async def update_user(user_id: str, request: Request, user: UserUpdateRequest,
current_user: User = Depends(get_current_superuser)):
"""Router to allow admin users to update other user account"""
metrics.add('http_requests_total', 1)
user_from_id = await db.find_by_id(User, user_id)
if not user_from_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"User not found with id: {user_id}",
)
if user.username and user.username != user_from_id.username:
existing_user = await db.find_one(User, username=user.username)
if existing_user:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Username already exists: {user.username}",
)
groups = []
if user.groups:
for group_name in user.groups:
group = await db.find_one(UserGroup, name=group_name)
if not group:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"User group does not exist with name: \
{group_name}")
groups.append(group)
user_update = UserUpdate(**(user.model_dump(
exclude={'groups'}, exclude_none=True)))
if groups:
user_update.groups = groups
updated_user = await users_router.routes[3].endpoint(
user_update, request, user_from_id, user_manager
)
# Update user to be an admin user explicitly if requested as
# `fastapi-users` user update route does not allow it
if user.is_superuser:
user_from_id = await db.find_by_id(User, updated_user.id)
user_from_id.is_superuser = True
updated_user = await db.update(user_from_id)
return updated_user
async def authorize_user(node_id: str,
user: User = Depends(get_current_user)):
"""Return the user if active, authenticated, and authorized"""
# Only the user that created the node or any other user from the permitted
# user groups will be allowed to update the node
node_from_id = await db.find_by_id(Node, node_id)
if not node_from_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Node not found with id: {node_id}"
)
if not user.username == node_from_id.owner:
if not any(group.name in node_from_id.user_groups
for group in user.groups):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized to complete the operation"
)
return user
@app.get('/users', response_model=PageModel, tags=["user"],
response_model_exclude={"items": {"__all__": {
"hashed_password"}}})
async def get_users(request: Request,
current_user: User = Depends(get_current_user)):
"""Get all the users if no request parameters have passed.
Get the matching users otherwise."""
metrics.add('http_requests_total', 1)
query_params = dict(request.query_params)
# Drop pagination parameters from query as they're already in arguments
for pg_key in ['limit', 'offset']:
query_params.pop(pg_key, None)
paginated_resp = await db.find_by_attributes(User, query_params)
paginated_resp.items = serialize_paginated_data(
User, paginated_resp.items)
return paginated_resp
@app.post("/user/update-password", tags=["user"])
async def update_password(request: Request,
credentials: OAuth2PasswordRequestForm = Depends(),
new_password: str = Form(None)):
"""Update user password"""
metrics.add('http_requests_total', 1)
user = await user_manager.authenticate(credentials)
if user is None or not user.is_active:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="LOGIN_BAD_CREDENTIALS",
)
user_update = UserUpdate(password=new_password)
user_from_username = await db.find_one(User, username=credentials.username)
await users_router.routes[3].endpoint(
user_update, request, user_from_username, user_manager
)
# -----------------------------------------------------------------------------
# User groups
@app.post('/group', response_model=UserGroup, response_model_by_alias=False)
async def post_user_group(
group: UserGroup,
current_user: User = Depends(get_current_superuser)):
"""Create new user group"""
metrics.add('http_requests_total', 1)
try:
obj = await db.create(group)
except DuplicateKeyError as error:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Group '{group.name}' already exists. \
Use a different group name."
) from error
await pubsub.publish_cloudevent('user_group', {'op': 'created',
'id': str(obj.id)})
return obj
@app.get('/groups', response_model=PageModel)
async def get_user_groups(request: Request):
"""Get all the user groups if no request parameters have passed.
Get all the matching user groups otherwise."""
metrics.add('http_requests_total', 1)
query_params = dict(request.query_params)
# Drop pagination parameters from query as they're already in arguments
for pg_key in ['limit', 'offset']:
query_params.pop(pg_key, None)
paginated_resp = await db.find_by_attributes(UserGroup, query_params)
paginated_resp.items = serialize_paginated_data(
UserGroup, paginated_resp.items)
return paginated_resp
@app.get('/group/{group_id}', response_model=Union[UserGroup, None],
response_model_by_alias=False)
async def get_group(group_id: str):
"""Get user group information from the provided group id"""
metrics.add('http_requests_total', 1)
return await db.find_by_id(UserGroup, group_id)
@app.delete('/group/{group_id}',
dependencies=[Depends(pagination_ctx(PageModel))],
status_code=status.HTTP_204_NO_CONTENT)
async def delete_group(group_id: str,
current_user: User = Depends(get_current_superuser)):
"""Delete user group matching the provided group id"""
metrics.add('http_requests_total', 1)
group_from_id = await db.find_by_id(UserGroup, group_id)
if not group_from_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Group not found with id: {group_id}"
)
# Remove users from the group before deleting it
users = await db.find_by_attributes(
User, {"groups.name": group_from_id.name})
for user in users.items:
user['groups'].remove(group_from_id)
await db.update(User(**user))
# Remove group from user groups that are permitted to update node
nodes = await db.find_by_attributes(
Node, {"user_groups": group_from_id.name})
for node in nodes.items:
node['user_groups'].remove(group_from_id.name)
await db.update(Node(**node))
await db.delete_by_id(UserGroup, group_id)
# -----------------------------------------------------------------------------
# EventHistory
def _get_eventhistory(evdict):
"""Get EventHistory object from dictionary"""
evhist = EventHistory()
evhist.data = evdict
return evhist
# TBD: Restrict response by Pydantic model
@app.get('/events')
async def get_events(request: Request):
"""Get all the events if no request parameters have passed.
Format: [{event1}, {event2}, ...] or if recursive is set to true,
then we add to each event the node information.
Get all the matching events otherwise.
Query parameters can be used to filter the events:
- limit: Number of events to return
- from: Start timestamp (unix epoch) to filter events
- kind: Event kind to filter events
- state: Event state to filter events
- recursive: Retrieve node together with event
This API endpoint is under development and may change in future.
"""
metrics.add('http_requests_total', 1)
query_params = dict(request.query_params)
recursive = query_params.pop('recursive', None)
limit = query_params.pop('limit', None)
kind = query_params.pop('kind', None)
state = query_params.pop('state', None)
from_ts = query_params.pop('from', None)
if from_ts:
if isinstance(from_ts, str):
from_ts = datetime.fromisoformat(from_ts)
query_params['timestamp'] = {'$gt': from_ts}
if kind:
query_params['data.kind'] = kind
if state:
query_params['data.state'] = state
if limit:
query_params['limit'] = int(limit)
resp = await db.find_by_attributes_nonpaginated(EventHistory, query_params)
resp_list = []
for item in resp:
item['id'] = str(item['_id'])
item.pop('_id')
if recursive:
node = await db.find_by_id(Node, item['data']['id'])
if node:
item['node'] = node
resp_list.append(item)
json_comp = jsonable_encoder(resp_list)
return JSONResponse(content=json_comp)
# -----------------------------------------------------------------------------
# Nodes
def _get_node_event_data(operation, node, is_hierarchy=False):
return {
'op': operation,
'id': str(node.id),
'kind': node.kind,
'name': node.name,
'path': node.path,
'group': node.group,
'state': node.state,
'result': node.result,
'owner': node.owner,
'data': node.data,
'is_hierarchy': is_hierarchy,
}
async def translate_null_query_params(query_params: dict):
"""Translate null query parameters to None"""
translated = query_params.copy()
for key, value in query_params.items():
if value == 'null':
translated[key] = None
return translated
@app.get('/node/{node_id}', response_model=Union[Node, None],
response_model_by_alias=False)
async def get_node(node_id: str):
"""Get node information from the provided node id"""
metrics.add('http_requests_total', 1)
try:
return await db.find_by_id(Node, node_id)
except KeyError as error:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Node not found with the kind: {str(error)}"
) from error
def serialize_paginated_data(model, data: list):
"""
Serialize models to generate response without using alias.
This is required to get models with `id` field in the response
instead of `_id`.
In usual cases providing `response_model_by_alias` to endpoint
definition serves the purpose. However, that doesn't work in case
of paginated data. Hence, need to serialize it manually.
"""
serialized_data = []
for obj in data:
serialized_data.append(model(**obj).model_dump(mode='json'))
return serialized_data
@app.get('/nodes', response_model=PageModel)
async def get_nodes(request: Request):
"""Get all the nodes if no request parameters have passed.
Get all the matching nodes otherwise, within the pagination limit."""
metrics.add('http_requests_total', 1)
query_params = dict(request.query_params)
# Drop pagination parameters from query as they're already in arguments
for pg_key in ['limit', 'offset']:
query_params.pop(pg_key, None)
query_params = await translate_null_query_params(query_params)
try:
# Query using the base Node model, regardless of the specific
# node type
model = Node
translated_params = model.translate_fields(query_params)
paginated_resp = await db.find_by_attributes(model, translated_params)
paginated_resp.items = serialize_paginated_data(
model, paginated_resp.items)
return paginated_resp
except KeyError as error:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Node not found with the kind: {str(error)}"
) from error
add_pagination(app)
async def db_find_node_nonpaginated(query_params):
"""Find all the matching nodes without pagination"""
model = Node
translated_params = model.translate_fields(query_params)
return await db.find_by_attributes_nonpaginated(model, translated_params)
@app.get('/nodes/fast', response_model=List[Node])
async def get_nodes_fast(request: Request):
"""Get all the nodes if no request parameters have passed.
This is non-paginated version of get_nodes.
Still options limit=NNN and offset=NNN works and forwarded
as limit and skip to the MongoDB.
"""
query_params = dict(request.query_params)
query_params = await translate_null_query_params(query_params)
try:
# Query using the base Node model, regardless of the specific
# node type, use asyncio.wait_for with timeout 30 seconds
resp = await asyncio.wait_for(
db_find_node_nonpaginated(query_params),
timeout=15
)
return resp
except KeyError as error:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Node not found with the kind: {str(error)}"
) from error
@app.get('/count', response_model=int)
async def get_nodes_count(request: Request):
"""Get the count of all the nodes if no request parameters have passed.
Get the count of all the matching nodes otherwise."""
metrics.add('http_requests_total', 1)
query_params = dict(request.query_params)
query_params = await translate_null_query_params(query_params)
try:
# Query using the base Node model, regardless of the specific
# node type
model = Node
translated_params = model.translate_fields(query_params)
return await db.count(model, translated_params)
except KeyError as error:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Node not found with the kind: {str(error)}"
) from error
async def _verify_user_group_existence(user_groups: List[str]):
"""Check if user group exists"""
for group_name in user_groups:
if not await db.find_one(UserGroup, name=group_name):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"User group does not exist with name: {group_name}")
def _translate_version_fields(node: Node):
"""Translate Node version fields"""
data = node.data
if data:
version = data.get('kernel_revision', {}).get('version')
if version:
version = KernelVersion.translate_version_fields(version)
node.data['kernel_revision']['version'] = version
return node
@app.post('/node', response_model=Node, response_model_by_alias=False)
async def post_node(node: Node,
authorization: str | None = Header(default=None),
current_user: User = Depends(get_current_user)):
"""Create a new node"""
metrics.add('http_requests_total', 1)
# [TODO] Remove translation below once we can use it in the pipeline
node = _translate_version_fields(node)
# Explicit pydantic model validation
parse_node_obj(node)
# [TODO] Implement sanity checks depending on the node kind
if node.parent:
parent = await db.find_by_id(Node, node.parent)
if not parent:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Parent not found with id: {node.parent}"
)
await _verify_user_group_existence(node.user_groups)
node.owner = current_user.username
# The node is handled as a generic Node by the DB, regardless of its
# specific kind. The concrete Node submodel (Kbuild, Checkout, etc.)
# is only used for data format validation
obj = await db.create(node)
data = _get_node_event_data('created', obj)
attributes = {}
if data.get('owner', None):
attributes['owner'] = data['owner']
await pubsub.publish_cloudevent('node', data, attributes)
evhist = _get_eventhistory(data)
await db.create(evhist)
return obj
def is_same_flags(old_node, new_node):
""" Compare processed_by_kcidb_bridge flags
Returns True if flags are same, False otherwise
"""
old_flag = old_node.processed_by_kcidb_bridge
new_flag = new_node.processed_by_kcidb_bridge
if old_flag == new_flag:
return True
return False
@app.put('/node/{node_id}', response_model=Node, response_model_by_alias=False)
async def put_node(node_id: str, node: Node,
user: str = Depends(authorize_user),
noevent: Optional[bool] = Query(None)):
"""Update an already added node"""
metrics.add('http_requests_total', 1)
node.id = ObjectId(node_id)
node_from_id = await db.find_by_id(Node, node_id)
if not node_from_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Node not found with id: {node.id}"
)
# [TODO] Remove translation below once we can use it in the pipeline
node = _translate_version_fields(node)
# Sanity checks
# Note: do not update node ownership fields, don't update 'state'
# until we've checked the state transition is valid.
update_data = node.model_dump(
exclude={'owner', 'submitter', 'user_groups', 'state'})
new_node_def = node_from_id.model_copy(update=update_data)
# 1- Parse and validate node to specific subtype
specialized_node = parse_node_obj(new_node_def)
# 2 - State transition checks
is_valid, message = specialized_node.validate_node_state_transition(
node.state)
if not is_valid:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=message
)
# if state changes, reset processed_by_kcidb_bridge flag
if node.state != new_node_def.state:
new_node_def.processed_by_kcidb_bridge = False
# Now we can update the state
new_node_def.state = node.state
# KCIDB flags are reset on any update, because this means we need
# to reprocess updated node.
# So reset flag, unless flag is changed in the request
if is_same_flags(node_from_id, node):
new_node_def.processed_by_kcidb_bridge = False
# Update node in the DB
obj = await db.update(new_node_def)
data = _get_node_event_data('updated', obj)
attributes = {}
if data.get('owner', None):
attributes['owner'] = data['owner']
if not noevent:
await pubsub.publish_cloudevent('node', data, attributes)
evhist = _get_eventhistory(data)
await db.create(evhist)
return obj
class NodeUpdateRequest(BaseModel):
"""Request model for updating multiple nodes"""
nodes: List[str]
field: str
value: str
@app.put('/batch/nodeset', response_model=int)
async def put_batch_nodeset(data: NodeUpdateRequest,
user: str = Depends(get_current_user)):
"""
Set a field to a value for multiple nodes
TBD: Make db.bulkupdate to update multiple nodes in one go
"""
metrics.add('http_requests_total', 1)
updated = 0
nodes = data.nodes
field = data.field
value = data.value
for node_id in nodes:
node_from_id = await db.find_by_id(Node, node_id)
if not node_from_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Node not found with id: {node_id}"
)
# verify ownership, and ignore if not owner
if not user.username == node_from_id.owner:
continue
# right now we support only field:
# processed_by_kcidb_bridge, also value should be boolean
if field == 'processed_by_kcidb_bridge':
if value in ['true', 'True']:
value = True
elif value in ['false', 'False']:
value = False
setattr(node_from_id, field, value)
await db.update(node_from_id)
updated += 1
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Field not supported"
)
return updated
async def _set_node_ownership_recursively(user: User, hierarchy: Hierarchy,
submitter: str, treeid: str):
"""Set node ownership information for a hierarchy of nodes"""
if not hierarchy.node.owner:
hierarchy.node.owner = user.username
hierarchy.node.submitter = submitter
hierarchy.node.treeid = treeid
for node in hierarchy.child_nodes:
await _set_node_ownership_recursively(user, node, submitter, treeid)
@app.put('/nodes/{node_id}', response_model=List[Node],
response_model_by_alias=False)
async def put_nodes(
node_id: str, nodes: Hierarchy,
authorization: str | None = Header(default=None),
user: str = Depends(authorize_user)):
"""Add a hierarchy of nodes to an existing root node"""
metrics.add('http_requests_total', 1)
nodes.node.id = ObjectId(node_id)
# Retrieve the root node from the DB and submitter
node_from_id = await db.find_by_id(Node, node_id)
if not node_from_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Node not found with id: {node_id}"
)
submitter = node_from_id.submitter
treeid = node_from_id.treeid
await _set_node_ownership_recursively(user, nodes, submitter, treeid)
obj_list = await db.create_hierarchy(nodes, Node)
data = _get_node_event_data('updated', obj_list[0], True)
attributes = {}
if data.get('owner', None):
attributes['owner'] = data['owner']
await pubsub.publish_cloudevent('node', data, attributes)
evhist = _get_eventhistory(data)
await db.create(evhist)
return obj_list
# -----------------------------------------------------------------------------
# Key/Value namespace enabled store
@app.get('/kv/{namespace}/{key}', response_model=Union[str, None])
async def get_kv(namespace: str, key: str,
user: User = Depends(get_current_user)):
"""Get a key value pair from the store"""
metrics.add('http_requests_total', 1)
return await db.get_kv(namespace, key)
@app.post('/kv/{namespace}/{key}', response_model=Optional[str])
async def post_kv(namespace: str, key: str,
value: Optional[str] = Body(default=None),
user: User = Depends(get_current_user)):
"""Set a key-value pair in the store
namespace and key are part of the URL
value is part of the request body.
If value is not provided, we need to call delete_kv to remove the key.
"""
metrics.add('http_requests_total', 1)
if not value:
await db.del_kv(namespace, key)
return "OK"
ret = await db.set_kv(namespace, key, value)
if ret:
return "OK"
raise HTTPException(status_code=500, detail="Failed to set key-value pair")
# Delete a key-value pair from the store
@app.delete('/kv/{namespace}/{key}', response_model=Optional[str])
async def delete_kv(namespace: str, key: str,
user: User = Depends(get_current_user)):
"""Delete a key-value pair from the store"""
metrics.add('http_requests_total', 1)
await db.del_kv(namespace, key)
response = "Key-value pair deleted successfully"
return response
# -----------------------------------------------------------------------------
# Pub/Sub
@app.post('/subscribe/{channel}', response_model=Subscription)
async def subscribe(channel: str, user: User = Depends(get_current_user),
promisc: Optional[bool] = Query(None)):
"""Subscribe handler for Pub/Sub channel"""
metrics.add('http_requests_total', 1)
options = {}
if promisc:
options['promiscuous'] = promisc
return await pubsub.subscribe(channel, user.username, options)
@app.post('/unsubscribe/{sub_id}')
async def unsubscribe(sub_id: int, user: User = Depends(get_current_user)):
"""Unsubscribe handler for Pub/Sub channel"""
metrics.add('http_requests_total', 1)
try:
await pubsub.unsubscribe(sub_id, user.username)
except KeyError as error:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Subscription id not found: {str(error)}"
) from error
except RuntimeError as error:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(error)
) from error
@app.get('/listen/{sub_id}')
async def listen(sub_id: int, user: User = Depends(get_current_user)):
"""Listen messages from a subscribed Pub/Sub channel"""
metrics.add('http_requests_total', 1)
try:
return await pubsub.listen(sub_id, user.username)
except KeyError as error:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Subscription id not found: {str(error)}"
) from error
except RuntimeError as error:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Error while listening to sub id {sub_id}: {str(error)}"
) from error
@app.post('/publish/{channel}')
async def publish(event: PublishEvent, channel: str,
user: User = Depends(get_current_user)):
"""Publish an event on the provided Pub/Sub channel"""
metrics.add('http_requests_total', 1)
event_dict = PublishEvent.dict(event)
# 1 - Extract data and attributes from the event
# 2 - Add the owner as an extra attribute
# 3 - Collect all the other extra attributes, if available, without
# overwriting any of the standard ones in the dict
data = event_dict.pop('data')
extra_attributes = event_dict.pop("attributes")
attributes = event_dict
attributes['owner'] = user.username
if extra_attributes:
for k in extra_attributes:
if k not in attributes:
attributes[k] = extra_attributes[k]
await pubsub.publish_cloudevent(channel, data, attributes)
@app.post('/push/{list_name}')
async def push(raw: dict, list_name: str,
user: User = Depends(get_current_user)):
"""Push a message on the provided list"""
metrics.add('http_requests_total', 1)
attributes = dict(raw)
data = attributes.pop('data')
await pubsub.push_cloudevent(list_name, data, attributes)
@app.get('/pop/{list_name}')
async def pop(list_name: str, user: User = Depends(get_current_user)):
"""Pop a message from a given list"""
metrics.add('http_requests_total', 1)