Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
GÉANT Service Orchestrator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository 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
GÉANT Orchestration and Automation Team
GAP
GÉANT Service Orchestrator
Merge requests
!215
Feature/nat 468 refactor auth
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Feature/nat 468 refactor auth
feature/NAT-468-refactor-auth
into
develop
Overview
0
Commits
8
Pipelines
13
Changes
5
Merged
Mohammad Torkashvand
requested to merge
feature/NAT-468-refactor-auth
into
develop
1 year ago
Overview
0
Commits
8
Pipelines
13
Changes
5
Expand
0
0
Merge request reports
Viewing commit
edce322f
Prev
Next
Show latest version
5 files
+
69
−
8
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
5
Search (e.g. *.vue) (Ctrl+P)
edce322f
upgrade to orchestrato-core v2
· edce322f
Mohammad Torkashvand
authored
1 year ago
gso/auth/opa.py
0 → 100644
+
44
−
0
Options
from
http
import
HTTPStatus
from
fastapi.exceptions
import
HTTPException
from
fastapi.params
import
Depends
from
httpx
import
AsyncClient
,
NetworkError
from
oauth2_lib.fastapi
import
OIDCUserModel
,
OPAAuthorization
,
OPAResult
from
oauth2_lib.settings
import
oauth2lib_settings
from
starlette.requests
import
Request
from
structlog
import
get_logger
from
gso.auth.oidc
import
oidc_instance
logger
=
get_logger
(
__name__
)
class
OPAAuthorization
(
OPAAuthorization
):
_instance
=
None
def
__new__
(
cls
,
*
args
,
**
kwargs
):
if
cls
.
_instance
is
None
:
cls
.
_instance
=
super
(
OPAAuthorization
,
cls
).
__new__
(
cls
)
return
cls
.
_instance
async
def
authorize
(
self
,
request
:
Request
,
user_info
:
OIDCUserModel
=
Depends
(
oidc_instance
.
authenticate
)
)
->
bool
|
None
:
return
await
super
().
authorize
(
request
,
user_info
)
async
def
get_decision
(
self
,
async_request
:
AsyncClient
,
opa_input
:
dict
)
->
OPAResult
:
logger
.
debug
(
"
Posting input json to Policy agent
"
,
opa_url
=
self
.
opa_url
,
input
=
opa_input
)
try
:
result
=
await
async_request
.
post
(
self
.
opa_url
,
json
=
opa_input
)
except
(
NetworkError
,
TypeError
)
as
exc
:
logger
.
debug
(
"
Could not get decision from policy agent
"
,
error
=
str
(
exc
))
raise
HTTPException
(
status_code
=
HTTPStatus
.
SERVICE_UNAVAILABLE
,
detail
=
"
Policy agent is unavailable
"
)
json_result
=
result
.
json
()
logger
.
debug
(
"
Received decision from policy agent
"
,
decision
=
json_result
)
return
OPAResult
(
decision_id
=
json_result
[
"
decision_id
"
],
result
=
json_result
[
"
result
"
][
"
allow
"
])
opa_instance
=
OPAAuthorization
(
opa_url
=
oauth2lib_settings
.
OPA_URL
,
)
Loading