Merge lp:~smoser/ubuntu/trusty/cloud-init/trusty-1461242 into lp:ubuntu/trusty/cloud-init

Proposed by Scott Moser
Status: Merged
Merged at revision: 351
Proposed branch: lp:~smoser/ubuntu/trusty/cloud-init/trusty-1461242
Merge into: lp:ubuntu/trusty/cloud-init
Diff against target: 411 lines (+294/-31)
6 files modified
.pc/applied-patches (+1/-0)
.pc/lp-1461242-generate-ed25519-host-keys.patch/cloudinit/config/cc_ssh.py (+138/-0)
cloudinit/config/cc_ssh.py (+33/-30)
debian/changelog (+6/-1)
debian/patches/lp-1461242-generate-ed25519-host-keys.patch (+115/-0)
debian/patches/series (+1/-0)
To merge this branch: bzr merge lp:~smoser/ubuntu/trusty/cloud-init/trusty-1461242
Reviewer Review Type Date Requested Status
Ben Howard (community) Approve
Dan Watkins Pending
Review via email: mp+270711@code.launchpad.net
To post a comment you must log in.
352. By Scott Moser

remove use of 'decode_binary'

there is no 'decode_binary' here.
the return values from subp in python2 are strings.

Revision history for this message
Ben Howard (darkmuggle-deactivatedaccount) wrote :

Tested and okay for merger. It works as expected by creating ed25519 keys in /etc/ssh.

