Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
Manifest Ingestion DAG
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSDU
OSDU Data Platform
Data Flow
Data Ingestion
Manifest Ingestion DAG
Commits
48e9789a
Commit
48e9789a
authored
3 years ago
by
Yan Sushchynski (EPAM)
Committed by
Siarhei Khaletski (EPAM)
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add BaseTokenRefresher
parent
2d1ee64d
No related branches found
No related tags found
1 merge request
!34
Added BaseTokenRefresher
Pipeline
#33124
passed
3 years ago
Stage: linters
Stage: unit_tests
Stage: test_dags
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/dags/libs/refresh_token.py
+39
-18
39 additions, 18 deletions
src/dags/libs/refresh_token.py
tests/plugin-unit-tests/test_refresh_token.py
+25
-1
25 additions, 1 deletion
tests/plugin-unit-tests/test_refresh_token.py
with
64 additions
and
19 deletions
src/dags/libs/refresh_token.py
+
39
−
18
View file @
48e9789a
...
...
@@ -15,15 +15,9 @@
"""
Auth and refresh token utility functions.
"""
import
json
import
logging
import
os
from
abc
import
ABC
,
abstractmethod
from
functools
import
partial
from
http
import
HTTPStatus
import
requests
from
osdu_api.libs.auth.authorization
import
TokenRefresher
,
authorize
from
osdu_api.libs.auth.authorization
import
TokenRefresher
from
providers
import
credentials
from
providers.types
import
BaseCredentials
from
tenacity
import
retry
,
stop_after_attempt
...
...
@@ -33,14 +27,12 @@ logger = logging.getLogger(__name__)
RETRIES
=
3
class
Airflow
TokenRefresher
(
TokenRefresher
):
"""
Simple wrapper for credentials to be used in refresh_token decorator within Airflow.
"""
class
Base
TokenRefresher
(
TokenRefresher
):
"""
Base Token refresher, that works with Credentials and has methods to refresh access tokens
"""
def
__init__
(
self
,
creds
:
BaseCredentials
=
None
):
super
().
__init__
()
self
.
_credentials
=
creds
or
credentials
.
get_credentials
()
from
airflow.models
import
Variable
self
.
airflow_variables
=
Variable
self
.
_access_token
=
None
@retry
(
stop
=
stop_after_attempt
(
RETRIES
))
def
refresh_token
(
self
)
->
str
:
...
...
@@ -50,7 +42,6 @@ class AirflowTokenRefresher(TokenRefresher):
:rtype: str
"""
self
.
_credentials
.
refresh_token
()
self
.
airflow_variables
.
set
(
"
core__auth__access_token
"
,
self
.
_credentials
.
access_token
)
self
.
_access_token
=
self
.
_credentials
.
access_token
return
self
.
_access_token
...
...
@@ -61,11 +52,6 @@ class AirflowTokenRefresher(TokenRefresher):
:return: The access token
:rtype: str
"""
if
not
self
.
_access_token
:
try
:
self
.
_access_token
=
self
.
airflow_variables
.
get
(
"
core__auth__access_token
"
)
except
KeyError
:
self
.
refresh_token
()
return
self
.
_access_token
@property
...
...
@@ -76,3 +62,38 @@ class AirflowTokenRefresher(TokenRefresher):
:rtype: dict
"""
return
{
"
Authorization
"
:
f
"
Bearer
{
self
.
access_token
}
"
}
class
AirflowTokenRefresher
(
BaseTokenRefresher
):
"""
Simple wrapper for credentials to be used in refresh_token decorator within Airflow.
"""
def
__init__
(
self
,
creds
:
BaseCredentials
=
None
):
super
().
__init__
(
creds
)
from
airflow.models
import
Variable
self
.
airflow_variables
=
Variable
self
.
_access_token
=
None
@retry
(
stop
=
stop_after_attempt
(
RETRIES
))
def
refresh_token
(
self
)
->
str
:
"""
Refresh the token and cache token using airflow variables.
:return: The refreshed token
:rtype: str
"""
super
().
refresh_token
()
self
.
airflow_variables
.
set
(
"
core__auth__access_token
"
,
self
.
_access_token
)
return
self
.
_access_token
@property
def
access_token
(
self
)
->
str
:
"""
The access token.
:return: The access token
:rtype: str
"""
if
not
self
.
_access_token
:
try
:
self
.
_access_token
=
self
.
airflow_variables
.
get
(
"
core__auth__access_token
"
)
except
KeyError
:
self
.
refresh_token
()
return
self
.
_access_token
This diff is collapsed.
Click to expand it.
tests/plugin-unit-tests/test_refresh_token.py
+
25
−
1
View file @
48e9789a
...
...
@@ -23,10 +23,34 @@ from google.oauth2 import service_account
sys
.
path
.
append
(
f
"
{
os
.
getenv
(
'
AIRFLOW_SRC_DIR
'
)
}
/dags
"
)
from
libs.refresh_token
import
AirflowTokenRefresher
from
libs.refresh_token
import
AirflowTokenRefresher
,
BaseTokenRefresher
from
mock_providers
import
get_test_credentials
class
TestBaseTokenRefresher
:
@pytest.fixture
()
def
token_refresher
(
self
,
access_token
:
str
)
->
BaseTokenRefresher
:
creds
=
get_test_credentials
()
creds
.
access_token
=
access_token
token_refresher
=
BaseTokenRefresher
(
creds
)
return
token_refresher
@pytest.mark.parametrize
(
"
access_token
"
,
[
"
test
"
,
"
aaaa
"
]
)
def
test_authorization_header
(
self
,
token_refresher
:
BaseTokenRefresher
,
access_token
:
str
):
"""
Check if Authorization header is
'
Bearer <access_token>
'
"""
token_refresher
.
refresh_token
()
assert
token_refresher
.
authorization_header
.
get
(
"
Authorization
"
)
==
f
"
Bearer
{
access_token
}
"
class
TestAirflowTokenRefresher
:
@pytest.fixture
()
...
...
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