Skip to content

Commit 4cab343

Browse files
author
maxim-lixakov
committed
[DOP-21268] - minor fixes
1 parent f09c543 commit 4cab343

File tree

10 files changed

+34
-31
lines changed

10 files changed

+34
-31
lines changed

syncmaster/backend/api/v1/auth.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
22
# SPDX-License-Identifier: Apache-2.0
3-
from typing import TYPE_CHECKING, Annotated
3+
from typing import Annotated
44

55
from fastapi import APIRouter, Depends, HTTPException, Request
66
from fastapi.responses import RedirectResponse
77
from fastapi.security import OAuth2PasswordRequestForm
88

99
from syncmaster.backend.dependencies import Stub
10-
from syncmaster.backend.providers.auth import AuthProvider
10+
from syncmaster.backend.providers.auth import (
11+
AuthProvider,
12+
DummyAuthProvider,
13+
KeycloakAuthProvider,
14+
)
1115
from syncmaster.backend.utils.state import validate_state
1216
from syncmaster.errors.registration import get_error_responses
1317
from syncmaster.errors.schemas.invalid_request import InvalidRequestSchema
1418
from syncmaster.errors.schemas.not_authorized import NotAuthorizedSchema
1519
from syncmaster.schemas.v1.auth import AuthTokenSchema
1620

