1
1
import logging
2
2
import subprocess
3
+ import httpx
3
4
4
5
from fastapi import HTTPException , Request
5
6
8
9
9
10
10
11
TEUTHOLOGY_PATH = settings .teuthology_path
12
+ ADMIN_TEAM = settings .admin_team
11
13
log = logging .getLogger (__name__ )
12
14
13
15
14
- def run (args , send_logs : bool , access_token : str , request : Request ):
16
+ async def run (args , send_logs : bool , access_token : dict , request : Request ):
15
17
"""
16
18
Kill running teuthology jobs.
17
19
"""
@@ -30,16 +32,19 @@ def run(args, send_logs: bool, access_token: str, request: Request):
30
32
else :
31
33
log .error ("teuthology-kill is missing --run" )
32
34
raise HTTPException (status_code = 400 , detail = "--run is a required argument" )
33
- # TODO if user has admin priviledge, then they can kill any run/job.
35
+
34
36
if run_owner .lower () != username .lower ():
35
- log .error (
36
- "%s doesn't have permission to kill a job scheduled by: %s" ,
37
- username ,
38
- run_owner ,
39
- )
40
- raise HTTPException (
41
- status_code = 401 , detail = "You don't have permission to kill this run/job"
42
- )
37
+ isUserAdmin = await isAdmin (username , access_token )
38
+ if not isUserAdmin :
39
+ log .error (
40
+ "%s doesn't have permission to kill a job scheduled by: %s" ,
41
+ username ,
42
+ run_owner ,
43
+ )
44
+ raise HTTPException (
45
+ status_code = 401 , detail = "You don't have permission to kill this run/job"
46
+ )
47
+ log .info ("Killing with admin privileges" )
43
48
try :
44
49
kill_cmd = [f"{ TEUTHOLOGY_PATH } /virtualenv/bin/teuthology-kill" ]
45
50
for flag , flag_value in args .items ():
@@ -61,3 +66,19 @@ def run(args, send_logs: bool, access_token: str, request: Request):
61
66
except Exception as exc :
62
67
log .error ("teuthology-kill command failed with the error: %s" , repr (exc ))
63
68
raise HTTPException (status_code = 500 , detail = repr (exc )) from exc
69
+
70
+
71
+ async def isAdmin (username , token ):
72
+ TEAM_MEMBER_URL = (
73
+ f"https://api.github.com/orgs/ceph/teams/{ ADMIN_TEAM } /memberships/{ username } "
74
+ )
75
+ async with httpx .AsyncClient () as client :
76
+ headers = {
77
+ "Authorization" : "token " + token ["access_token" ],
78
+ "Accept" : "application/json" ,
79
+ }
80
+ response_org = await client .get (url = TEAM_MEMBER_URL , headers = headers )
81
+ response_org_dic = dict (response_org .json ())
82
+ if response_org_dic .get ("state" ) == "active" :
83
+ return True
84
+ return False
0 commit comments