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

Bugfix: Sync Access Control defined in DAGs when running sync-perm #13377

Merged
merged 1 commit into from
Dec 30, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion airflow/cli/commands/sync_perm_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def sync_perm(args):
# Add missing permissions for all the Base Views
appbuilder.add_permissions(update_perms=True)
print('Updating permission on all DAG views')
dags = DagBag(read_dags_from_db=True).dags.values()
dagbag = DagBag(read_dags_from_db=True)
dagbag.collect_dags_from_db()
dags = dagbag.dags.values()
for dag in dags:
appbuilder.sm.sync_perm_for_dag(dag.dag_id, dag.access_control)
25 changes: 13 additions & 12 deletions tests/cli/commands/test_sync_perm_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,18 @@ def setUpClass(cls):
@mock.patch("airflow.cli.commands.sync_perm_command.cached_app")
@mock.patch("airflow.cli.commands.sync_perm_command.DagBag")
def test_cli_sync_perm(self, dagbag_mock, mock_cached_app):
self.expect_dagbag_contains(
[
DAG('has_access_control', access_control={'Public': {permissions.ACTION_CAN_READ}}),
DAG('no_access_control'),
],
dagbag_mock,
)
dags = [
DAG('has_access_control', access_control={'Public': {permissions.ACTION_CAN_READ}}),
DAG('no_access_control'),
]

collect_dags_from_db_mock = mock.Mock()
dagbag = mock.Mock()

dagbag.dags = {dag.dag_id: dag for dag in dags}
dagbag.collect_dags_from_db = collect_dags_from_db_mock
dagbag_mock.return_value = dagbag

appbuilder = mock_cached_app.return_value.appbuilder
appbuilder.sm = mock.Mock()

Expand All @@ -51,6 +56,7 @@ def test_cli_sync_perm(self, dagbag_mock, mock_cached_app):
assert appbuilder.sm.sync_roles.call_count == 1

dagbag_mock.assert_called_once_with(read_dags_from_db=True)
collect_dags_from_db_mock.assert_called_once_with()
kaxil marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(2, len(appbuilder.sm.sync_perm_for_dag.mock_calls))
appbuilder.sm.sync_perm_for_dag.assert_any_call(
'has_access_control', {'Public': {permissions.ACTION_CAN_READ}}
Expand All @@ -60,8 +66,3 @@ def test_cli_sync_perm(self, dagbag_mock, mock_cached_app):
None,
)
appbuilder.add_permissions.assert_called_once_with(update_perms=True)

def expect_dagbag_contains(self, dags, dagbag_mock):
dagbag = mock.Mock()
dagbag.dags = {dag.dag_id: dag for dag in dags}
dagbag_mock.return_value = dagbag