Skip to content

feat: add ability to check for multiple default ssh key locations#406

Merged
a-dubs merged 1 commit intocanonical:mainfrom
a-dubs:GH-341-check-multiple-pubkeys
Oct 15, 2024
Merged

feat: add ability to check for multiple default ssh key locations#406
a-dubs merged 1 commit intocanonical:mainfrom
a-dubs:GH-341-check-multiple-pubkeys

Conversation

@a-dubs
Copy link
Contributor

@a-dubs a-dubs commented Aug 26, 2024

Since Ubuntu 24.04 LTS and newer, the default location for ssh keys is
~/.ssh/id_ed25519 instead of ~/.ssh/id_rsa. Thus, when no ssh key
path is specified, this commit changes pycloudlib to check for an
existing ssh key at either ~/.ssh/id_ed25519 or ~/.ssh/id_rsa.

Fixes GH #341


Testing of changes

Testing id_ed25519 can be found

  1. made sure no key exists at ~/.ssh/id_rsa
  2. created new key at ~/.ssh/id_ed25519
  3. removed public_key_path and private_key_path from [oci] config in my pycloudlib.toml config file
  4. ran the oracle example script: python3 examples/oracle.py
  5. made sure the following line showed up in the log (was one of the first few lines logged):
INFO:pycloudlib.cloud.OCI:using SSH pubkey: /home/a-dubs/.ssh/id_ed25519.pub

Testing error is raised when neither default key is found

  1. Removed keys at ~/.ssh/id_ed25519
  2. ran the oracle example script: python3 examples/oracle.py
  3. Ensured error stating the following was thrown:
pycloudlib.errors.PycloudlibError: No public key path provided and no key found in default locations: '~/.ssh/id_rsa.pub' or '~/.ssh/id_ed25519.pub'

Testing error is raised when key from config does not exist:

  1. set public_key_path in [oci] config in my pycloudlib.toml config file to be ~/.ssh/nonexistant-key.pub
  2. ran the oracle example script: python3 examples/oracle.py
  3. Ensured error stating the following was thrown:
pycloudlib.errors.PycloudlibError: Provided public key path '/home/a-dubs/.ssh/oracle-ipv6-testing.pub1' does not exist

@a-dubs a-dubs force-pushed the GH-341-check-multiple-pubkeys branch 6 times, most recently from 6d98b02 to 2e1190a Compare August 28, 2024 22:19
@a-dubs
Copy link
Contributor Author

a-dubs commented Aug 29, 2024

putting this into draft until i fix tests.

@a-dubs a-dubs marked this pull request as draft August 29, 2024 00:08
@a-dubs
Copy link
Contributor Author

a-dubs commented Aug 29, 2024

