Skip to content

Commit a0b8f1f

Browse files
author
maxim-lixakov
committed
[DOP-21268] - minor fixes
1 parent 772dc91 commit a0b8f1f

File tree

20 files changed

+47
-59
lines changed

20 files changed

+47
-59
lines changed

.env.docker

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SYNCMASTER__SERVER__DEBUG=true
77
# Logging
88
SYNCMASTER__LOGGING__SETUP=True
99
SYNCMASTER__LOGGING__PRESET=colored
10+
SYNCMASTER__LOG_URL_TEMPLATE=https://grafana.example.com?correlation_id={{ correlation_id }}&run_id={{ run.id }}
1011

1112
# Encrypt / Decrypt credentials data
1213
SYNCMASTER__CRYPTO_KEY=UBgPTioFrtH2unlC4XFDiGf5sYfzbdSf_VgiUSaQc94=

.env.local

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export SYNCMASTER__SERVER__DEBUG=true
77
# Logging
88
export SYNCMASTER__LOGGING__SETUP=True
99
export SYNCMASTER__LOGGING__PRESET=colored
10+
export SYNCMASTER__LOG_URL_TEMPLATE="https://grafana.example.com?correlation_id={{ correlation_id }}&run_id={{ run.id }}"
1011

1112
# Encrypt / Decrypt credentials data
1213
export SYNCMASTER__CRYPTO_KEY=UBgPTioFrtH2unlC4XFDiGf5sYfzbdSf_VgiUSaQc94=

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/dependencies/get_access_token.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from fastapi.security.utils import get_authorization_scheme_param
55

66

7-
async def get_access_token(request: Request):
7+
async def get_access_token(request: Request) -> str | None:
88
authorization = request.headers.get("Authorization")
99
scheme, token = get_authorization_scheme_param(authorization)
1010
if not authorization or scheme.lower() != "bearer":

syncmaster/backend/middlewares/static_files.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
22
# SPDX-License-Identifier: Apache-2.0
33
from fastapi import FastAPI
4-
from fastapi.staticfiles import StaticFiles
54

65
from syncmaster.backend.settings.server.static_files import StaticFilesSettings
76

@@ -12,5 +11,5 @@ def apply_static_files(app: FastAPI, settings: StaticFilesSettings) -> FastAPI:
1211
return app
1312

1413
# https://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/#serve-the-static-files
15-
app.mount("/static", StaticFiles(directory=settings.directory), name="static")
14+
# app.mount("/static", StaticFiles(directory=settings.directory), name="static")
1615
return app

syncmaster/backend/providers/auth/dummy_provider.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ async def get_token_password_grant(
6565
user = await self._uow.user.create(
6666
username=login,
6767
email=f"{login}@example.com",
68-
first_name=f"{login}_first",
69-
middle_name=f"{login}_middle",
70-
last_name=f"{login}_last",
7168
is_active=True,
7269
)
7370

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: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from collections.abc import Callable, Coroutine
44
from typing import Annotated, Any
55

6-
from fastapi import Depends, Request, status
7-
from fastapi.exceptions import HTTPException
6+
from fastapi import Depends, Request
87
from fastapi.security import OAuth2PasswordBearer
98

109
from syncmaster.backend.dependencies import Stub
1110
from syncmaster.backend.providers.auth import AuthProvider
1211
from syncmaster.db.models import User
12+
from syncmaster.exceptions import ActionNotAllowedError, EntityNotFoundError
1313

1414
oauth_schema = OAuth2PasswordBearer(tokenUrl="v1/auth/token", auto_error=False)
1515

@@ -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
@@ -31,20 +31,11 @@ async def wrapper(
3131
request=request,
3232
)
3333
if user is None:
34-
raise HTTPException(
35-
status_code=status.HTTP_404_NOT_FOUND,
36-
detail="User not found",
37-
)
34+
raise EntityNotFoundError("User not found")
3835
if is_active and not user.is_active:
39-
raise HTTPException(
40-
status_code=status.HTTP_403_FORBIDDEN,
41-
detail="Inactive user",
42-
)
36+
raise ActionNotAllowedError("Inactive user")
4337
if is_superuser and not user.is_superuser:
44-
raise HTTPException(
45-
status_code=status.HTTP_403_FORBIDDEN,
46-
detail="You have no power here",
47-
)
38+
raise ActionNotAllowedError("You have no power here")
4839
return user
4940

5041
return wrapper

syncmaster/db/repositories/user.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ async def create(
6060
username: str,
6161
email: str,
6262
is_active: bool,
63-
first_name: str | None,
64-
middle_name: str | None,
65-
last_name: str | None,
63+
first_name: str | None = None,
64+
middle_name: str | None = None,
65+
last_name: str | None = None,
6666
is_superuser: bool = False,
6767
) -> User:
6868
query = (

0 commit comments

Comments
 (0)