root / lib / ssh.py @ 64272529
History | View | Annotate | Download (6.6 kB)
1 | 2f31098c | Iustin Pop | #
|
---|---|---|---|
2 | a8083063 | Iustin Pop | #
|
3 | a8083063 | Iustin Pop | |
4 | a8083063 | Iustin Pop | # Copyright (C) 2006, 2007 Google Inc.
|
5 | a8083063 | Iustin Pop | #
|
6 | a8083063 | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | a8083063 | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | a8083063 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | a8083063 | Iustin Pop | # (at your option) any later version.
|
10 | a8083063 | Iustin Pop | #
|
11 | a8083063 | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | a8083063 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | a8083063 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | a8083063 | Iustin Pop | # General Public License for more details.
|
15 | a8083063 | Iustin Pop | #
|
16 | a8083063 | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | a8083063 | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | a8083063 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | a8083063 | Iustin Pop | # 02110-1301, USA.
|
20 | a8083063 | Iustin Pop | |
21 | a8083063 | Iustin Pop | |
22 | a8083063 | Iustin Pop | """Module encapsulating ssh functionality.
|
23 | a8083063 | Iustin Pop |
|
24 | a8083063 | Iustin Pop | """
|
25 | a8083063 | Iustin Pop | |
26 | a8083063 | Iustin Pop | |
27 | a8083063 | Iustin Pop | import os |
28 | a8083063 | Iustin Pop | |
29 | a8083063 | Iustin Pop | from ganeti import utils |
30 | a8083063 | Iustin Pop | from ganeti import errors |
31 | 82122173 | Iustin Pop | from ganeti import constants |
32 | 82122173 | Iustin Pop | |
33 | 82122173 | Iustin Pop | |
34 | 70d9e3d8 | Iustin Pop | def GetUserFiles(user, mkdir=False): |
35 | 70d9e3d8 | Iustin Pop | """Return the paths of a user's ssh files.
|
36 | 70d9e3d8 | Iustin Pop |
|
37 | 70d9e3d8 | Iustin Pop | The function will return a triplet (priv_key_path, pub_key_path,
|
38 | 70d9e3d8 | Iustin Pop | auth_key_path) that are used for ssh authentication. Currently, the
|
39 | 70d9e3d8 | Iustin Pop | keys used are DSA keys, so this function will return:
|
40 | 70d9e3d8 | Iustin Pop | (~user/.ssh/id_dsa, ~user/.ssh/id_dsa.pub,
|
41 | 70d9e3d8 | Iustin Pop | ~user/.ssh/authorized_keys).
|
42 | 70d9e3d8 | Iustin Pop |
|
43 | 70d9e3d8 | Iustin Pop | If the optional parameter mkdir is True, the ssh directory will be
|
44 | 70d9e3d8 | Iustin Pop | created if it doesn't exist.
|
45 | 70d9e3d8 | Iustin Pop |
|
46 | 70d9e3d8 | Iustin Pop | Regardless of the mkdir parameters, the script will raise an error
|
47 | 70d9e3d8 | Iustin Pop | if ~user/.ssh is not a directory.
|
48 | 70d9e3d8 | Iustin Pop |
|
49 | 70d9e3d8 | Iustin Pop | """
|
50 | 70d9e3d8 | Iustin Pop | user_dir = utils.GetHomeDir(user) |
51 | 70d9e3d8 | Iustin Pop | if not user_dir: |
52 | 70d9e3d8 | Iustin Pop | raise errors.OpExecError("Cannot resolve home of user %s" % user) |
53 | 70d9e3d8 | Iustin Pop | |
54 | 70d9e3d8 | Iustin Pop | ssh_dir = os.path.join(user_dir, ".ssh")
|
55 | 70d9e3d8 | Iustin Pop | if not os.path.lexists(ssh_dir): |
56 | 70d9e3d8 | Iustin Pop | if mkdir:
|
57 | 70d9e3d8 | Iustin Pop | try:
|
58 | 70d9e3d8 | Iustin Pop | os.mkdir(ssh_dir, 0700)
|
59 | 70d9e3d8 | Iustin Pop | except EnvironmentError, err: |
60 | 70d9e3d8 | Iustin Pop | raise errors.OpExecError("Can't create .ssh dir for user %s: %s" % |
61 | 70d9e3d8 | Iustin Pop | (user, str(err)))
|
62 | 70d9e3d8 | Iustin Pop | elif not os.path.isdir(ssh_dir): |
63 | 70d9e3d8 | Iustin Pop | raise errors.OpExecError("path ~%s/.ssh is not a directory" % user) |
64 | 70d9e3d8 | Iustin Pop | |
65 | 70d9e3d8 | Iustin Pop | return [os.path.join(ssh_dir, base)
|
66 | 70d9e3d8 | Iustin Pop | for base in ["id_dsa", "id_dsa.pub", "authorized_keys"]] |
67 | 70d9e3d8 | Iustin Pop | |
68 | 70d9e3d8 | Iustin Pop | |
69 | c92b310a | Michael Hanselmann | class SshRunner: |
70 | c92b310a | Michael Hanselmann | """Wrapper for SSH commands.
|
71 | a8083063 | Iustin Pop |
|
72 | a8083063 | Iustin Pop | """
|
73 | 56bece1f | Iustin Pop | def __init__(self, cluster_name): |
74 | 56bece1f | Iustin Pop | self.cluster_name = cluster_name
|
75 | 1ff08570 | Michael Hanselmann | |
76 | 652d6694 | Michael Hanselmann | def _BuildSshOptions(self, batch, ask_key, use_cluster_key, |
77 | 652d6694 | Michael Hanselmann | strict_host_check): |
78 | f6d9f4c3 | Michael Hanselmann | options = [ |
79 | f6d9f4c3 | Michael Hanselmann | "-oEscapeChar=none",
|
80 | f6d9f4c3 | Michael Hanselmann | "-oHashKnownHosts=no",
|
81 | f6d9f4c3 | Michael Hanselmann | "-oGlobalKnownHostsFile=%s" % constants.SSH_KNOWN_HOSTS_FILE,
|
82 | f6d9f4c3 | Michael Hanselmann | "-oUserKnownHostsFile=/dev/null",
|
83 | f6d9f4c3 | Michael Hanselmann | ] |
84 | f6d9f4c3 | Michael Hanselmann | |
85 | f6d9f4c3 | Michael Hanselmann | if use_cluster_key:
|
86 | 56bece1f | Iustin Pop | options.append("-oHostKeyAlias=%s" % self.cluster_name) |
87 | f6d9f4c3 | Michael Hanselmann | |
88 | 652d6694 | Michael Hanselmann | # TODO: Too many boolean options, maybe convert them to more descriptive
|
89 | 652d6694 | Michael Hanselmann | # constants.
|
90 | 652d6694 | Michael Hanselmann | |
91 | f6d9f4c3 | Michael Hanselmann | # Note: ask_key conflicts with batch mode
|
92 | f6d9f4c3 | Michael Hanselmann | if batch:
|
93 | f6d9f4c3 | Michael Hanselmann | if ask_key:
|
94 | f6d9f4c3 | Michael Hanselmann | raise errors.ProgrammerError("SSH call requested conflicting options") |
95 | f6d9f4c3 | Michael Hanselmann | |
96 | 652d6694 | Michael Hanselmann | options.append("-oBatchMode=yes")
|
97 | 652d6694 | Michael Hanselmann | |
98 | 652d6694 | Michael Hanselmann | if strict_host_check:
|
99 | 652d6694 | Michael Hanselmann | options.append("-oStrictHostKeyChecking=yes")
|
100 | 652d6694 | Michael Hanselmann | else:
|
101 | 652d6694 | Michael Hanselmann | options.append("-oStrictHostKeyChecking=no")
|
102 | f6d9f4c3 | Michael Hanselmann | |
103 | f6d9f4c3 | Michael Hanselmann | elif ask_key:
|
104 | f6d9f4c3 | Michael Hanselmann | options.extend([ |
105 | f6d9f4c3 | Michael Hanselmann | "-oStrictHostKeyChecking=ask",
|
106 | f6d9f4c3 | Michael Hanselmann | ]) |
107 | f6d9f4c3 | Michael Hanselmann | |
108 | f6d9f4c3 | Michael Hanselmann | return options
|
109 | 1ff08570 | Michael Hanselmann | |
110 | 8f07f831 | Michael Hanselmann | def BuildCmd(self, hostname, user, command, batch=True, ask_key=False, |
111 | 652d6694 | Michael Hanselmann | tty=False, use_cluster_key=True, strict_host_check=True): |
112 | c92b310a | Michael Hanselmann | """Build an ssh command to execute a command on a remote node.
|
113 | c92b310a | Michael Hanselmann |
|
114 | c92b310a | Michael Hanselmann | Args:
|
115 | c92b310a | Michael Hanselmann | hostname: the target host, string
|
116 | c92b310a | Michael Hanselmann | user: user to auth as
|
117 | c92b310a | Michael Hanselmann | command: the command
|
118 | c92b310a | Michael Hanselmann | batch: if true, ssh will run in batch mode with no prompting
|
119 | c92b310a | Michael Hanselmann | ask_key: if true, ssh will run with StrictHostKeyChecking=ask, so that
|
120 | c92b310a | Michael Hanselmann | we can connect to an unknown host (not valid in batch mode)
|
121 | 51144e33 | Michael Hanselmann | use_cluster_key: Whether to expect and use the cluster-global SSH key
|
122 | 652d6694 | Michael Hanselmann | strict_host_check: Whether to check the host's SSH key at all
|
123 | c92b310a | Michael Hanselmann |
|
124 | c92b310a | Michael Hanselmann | Returns:
|
125 | c92b310a | Michael Hanselmann | The ssh call to run 'command' on the remote host.
|
126 | c92b310a | Michael Hanselmann |
|
127 | c92b310a | Michael Hanselmann | """
|
128 | fff33d70 | Michael Hanselmann | argv = [constants.SSH, "-q"]
|
129 | 652d6694 | Michael Hanselmann | argv.extend(self._BuildSshOptions(batch, ask_key, use_cluster_key,
|
130 | 652d6694 | Michael Hanselmann | strict_host_check)) |
131 | 8f07f831 | Michael Hanselmann | if tty:
|
132 | 8f07f831 | Michael Hanselmann | argv.append("-t")
|
133 | c92b310a | Michael Hanselmann | argv.extend(["%s@%s" % (user, hostname), command])
|
134 | c92b310a | Michael Hanselmann | return argv
|
135 | c92b310a | Michael Hanselmann | |
136 | 54ab6aec | Michael Hanselmann | def Run(self, *args, **kwargs): |
137 | c92b310a | Michael Hanselmann | """Runs a command on a remote node.
|
138 | c92b310a | Michael Hanselmann |
|
139 | c92b310a | Michael Hanselmann | This method has the same return value as `utils.RunCmd()`, which it
|
140 | c92b310a | Michael Hanselmann | uses to launch ssh.
|
141 | c92b310a | Michael Hanselmann |
|
142 | c92b310a | Michael Hanselmann | Args:
|
143 | 54ab6aec | Michael Hanselmann | See SshRunner.BuildCmd.
|
144 | c92b310a | Michael Hanselmann |
|
145 | c92b310a | Michael Hanselmann | Returns:
|
146 | c92b310a | Michael Hanselmann | `utils.RunResult` like `utils.RunCmd()`
|
147 | c92b310a | Michael Hanselmann |
|
148 | c92b310a | Michael Hanselmann | """
|
149 | 54ab6aec | Michael Hanselmann | return utils.RunCmd(self.BuildCmd(*args, **kwargs)) |
150 | c92b310a | Michael Hanselmann | |
151 | c92b310a | Michael Hanselmann | def CopyFileToNode(self, node, filename): |
152 | c92b310a | Michael Hanselmann | """Copy a file to another node with scp.
|
153 | c92b310a | Michael Hanselmann |
|
154 | c92b310a | Michael Hanselmann | Args:
|
155 | c92b310a | Michael Hanselmann | node: node in the cluster
|
156 | c92b310a | Michael Hanselmann | filename: absolute pathname of a local file
|
157 | c92b310a | Michael Hanselmann |
|
158 | c92b310a | Michael Hanselmann | Returns:
|
159 | c92b310a | Michael Hanselmann | success: True/False
|
160 | a8083063 | Iustin Pop |
|
161 | c92b310a | Michael Hanselmann | """
|
162 | c92b310a | Michael Hanselmann | if not os.path.isabs(filename): |
163 | 23828f1c | Iustin Pop | logging.error("File %s must be an absolute path", filename)
|
164 | c92b310a | Michael Hanselmann | return False |
165 | a8083063 | Iustin Pop | |
166 | 1d544ba3 | Michael Hanselmann | if not os.path.isfile(filename): |
167 | 23828f1c | Iustin Pop | logging.error("File %s does not exist", filename)
|
168 | 1d544ba3 | Michael Hanselmann | return False |
169 | 1d544ba3 | Michael Hanselmann | |
170 | fff33d70 | Michael Hanselmann | command = [constants.SCP, "-q", "-p"] |
171 | 652d6694 | Michael Hanselmann | command.extend(self._BuildSshOptions(True, False, True, True)) |
172 | c92b310a | Michael Hanselmann | command.append(filename) |
173 | c92b310a | Michael Hanselmann | command.append("%s:%s" % (node, filename))
|
174 | a8083063 | Iustin Pop | |
175 | c92b310a | Michael Hanselmann | result = utils.RunCmd(command) |
176 | a8083063 | Iustin Pop | |
177 | c92b310a | Michael Hanselmann | if result.failed:
|
178 | 23828f1c | Iustin Pop | logging.error("Copy to node %s failed (%s) error %s,"
|
179 | 23828f1c | Iustin Pop | " command was %s",
|
180 | 23828f1c | Iustin Pop | node, result.fail_reason, result.output, result.cmd) |
181 | a8083063 | Iustin Pop | |
182 | c92b310a | Michael Hanselmann | return not result.failed |
183 | a8083063 | Iustin Pop | |
184 | c92b310a | Michael Hanselmann | def VerifyNodeHostname(self, node): |
185 | c92b310a | Michael Hanselmann | """Verify hostname consistency via SSH.
|
186 | a8083063 | Iustin Pop |
|
187 | c92b310a | Michael Hanselmann | This functions connects via ssh to a node and compares the hostname
|
188 | c92b310a | Michael Hanselmann | reported by the node to the name with have (the one that we
|
189 | c92b310a | Michael Hanselmann | connected to).
|
190 | a8083063 | Iustin Pop |
|
191 | c92b310a | Michael Hanselmann | This is used to detect problems in ssh known_hosts files
|
192 | c92b310a | Michael Hanselmann | (conflicting known hosts) and incosistencies between dns/hosts
|
193 | c92b310a | Michael Hanselmann | entries and local machine names
|
194 | a8083063 | Iustin Pop |
|
195 | c92b310a | Michael Hanselmann | Args:
|
196 | c92b310a | Michael Hanselmann | node: nodename of a host to check. can be short or full qualified hostname
|
197 | a8083063 | Iustin Pop |
|
198 | c92b310a | Michael Hanselmann | Returns:
|
199 | c92b310a | Michael Hanselmann | (success, detail)
|
200 | c92b310a | Michael Hanselmann | where
|
201 | c92b310a | Michael Hanselmann | success: True/False
|
202 | c92b310a | Michael Hanselmann | detail: String with details
|
203 | a8083063 | Iustin Pop |
|
204 | c92b310a | Michael Hanselmann | """
|
205 | c92b310a | Michael Hanselmann | retval = self.Run(node, 'root', 'hostname') |
206 | a8083063 | Iustin Pop | |
207 | c92b310a | Michael Hanselmann | if retval.failed:
|
208 | c92b310a | Michael Hanselmann | msg = "ssh problem"
|
209 | c92b310a | Michael Hanselmann | output = retval.output |
210 | c92b310a | Michael Hanselmann | if output:
|
211 | c92b310a | Michael Hanselmann | msg += ": %s" % output
|
212 | c92b310a | Michael Hanselmann | return False, msg |
213 | a8083063 | Iustin Pop | |
214 | c92b310a | Michael Hanselmann | remotehostname = retval.stdout.strip() |
215 | a8083063 | Iustin Pop | |
216 | c92b310a | Michael Hanselmann | if not remotehostname or remotehostname != node: |
217 | c92b310a | Michael Hanselmann | return False, "hostname mismatch, got %s" % remotehostname |
218 | a8083063 | Iustin Pop | |
219 | c92b310a | Michael Hanselmann | return True, "host matches" |
220 | 75a5f456 | Michael Hanselmann | |
221 | 75a5f456 | Michael Hanselmann | |
222 | 7688d0d3 | Michael Hanselmann | def WriteKnownHostsFile(cfg, file_name): |
223 | 75a5f456 | Michael Hanselmann | """Writes the cluster-wide equally known_hosts file.
|
224 | 75a5f456 | Michael Hanselmann |
|
225 | 75a5f456 | Michael Hanselmann | """
|
226 | 75a5f456 | Michael Hanselmann | utils.WriteFile(file_name, mode=0700,
|
227 | 7688d0d3 | Michael Hanselmann | data="%s ssh-rsa %s\n" % (cfg.GetClusterName(),
|
228 | 75a5f456 | Michael Hanselmann | cfg.GetHostKey())) |