17-
if TYPE_CHECKING:
18-
from syncmaster.backend.providers.auth import (
19-
DummyAuthProvider,
20-
KeycloakAuthProvider,
21-
)
22-
2321
router = APIRouter(
2422
prefix="/auth",
2523
tags=["Auth"],
@@ -29,7 +27,7 @@
2927

3028
@router.post("/token")
3129
async def token(
32-
auth_provider: Annotated["DummyAuthProvider", Depends(Stub(AuthProvider))],
30+
auth_provider: Annotated[DummyAuthProvider, Depends(Stub(AuthProvider))],
3331
form_data: OAuth2PasswordRequestForm = Depends(),
3432
) -> AuthTokenSchema:
3533
token = await auth_provider.get_token_password_grant(
@@ -48,7 +46,7 @@ async def auth_callback(
4846
request: Request,
4947
code: str,
5048
state: str,
51-
auth_provider: Annotated["KeycloakAuthProvider", Depends(Stub(AuthProvider))],
49+
auth_provider: Annotated[KeycloakAuthProvider, Depends(Stub(AuthProvider))],
5250
):
5351
original_redirect_url = validate_state(state)
5452
if not original_redirect_url:

syncmaster/backend/api/v1/runs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
RunPageSchema,
2323
)
2424
from syncmaster.worker.config import celery
25-
from syncmaster.worker.settings import worker_settings
25+
26+
# TODO: remove global import of WorkerSettings
27+
from syncmaster.worker.settings import WorkerSettings as Settings
2628

2729
router = APIRouter(tags=["Runs"], responses=get_error_responses())
2830

@@ -117,7 +119,7 @@ async def start_run(
117119
type=RunType.MANUAL,
118120
)
119121

120-
log_url = Template(worker_settings.LOG_URL_TEMPLATE).render(
122+
log_url = Template(Settings().LOG_URL_TEMPLATE).render(
121123
run=run,
122124
correlation_id=correlation_id.get(),
123125
)

syncmaster/backend/providers/auth/keycloak_provider.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ async def get_current_user(self, access_token: str, *args, **kwargs) -> Any:
105105
log.debug("Failed to refresh access token: %s", e)
106106
self.redirect_to_auth(request.url.path)
107107

108+
# these names are hardcoded in keycloak:
109+
# https://github.com/keycloak/keycloak/blob/3ca3a4ad349b4d457f6829eaf2ae05f1e01408be/core/src/main/java/org/keycloak/representations/IDToken.java
108110
user_id = token_info.get("sub")
109111
login = token_info.get("preferred_username")
110112
email = token_info.get("email")
@@ -133,9 +135,6 @@ async def refresh_access_token(self, refresh_token: str) -> dict[str, Any]:
133135
new_tokens = self.keycloak_openid.refresh_token(refresh_token)
134136
return new_tokens
135137

136-
async def get_user_info(self, access_token: str) -> dict[str, Any]:
137-
return self.keycloak_openid.userinfo(access_token)
138-
139138
def redirect_to_auth(self, path: str) -> None:
140139
state = generate_state(path)
141140
auth_url = self.keycloak_openid.auth_url(

syncmaster/backend/services/get_user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def get_user(
2121
async def wrapper(
2222
request: Request,
2323
auth_provider: Annotated[AuthProvider, Depends(Stub(AuthProvider))],
24-
access_token: Annotated[str, Depends(oauth_schema)],
24+
access_token: Annotated[str | None, Depends(oauth_schema)],
2525
) -> User:
2626
# keycloak provider patches session and store access_token in cookie,
2727
# when dummy auth stores it in "Authorization" header

syncmaster/worker/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
from celery import Task
44
from sqlalchemy import create_engine
55

6-
from syncmaster.worker.settings import worker_settings
6+
# TODO: remove global import of WorkerSettings
7+
from syncmaster.worker.settings import WorkerSettings as Settings
78

89

910
class WorkerTask(Task):
1011
def __init__(self) -> None:
11-
self.settings = worker_settings
12+
self.settings = Settings()
1213
self.engine = create_engine(
1314
url=self.settings.database.sync_url,
1415
)

syncmaster/worker/config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
from celery import Celery
44

55
from syncmaster.worker.base import WorkerTask
6-
from syncmaster.worker.settings import worker_settings
76

7+
# TODO: remove global import of WorkerSettings
8+
from syncmaster.worker.settings import WorkerSettings as Settings
9+
10+
worker_settings = Settings()
811
celery = Celery(
912
__name__,
1013
broker=worker_settings.broker.url,

syncmaster/worker/controller.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
from syncmaster.worker.handlers.db.postgres import PostgresHandler
2626
from syncmaster.worker.handlers.file.hdfs import HDFSHandler
2727
from syncmaster.worker.handlers.file.s3 import S3Handler
28-
from syncmaster.worker.settings import worker_settings
28+
29+
# TODO: remove global import of WorkerSettings
30+
from syncmaster.worker.settings import WorkerSettings as Settings
2931

3032
logger = logging.getLogger(__name__)
3133

@@ -84,7 +86,7 @@ def __init__(
8486
)
8587

8688
def perform_transfer(self) -> None:
87-
spark = worker_settings.CREATE_SPARK_SESSION_FUNCTION(
89+
spark = Settings().CREATE_SPARK_SESSION_FUNCTION(
8890
run=self.run,
8991
source=self.source_handler.connection_dto,
9092
target=self.target_handler.connection_dto,

syncmaster/worker/settings/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,3 @@ class WorkerSettings(SyncmasterSettings):
2020
LOG_URL_TEMPLATE: str = ""
2121

2222
CREATE_SPARK_SESSION_FUNCTION: ImportString = "syncmaster.worker.spark.get_worker_spark_session"
23-
24-
25-
worker_settings = WorkerSettings()

syncmaster/worker/transfer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
from syncmaster.worker.base import WorkerTask
1919
from syncmaster.worker.config import celery
2020
from syncmaster.worker.controller import TransferController
21-
from syncmaster.worker.settings import worker_settings
21+
from syncmaster.worker.settings import WorkerSettings as WorkerSettings
2222

2323
logger = get_task_logger(__name__)
2424

25-
CORRELATION_CELERY_HEADER_ID = worker_settings.CORRELATION_CELERY_HEADER_ID
25+
# TODO: remove global import of WorkerSettings
26+
CORRELATION_CELERY_HEADER_ID = WorkerSettings().CORRELATION_CELERY_HEADER_ID
2627

2728

2829
@celery.task(name="run_transfer_task", bind=True, track_started=True)
@@ -85,7 +86,7 @@ def run_transfer(session: Session, run_id: int, settings: Settings):
8586

8687
@after_setup_logger.connect
8788
def setup_loggers(*args, **kwargs):
88-
setup_logging(worker_settings.logging.get_log_config_path())
89+
setup_logging(Settings().logging.get_log_config_path())
8990

9091

9192
@before_task_publish.connect()

tests/test_unit/test_runs/test_create_run.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from sqlalchemy.ext.asyncio import AsyncSession
77

88
from syncmaster.db.models import Run, RunType, Status
9-
from syncmaster.worker.settings import worker_settings
9+
10+
# TODO: remove global import of WorkerSettings
11+
from syncmaster.worker.settings import WorkerSettings as Settings
1012
from tests.mocks import MockGroup, MockTransfer, MockUser, UserTestRoles
1113

1214
pytestmark = [pytest.mark.asyncio, pytest.mark.backend]
@@ -140,9 +142,7 @@ async def test_superuser_can_create_run(
140142
mocker,
141143
) -> None:
142144
# Arrange
143-
worker_settings.LOG_URL_TEMPLATE = (
144-
"https://grafana.example.com?correlation_id={{ correlation_id }}&run_id={{ run.id }}"
145-
)
145+
Settings().LOG_URL_TEMPLATE = "https://grafana.example.com?correlation_id={{ correlation_id }}&run_id={{ run.id }}"
146146
mock_send_task = mocker.patch("syncmaster.worker.config.celery.send_task")
147147
mock_to_thread = mocker.patch("asyncio.to_thread", new_callable=AsyncMock)
148148

0 commit comments

Comments
 (0)