forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ClusterPolicyViolation support to airflow local settings (apache#…
…10282) This change will allow users to throw other exceptions (namely `AirflowClusterPolicyViolation`) than `DagCycleException` as part of Cluster Policies. This can be helpful for running checks on tasks / DAGs (e.g. asserting task has a non-airflow owner) and failing to run tasks aren't compliant with these checks. This is meant as a tool for airflow admins to prevent user mistakes (especially in shared Airflow infrastructure with newbies) than as a strong technical control for security/compliance posture.
- Loading branch information
Jacob Ferriero
authored
Aug 12, 2020
1 parent
f6734b3
commit 7f76b8b
Showing
7 changed files
with
199 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,59 @@ | ||
# | ||
# 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 typing import Callable, List | ||
|
||
from airflow.configuration import conf | ||
from airflow.exceptions import AirflowClusterPolicyViolation | ||
from airflow.models.baseoperator import BaseOperator | ||
|
||
|
||
# [START example_cluster_policy_rule] | ||
def task_must_have_owners(task: BaseOperator): | ||
if not task.owner or task.owner.lower() == conf.get('operators', | ||
'default_owner'): | ||
raise AirflowClusterPolicyViolation( | ||
f'''Task must have non-None non-default owner. Current value: {task.owner}''') | ||
# [END example_cluster_policy_rule] | ||
|
||
|
||
# [START example_list_of_cluster_policy_rules] | ||
TASK_RULES: List[Callable[[BaseOperator], None]] = [ | ||
task_must_have_owners, | ||
] | ||
|
||
|
||
def _check_task_rules(current_task: BaseOperator): | ||
"""Check task rules for given task.""" | ||
notices = [] | ||
for rule in TASK_RULES: | ||
try: | ||
rule(current_task) | ||
except AirflowClusterPolicyViolation as ex: | ||
notices.append(str(ex)) | ||
if notices: | ||
notices_list = " * " + "\n * ".join(notices) | ||
raise AirflowClusterPolicyViolation( | ||
f"DAG policy violation (DAG ID: {current_task.dag_id}, Path: {current_task.dag.filepath}):\n" | ||
f"Notices:\n" | ||
f"{notices_list}") | ||
|
||
|
||
def cluster_policy(task: BaseOperator): | ||
"""Ensure Tasks have non-default owners.""" | ||
_check_task_rules(task) | ||
# [END example_list_of_cluster_policy_rules] |
This file contains 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,32 @@ | ||
# | ||
# 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 datetime import timedelta | ||
|
||
from airflow import DAG | ||
from airflow.operators.dummy_operator import DummyOperator | ||
from airflow.utils.dates import days_ago | ||
|
||
with DAG( | ||
dag_id="test_missing_owner", | ||
schedule_interval="0 0 * * *", | ||
start_date=days_ago(2), | ||
dagrun_timeout=timedelta(minutes=60), | ||
tags=["example"], | ||
) as dag: | ||
run_this_last = DummyOperator(task_id="test_task",) |
This file contains 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,32 @@ | ||
# | ||
# 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 datetime import timedelta | ||
|
||
from airflow import DAG | ||
from airflow.operators.dummy_operator import DummyOperator | ||
from airflow.utils.dates import days_ago | ||
|
||
with DAG( | ||
dag_id="test_with_non_default_owner", | ||
schedule_interval="0 0 * * *", | ||
start_date=days_ago(2), | ||
dagrun_timeout=timedelta(minutes=60), | ||
tags=["example"], | ||
) as dag: | ||
run_this_last = DummyOperator(task_id="test_task", owner="John",) |
This file contains 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