Skip to content

Commit e706e86

Browse files
justrpAleksandr Chikovanikharkevich
authored
build(tests): lint, unit tests, and ci for that (#32)
Co-authored-by: Aleksandr Chikovani <[email protected]> Co-authored-by: Alexander Kharkevich <[email protected]>
1 parent 2144744 commit e706e86

25 files changed

+912
-31
lines changed

.coveragerc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[run]
22
source = ./
3-
omit = mlflow_oidc_auth/tests/*,mlflow_oidc_auth/db/migrations/versions/*
3+
omit = mlflow_oidc_auth/tests/*,mlflow_oidc_auth/db/migrations/versions/*,mlflow_oidc_auth/views/*

.github/workflows/pre-commit.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Run pre-commit
2+
on:
3+
pull_request:
4+
types:
5+
- opened
6+
- edited
7+
- reopened
8+
- synchronize
9+
jobs:
10+
pre-commit:
11+
name: Run pre-commit
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
16+
with:
17+
fetch-depth: 0
18+
- name: Set up Python
19+
uses: actions/setup-python@39cd14951b08e74b54015e9e001cdefcf80e669f # v5.1.1
20+
with:
21+
python-version: 3.11
22+
- name: Run pre-commit
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install pre-commit
26+
pre-commit install
27+
pre-commit run --all-files --show-diff-on-failure

.github/workflows/unit-tests.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Unit tests
2+
on:
3+
pull_request:
4+
types:
5+
- opened
6+
- edited
7+
- reopened
8+
- synchronize
9+
push:
10+
branches:
11+
- main
12+
jobs:
13+
python-tests:
14+
name: Run python tests
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
19+
with:
20+
fetch-depth: 0
21+
- name: Set up Python
22+
uses: actions/setup-python@39cd14951b08e74b54015e9e001cdefcf80e669f # v5.1.1
23+
with:
24+
python-version: 3.11
25+
- name: Run tests
26+
run: |
27+
python -m pip install --upgrade pip
28+
pip install tox
29+
tox -e py
30+
- name: Override Coverage Source Path for Sonar
31+
run: sed -i "s@<source>/home/runner/work/mlflow-oidc-auth/mlflow-oidc-auth</source>@<source>/github/workspace</source>@g" /home/runner/work/mlflow-oidc-auth/mlflow-oidc-auth/coverage.xml
32+
- name: SonarCloud Scan
33+
uses: SonarSource/sonarcloud-github-action@e44258b109568baa0df60ed515909fc6c72cba92 # v2.3.0
34+
env:
35+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,4 @@ flask_session/
176176
node_modules/
177177
.angular/
178178
mlflow_oidc_auth/ui
179+
pytest-coverage.txt

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ The plugin required the following environment variables but also supported `.env
3333
| OIDC_TOKEN_URL | OIDC Token URL (if discovery URL is not defined) |
3434
| OIDC_USER_URL | OIDC User info URL (if discovery URL is not defined) |
3535
| SECRET_KEY | Key to perform cookie encryption |
36-
| OAUTHLIB_INSECURE_TRANSPORT | Development only. Allow to use insecure endpoints for OIDC |
3736
| LOG_LEVEL | Application log level |
3837
| OIDC_USERS_DB_URI | Database connection string |
3938

docs/configuration/examples/microsoft.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ OIDC_SCOPE = "openid,profile,email"
1919
OIDC_GROUP_NAME = "mlflow_users_group_name"
2020
OIDC_ADMIN_GROUP_NAME = "mlflow_admins_group_name"
2121
```
22-

docs/configuration/index.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ The plugin required the following environment variables but also supported `.env
1717
| OIDC_TOKEN_URL | OIDC Token URL (if discovery URL is not defined) |
1818
| OIDC_USER_URL | OIDC User info URL (if discovery URL is not defined) |
1919
| SECRET_KEY | Key to perform cookie encryption |
20-
| OAUTHLIB_INSECURE_TRANSPORT | Development only. Allow to use insecure endpoints for OIDC |
2120
| LOG_LEVEL | Application log level |
2221
| OIDC_USERS_DB_URI | Database connection string |
2322

@@ -35,5 +34,3 @@ The plugin required the following environment variables but also supported `.env
3534
| REDIS_USERNAME | Redis username | None |
3635
| REDIS_PASSWORD | Redis password | None |
3736
| REDIS_SSL | Use SSL | false |
38-
39-

docs/permission-management/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@
44

55

66
## Permissions hierarchy
7-

mlflow_oidc_auth/auth.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
1-
from typing import Union
1+
from typing import Union, Optional
22

33
import requests
44
from authlib.integrations.flask_client import OAuth
55
from authlib.jose import jwt
66
from flask import Response, request
77
from werkzeug.datastructures import Authorization
88

9-
from mlflow_oidc_auth.app import app
109
from mlflow_oidc_auth.config import config
1110
from mlflow_oidc_auth.store import store
12-
oauth = OAuth(app)
1311

14-
oauth.register(
15-
name="oidc",
16-
client_id=config.OIDC_CLIENT_ID,
17-
client_secret=config.OIDC_CLIENT_SECRET,
18-
server_metadata_url=config.OIDC_DISCOVERY_URL,
19-
client_kwargs={"scope": config.OIDC_SCOPE},
20-
)
12+
13+
_oauth_instance: Optional[OAuth] = None
14+
15+
16+
def get_oauth_instance(app) -> OAuth:
17+
# returns a singleton instance of OAuth
18+
# to avoid circular imports
19+
global _oauth_instance
20+
21+
if _oauth_instance is None:
22+
_oauth_instance = OAuth(app)
23+
_oauth_instance.register(
24+
name="oidc",
25+
client_id=config.OIDC_CLIENT_ID,
26+
client_secret=config.OIDC_CLIENT_SECRET,
27+
server_metadata_url=config.OIDC_DISCOVERY_URL,
28+
client_kwargs={"scope": config.OIDC_SCOPE},
29+
)
30+
return _oauth_instance
31+
2132

2233
def _get_oidc_jwks():
23-
from mlflow_oidc_auth.app import cache
34+
from mlflow_oidc_auth.app import cache, app
35+
2436
jwks = cache.get("jwks")
2537
if jwks:
2638
app.logger.debug("JWKS cache hit")
@@ -41,6 +53,8 @@ def validate_token(token):
4153

4254

4355
def authenticate_request_basic_auth() -> Union[Authorization, Response]:
56+
from mlflow_oidc_auth.app import app
57+
4458
username = request.authorization.username
4559
password = request.authorization.password
4660
app.logger.debug("Authenticating user %s", username)
@@ -53,6 +67,8 @@ def authenticate_request_basic_auth() -> Union[Authorization, Response]:
5367

5468

5569
def authenticate_request_bearer_token() -> Union[Authorization, Response]:
70+
from mlflow_oidc_auth.app import app
71+
5672
token = request.authorization.token
5773
try:
5874
user = validate_token(token)

mlflow_oidc_auth/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
load_dotenv() # take environment variables from .env.
1111
app.logger.setLevel(os.environ.get("LOG_LEVEL", "INFO"))
1212

13+
1314
class AppConfig:
1415
def __init__(self):
1516
self.DEFAULT_MLFLOW_PERMISSION = os.environ.get("DEFAULT_MLFLOW_PERMISSION", "MANAGE")
@@ -52,4 +53,5 @@ def __init__(self):
5253
except ImportError:
5354
app.logger.error(f"Cache module for {self.CACHE_TYPE} could not be imported.")
5455

56+
5557
config = AppConfig()

0 commit comments

Comments
 (0)