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

Do not silently allow the use of undefined variables in jinja2 templates #11016

Merged
merged 1 commit into from
Sep 25, 2020

Conversation

lattwood
Copy link
Contributor

@lattwood lattwood commented Sep 18, 2020

This can have extremely bad consequences. After this change, a jinja2 template like the one below will cause the task instance to fail, if the DAG being executed is not a sub-DAG. This may also display an error on the Rendered tab of the Task Instance page.

task_instance.xcom_pull('z', key='return_value', dag_id=dag.parent_dag.dag_id)

Prior to the change in this commit, the above template would pull the latest value for task_id 'z', for the given execution_date, from any DAG. If your task_ids between DAGs are all unique, or if DAGs using the same task_id always have different execution_date values, this will appear to act like dag_id=None.

Our current theory is SQLAlchemy/Python doesn't behave as expected when comparing jinja2.Undefined to None.

Admittedly this issue boils down to PEBKAC, but this is also a case of Airflow not doing the (perceived) right thing in the face of ambiguity, and this PR fixes that up.

I did find this note about why it was only set to jinja2.Undefined, but given the way this can fail (and the difficulty in tracking down the root cause), I strongly feel this warrants breaking backwards compatibility with DAGs/task instances that are attempting to erroneously use undefined variables.

#4951 (comment)

@boring-cyborg
Copy link

boring-cyborg bot commented Sep 18, 2020

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/airflow/blob/master/CONTRIBUTING.rst)
Here are some useful points:

  • Pay attention to the quality of your code (flake8, pylint and type annotations). Our pre-commits will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it’s a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://apache-airflow-slack.herokuapp.com/

@lattwood
Copy link
Contributor Author

@Fokko, @kaxil, @jmcarp, since all of your names were on the original PR making the behaviour configurable (and the PR template says to feel free to ping folks), I'm pinging ya'll

@mik-laj
Copy link
Member

mik-laj commented Sep 20, 2020

@lattwood Can you add etry in UPDATING.md? This will allow us to avoid surprises after migrating to Airflow 2.0

@lattwood
Copy link
Contributor Author

@mik-laj will do

@lattwood
Copy link
Contributor Author

@mik-laj done

This can have *extremely* bad consequences. After this change, a jinja2
template like the one below will cause the task instance to fail, if the
DAG being executed is not a sub-DAG. This may also display an error on
the Rendered tab of the Task Instance page.

task_instance.xcom_pull('z', key='return_value', dag_id=dag.parent_dag.dag_id)

Prior to the change in this commit, the above template would pull the
latest value for task_id 'z', for the given execution_date, from *any DAG*.
If your task_ids between DAGs are all unique, or if DAGs using the same
task_id always have different execution_date values, this will appear to
act like dag_id=None.

Our current theory is SQLAlchemy/Python doesn't behave as expected when
comparing `jinja2.Undefined` to `None`.
@lattwood
Copy link
Contributor Author

@mik-laj, docs added, PR is green!

@potiuk potiuk merged commit 37798f0 into apache:master Sep 25, 2020
@boring-cyborg
Copy link

boring-cyborg bot commented Sep 25, 2020

Awesome work, congrats on your first merged pull request!

```python
import jinja2

dag = DAG('simple_dag', template_undefined=jinja2.Undefined)
Copy link
Member

Choose a reason for hiding this comment

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

Is it also possible on a template-by-template basis to do it as {{ something | default "x" }}?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I believe all standard jinja2 templating features are still available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants