-
Notifications
You must be signed in to change notification settings - Fork 14.6k
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
SSHHook: Using correct hostname for host_key when using non-default ssh port #15964
Conversation
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)
|
airflow/providers/ssh/hooks/ssh.py
Outdated
remote_host = f"[{self.remote_host}]:{self.port}" if self.port != SSH_PORT else self.remote_host | ||
client_host_keys.add(remote_host, 'ssh-rsa', self.host_key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned in the linked issue, it may be easier to do it this way:
client_host_keys.add(self.remote_host, 'ssh-rsa', self.host_key)
if self.port:
client_host_keys.add(f"{self.remote_host}:{self.port}", 'ssh-rsa', self.host_key)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced it is a good idea to add the host key without port unconditionally. At least in theory it might be possible that there is one server listening on the default port and another one on self.port. Those servers could have different public keys.
However, it might be more clear to have that explicit branch instead of the inline if. I will adjust the PR accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this?
if self.port is None or self.port == SSH_PORT:
client_host_keys.add(self.remote_host, 'ssh-rsa', self.host_key)
if self.port:
client_host_keys.add(f"{self.remote_host}:{self.port}", 'ssh-rsa', self.host_key)
So SSH servers exposed on the default port can have both registered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In line 196 the port is set to SSH_PORT if it is None:
self.port = self.port or SSH_PORT
Hence, checking for self.port is None
is always false. Your suggestion results in adding both self.remote_host
and f"{[self.remote_host}]:{self.port}"
in most cases. Adding the latter for the standard port is not required according to the OpenSSH documentation (https://en.wikibooks.org/wiki/OpenSSH/Client_Configuration_Files#~/.ssh/known_hosts). As Paramiko expects the strings according to the OpenSSH format, we would just be adding a redundant, never used entry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how it is now actually. I am also all for adding host without port when the default port is used. This is what OpenSSH is doing usually.
ce6f985
to
86159cd
Compare
86159cd
to
124ef38
Compare
Improved formatting
When using the SSHHook to connect to an ssh server on a non default port, the host_key setting was not added with the correct hostname to the list of known hosts. In more detail:
yielded the exception
closes: #15963