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

Affix webserver access_denied warning to be configurable #33022

Merged
merged 3 commits into from Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions airflow/config_templates/config.yml
Expand Up @@ -1349,6 +1349,13 @@ operators:
webserver:
description: ~
options:
access_denied_message:
description: |
The message displayed when a user attempts to execute actions beyond their authorised privileges.
version_added: 2.7.0
type: string
example: ~
default: "Access is Denied"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default: "Access is Denied"
default: "Unauthorized Access: Denied"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we can do that. The current message is "Access is Denied" so in order to keep the current experience, we got to have this default value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. We do not want to break the backward compatibility experience. If we want to make this change, we should have a different task for it instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @vincbeck

config_file:
description: |
Path of webserver config file used for configuring the webserver parameters
Expand Down
6 changes: 5 additions & 1 deletion airflow/www/auth.py
Expand Up @@ -28,6 +28,10 @@
T = TypeVar("T", bound=Callable)


def get_access_denied_message():
return conf.get("webserver", "access_denied_message")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should either use get_mandatory or provide a fallback.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. The get_mandatory is needed only when we want to have a value but the value has no default (for example when we want to get a URL that has no default but we really need it to be configured).

The way it works when it is put in config.yml is that "default" is automatically used as fallback if there is no fallback (It's been working like that already for quite some time via defaul_config.cfg - so my recent change has not changed it - jut made it works without the default_config.cfg

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is needed as @potiuk mentions here. The older default text is passed as the default in the new property in config.yml, which is basically the fallback.



def has_access(permissions: Sequence[tuple[str, str]] | None = None) -> Callable[[T], T]:
"""Factory for decorator that checks current user's permissions against required permissions."""

Expand Down Expand Up @@ -59,7 +63,7 @@ def decorated(*args, **kwargs):
403,
)
else:
access_denied = "Access is Denied"
access_denied = get_access_denied_message()
flash(access_denied, "danger")
return redirect(get_auth_manager().get_url_login(next=request.url))

Expand Down
15 changes: 15 additions & 0 deletions tests/www/test_security.py
Expand Up @@ -20,6 +20,7 @@
import contextlib
import datetime
import logging
import os
from unittest import mock
from unittest.mock import patch

Expand All @@ -32,6 +33,7 @@
from airflow.auth.managers.fab.auth.anonymous_user import AnonymousUser
from airflow.auth.managers.fab.fab_auth_manager import FabAuthManager
from airflow.auth.managers.fab.models import User, assoc_permission_role
from airflow.configuration import conf, initialize_config
from airflow.exceptions import AirflowException
from airflow.models import DagModel
from airflow.models.base import Base
Expand Down Expand Up @@ -969,3 +971,16 @@ def test_users_can_be_found(app, security_manager, session, caplog):
assert len(users) == 1
delete_user(app, "Test")
assert "Error adding new user to database" in caplog.text


def test_access_denied_message():
with mock.patch.dict(
os.environ,
{"AIRFLOW__WEBSERVER__ACCESS_DENIED_MESSAGE": "My custom access denied message"},
clear=True,
):
initialize_config()
assert conf.get("webserver", "access_denied_message") == "My custom access denied message"

initialize_config()
assert conf.get("webserver", "access_denied_message") == "Access is Denied"
amoghrajesh marked this conversation as resolved.
Show resolved Hide resolved