Also, should i add new unit tests that verify the functionality of the added changes? @holmanb (tag you're it)

@holmanb holmanb self-assigned this Aug 30, 2024
Copy link
Member

@holmanb holmanb left a comment

Choose a reason for hiding this comment

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

Also, should i add new unit tests that verify the functionality of the added changes? @holmanb (tag you're it)

Pycloudlib's test coverage is unfortunately very light on tests, but in this case the feature being tested is something that a user would immediately notice if it isn't working.

I'm honestly not sure whether it would be worth the effort in this case to write and maintain a test given the infrequency of changes to this part of the code and the immediate feedback that we would receive.

What do you think?

self._check_and_set_config(config_file, required_values)

user = getpass.getuser()
# check if id_rsa or id_ed25519 keys exist in the user's .ssh directory
Copy link
Member

Choose a reason for hiding this comment

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

This turns __init__() mostly into a key-handling function. I would prefer a helper method _get_ssh_keys()[1] to contain this logic and get called from __init__()

[1] maybe a better name, I didn't put any thought into that one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha agreed! Way too much going on there. And would allow for actually unit testing that (if so desired)

@a-dubs
Copy link
Contributor Author

a-dubs commented Sep 9, 2024

Also, should i add new unit tests that verify the functionality of the added changes? @holmanb (tag you're it)

Pycloudlib's test coverage is unfortunately very light on tests, but in this case the feature being tested is something that a user would immediately notice if it isn't working.

I'm honestly not sure whether it would be worth the effort in this case to write and maintain a test given the infrequency of changes to this part of the code and the immediate feedback that we would receive.

What do you think?

Let me go fix the existing unit tests that this change broke, and see if this change gets inherently tested that way. If not, it should be sufficient to just test the helper function that I'll create encapsulating this functionality.

@a-dubs a-dubs force-pushed the GH-341-check-multiple-pubkeys branch 2 times, most recently from e540971 to 92b30f6 Compare September 24, 2024 14:54
@a-dubs a-dubs marked this pull request as ready for review September 24, 2024 14:57
@a-dubs
Copy link
Contributor Author

a-dubs commented Sep 24, 2024

@holmanb ready for re-review!

@a-dubs a-dubs requested a review from holmanb September 24, 2024 14:57
@a-dubs a-dubs force-pushed the GH-341-check-multiple-pubkeys branch 4 times, most recently from 880791c to 2b0eb2b Compare October 14, 2024 20:57
Copy link
Member

@holmanb holmanb left a comment

Choose a reason for hiding this comment

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

@a-dubs Nit: I see some unnecessary whitespace changes in diff, but other than that everything else looks good. Please clean up the whitespace changes, then feel free to merge.

@a-dubs
Copy link
Contributor Author

a-dubs commented Oct 15, 2024

@a-dubs Nit: I see some unnecessary whitespace changes in diff, but other than that everything else looks good. Please clean up the whitespace changes, then feel free to merge.

@holmanb all those double whitespaces are because they are between classes not functions and this is the formatting enforced by ruff. to verify, I removed the double whitespace then ran tox -e format and it added the second newline back in.

Since Ubuntu 24.04 LTS and newer, the default location for ssh keys is
`~/.ssh/id_ed25519` instead of `~/.ssh/id_rsa`. Thus, when no ssh key
path is specified, this commit changes pycloudlib to check for an
existing ssh key at either  `~/.ssh/id_ed25519` or `~/.ssh/id_rsa`.

Fixes GH canonical#341
@a-dubs a-dubs force-pushed the GH-341-check-multiple-pubkeys branch from 2b0eb2b to bbb2e80 Compare October 15, 2024 13:04
@a-dubs a-dubs merged commit 335c4a4 into canonical:main Oct 15, 2024
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Oct 17, 2024
In PR canonical#406, two pytest marks were added to help with mocking ssh
keys in unit tests, but the marks were not registered with pytest
so it was raising errors. This commit fixes that!
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Oct 17, 2024
In PR canonical#406, two pytest marks were added to help with mocking ssh
keys in unit tests, but the marks were not registered with pytest
so it was raising errors. This commit fixes that!
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Oct 17, 2024
In PR canonical#406, two pytest marks were added to help with mocking ssh
keys in unit tests, but the marks were not registered with pytest
so it was raising errors. This commit fixes that!
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Oct 17, 2024
In PR canonical#406, two pytest marks were added to help with mocking ssh
keys in unit tests, but the marks were not registered with pytest
so it was raising errors. This commit fixes that!

Also, migrate existing pytest configuration from tox.ini to
pyproject.toml
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Oct 18, 2024
In PR canonical#406, two pytest marks were added to help with mocking ssh
keys in unit tests, but the marks were not registered with pytest
so it was raising errors. This commit fixes that!

Also, migrate existing pytest configuration from tox.ini to
pyproject.toml
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Oct 18, 2024
In PR canonical#406, two pytest marks were added to help with mocking ssh
keys in unit tests, but the marks were not registered with pytest
so it was raising errors. This commit fixes that!

Also, migrate existing pytest configuration from tox.ini to
pyproject.toml
a-dubs added a commit that referenced this pull request Oct 18, 2024
In PR #406, two pytest marks were added to help with mocking ssh
keys in unit tests, but the marks were not registered with pytest
so it was raising errors. This commit fixes that!

Also, migrate existing pytest configuration from tox.ini to
pyproject.toml
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Nov 19, 2024
PR canonical#406 broke some end consumers/users of pycloudlib on systems where
1) ssh keys are not set in the pycloudlib.toml and the ssh key is later
set using the Cloud.use_key() method
2) no ssh keys exist at the default paths of '~/.ssh/id_rsa.pub' or
'~/.ssh/id_ed25519.pub'. This commit removes/reverts the error that
gets raised at the Cloud class instantation when those two cases
are true. There was no way for an end user to override/prevent
pycloudlib from erroring out besides for creating a fake ssh key
at one of the two default paths. Now, a warning is just logged instead,
restoring the flexibility pycloudlib preivously provided to end users
for setting their ssh keys.
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Nov 19, 2024
PR canonical#406 broke some end consumers/users of pycloudlib on systems where
1) ssh keys are not set in the pycloudlib.toml and the ssh key is later
set using the Cloud.use_key() method
2) no ssh keys exist at the default paths of '~/.ssh/id_rsa.pub' or
'~/.ssh/id_ed25519.pub'. This commit removes/reverts the error that
gets raised at the Cloud class instantation when those two cases
are true. There was no way for an end user to override/prevent
pycloudlib from erroring out besides for creating a fake ssh key
at one of the two default paths. Now, a warning is just logged instead,
restoring the flexibility pycloudlib preivously provided to end users
for setting their ssh keys.
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Nov 19, 2024
PR canonical#406 broke some end consumers/users of pycloudlib on systems where
1) ssh keys are not set in the pycloudlib.toml and the ssh key is later
set using the Cloud.use_key() method
2) no ssh keys exist at the default paths of '~/.ssh/id_rsa.pub' or
'~/.ssh/id_ed25519.pub'. This commit removes/reverts the error that
gets raised at the Cloud class instantation when those two cases
are true. There was no way for an end user to override/prevent
pycloudlib from erroring out besides for creating a fake ssh key
at one of the two default paths. Now, a warning is just logged instead,
restoring the flexibility pycloudlib preivously provided to end users
for setting their ssh keys.
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Nov 19, 2024
This commit will once again allow for setting ssh keys at runtime
using the `Cloud.use_key()` method when default ssh keys dont exist
on the system and no ssh key paths are provided in pycloudlib.toml.

