Skip to content

Commit 15467e5

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

File tree

34 files changed

+826
-295
lines changed

34 files changed

+826
-295
lines changed

.env.docker

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,40 @@ SYNCMASTER__SERVER__LOGGING__PRESET=colored
1212
SYNCMASTER__WORKER__LOGGING__SETUP=True
1313
SYNCMASTER__WORKER__LOGGING__PRESET=json
1414

15+
# Encrypt / Decrypt credentials data
16+
SYNCMASTER__CRYPTO_KEY=UBgPTioFrtH2unlC4XFDiGf5sYfzbdSf_VgiUSaQc94=
17+
1518
# Postgres
1619
SYNCMASTER__DATABASE__URL=postgresql+asyncpg://syncmaster:changeme@db:5432/syncmaster
1720

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

.env.local

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@ export SYNCMASTER__SERVER__LOGGING__PRESET=colored
1212
export SYNCMASTER__WORKER__LOGGING__SETUP=True
1313
export SYNCMASTER__WORKER__LOGGING__PRESET=json
1414

15+
# Encrypt / Decrypt credentials data
16+
export SYNCMASTER__CRYPTO_KEY=UBgPTioFrtH2unlC4XFDiGf5sYfzbdSf_VgiUSaQc94=
17+
1518
# Postgres
1619
export SYNCMASTER__DATABASE__URL=postgresql+asyncpg://syncmaster:changeme@localhost:5432/syncmaster
1720

21+
# Auth
22+
export SYNCMASTER__AUTH__PROVIDER=syncmaster.backend.providers.auth.dummy.DummyAuthProvider
23+
export SYNCMASTER__AUTH__ACCESS_TOKEN__SECRET_KEY=bae1thahr8Iyaisai0kohvoh1aeg5quu
24+
1825
# RabbitMQ
1926
export SYNCMASTER__BROKER__URL=amqp://guest:guest@localhost:5672/
2027

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: 212 additions & 121 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ python-multipart = { version = ">=0.0.9,<0.0.18", optional = true }
6161
celery = { version = "^5.4.0", optional = true }
6262
onetl = { version = "^0.12.0", extras = ["spark"], optional = true }
6363
# due to not supporting MacOS 14.x https://www.psycopg.org/psycopg3/docs/news.html#psycopg-3-1-20
64-
psycopg = { version = ">=3.1.0,<3.2.4", extras = ["binary"], optional = true }
64+
psycopg = { version = ">=3.1.0, <3.1.20", extras = ["binary"], optional = true }
6565
uuid6 = "^2024.7.10"
6666
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 }
7070
starlette-exporter = {version = "^0.23.0", optional = true}
71+
python-keycloak = {version = "^4.7.0", optional = true}
72+
devtools = {version = "*", optional = true}
7173

7274
[tool.poetry.extras]
7375
backend = [
@@ -87,6 +89,8 @@ backend = [
8789
"coloredlogs",
8890
"python-json-logger",
8991
"asyncpg",
92+
"devtools",
93+
"python-keycloak",
9094
# migrations only
9195
"celery",
9296
"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

syncmaster/backend/api/v1/auth.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
2+
# SPDX-License-Identifier: Apache-2.0
3+
from typing import Annotated
4+
5+
from fastapi import APIRouter, Depends
6+
from fastapi.security import OAuth2PasswordRequestForm
7+
8+
from syncmaster.backend.dependencies import Stub
9+
from syncmaster.backend.providers.auth import AuthProvider
10+
from syncmaster.errors.registration import get_error_responses
11+
from syncmaster.errors.schemas.invalid_request import InvalidRequestSchema
12+
from syncmaster.errors.schemas.not_authorized import NotAuthorizedSchema
13+
from syncmaster.schemas.v1.auth import AuthTokenSchema
14+
15+
router = APIRouter(
16+
prefix="/auth",
17+
tags=["Auth"],
18+
responses=get_error_responses(include={NotAuthorizedSchema, InvalidRequestSchema}),
19+
)
20+
21+
22+
@router.post("/token")
23+
async def login(
24+
auth_provider: Annotated[AuthProvider, Depends(Stub(AuthProvider))],
25+
form_data: OAuth2PasswordRequestForm = Depends(),
26+
) -> AuthTokenSchema:
27+
token = await auth_provider.get_token(
28+
grant_type=form_data.grant_type,
29+
login=form_data.username,
30+
password=form_data.password,
31+
scopes=form_data.scopes,
32+
client_id=form_data.client_id,
33+
client_secret=form_data.client_secret,
34+
)
35+
return AuthTokenSchema.parse_obj(token)

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

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

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

0 commit comments

Comments
 (0)