-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Add SimpleAllAdminMiddleware to allow api usage without auth header in request #49599
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
Merged
pierrejeambrun
merged 16 commits into
apache:main
from
rawwar:kalyan/auth/sam/all_admin_allow_api_calls
Apr 24, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c1a15a9
add SimpleAllAdminMiddleware
rawwar 2059b92
add tests
rawwar 754b6a7
update test
rawwar cd1bb35
update test name
rawwar e0e5cdc
Merge branch 'main' of github.com:apache/airflow into kalyan/auth/sam…
rawwar 4181bb1
refactor test
rawwar c503df8
sort test order
rawwar de0e376
hardcode endpoints
rawwar 66f4f29
use session
rawwar 94faef6
db_test mark
rawwar 5499697
Merge branch 'main' of github.com:apache/airflow into kalyan/auth/sam…
rawwar f4e44e3
iterate over app.routes
rawwar 7ad6cff
Merge branch 'main' of github.com:apache/airflow into kalyan/auth/sam…
rawwar 019296c
use fewer endpoints
rawwar 6f18eb3
update comment
rawwar fe71f6d
update scope
rawwar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
airflow-core/src/airflow/api_fastapi/auth/managers/simple/middleware.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # 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 fastapi import Request | ||
| from starlette.middleware.base import BaseHTTPMiddleware | ||
|
|
||
| from airflow.api_fastapi.auth.managers.simple.services.login import SimpleAuthManagerLogin | ||
|
|
||
|
|
||
| class SimpleAllAdminMiddleware(BaseHTTPMiddleware): | ||
| """Middleware that automatically generates and includes auth header for simple auth manager.""" | ||
|
|
||
| async def dispatch(self, request: Request, call_next): | ||
| # Starlette Request is expected to be immutable, but we modify it to add the auth header | ||
| # https://github.com/fastapi/fastapi/issues/2727#issuecomment-770202019 | ||
| token = SimpleAuthManagerLogin.create_token_all_admins() | ||
| request.scope["headers"].append((b"authorization", f"Bearer {token}".encode())) | ||
| return await call_next(request) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
airflow-core/tests/unit/api_fastapi/auth/managers/simple/test_middleware.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # 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 | ||
|
|
||
| import pytest | ||
| from fastapi.testclient import TestClient | ||
|
|
||
| from airflow.api_fastapi.app import create_app | ||
|
|
||
| from tests_common.test_utils.config import conf_vars | ||
|
|
||
| pytestmark = pytest.mark.db_test | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def all_access_test_client(): | ||
| with conf_vars( | ||
| { | ||
| ("core", "simple_auth_manager_all_admins"): "true", | ||
| ("webserver", "expose_config"): "true", | ||
| } | ||
| ): | ||
| app = create_app() | ||
| yield TestClient(app) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "method, path", | ||
| [ | ||
| ("GET", "/api/v2/assets"), | ||
| ("POST", "/api/v2/backfills"), | ||
| ("GET", "/api/v2/config"), | ||
| ("GET", "/api/v2/dags"), | ||
| ("POST", "/api/v2/dags/{dag_id}/clearTaskInstances"), | ||
| ("GET", "/api/v2/dags/{dag_id}/dagRuns"), | ||
| ("GET", "/api/v2/eventLogs"), | ||
| ("GET", "/api/v2/jobs"), | ||
| ("GET", "/api/v2/variables"), | ||
| ("GET", "/api/v2/version"), | ||
| ], | ||
| ) | ||
| def test_all_endpoints_without_auth_header(all_access_test_client, method, path): | ||
| response = all_access_test_client.request(method, path) | ||
| assert response.status_code not in {401, 403}, ( | ||
| f"Unexpected status code {response.status_code} for {method} {path}" | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.