Further context:
PR canonical#406 broke some end consumers/users of pycloudlib on systems where
1) ssh keys are not set in the pycloudlib.toml and the ssh key is later
set using the Cloud.use_key() method
2) no ssh keys exist at the default paths of '~/.ssh/id_rsa.pub' or
'~/.ssh/id_ed25519.pub'.
This commit removes / reverts the error that was introduced in PR canonical#406
which gets raised at the Cloud class instantation when those two cases
are true. The biggest issue with this change, is there was no way for
an end user to override/prevent pycloudlib from erroring out if those
cases are true - besides for creating a fake ssh key at one of the two
default paths. Now, a warning is just logged instead, restoring the
flexibility pycloudlib preivously provided to end users for setting /
using ssh keys.

If both scenarios 1 and 2 are true (ssh key is unset/doesn't exist),
a new exception type `UnsetSSHKeyError` will be raised with verbose
exception message explaining why this happened and how to fix.
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Nov 21, 2024
This commit will once again allow for setting ssh keys at runtime
using the `Cloud.use_key()` method when default ssh keys dont exist
on the system and no ssh key paths are provided in pycloudlib.toml.

Further context:
PR canonical#406 broke some end consumers/users of pycloudlib on systems where
1) ssh keys are not set in the pycloudlib.toml and the ssh key is later
set using the Cloud.use_key() method
2) no ssh keys exist at the default paths of '~/.ssh/id_rsa.pub' or
'~/.ssh/id_ed25519.pub'.
This commit removes / reverts the error that was introduced in PR canonical#406
which gets raised at the Cloud class instantation when those two cases
are true. The biggest issue with this change, is there was no way for
an end user to override/prevent pycloudlib from erroring out if those
cases are true - besides for creating a fake ssh key at one of the two
default paths. Now, a warning is just logged instead, restoring the
flexibility pycloudlib preivously provided to end users for setting /
using ssh keys.

If both scenarios 1 and 2 are true (ssh key is unset/doesn't exist),
a new exception type `UnsetSSHKeyError` will be raised with verbose
exception message explaining why this happened and how to fix.
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Nov 27, 2024
This commit will once again allow for setting ssh keys at runtime
using the `Cloud.use_key()` method when default ssh keys dont exist
on the system and no ssh key paths are provided in pycloudlib.toml.

Further context:
PR canonical#406 broke some end consumers/users of pycloudlib on systems where
1) ssh keys are not set in the pycloudlib.toml and the ssh key is later
set using the Cloud.use_key() method
2) no ssh keys exist at the default paths of '~/.ssh/id_rsa.pub' or
'~/.ssh/id_ed25519.pub'.
This commit removes / reverts the error that was introduced in PR canonical#406
which gets raised at the Cloud class instantation when those two cases
are true. The biggest issue with this change, is there was no way for
an end user to override/prevent pycloudlib from erroring out if those
cases are true - besides for creating a fake ssh key at one of the two
default paths. Now, a warning is just logged instead, restoring the
flexibility pycloudlib preivously provided to end users for setting /
using ssh keys.

