Skip to content

Commit dbe6e98

Browse files
author
maxim-lixakov
committed
[DOP-21268] - refactor auth configuration settings (add providers)
1 parent cd6386a commit dbe6e98

File tree

31 files changed

+726
-144
lines changed

31 files changed

+726
-144
lines changed

.env.docker

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,34 @@ SYNCMASTER__WORKER__LOGGING__PRESET=json
1515
# Postgres
1616
SYNCMASTER__DATABASE__URL=postgresql+asyncpg://syncmaster:changeme@db:5432/syncmaster
1717

18+
# Keycloack (MTS)
19+
SYNCMASTER__AUTH__KEYCLOAK_SERVER_URL=https://isso.mts.ru/auth/
20+
SYNCMASTER__AUTH__KEYCLOAK_REALM_NAME=mts
21+
SYNCMASTER__AUTH__KEYCLOAK_CLIENT_ID=syncmaster_dev
22+
SYNCMASTER__AUTH__KEYCLOAK_CLIENT_SECRET=secret
23+
SYNCMASTER__AUTH__KEYCLOAK_REDIRECT_URI=http://localhost:8000/callback
24+
SYNCMASTER__AUTH__KEYCLOAK_ADMIN_REDIRECT_URI=http://localhost:8000/admin/callback
25+
SYNCMASTER__AUTH__KEYCLOAK_SCOPE=email
26+
SYNCMASTER__AUTH__KEYCLOAK_INTROSPECTION_DELAY=60
27+
SYNCMASTER__AUTH__PROVIDER=syncmaster.backend.providers.auth.keycloak.KeycloakAuthProvider
28+
SYNCMASTER__AUTH__KEYCLOAK_TOKEN_URL=https://isso.mts.ru/auth/realms/mts/protocol/openid-connect/token
29+
30+
31+
SYNCMASTER__AUTH__KEYCLOAK__SERVER_URL=http://localhost:8080/auth/
32+
SYNCMASTER__AUTH__KEYCLOAK__REALM_NAME=fastapi-realm
33+
SYNCMASTER__AUTH__KEYCLOAK__CLIENT_ID=fastapi-client
34+
SYNCMASTER__AUTH__KEYCLOAK__CLIENT_SECRET=VoLrqGz1HGjp6MiwzRaGWIu7z7imKIHb
35+
SYNCMASTER__AUTH__KEYCLOAK__REDIRECT_URI=http://localhost:8000/callback
36+
SYNCMASTER__AUTH__KEYCLOAK__ADMIN_REDIRECT_URI=http://localhost:8000/admin/callback
37+
SYNCMASTER__AUTH__KEYCLOAK__SCOPE=email
38+
SYNCMASTER__AUTH__KEYCLOAK__INTROSPECTION_DELAY=60
39+
SYNCMASTER__AUTH__PROVIDER=syncmaster.backend.providers.auth.keycloak.KeycloakAuthProvider
40+
SYNCMASTER__AUTH__KEYCLOAK__TOKEN_URL=http://localhost:8080/auth/realms/fastapi-realm/protocol/openid-connect/token
41+
42+
43+
SYNCMASTER__AUTH__PROVIDER=syncmaster.backend.providers.auth.dummy.DummyAuthProvider
44+
SYNCMASTER__AUTH__ACCESS_TOKEN__SECRET_KEY=bae1thahr8Iyaisai0kohvoh1aeg5quu
45+
1846
# RabbitMQ
1947
SYNCMASTER__BROKER__URL=amqp://guest:guest@rabbitmq:5672/
2048

.env.local

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export SYNCMASTER__WORKER__LOGGING__PRESET=json
1515
# Postgres
1616
export SYNCMASTER__DATABASE__URL=postgresql+asyncpg://syncmaster:changeme@localhost:5432/syncmaster
1717

18+
# Auth
19+
export SYNCMASTER__AUTH__PROVIDER=syncmaster.backend.providers.auth.dummy.DummyAuthProvider
20+
export SYNCMASTER__AUTH__ACCESS_TOKEN__SECRET_KEY=bae1thahr8Iyaisai0kohvoh1aeg5quu
21+
1822
# RabbitMQ
1923
export SYNCMASTER__BROKER__URL=amqp://guest:guest@localhost:5672/
2024

docker-compose.test.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ services:
153153
retries: 3
154154
profiles: [hive, hdfs, all]
155155

156+
keycloak:
157+
image: quay.io/keycloak/keycloak:latest
158+
command: start-dev
159+
restart: unless-stopped
160+
environment:
161+
KEYCLOAK_ADMIN: admin
162+
KEYCLOAK_ADMIN_PASSWORD: admin
163+
ports:
164+
- 8080:8080
165+
volumes:
166+
- keycloak_data:/opt/keycloak/data
167+
profiles: [keycloak, all]
168+
156169
test-hive:
157170
image: mtsrus/hadoop:hadoop2.7.3-hive2.3.9
158171
restart: unless-stopped

