Merge ~jslarraz/ubuntu-qa-tools:create-ssh-key into ubuntu-qa-tools:master

Proposed by Jorge Sancho Larraz
Status: Merged
Merge reported by: Jorge Sancho Larraz
Merged at revision: a50eeb7bd61db154b2285689e97bb4f39ded18ec
Proposed branch: ~jslarraz/ubuntu-qa-tools:create-ssh-key
Merge into: ubuntu-qa-tools:master
Diff against target: 46 lines (+31/-3)
1 file modified
vm-tools/uvt (+31/-3)
Reviewer Review Type Date Requested Status
Marc Deslauriers Approve
Review via email: mp+460669@code.launchpad.net

Commit message

uvt: automatically create ssh key if it does not exist

Description of the change

Currently uvt instruct the user to create an ssh key if it can not find one. This MR will create the key for the user.

To post a comment you must log in.
Revision history for this message
Marc Deslauriers (mdeslaur) wrote :

I'd be pretty surprised if a tool created an ssh key for me without asking, could you please ask the user first for confirmation and specify where and what type of key will be created?

Make sure you create the .ssh directory with 700 permissions.

review: Needs Fixing
0903b96... by Jorge Sancho Larraz

uvt: ask the user for confirmation before creating the ssh key

Revision history for this message
Jorge Sancho Larraz (jslarraz) wrote :
9d7ed47... by Jorge Sancho Larraz

uvt: add extra print to improve readability

Revision history for this message
Marc Deslauriers (mdeslaur) wrote :

Prompt looks good!

I would switch makedirs() to mkdir(), we shouldn't be creating the user home directory, something clearly went wrong if we need to do that.

Please set permissions to 0o700 not 0o755, and use the mode parameter of mkdir() instead of doing a separate chmod.

Thanks!

review: Needs Fixing
01c8b4d... by Jorge Sancho Larraz

uvt: fix ~/.ssh creation

8623627... by Jorge Sancho Larraz

uvt: fix ~/.ssh creation

a50eeb7... by Jorge Sancho Larraz

uvt: fix ~/.ssh creation

Revision history for this message
Jorge Sancho Larraz (jslarraz) wrote :
Revision history for this message
Marc Deslauriers (mdeslaur) wrote :

looks good, thanks!

review: Approve
Revision history for this message
Jorge Sancho Larraz (jslarraz) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/vm-tools/uvt b/vm-tools/uvt
2index 7d655b1..fb7f5f2 100755
3--- a/vm-tools/uvt
4+++ b/vm-tools/uvt
5@@ -3367,10 +3367,38 @@ def check_required_tools():
6 def check_ssh_key():
7 '''Checks if the user has an ssh key'''
8
9+ # Ensure .ssh directory is initialized anyway
10+ if not os.path.exists(os.path.expanduser("~/.ssh")):
11+ os.mkdir(os.path.expanduser("~/.ssh"), mode=0o700)
12+
13 if not os.path.exists(uvt_conf['vm_ssh_key']):
14- print("\nYour user must have an ssh key.\n" +
15- "Please create one now with 'ssh-keygen -t rsa' and try again.\n", file=sys.stderr)
16- sys.exit(1)
17+
18+ # Infer key type from name, defaults to rsa
19+ for key_type in ["ed25519-sk", "ed25519", "ecdsa-sk", "ecdsa", "dsa", "rsa"]:
20+ if key_type in os.path.basename(uvt_conf['vm_ssh_key']):
21+ break
22+
23+ conf_file = os.path.join(os.path.expanduser("~"), config_file)
24+ if not os.path.exists(conf_file):
25+ print("ssh key '~/.ssh/id_rsa.pub' defined in the default configuration does "
26+ "not exists. If you wish to use a different ssh key, you can launch "
27+ "'uvt config', which will create a default config file in ~/%s "
28+ "that you can customize." % config_file)
29+
30+ else:
31+ print("ssh key '%s' defined in the configuration file '~/%s' does "
32+ "not exists." % (uvt_conf['vm_ssh_key'], config_file))
33+
34+ print("")
35+ if not confirm("Would you like to create a '%s' ssh key in '%s'" % (key_type, uvt_conf['vm_ssh_key'])):
36+ print("Aborting.")
37+ sys.exit(1)
38+
39+ os.makedirs(os.path.dirname(os.path.expanduser(uvt_conf['vm_ssh_key'])), exist_ok=True)
40+ rc, out = runcmd(["ssh-keygen", "-t", key_type, "-f", uvt_conf['vm_ssh_key'].split(".pub")[0], "-q", "-N", ""])
41+ if rc != 0:
42+ print("Error while creating ssh key, please create it manually with 'ssh-keygen -t rsa'.")
43+ sys.exit(1)
44
45 def parse_config_file(conf_file):
46 '''Parses a config file'''

Subscribers

People subscribed via source and target branches