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

pyinfra/connectors: Fix overwriting of users known_hosts file (#1209) #1252

Merged
merged 1 commit into from
Dec 11, 2024

Conversation

evoldstad
Copy link
Contributor

@evoldstad evoldstad commented Dec 2, 2024

This fixes issue #1209, making it so that we append new keys to the users known_hosts file instead of overwriting it.

Additionally:

  • Added a testcase that should discover this breaking in the future
  • Broke out the append functionality into a "append_hostkey" function, making it so we don't needlessly reuse code for AskPolicy
  • Linting actually correct this time.
  • Merged commits correctly this time.

Previous behaviour when adding a new

  • Create a paramiko.HostKeys object
  • Read the users known_hosts file
  • Add the new key to the paramiko object.
  • Save the object, overwriting the users host_keys file without comments and entries incompatible with paramiko.

New behaviour:

  • Create a paramiko.HostKeyEntry object using the new hostname and
    corresponding key.
  • Append this key to the existing known_hosts file.

@evoldstad
Copy link
Contributor Author

The spelling issue seems to be a false positive, as it reacts on a part of the example key used in the test. Any way to override this behaviour, or mark it as a non-issue?

@simonhammes
Copy link
Contributor

The spelling issue seems to be a false positive, as it reacts on a part of the example key used in the test. Any way to override this behaviour, or mark it as a non-issue?

You can either ignore the whole file or ignore certain expressions based on regular expressions: https://github.com/pyinfra-dev/pyinfra/blob/3.x/.typos.toml

…a-dev#1209)

This fixes issue pyinfra-dev#1209, making it so that we append new keys to the
users known_hosts file instead of overwriting it.

Additionally:

- Added a testcase that should discover this breaking in the future.

- Broke out the append functionality into a "append_hostkey" function,
    making it so we don't needlessly reuse code for AskPolicy and

- AcceptNewPolicy.

- Linting actually correct this time.

This commit changes the behaviour when adding a new key to known_hosts:

Previous behaviour when adding a new key:

- Create a paramiko.HostKeys object

- Read the users known_hosts file

- Add the new key to the object

- Save the object, overwriting the users host_keys file.

New behaviour:

- Create a paramiko.HostKeyEntry object using the new hostname and
corresponding key.

- Append this key to the existing known_hosts file.
@evoldstad
Copy link
Contributor Author

The spelling issue seems to be a false positive, as it reacts on a part of the example key used in the test. Any way to override this behaviour, or mark it as a non-issue?

You can either ignore the whole file or ignore certain expressions based on regular expressions: https://github.com/pyinfra-dev/pyinfra/blob/3.x/.typos.toml

Thanks, that fixed it!

Copy link
Member

@Fizzadar Fizzadar left a comment

Choose a reason for hiding this comment

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

Amazing, thank you for fixing this @evoldstad!

@Fizzadar Fizzadar merged commit e0d4ec6 into pyinfra-dev:3.x Dec 11, 2024
23 checks passed
@evoldstad evoldstad deleted the known_hosts_fix branch December 11, 2024 23:12
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.

4 participants