Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
shared-libs
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pi-Lab
shared-libs
Commits
89244434
Commit
89244434
authored
2 years ago
by
istmxrein
Browse files
Options
Downloads
Patches
Plain Diff
Add background worker for event subscription
parent
261c7c41
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pilab/auth.py
+55
-11
55 additions, 11 deletions
pilab/auth.py
setup.py
+1
-1
1 addition, 1 deletion
setup.py
with
56 additions
and
12 deletions
pilab/auth.py
+
55
−
11
View file @
89244434
import
logging
import
logging
from
typing
import
Optional
,
List
,
Dict
from
collections
import
Counter
from
typing
import
Optional
,
List
,
Dict
,
Callable
from
fastapi
import
Header
,
HTTPException
from
fastapi
import
Header
,
HTTPException
from
pilab.events
import
reservation
from
datetime
import
datetime
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
ADMIN_GROUPS
=
[
"
/admin
"
]
ADMIN_GROUPS
=
[
"
/admin
"
]
CUBE_ACCESS_LIST
:
Dict
[
int
,
List
[
str
]]
=
{}
cube_reservations
:
Dict
[
int
:
reservation
.
Reservation
]
=
{}
res_thread
:
reservation
.
ReservationConsumer
=
None
custom_callback
:
Callable
=
None
def
is_admin
(
x_forwarded_groups
:
str
):
def
is_admin
(
x_forwarded_groups
:
str
):
...
@@ -25,13 +30,30 @@ def get_username(usernames: List[str]):
...
@@ -25,13 +30,30 @@ def get_username(usernames: List[str]):
return
None
return
None
def
get_active_users
(
cube_id
:
int
):
res
:
reservation
.
Reservation
=
cube_reservations
.
get
(
cube_id
)
if
res
and
res
.
endtime
>
datetime
.
utcnow
():
return
[
*
res
.
extraUsers
,
res
.
owner
]
else
:
return
[]
def
get_active_reservation
(
cube_id
:
int
):
res
:
reservation
.
Reservation
=
cube_reservations
.
get
(
cube_id
)
if
res
and
res
.
endtime
>
datetime
.
utcnow
():
return
res
else
:
return
None
async
def
get_user
(
x_forwarded_user
:
Optional
[
str
]
=
Header
(
None
),
async
def
get_user
(
x_forwarded_user
:
Optional
[
str
]
=
Header
(
None
),
x_forwarded_preferred_username
:
Optional
[
str
]
=
Header
(
None
),
x_forwarded_preferred_username
:
Optional
[
str
]
=
Header
(
None
),
x_forwarded_groups
:
Optional
[
str
]
=
Header
(
None
)):
x_forwarded_groups
:
Optional
[
str
]
=
Header
(
None
)):
"""
"""
Extract the username and admin status from the http headers oauth2-proxy provides
Extract the username and admin status from the http headers oauth2-proxy provides
"""
"""
logger
.
debug
([
"
X-Forwarded-Preferred-Username:
"
+
x_forwarded_preferred_username
if
x_forwarded_preferred_username
else
""
]
+
logger
.
debug
([
"
X-Forwarded-Preferred-Username:
"
+
x_forwarded_preferred_username
if
x_forwarded_preferred_username
else
""
]
+
[
"
X-Forwarded-User:
"
+
x_forwarded_user
if
x_forwarded_user
else
""
]
+
[
"
X-Forwarded-User:
"
+
x_forwarded_user
if
x_forwarded_user
else
""
]
+
[
"
X-Forwarded-Groups:
"
+
x_forwarded_groups
if
x_forwarded_groups
else
""
])
[
"
X-Forwarded-Groups:
"
+
x_forwarded_groups
if
x_forwarded_groups
else
""
])
...
@@ -43,14 +65,12 @@ async def get_user(x_forwarded_user: Optional[str] = Header(None),
...
@@ -43,14 +65,12 @@ async def get_user(x_forwarded_user: Optional[str] = Header(None),
async
def
verify_user
(
cube_id
:
int
,
x_forwarded_preferred_username
:
Optional
[
str
]
=
Header
(
None
),
async
def
verify_user
(
cube_id
:
int
,
x_forwarded_preferred_username
:
Optional
[
str
]
=
Header
(
None
),
x_forwarded_user
:
Optional
[
str
]
=
Header
(
None
),
x_forwarded_user
:
Optional
[
str
]
=
Header
(
None
),
x_forwarded_groups
:
Optional
[
str
]
=
Header
(
None
)):
x_forwarded_groups
:
Optional
[
str
]
=
Header
(
None
)):
admin
=
is_admin
(
x_forwarded_groups
)
admin
=
is_admin
(
x_forwarded_groups
)
username
=
get_username
([
x_forwarded_preferred_username
,
x_forwarded_user
])
username
=
get_username
([
x_forwarded_preferred_username
,
x_forwarded_user
])
if
admin
:
if
admin
:
return
True
return
True
if
username
is
not
None
and
CUBE_ACCESS_LIST
.
get
(
cube_id
)
is
not
None
:
if
username
is
not
None
and
username
in
get_active_users
(
cube_id
):
if
username
in
CUBE_ACCESS_LIST
.
get
(
cube_id
):
return
True
return
True
raise
HTTPException
(
status_code
=
401
,
detail
=
"
Unauthorized
"
)
raise
HTTPException
(
status_code
=
401
,
detail
=
"
Unauthorized
"
)
...
@@ -58,8 +78,32 @@ async def verify_user(cube_id: int, x_forwarded_preferred_username: Optional[str
...
@@ -58,8 +78,32 @@ async def verify_user(cube_id: int, x_forwarded_preferred_username: Optional[str
def
verify_user_pi
(
cube_id
:
int
,
username
:
str
,
admin
:
bool
):
def
verify_user_pi
(
cube_id
:
int
,
username
:
str
,
admin
:
bool
):
if
admin
:
if
admin
:
return
True
return
True
elif
username
is
not
None
and
CUBE_ACCESS_LIST
.
get
(
cube_id
)
is
not
None
:
if
username
is
not
None
and
username
in
get_active_users
(
cube_id
):
if
username
in
CUBE_ACCESS_LIST
.
get
(
cube_id
):
return
True
return
True
return
False
return
False
def
restart_reservation_consumer
(
ids
:
List
[
int
],
callback
:
Callable
=
None
):
global
res_thread
global
custom_callback
custom_callback
=
callback
def
res_event_callback
(
res
:
reservation
.
Reservation
):
logger
.
info
(
f
'
Received Reservation for cube
{
res
.
cube_id
}
;
{
res
}
'
)
if
custom_callback
:
custom_callback
(
res
)
cube_reservations
[
res
.
cube_id
]
=
res
if
not
res_thread
:
res_thread
=
reservation
.
ReservationConsumer
(
cubes
=
ids
,
callback
=
res_event_callback
)
res_thread
.
start
()
elif
Counter
(
ids
)
!=
Counter
(
res_thread
.
cubes
):
res_thread
.
stop
()
res_thread
.
join
()
res_thread
=
reservation
.
ReservationConsumer
(
cubes
=
ids
,
callback
=
res_event_callback
)
res_thread
.
start
()
return
res_thread
This diff is collapsed.
Click to expand it.
setup.py
+
1
−
1
View file @
89244434
...
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
...
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup
(
setup
(
name
=
'
pilab
'
,
name
=
'
pilab
'
,
version
=
'
1.
3
.5
'
,
version
=
'
1.
4
.5
'
,
description
=
'
Shared-Libs for the pi-lab microservices
'
,
description
=
'
Shared-Libs for the pi-lab microservices
'
,
url
=
'
https://code.fbi.h-da.de/api/v4/projects/27896/packages/pypi/pilab
'
,
url
=
'
https://code.fbi.h-da.de/api/v4/projects/27896/packages/pypi/pilab
'
,
author
=
'
Max Reinheimer
'
,
author
=
'
Max Reinheimer
'
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment