Skip to content
Closed
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
7 changes: 6 additions & 1 deletion airflow/providers/sftp/hooks/sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ def get_conn(self) -> pysftp.Connection:
}
if self.password and self.password.strip():
conn_params['password'] = self.password
if self.key_file:

# Try to use the paramiko key from the SSH hook

@lidalei lidalei Jun 10, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion: Better to remove line 107 and 108

                if 'private_key' in extra_options:
                    self.key_file = extra_options.get('private_key')

and update doc for SFTPHook.

Note pysftp only supports RSA and DSS keys while SSHHook also supports ECDSA and Ed25519 keys. Worth to note in doc.

Ref: https://bitbucket.org/dundeemt/pysftp/src/1c0791759688a733a558b1a25d9ae04f52cf6a64/pysftp/__init__.py#lines-165:171

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would you please rebase to latest main @hx-markterry and add the doc explanation ? Also a unit test covering this case is needed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok will do

@aladinoss aladinoss Aug 14, 2021

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@potiuk & @hx-markterry what is the benefit of using separate hook for sftp sensor and operator ?
Is it better to change the documentation and use ssh hook for both sftp and ssh ?
example:

import logging
from datetime import datetime
from paramiko import SFTP_NO_SUCH_FILE
from airflow.contrib.hooks.ssh_hook import SSHHook
from airflow.operators.sensors import BaseSensorOperator
from airflow.utils.decorators import apply_defaults
from airflow.plugins_manager import AirflowPlugin

log = logging.getLogger(__name__)


class SgSFTPSensor(BaseSensorOperator):
    """
    Waits for a file or directory to be present on SFTP.

    :param path: Remote file or directory path
    :type path: str
    :param ssh_conn_id: The connection to run the sensor against
    :type shh_conn_id: str
    """
    template_fields = ('path',)

    @apply_defaults
    def __init__(self, path, shh_conn_id='ssh_default', *args, **kwargs):
        super(SgSFTPSensor, self).__init__(*args, **kwargs)
        self.path = path
        self.hook = None
        self.shh_conn_id = shh_conn_id

    def get_mod_time(self, path):
        ssh_hook = SSHHook(ssh_conn_id=self.shh_conn_id)
        conn = ssh_hook.get_conn().open_sftp()
        ftp_mdtm = conn.stat(path).st_mtime
        return datetime.fromtimestamp(ftp_mdtm).strftime('%Y%m%d%H%M%S')

    def poke(self, context):
        self.log.info('Poking for %s', self.path)
        try:
            self.get_mod_time(self.path)
        except IOError as e:
            log.info(SFTP_NO_SUCH_FILE)
            if e.errno != SFTP_NO_SUCH_FILE:
                raise e
            log.info(f"{self.path} NOT FOUND, sensor will retry!")
            return False
        log.info(f"{self.path} FOUND, sensor will exit now.")
        return True


class SgSFTPPlugin(AirflowPlugin):
    name = "sgsftpplugin"
    sensors = [
        SgSFTPSensor
    ]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

SFTPHook has several methods that are sftp-specific (such as retrieve_file/store_file but also many others). Those make no sense for "ssh hook".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@lidalei I like your suggestion because it makes the definition of private_key consistent between SFTPHook and SSHHook.

But it would be a breaking change for clients who currently pass a file path to SFTPHook's private_key. How should I go about that?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@30blay Thx! I don't think private_key works for SFTPHook. SFTPHook inherits SSHHook. If we have an SFTP connection with private_key in extra fields, it will fail to initialize an SFTPHook.

https://github.com/apache/airflow/blob/main/airflow/providers/sftp/hooks/sftp.py#L69
https://github.com/apache/airflow/blob/main/airflow/providers/ssh/hooks/ssh.py#L155-L158

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Forgot to mention that, it didn't work for us.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You're right, passing a private key file path as private_key does not work for SFTPHook, despite what the doc says. I will update the doc and implement your solution

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Done! In #18988

if self.pkey:
conn_params['private_key'] = self.pkey
elif self.key_file:
conn_params['private_key'] = self.key_file

if self.private_key_pass:
conn_params['private_key_pass'] = self.private_key_pass

Expand Down