If both scenarios 1 and 2 are true (ssh key is unset/doesn't exist),
a new exception type `UnsetSSHKeyError` will be raised with verbose
exception message explaining why this happened and how to fix.
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Nov 27, 2024
This commit will once again allow for setting ssh keys at runtime
using the `Cloud.use_key()` method when default ssh keys dont exist
on the system and no ssh key paths are provided in pycloudlib.toml.

Further context:
PR canonical#406 broke some end consumers/users of pycloudlib on systems where
1) ssh keys are not set in the pycloudlib.toml and the ssh key is later
set using the Cloud.use_key() method
2) no ssh keys exist at the default paths of '~/.ssh/id_rsa.pub' or
'~/.ssh/id_ed25519.pub'.
This commit removes / reverts the error that was introduced in PR canonical#406
which gets raised at the Cloud class instantation when those two cases
are true. The biggest issue with this change, is there was no way for
an end user to override/prevent pycloudlib from erroring out if those
cases are true - besides for creating a fake ssh key at one of the two
default paths. Now, a warning is just logged instead, restoring the
flexibility pycloudlib preivously provided to end users for setting /
using ssh keys.

If both scenarios 1 and 2 are true (ssh key is unset/doesn't exist),
a new exception type `UnsetSSHKeyError` will be raised with verbose
exception message explaining why this happened and how to fix.
a-dubs added a commit to a-dubs/pycloudlib that referenced this pull request Nov 27, 2024
This commit will once again allow for setting ssh keys at runtime
using the `Cloud.use_key()` method when default ssh keys dont exist
on the system and no ssh key paths are provided in pycloudlib.toml.

Further context:
PR canonical#406 broke some end consumers/users of pycloudlib on systems where
1) ssh keys are not set in the pycloudlib.toml and the ssh key is later
set using the Cloud.use_key() method
2) no ssh keys exist at the default paths of '~/.ssh/id_rsa.pub' or
'~/.ssh/id_ed25519.pub'.
This commit removes / reverts the error that was introduced in PR canonical#406
which gets raised at the Cloud class instantation when those two cases
are true. The biggest issue with this change, is there was no way for
an end user to override/prevent pycloudlib from erroring out if those
cases are true - besides for creating a fake ssh key at one of the two
default paths. Now, a warning is just logged instead, restoring the
flexibility pycloudlib preivously provided to end users for setting /
using ssh keys.

If both scenarios 1 and 2 are true (ssh key is unset/doesn't exist),
a new exception type `UnsetSSHKeyError` will be raised with verbose
exception message explaining why this happened and how to fix.
a-dubs added a commit that referenced this pull request Nov 27, 2024
This commit will once again allow for setting ssh keys at runtime
using the `Cloud.use_key()` method when default ssh keys dont exist
on the system and no ssh key paths are provided in pycloudlib.toml.

Further context:
PR #406 broke some end consumers/users of pycloudlib on systems where
1) ssh keys are not set in the pycloudlib.toml and the ssh key is later
set using the Cloud.use_key() method
2) no ssh keys exist at the default paths of '~/.ssh/id_rsa.pub' or
'~/.ssh/id_ed25519.pub'.
This commit removes / reverts the error that was introduced in PR #406
which gets raised at the Cloud class instantation when those two cases
are true. The biggest issue with this change, is there was no way for
an end user to override/prevent pycloudlib from erroring out if those
cases are true - besides for creating a fake ssh key at one of the two
default paths. Now, a warning is just logged instead, restoring the
flexibility pycloudlib preivously provided to end users for setting /
using ssh keys.

If both scenarios 1 and 2 are true (ssh key is unset/doesn't exist),
a new exception type `UnsetSSHKeyError` will be raised with verbose
exception message explaining why this happened and how to fix.
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.

2 participants