I would merge it, but bzr is complaining of a "readonly transport."

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file '.pc/applied-patches'
--- .pc/applied-patches 2015-09-10 16:24:44 +0000
+++ .pc/applied-patches 2015-09-10 16:50:23 +0000
@@ -14,3 +14,4 @@
14lp-1470890-include-regions-in-dynamic-mirror-discovery.patch14lp-1470890-include-regions-in-dynamic-mirror-discovery.patch
15lp-1490796-azure-fix-mount_cb-for-symlinks.patch15lp-1490796-azure-fix-mount_cb-for-symlinks.patch
16lp-1469260-fix-consumption-of-vendor-data.patch16lp-1469260-fix-consumption-of-vendor-data.patch
17lp-1461242-generate-ed25519-host-keys.patch
1718
=== added directory '.pc/lp-1461242-generate-ed25519-host-keys.patch'
=== added directory '.pc/lp-1461242-generate-ed25519-host-keys.patch/cloudinit'
=== added directory '.pc/lp-1461242-generate-ed25519-host-keys.patch/cloudinit/config'
=== added file '.pc/lp-1461242-generate-ed25519-host-keys.patch/cloudinit/config/cc_ssh.py'
--- .pc/lp-1461242-generate-ed25519-host-keys.patch/cloudinit/config/cc_ssh.py 1970-01-01 00:00:00 +0000
+++ .pc/lp-1461242-generate-ed25519-host-keys.patch/cloudinit/config/cc_ssh.py 2015-09-10 16:50:23 +0000
@@ -0,0 +1,138 @@
1# vi: ts=4 expandtab
2#
3# Copyright (C) 2009-2010 Canonical Ltd.
4# Copyright (C) 2012, 2013 Hewlett-Packard Development Company, L.P.
5#
6# Author: Scott Moser <scott.moser@canonical.com>
7# Author: Juerg Haefliger <juerg.haefliger@hp.com>
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License version 3, as
11# published by the Free Software Foundation.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21import glob
22import os
23
24# Ensure this is aliased to a name not 'distros'
25# since the module attribute 'distros'
26# is a list of distros that are supported, not a sub-module
27from cloudinit import distros as ds
28
29from cloudinit import ssh_util
30from cloudinit import util
31
32DISABLE_ROOT_OPTS = ("no-port-forwarding,no-agent-forwarding,"
33"no-X11-forwarding,command=\"echo \'Please login as the user \\\"$USER\\\" "
34"rather than the user \\\"root\\\".\';echo;sleep 10\"")
35
36KEY_2_FILE = {
37 "rsa_private": ("/etc/ssh/ssh_host_rsa_key", 0600),
38 "rsa_public": ("/etc/ssh/ssh_host_rsa_key.pub", 0644),
39 "dsa_private": ("/etc/ssh/ssh_host_dsa_key", 0600),
40 "dsa_public": ("/etc/ssh/ssh_host_dsa_key.pub", 0644),
41 "ecdsa_private": ("/etc/ssh/ssh_host_ecdsa_key", 0600),
42 "ecdsa_public": ("/etc/ssh/ssh_host_ecdsa_key.pub", 0644),
43}
44
45PRIV_2_PUB = {
46 'rsa_private': 'rsa_public',
47 'dsa_private': 'dsa_public',
48 'ecdsa_private': 'ecdsa_public',
49}
50
51KEY_GEN_TPL = 'o=$(ssh-keygen -yf "%s") && echo "$o" root@localhost > "%s"'
52
53GENERATE_KEY_NAMES = ['rsa', 'dsa', 'ecdsa']
54
55KEY_FILE_TPL = '/etc/ssh/ssh_host_%s_key'
56
57
58def handle(_name, cfg, cloud, log, _args):
59
60 # remove the static keys from the pristine image
61 if cfg.get("ssh_deletekeys", True):
62 key_pth = os.path.join("/etc/ssh/", "ssh_host_*key*")
63 for f in glob.glob(key_pth):
64 try:
65 util.del_file(f)
66 except:
67 util.logexc(log, "Failed deleting key file %s", f)
68
69 if "ssh_keys" in cfg:
70 # if there are keys in cloud-config, use them
71 for (key, val) in cfg["ssh_keys"].iteritems():
72 if key in KEY_2_FILE:
73 tgt_fn = KEY_2_FILE[key][0]
74 tgt_perms = KEY_2_FILE[key][1]
75 util.write_file(tgt_fn, val, tgt_perms)
76
77 for (priv, pub) in PRIV_2_PUB.iteritems():
78 if pub in cfg['ssh_keys'] or not priv in cfg['ssh_keys']:
79 continue
80 pair = (KEY_2_FILE[priv][0], KEY_2_FILE[pub][0])
81 cmd = ['sh', '-xc', KEY_GEN_TPL % pair]
82 try:
83 # TODO(harlowja): Is this guard needed?
84 with util.SeLinuxGuard("/etc/ssh", recursive=True):
85 util.subp(cmd, capture=False)
86 log.debug("Generated a key for %s from %s", pair[0], pair[1])
87 except:
88 util.logexc(log, "Failed generated a key for %s from %s",
89 pair[0], pair[1])
90 else:
91 # if not, generate them
92 genkeys = util.get_cfg_option_list(cfg,
93 'ssh_genkeytypes',
94 GENERATE_KEY_NAMES)
95 for keytype in genkeys:
96 keyfile = KEY_FILE_TPL % (keytype)
97 util.ensure_dir(os.path.dirname(keyfile))
98 if not os.path.exists(keyfile):
99 cmd = ['ssh-keygen', '-t', keytype, '-N', '', '-f', keyfile]
100 try:
101 # TODO(harlowja): Is this guard needed?
102 with util.SeLinuxGuard("/etc/ssh", recursive=True):
103 util.subp(cmd, capture=False)
104 except:
105 util.logexc(log, "Failed generating key type %s to "
106 "file %s", keytype, keyfile)
107
108 try:
109 (users, _groups) = ds.normalize_users_groups(cfg, cloud.distro)
110 (user, _user_config) = ds.extract_default(users)
111 disable_root = util.get_cfg_option_bool(cfg, "disable_root", True)
112 disable_root_opts = util.get_cfg_option_str(cfg, "disable_root_opts",
113 DISABLE_ROOT_OPTS)
114
115 keys = cloud.get_public_ssh_keys() or []
116 if "ssh_authorized_keys" in cfg:
117 cfgkeys = cfg["ssh_authorized_keys"]
118 keys.extend(cfgkeys)
119
120 apply_credentials(keys, user, disable_root, disable_root_opts)
121 except:
122 util.logexc(log, "Applying ssh credentials failed!")
123
124
125def apply_credentials(keys, user, disable_root, disable_root_opts):
126
127 keys = set(keys)
128 if user:
129 ssh_util.setup_user_keys(keys, user)
130
131 if disable_root:
132 if not user:
133 user = "NONE"
134 key_prefix = disable_root_opts.replace('$USER', user)
135 else:
136 key_prefix = ''
137
138 ssh_util.setup_user_keys(keys, 'root', options=key_prefix)
0139
=== modified file 'cloudinit/config/cc_ssh.py'
--- cloudinit/config/cc_ssh.py 2013-07-10 13:35:59 +0000
+++ cloudinit/config/cc_ssh.py 2015-09-10 16:50:23 +0000
@@ -20,6 +20,7 @@
2020
21import glob21import glob
22import os22import os
23import sys
2324
24# Ensure this is aliased to a name not 'distros'25# Ensure this is aliased to a name not 'distros'
25# since the module attribute 'distros'26# since the module attribute 'distros'
@@ -33,27 +34,19 @@
33"no-X11-forwarding,command=\"echo \'Please login as the user \\\"$USER\\\" "34"no-X11-forwarding,command=\"echo \'Please login as the user \\\"$USER\\\" "
34"rather than the user \\\"root\\\".\';echo;sleep 10\"")35"rather than the user \\\"root\\\".\';echo;sleep 10\"")
3536
36KEY_2_FILE = {37GENERATE_KEY_NAMES = ['rsa', 'dsa', 'ecdsa', 'ed25519']
37 "rsa_private": ("/etc/ssh/ssh_host_rsa_key", 0600),38KEY_FILE_TPL = '/etc/ssh/ssh_host_%s_key'
38 "rsa_public": ("/etc/ssh/ssh_host_rsa_key.pub", 0644),
39 "dsa_private": ("/etc/ssh/ssh_host_dsa_key", 0600),
40 "dsa_public": ("/etc/ssh/ssh_host_dsa_key.pub", 0644),
41 "ecdsa_private": ("/etc/ssh/ssh_host_ecdsa_key", 0600),
42 "ecdsa_public": ("/etc/ssh/ssh_host_ecdsa_key.pub", 0644),
43}
4439
45PRIV_2_PUB = {40CONFIG_KEY_TO_FILE = {}
46 'rsa_private': 'rsa_public',41PRIV_TO_PUB = {}
47 'dsa_private': 'dsa_public',42for k in GENERATE_KEY_NAMES:
48 'ecdsa_private': 'ecdsa_public',43 CONFIG_KEY_TO_FILE.update({"%s_private" % k: (KEY_FILE_TPL % k, 0o600)})
49}44 CONFIG_KEY_TO_FILE.update(
45 {"%s_public" % k: (KEY_FILE_TPL % k + ".pub", 0o600)})
46 PRIV_TO_PUB["%s_private" % k] = "%s_public" % k
5047
51KEY_GEN_TPL = 'o=$(ssh-keygen -yf "%s") && echo "$o" root@localhost > "%s"'48KEY_GEN_TPL = 'o=$(ssh-keygen -yf "%s") && echo "$o" root@localhost > "%s"'
5249
53GENERATE_KEY_NAMES = ['rsa', 'dsa', 'ecdsa']
54
55KEY_FILE_TPL = '/etc/ssh/ssh_host_%s_key'
56
5750
58def handle(_name, cfg, cloud, log, _args):51def handle(_name, cfg, cloud, log, _args):
5952
@@ -69,15 +62,15 @@
69 if "ssh_keys" in cfg:62 if "ssh_keys" in cfg:
70 # if there are keys in cloud-config, use them63 # if there are keys in cloud-config, use them
71 for (key, val) in cfg["ssh_keys"].iteritems():64 for (key, val) in cfg["ssh_keys"].iteritems():
72 if key in KEY_2_FILE:65 if key in CONFIG_KEY_TO_FILE:
73 tgt_fn = KEY_2_FILE[key][0]66 tgt_fn = CONFIG_KEY_TO_FILE[key][0]
74 tgt_perms = KEY_2_FILE[key][1]67 tgt_perms = CONFIG_KEY_TO_FILE[key][1]
75 util.write_file(tgt_fn, val, tgt_perms)68 util.write_file(tgt_fn, val, tgt_perms)
7669
77 for (priv, pub) in PRIV_2_PUB.iteritems():70 for (priv, pub) in PRIV_TO_PUB.items():
78 if pub in cfg['ssh_keys'] or not priv in cfg['ssh_keys']:71 if pub in cfg['ssh_keys'] or not priv in cfg['ssh_keys']:
79 continue72 continue
80 pair = (KEY_2_FILE[priv][0], KEY_2_FILE[pub][0])73 pair = (CONFIG_KEY_TO_FILE[priv][0], CONFIG_KEY_TO_FILE[pub][0])
81 cmd = ['sh', '-xc', KEY_GEN_TPL % pair]74 cmd = ['sh', '-xc', KEY_GEN_TPL % pair]
82 try:75 try:
83 # TODO(harlowja): Is this guard needed?76 # TODO(harlowja): Is this guard needed?
@@ -92,18 +85,28 @@
92 genkeys = util.get_cfg_option_list(cfg,85 genkeys = util.get_cfg_option_list(cfg,
93 'ssh_genkeytypes',86 'ssh_genkeytypes',
94 GENERATE_KEY_NAMES)87 GENERATE_KEY_NAMES)
88 lang_c = os.environ.copy()
89 lang_c['LANG'] = 'C'
95 for keytype in genkeys:90 for keytype in genkeys:
96 keyfile = KEY_FILE_TPL % (keytype)91 keyfile = KEY_FILE_TPL % (keytype)
92 if os.path.exists(keyfile):
93 continue
97 util.ensure_dir(os.path.dirname(keyfile))94 util.ensure_dir(os.path.dirname(keyfile))
98 if not os.path.exists(keyfile):95 cmd = ['ssh-keygen', '-t', keytype, '-N', '', '-f', keyfile]
99 cmd = ['ssh-keygen', '-t', keytype, '-N', '', '-f', keyfile]96
97 # TODO(harlowja): Is this guard needed?
98 with util.SeLinuxGuard("/etc/ssh", recursive=True):
100 try:99 try:
101 # TODO(harlowja): Is this guard needed?100 out, err = util.subp(cmd, capture=True, env=lang_c)
102 with util.SeLinuxGuard("/etc/ssh", recursive=True):101 sys.stdout.write(out)
103 util.subp(cmd, capture=False)102 except util.ProcessExecutionError as e:
104 except:103 err = e.stderr.lower()
105 util.logexc(log, "Failed generating key type %s to "104 if (e.exit_code == 1 and
106 "file %s", keytype, keyfile)105 err.lower().startswith("unknown key")):
106 log.debug("ssh-keygen: unknown key type '%s'", keytype)
107 else:
108 util.logexc(log, "Failed generating key type %s to "
109 "file %s", keytype, keyfile)
107110
108 try:111 try:
109 (users, _groups) = ds.normalize_users_groups(cfg, cloud.distro)112 (users, _groups) = ds.normalize_users_groups(cfg, cloud.distro)
110113
=== modified file 'debian/changelog'
--- debian/changelog 2015-09-10 16:24:44 +0000
+++ debian/changelog 2015-09-10 16:50:23 +0000
@@ -1,10 +1,15 @@
1cloud-init (0.7.5-0ubuntu1.11) UNRELEASED; urgency=medium1cloud-init (0.7.5-0ubuntu1.11) UNRELEASED; urgency=medium
22
3 [ Felipe Reyes ]
3 * d/patches/fix-consumption-of-vendor-data.patch:4 * d/patches/fix-consumption-of-vendor-data.patch:
4 - Fix consumption of vendor-data in OpenStack to allow namespacing5 - Fix consumption of vendor-data in OpenStack to allow namespacing
5 (LP: #1469260).6 (LP: #1469260).
67
7 -- Felipe Reyes <felipe.reyes@canonical.com> Thu, 10 Sep 2015 12:50:32 -03008 [ Scott Moser ]
9 * d/patches/lp-1461242-generate-ed25519-host-keys.patch:
10 - ssh: generate ed25519 host keys if supported (LP: #1461242)
11
12 -- Scott Moser <smoser@ubuntu.com> Thu, 10 Sep 2015 12:36:14 -0400
813
9cloud-init (0.7.5-0ubuntu1.10) trusty; urgency=medium14cloud-init (0.7.5-0ubuntu1.10) trusty; urgency=medium
1015
1116
=== added file 'debian/patches/lp-1461242-generate-ed25519-host-keys.patch'
--- debian/patches/lp-1461242-generate-ed25519-host-keys.patch 1970-01-01 00:00:00 +0000
+++ debian/patches/lp-1461242-generate-ed25519-host-keys.patch 2015-09-10 16:50:23 +0000
@@ -0,0 +1,115 @@
1Author: Scott Moser <smoser@ubuntu.com>
2Bug: https://launchpad.net/bugs/1461242
3Applied-Upstream: yes
4Origin: http://bazaar.launchpad.net/~cloud-init-dev/cloud-init/trunk/revision/1125
5Description: ssh: generate ed25519 host keys if supported
6 .
7 now we attempt to generate ed25519 host keys.
8 If ssh-keygen does not support it, a debug log message will be written.
9
10=== modified file 'cloudinit/config/cc_ssh.py'
11--- a/cloudinit/config/cc_ssh.py
12+++ b/cloudinit/config/cc_ssh.py
13@@ -20,6 +20,7 @@
14
15 import glob
16 import os
17+import sys
18
19 # Ensure this is aliased to a name not 'distros'
20 # since the module attribute 'distros'
21@@ -33,26 +34,18 @@ DISABLE_ROOT_OPTS = ("no-port-forwarding
22 "no-X11-forwarding,command=\"echo \'Please login as the user \\\"$USER\\\" "
23 "rather than the user \\\"root\\\".\';echo;sleep 10\"")
24
25-KEY_2_FILE = {
26- "rsa_private": ("/etc/ssh/ssh_host_rsa_key", 0600),
27- "rsa_public": ("/etc/ssh/ssh_host_rsa_key.pub", 0644),
28- "dsa_private": ("/etc/ssh/ssh_host_dsa_key", 0600),
29- "dsa_public": ("/etc/ssh/ssh_host_dsa_key.pub", 0644),
30- "ecdsa_private": ("/etc/ssh/ssh_host_ecdsa_key", 0600),
31- "ecdsa_public": ("/etc/ssh/ssh_host_ecdsa_key.pub", 0644),
32-}
33-
34-PRIV_2_PUB = {
35- 'rsa_private': 'rsa_public',
36- 'dsa_private': 'dsa_public',
37- 'ecdsa_private': 'ecdsa_public',
38-}
39-
40-KEY_GEN_TPL = 'o=$(ssh-keygen -yf "%s") && echo "$o" root@localhost > "%s"'
41+GENERATE_KEY_NAMES = ['rsa', 'dsa', 'ecdsa', 'ed25519']
42+KEY_FILE_TPL = '/etc/ssh/ssh_host_%s_key'
43
44-GENERATE_KEY_NAMES = ['rsa', 'dsa', 'ecdsa']
45+CONFIG_KEY_TO_FILE = {}
46+PRIV_TO_PUB = {}
47+for k in GENERATE_KEY_NAMES:
48+ CONFIG_KEY_TO_FILE.update({"%s_private" % k: (KEY_FILE_TPL % k, 0o600)})
49+ CONFIG_KEY_TO_FILE.update(
50+ {"%s_public" % k: (KEY_FILE_TPL % k + ".pub", 0o600)})
51+ PRIV_TO_PUB["%s_private" % k] = "%s_public" % k
52
53-KEY_FILE_TPL = '/etc/ssh/ssh_host_%s_key'
54+KEY_GEN_TPL = 'o=$(ssh-keygen -yf "%s") && echo "$o" root@localhost > "%s"'
55
56
57 def handle(_name, cfg, cloud, log, _args):
58@@ -69,15 +62,15 @@ def handle(_name, cfg, cloud, log, _args
59 if "ssh_keys" in cfg:
60 # if there are keys in cloud-config, use them
61 for (key, val) in cfg["ssh_keys"].iteritems():
62- if key in KEY_2_FILE:
63- tgt_fn = KEY_2_FILE[key][0]
64- tgt_perms = KEY_2_FILE[key][1]
65+ if key in CONFIG_KEY_TO_FILE:
66+ tgt_fn = CONFIG_KEY_TO_FILE[key][0]
67+ tgt_perms = CONFIG_KEY_TO_FILE[key][1]
68 util.write_file(tgt_fn, val, tgt_perms)
69
70- for (priv, pub) in PRIV_2_PUB.iteritems():
71+ for (priv, pub) in PRIV_TO_PUB.items():
72 if pub in cfg['ssh_keys'] or not priv in cfg['ssh_keys']:
73 continue
74- pair = (KEY_2_FILE[priv][0], KEY_2_FILE[pub][0])
75+ pair = (CONFIG_KEY_TO_FILE[priv][0], CONFIG_KEY_TO_FILE[pub][0])
76 cmd = ['sh', '-xc', KEY_GEN_TPL % pair]
77 try:
78 # TODO(harlowja): Is this guard needed?
79@@ -92,18 +85,28 @@ def handle(_name, cfg, cloud, log, _args
80 genkeys = util.get_cfg_option_list(cfg,
81 'ssh_genkeytypes',
82 GENERATE_KEY_NAMES)
83+ lang_c = os.environ.copy()
84+ lang_c['LANG'] = 'C'
85 for keytype in genkeys:
86 keyfile = KEY_FILE_TPL % (keytype)
87+ if os.path.exists(keyfile):
88+ continue
89 util.ensure_dir(os.path.dirname(keyfile))
90- if not os.path.exists(keyfile):
91- cmd = ['ssh-keygen', '-t', keytype, '-N', '', '-f', keyfile]
92+ cmd = ['ssh-keygen', '-t', keytype, '-N', '', '-f', keyfile]
93+
94+ # TODO(harlowja): Is this guard needed?
95+ with util.SeLinuxGuard("/etc/ssh", recursive=True):
96 try:
97- # TODO(harlowja): Is this guard needed?
98- with util.SeLinuxGuard("/etc/ssh", recursive=True):
99- util.subp(cmd, capture=False)
100- except:
101- util.logexc(log, "Failed generating key type %s to "
102- "file %s", keytype, keyfile)
103+ out, err = util.subp(cmd, capture=True, env=lang_c)
104+ sys.stdout.write(out)
105+ except util.ProcessExecutionError as e:
106+ err = e.stderr.lower()
107+ if (e.exit_code == 1 and
108+ err.lower().startswith("unknown key")):
109+ log.debug("ssh-keygen: unknown key type '%s'", keytype)
110+ else:
111+ util.logexc(log, "Failed generating key type %s to "
112+ "file %s", keytype, keyfile)
113
114 try:
115 (users, _groups) = ds.normalize_users_groups(cfg, cloud.distro)
0116
=== modified file 'debian/patches/series'
--- debian/patches/series 2015-09-10 16:24:44 +0000
+++ debian/patches/series 2015-09-10 16:50:23 +0000
@@ -14,3 +14,4 @@
14lp-1470890-include-regions-in-dynamic-mirror-discovery.patch14lp-1470890-include-regions-in-dynamic-mirror-discovery.patch
15lp-1490796-azure-fix-mount_cb-for-symlinks.patch15lp-1490796-azure-fix-mount_cb-for-symlinks.patch
16lp-1469260-fix-consumption-of-vendor-data.patch16lp-1469260-fix-consumption-of-vendor-data.patch
17lp-1461242-generate-ed25519-host-keys.patch

Subscribers

People subscribed via source and target branches

to all changes: