-
Notifications
You must be signed in to change notification settings - Fork 16.3k
Migrate FAB POST /users to FastAPI #57480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2ee941c
14f9262
b012a08
390ba56
deab77d
02b7b5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,62 @@ | ||||||
| # Licensed to the Apache Software Foundation (ASF) under one | ||||||
| # or more contributor license agreements. See the NOTICE file | ||||||
| # distributed with this work for additional information | ||||||
| # regarding copyright ownership. The ASF licenses this file | ||||||
| # to you under the Apache License, Version 2.0 (the | ||||||
| # "License"); you may not use this file except in compliance | ||||||
| # with the License. You may obtain a copy of the License at | ||||||
| # | ||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| # | ||||||
| # Unless required by applicable law or agreed to in writing, | ||||||
| # software distributed under the License is distributed on an | ||||||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||
| # KIND, either express or implied. See the License for the | ||||||
| # specific language governing permissions and limitations | ||||||
| # under the License. | ||||||
| from __future__ import annotations | ||||||
|
|
||||||
| from datetime import datetime, timezone | ||||||
| from typing import TYPE_CHECKING | ||||||
|
|
||||||
| from pydantic import Field, SecretStr, field_validator | ||||||
|
|
||||||
| from airflow.api_fastapi.core_api.base import BaseModel, StrictBaseModel | ||||||
| from airflow.providers.fab.auth_manager.api_fastapi.datamodels.roles import Role | ||||||
|
|
||||||
| if TYPE_CHECKING: | ||||||
| from airflow.providers.fab.auth_manager.api_fastapi.datamodels.roles import Role | ||||||
|
|
||||||
|
|
||||||
| class UserBody(StrictBaseModel): | ||||||
| """Incoming payload for creating a user.""" | ||||||
|
|
||||||
| username: str = Field(min_length=1) | ||||||
| email: str = Field(min_length=1) | ||||||
| first_name: str = Field(min_length=1) | ||||||
| last_name: str = Field(min_length=1) | ||||||
| roles: list[Role] | None = None | ||||||
| password: SecretStr | ||||||
|
|
||||||
|
|
||||||
| class UserResponse(BaseModel): | ||||||
| """Outgoing representation of a user (no password).""" | ||||||
|
|
||||||
| username: str | ||||||
| email: str | ||||||
| first_name: str | ||||||
| last_name: str | ||||||
| roles: list[Role] | None = None | ||||||
| active: bool | None = None | ||||||
| last_login: datetime | None = None | ||||||
| login_count: int | None = None | ||||||
| fail_login_count: int | None = None | ||||||
| created_on: datetime | None = None | ||||||
| changed_on: datetime | None = None | ||||||
|
|
||||||
| @field_validator("created_on", "changed_on") | ||||||
| @classmethod | ||||||
| def _coerce_tzaware(cls, v: datetime | None) -> datetime | None: | ||||||
| if v is None: | ||||||
| return None | ||||||
| return v if (v.tzinfo and v.utcoffset() is not None) else v.replace(tzinfo=timezone.utc) | ||||||
|
Comment on lines
+54
to
+62
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to leverage airflow/airflow-core/src/airflow/api_fastapi/execution_api/datamodels/taskinstance.py Lines 282 to 283 in bc3a750
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,52 @@ | ||||||
| # Licensed to the Apache Software Foundation (ASF) under one | ||||||
| # or more contributor license agreements. See the NOTICE file | ||||||
| # distributed with this work for additional information | ||||||
| # regarding copyright ownership. The ASF licenses this file | ||||||
| # to you under the Apache License, Version 2.0 (the | ||||||
| # "License"); you may not use this file except in compliance | ||||||
| # with the License. You may obtain a copy of the License at | ||||||
| # | ||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| # | ||||||
| # Unless required by applicable law or agreed to in writing, | ||||||
| # software distributed under the License is distributed on an | ||||||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||
| # KIND, either express or implied. See the License for the | ||||||
| # specific language governing permissions and limitations | ||||||
| # under the License. | ||||||
| from __future__ import annotations | ||||||
|
|
||||||
| from typing import TYPE_CHECKING | ||||||
|
|
||||||
| from fastapi import Depends, status | ||||||
|
|
||||||
| from airflow.api_fastapi.common.router import AirflowRouter | ||||||
| from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc | ||||||
| from airflow.providers.fab.auth_manager.api_fastapi.datamodels.users import UserBody, UserResponse | ||||||
| from airflow.providers.fab.auth_manager.api_fastapi.security import requires_fab_custom_view | ||||||
| from airflow.providers.fab.auth_manager.api_fastapi.services.users import FABAuthManagerUsers | ||||||
| from airflow.providers.fab.auth_manager.cli_commands.utils import get_application_builder | ||||||
| from airflow.providers.fab.www.security import permissions | ||||||
|
|
||||||
| if TYPE_CHECKING: | ||||||
| from airflow.providers.fab.auth_manager.api_fastapi.datamodels.users import UserBody, UserResponse | ||||||
|
Comment on lines
+31
to
+32
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| users_router = AirflowRouter(prefix="/fab/v1", tags=["FabAuthManager"]) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking nit, can be done in follow-up PR: |
||||||
|
|
||||||
|
|
||||||
| @users_router.post( | ||||||
| "/users", | ||||||
| responses=create_openapi_http_exception_doc( | ||||||
| [ | ||||||
| status.HTTP_400_BAD_REQUEST, | ||||||
| status.HTTP_401_UNAUTHORIZED, | ||||||
| status.HTTP_403_FORBIDDEN, | ||||||
| status.HTTP_409_CONFLICT, | ||||||
| status.HTTP_500_INTERNAL_SERVER_ERROR, | ||||||
| ] | ||||||
| ), | ||||||
| dependencies=[Depends(requires_fab_custom_view("POST", permissions.RESOURCE_USER))], | ||||||
| ) | ||||||
| def create_user(body: UserBody) -> UserResponse: | ||||||
| with get_application_builder(): | ||||||
| return FABAuthManagerUsers.create_user(body=body) | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we don't need the
TYPE_CHECKINGas we already imported in line 25.