Skip to content

Commit

Permalink
feat: add ability to check for multiple default ssh key locations
Browse files Browse the repository at this point in the history
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
  • Loading branch information
a-dubs committed Aug 26, 2024
1 parent 8b6a0b5 commit 101ee78
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions pycloudlib/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,26 @@ def __init__(
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
possible__default_keys = [
os.path.expandvars(f"~{user}/.ssh/id_rsa.pub"),
os.path.expandvars(f"~{user}/.ssh/id_ed25519.pub"),
]
for target_public_key in possible__default_keys:
if os.path.exists(target_public_key):
break
public_key_path = os.path.expandvars(
self.config.get(
"public_key_path",
target_public_key,
)
)
self._log.debug("using SSH pubkey: %s", public_key_path)
private_key_path = self.config.get("private_key_path", "")
self.key_pair = KeyPair(
public_key_path=os.path.expandvars(
self.config.get("public_key_path", f"~{user}/.ssh/id_rsa.pub")
),
private_key_path=os.path.expandvars(
self.config.get("private_key_path", "")
),
name=self.config.get("key_name", user),
public_key_path,
private_key_path,
self.config.get("key_name", user),
)

self.tag = get_timestamped_tag(tag) if timestamp_suffix else tag
Expand Down

0 comments on commit 101ee78

Please sign in to comment.