josh-fell opened a new pull request #19323:
URL: https://github.com/apache/airflow/pull/19323
Closes: #19071
The `command` parameter in the `SSHOperator` is listed as a `template_field`
which means users could pass in an arg for this parameter as an output from a
TaskFlow function via `XComArgs`. However, the current constructor of the
operator performs a string validation on the `command` arg to set another
parameter, `get_pty`. This parameter is force to True if the `command` starts
with "sudo". If the arg is passed in as an `XComArg` the DAG fails to parse
with an `AttributeError: 'XComArg' object has no attribute 'startswith'`.
Additionally, if the `command` is passed as part of a Jinja template
expression but the string does not explicitly begin with "sudo", `get_pty` will
be set to an incorrect value. For example in the below DAG, the `get_pty`
parameter will be set to False even though the rendered `command` value will
begin with "sudo":
```python
def generate_sudo_command():
return "sudo command"
@dag(
schedule_interval=None,
start_date=datetime(2021, 10, 29),
tags=['example'],
user_defined_macros={"generate_sudo_command": generate_sudo_command},
)
def example_dag_decorator_ssh():
run = SSHOperator(task_id='run_my_command', command="{{
generate_sudo_command() }}")
dag = example_dag_decorator_ssh()
```
This PR moves the string validation to the `execute()` method of the
operator such that the `XComArg` and templated value can be rendered prior to
setting the `get_pty` attribute of the operator.
---
**^ Add meaningful description above**
Read the **[Pull Request
Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)**
for more information.
In case of fundamental code change, Airflow Improvement Proposal
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvements+Proposals))
is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party
License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in
[UPDATING.md](https://github.com/apache/airflow/blob/main/UPDATING.md).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]