Skip to content

Commit a6aecd1

Browse files
feat(airflow): Call OPA from FabAuthManager
1 parent 1eba0c8 commit a6aecd1

File tree

4 files changed

+71
-10
lines changed

4 files changed

+71
-10
lines changed

airflow/opa-auth-manager/opa_auth_manager/opa_fab_auth_manager.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
)
1717
from airflow.providers.fab.auth_manager.fab_auth_manager import FabAuthManager
1818
from airflow.utils.log.logging_mixin import LoggingMixin
19+
import requests
1920

2021
class OpaFabAuthManager(FabAuthManager, LoggingMixin):
2122
"""
@@ -42,7 +43,24 @@ def is_authorized_configuration(
4243

4344
self.log.info("Forward is_authorized_configuration to OPA")
4445

45-
return True
46+
if not user:
47+
user = self.get_user()
48+
49+
input= {
50+
'method': method,
51+
'details': details,
52+
'user': {
53+
'id': user.get_id(),
54+
'name': user.get_name(),
55+
},
56+
}
57+
response = requests.post(
58+
'http://opa:8081/v1/data/airflow/is_authorized_configuration',
59+
json=input,
60+
timeout=10
61+
).json()
62+
63+
return response.get("result") == "True"
4664

4765
def is_authorized_connection(
4866
self,

airflow/opa-auth-manager/poetry.lock

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

airflow/opa-auth-manager/pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ readme = "README.md"
77

88
[tool.poetry.dependencies]
99
python = ">=3.9.0,<3.13"
10+
requests = "^2.32.3"
1011

1112
[tool.poetry.group.dev.dependencies]
1213
apache-airflow = "^2.10.3"
1314
pylint = "^3.3.1"
1415
pytest = "^8.3.3"
1516

17+
18+
[tool.poetry.group.test.dependencies]
19+
requests-mock = "^1.12.1"
20+
1621
[build-system]
1722
requires = ["poetry-core"]
1823
build-backend = "poetry.core.masonry.api"

airflow/opa-auth-manager/tests/test_opa_fab_auth_manager.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from airflow.auth.managers.models.base_user import BaseUser
12
from airflow.auth.managers.models.resource_details import AccessView
23
import pytest
34

@@ -6,20 +7,41 @@
67
from opa_auth_manager.opa_fab_auth_manager import OpaFabAuthManager
78

89
@pytest.fixture
9-
def flask_app():
10+
def flask_app() -> Flask:
1011
return Flask(__name__)
1112

1213
@pytest.fixture
13-
def auth_manager(flask_app):
14+
def auth_manager(flask_app) -> OpaFabAuthManager:
1415
appbuilder = init_appbuilder(flask_app)
1516
return OpaFabAuthManager(appbuilder)
1617

18+
class User(BaseUser):
19+
20+
def __init__(self, username: str) -> None:
21+
self.username = username
22+
23+
def get_id(self) -> str:
24+
return self.username
25+
26+
def get_name(self) -> str:
27+
return self.username
28+
1729
@pytest.mark.db_test
1830
class TestOpaFabAuthManager:
1931

20-
def test_is_authorized_configuration(self, auth_manager: OpaFabAuthManager):
21-
result = auth_manager.is_authorized_configuration(
32+
def test_is_authorized_configuration(
33+
self,
34+
auth_manager: OpaFabAuthManager,
35+
requests_mock
36+
) -> None:
37+
requests_mock.post(
38+
'http://opa:8081/v1/data/airflow/is_authorized_configuration',
39+
text='{ "result": "True" }'
40+
)
41+
42+
result: bool = auth_manager.is_authorized_configuration(
2243
method="GET",
44+
user=User(username='testuser'),
2345
)
2446
expected_result = True
2547
assert result == expected_result

0 commit comments

Comments
 (0)