Skip to content
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

Refactor: Extract and reuse get_kerberos_principle func from get_kerberos_principle #34936

Merged
merged 6 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions airflow/security/kerberos.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
log = logging.getLogger(__name__)


def get_kerberos_principle(principal: str | None) -> str:
"""Retrieve Kerberos principal. Fallback to principal from Airflow configuration if not provided."""
return principal or conf.get_mandatory_value("kerberos", "principal").replace("_HOST", get_hostname())


def renew_from_kt(principal: str | None, keytab: str, exit_on_fail: bool = True):
"""
Renew kerberos token from keytab.
Expand All @@ -59,10 +64,7 @@ def renew_from_kt(principal: str | None, keytab: str, exit_on_fail: bool = True)
# minutes to give ourselves a large renewal buffer.
renewal_lifetime = f"{conf.getint('kerberos', 'reinit_frequency')}m"

cmd_principal = principal or conf.get_mandatory_value("kerberos", "principal").replace(
"_HOST", get_hostname()
)

cmd_principal = get_kerberos_principle(principal)
if conf.getboolean("kerberos", "forwardable"):
forwardable = "-f"
else:
Expand Down
13 changes: 12 additions & 1 deletion tests/security/test_kerberos.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import pytest

from airflow.security import kerberos
from airflow.security.kerberos import renew_from_kt
from airflow.security.kerberos import get_kerberos_principle, renew_from_kt
from tests.test_utils.config import conf_vars


Expand Down Expand Up @@ -281,3 +281,14 @@ def test_run(self, mock_sleep, mock_renew_from_kt):
mock.call("test-principal", "/tmp/keytab"),
mock.call("test-principal", "/tmp/keytab"),
]

def test_get_kerberos_principle(self):
expected_principal = "test-principal"
principal = get_kerberos_principle(expected_principal)
assert principal == expected_principal

@mock.patch("airflow.security.kerberos.get_hostname", return_value="REPLACEMENT_HOST")
@mock.patch("airflow.security.kerberos.conf.get_mandatory_value", return_value="test-principal/_HOST")
def test_get_kerberos_principle_resolve_null_principal(self, get_madantory_value_mock, get_hostname_mock):
principal = get_kerberos_principle(principal=None)
assert principal == "test-principal/REPLACEMENT_HOST"