poetry.lock

Lines changed: 182 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ coloredlogs = {version = "*", optional = true}
6767
python-json-logger = {version = "*", optional = true}
6868
asyncpg = { version = ">=0.29,<0.31", optional = true }
6969
apscheduler = { version = "^3.10.4", optional = true }
70+
python-keycloak = {version = "^4.7.0", optional = true}
71+
devtools = {version = "*", optional = true}
7072

7173
[tool.poetry.extras]
7274
backend = [
@@ -85,6 +87,8 @@ backend = [
8587
"coloredlogs",
8688
"python-json-logger",
8789
"asyncpg",
90+
"devtools",
91+
"python-keycloak",
8892
# migrations only
8993
"celery",
9094
"apscheduler",

syncmaster/backend/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
22
# SPDX-License-Identifier: Apache-2.0
3-
import uuid
3+
from typing import Type
44

55
from fastapi import FastAPI, HTTPException
66
from fastapi.exceptions import RequestValidationError
@@ -15,6 +15,7 @@
1515
validation_exception_handler,
1616
)
1717
from syncmaster.backend.middlewares import apply_middlewares
18+
from syncmaster.backend.providers.auth import AuthProvider
1819
from syncmaster.backend.services.unit_of_work import UnitOfWork
1920
from syncmaster.db.factory import create_session_factory, get_uow
2021
from syncmaster.exceptions import SyncmasterError
@@ -44,6 +45,9 @@ def application_factory(settings: Settings) -> FastAPI:
4445
},
4546
)
4647

48+
auth_class: type[AuthProvider] = settings.auth.provider # type: ignore[assignment]
49+
auth_class.setup(application)
50+
4751
apply_middlewares(application, settings)
4852
return application
4953

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
from fastapi import APIRouter, Depends
66
from fastapi.security import OAuth2PasswordRequestForm
77

8-
from syncmaster.backend.api.v1.auth.utils import sign_jwt
98
from syncmaster.backend.dependencies import Stub
9+
from syncmaster.backend.providers.auth import AuthProvider
1010
from syncmaster.backend.services import UnitOfWork
1111
from syncmaster.errors.registration import get_error_responses
1212
from syncmaster.errors.schemas.invalid_request import InvalidRequestSchema
1313
from syncmaster.errors.schemas.not_authorized import NotAuthorizedSchema
14-
from syncmaster.exceptions import EntityNotFoundError
1514
from syncmaster.schemas.v1.auth import AuthTokenSchema
1615
from syncmaster.settings import Settings
1716

@@ -24,18 +23,18 @@
2423

2524
@router.post("/token")
2625
async def login(
26+
auth_provider: Annotated[AuthProvider, Depends(Stub(AuthProvider))],
2727
settings: Annotated[Settings, Depends(Stub(Settings))],
28-
form_data: OAuth2PasswordRequestForm = Depends(),
2928
unit_of_work: UnitOfWork = Depends(UnitOfWork),
29+
form_data: OAuth2PasswordRequestForm = Depends(),
3030
) -> AuthTokenSchema:
3131
"""This is the test auth method!!! Not for production!!!!"""
32-
try:
33-
user = await unit_of_work.user.read_by_username(username=form_data.username)
34-
except EntityNotFoundError:
35-
async with unit_of_work:
36-
user = await unit_of_work.user.create(
37-
username=form_data.username,
38-
is_active=True,
39-
)
40-
token = sign_jwt(user_id=user.id, settings=settings)
41-
return AuthTokenSchema(access_token=token, refresh_token="refresh_token")
32+
token = await auth_provider.get_token(
33+
grant_type=form_data.grant_type,
34+
login=form_data.username,
35+
password=form_data.password,
36+
scopes=form_data.scopes,
37+
client_id=form_data.client_id,
38+
client_secret=form_data.client_secret,
39+
)
40+
return AuthTokenSchema.parse_obj(token)

syncmaster/backend/api/v1/auth/utils.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

syncmaster/backend/api/v1/router.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33
from fastapi import APIRouter
44

5-
from syncmaster.backend.api.v1.auth.router import router as auth_router
5+
from syncmaster.backend.api.v1.auth import router as auth_router
66
from syncmaster.backend.api.v1.connections import router as connection_router
77
from syncmaster.backend.api.v1.groups import router as group_router
88
from syncmaster.backend.api.v1.queue import router as queue_router
File renamed without changes.

0 commit comments

Comments
 (0)