Module encapsulating ssh functionality.
Class |
|
Wrapper for SSH commands. |
Function |
|
Adds an SSH public key to an authorized_keys file. |
Function |
|
Adds a list of SSH public key to an authorized_keys file. |
Function |
|
Adds a new key to the list of public keys. |
Function |
|
Check if there is at most one key per host in 'authorized_keys' file. |
Function |
|
Resets the content of the public key file. |
Function |
|
Checks the key bits to be used for a given key type, or provides defaults. |
Function |
|
Wrapper over GetUserFiles to retrieve files for all SSH key types. |
Function |
|
Return the paths of a user's SSH files. |
Function |
|
Check if a particular key is in the 'authorized_keys' file. |
Function |
|
Creates the public key file and adds the master node's SSH key. |
Function |
|
Setup the SSH configuration for the node. |
Function |
|
Overrides the public key file with a list of given keys. |
Function |
|
Retrieves a map of keys for the requested node UUIDs. |
Function |
|
Fetches a public SSH key from a node via SSH. |
Function |
|
Removes an SSH public key from an authorized_keys file. |
Function |
|
Removes public SSH keys from an authorized_keys file. |
Function |
|
Removes a key from the list of public keys. |
Function |
|
Replaces a host name with the node's corresponding UUID. |
Function |
|
Runs a command on a remote machine via SSH and provides input in stdin. |
Function |
|
Writes the cluster-wide equally known_hosts file. |
Constant | SSH |
Undocumented |
Variable |
|
Undocumented |
Function | _ |
Adds a new SSH key to the key file if it did not exist already. |
Function | _ |
Processes one line of the public key file when adding a key. |
Function | _ |
Ensured the correct Ganeti version before running a command via SSH. |
Function | _ |
Manipulates the list of public SSH keys of the cluster. |
Function | _ |
Parses a line of the public key file. |
Function | _ |
Logs when we tried to remove a key that does not exist. |
Function | _ |
Processes a line in the public key file when aiming for removing a key. |
Function | _ |
Logs a debug message when we try to replace a key that is not there. |
Function | _ |
Replaces a node's name with its UUID on a matching line in the key file. |
Function | _ |
Splits a line for SSH's authorized_keys file. |
Adds an SSH public key to an authorized_keys file.
Parameters | |
file | path to authorized_keys file |
key:str | string containing key |
Adds a list of SSH public key to an authorized_keys file.
Parameters | |
file | path to authorized_keys file |
keys:list of str | list of strings containing keys |
Adds a new key to the list of public keys.
See Also | |
_ManipulatePubKeyFile for parameter descriptions. |
Check if there is at most one key per host in 'authorized_keys' file.
Parameters | |
file | path to authorized_keys file |
node | list of names of nodes of the cluster |
Returns | |
a dictionary with hostnames which occur more than once |
Checks the key bits to be used for a given key type, or provides defaults.
Parameters | |
keyconstants.SSHK_ALL | The key type to use. |
key | The number of bits to use, if supplied by user. |
oldconstants.SSHK_ALL or None | The previously used key type, if any. |
old | The previously used number of bits, if any. |
Returns | |
positive int | The number of bits to use. |
Wrapper over GetUserFiles
to retrieve files for all SSH key types.
See GetUserFiles
for details.
Returns | |
tuple; (string, dict with string as key, tuple of (string, string) as value) | Undocumented |
Return the paths of a user's SSH files.
Parameters | |
user:string | Username |
mkdir:bool | Whether to create ".ssh" directory if it doesn't exist |
dircheck:bool | Whether to check if ".ssh" directory exists |
kind:string | One of constants.SSHK_ALL |
_homedir | Undocumented |
Returns | |
tuple; (string, string, string) | Tuple containing three file system paths; the private SSH key file, the public SSH key file and the user's authorized_keys file |
Raises | |
errors.OpExecError | When home directory of the user can not be determined |
errors.OpExecError | Regardless of the mkdir parameters, this exception is raised if ~$user/.ssh is not a directory and dircheck is set to True |
Check if a particular key is in the 'authorized_keys' file.
Parameters | |
file | path to authorized_keys file |
key:str | string containing key |
Creates the public key file and adds the master node's SSH key.
Parameters | |
master | the master node's UUID |
keyconstants.SSHK_ALL | the type of ssh key to be used |
key | name of the file containing the public keys |
Setup the SSH configuration for the node.
This generates a dsa keypair for root, adds the pub key to the permitted hosts and adds the hostkey to its own known hosts.
Parameters | |
key | the type of SSH keypair to be generated |
key | the key length, in bits, to be used |
error | Undocumented |
_homedir | Undocumented |
_suffix | Undocumented |
Overrides the public key file with a list of given keys.
Parameters | |
key | dictionary mapping uuids to lists of SSH keys |
key | Undocumented |
Retrieves a map of keys for the requested node UUIDs.
Parameters | |
target | UUID of the node to retrieve the key for or a list of UUIDs of nodes to retrieve the keys for |
key | filename of the file of public node keys (optional parameter for testing) |
error | Function that returns an exception, used to customize exception types depending on the calling context |
Returns | |
dict mapping strings to list of strings | dictionary mapping node uuids to their ssh keys |
Fetches a public SSH key from a node via SSH.
Parameters | |
pub | a tuple consisting of the file name of the public DSA key |
node | Undocumented |
cluster | Undocumented |
port | Undocumented |
ask | Undocumented |
strict | Undocumented |
Removes an SSH public key from an authorized_keys file.
Parameters | |
file | path to authorized_keys file |
key:str | string containing key |
Removes public SSH keys from an authorized_keys file.
Parameters | |
file | path to authorized_keys file |
keys:list of str | list of strings containing keys |
Removes a key from the list of public keys.
See Also | |
_ManipulatePubKeyFile for parameter descriptions. |
Replaces a host name with the node's corresponding UUID.
When a node is added to the cluster, we don't know it's UUID yet. So first its SSH key gets added to the public key file and in a second step, the node's name gets replaced with the node's UUID as soon as we know the UUID.
Parameters | |
node | the node's UUID to replace the node's name |
node | the node's name to be replaced by the node's UUID |
key | Undocumented |
error | Undocumented |
See Also | |
_ManipulatePubKeyFile for the other parameter descriptions. |
Runs a command on a remote machine via SSH and provides input in stdin.
Parameters | |
cluster | Cluster name |
node:string | Node name |
basecmd:string | Base command (path on the remote machine) |
port:int | The SSH port of the remote machine or None for the default |
data | JSON-serializable input data for script (passed to stdin) |
debug:bool | Enable debug output |
verbose:bool | Enable verbose output |
use | See ssh.SshRunner.BuildCmd |
ask | See ssh.SshRunner.BuildCmd |
strict | See ssh.SshRunner.BuildCmd |
ensure | Undocumented |
Undocumented
Value |
|
Adds a new SSH key to the key file if it did not exist already.
This is an auxiliary function for _ManipulatePublicKeyFile which is carried out when a new key is added to the public key file and after processing the whole file, we found out that the key does not exist in the file yet but needs to be appended at the end.
Parameters | |
new | the UUID of the node whose key is added |
new | the SSH key to be added |
Returns | |
string | a new line to be added to the file |
Processes one line of the public key file when adding a key.
This is a sub function that can be called within the _ManipulatePublicKeyFile function. It processes one line of the public key file, checks if this line contains the key to add already and if so, notes the occurrence in the return value.
Parameters | |
new | the node UUID of the node whose key is added |
new | the SSH key to be added |
line | Undocumented |
line | the SSH key of the node whose line in the public key file is processed in this function call |
found:boolean | whether or not the (UUID, key) pair of the node whose key is being added was found in the public key file already. |
Returns | |
(boolean, string) | a possibly updated value of found and the processed line |
Ensured the correct Ganeti version before running a command via SSH.
Before a command is run on a node via SSH, it makes sense in some situations to ensure that this node is indeed running the correct version of Ganeti like the rest of the cluster.
Parameters | |
cmd:string | string |
Returns | |
list of strings | a list of commands with the newly added ones at the beginning |
Manipulates the list of public SSH keys of the cluster.
This is a general function to manipulate the public key file. It needs two auxiliary functions process_line_fn and process_else_fn to work. Generally, the public key file is processed as follows: 1) The function processes each line of the original ganeti public key file, applies the process_line_fn function on it, which returns a possibly manipulated line and an indicator whether the line in question was found. If a line is returned, it is added to a list of lines for later writing to the file. 2) If all lines are processed and the 'found' variable is False, the seconds auxiliary function process_else_fn is called to possibly add more lines to the list of lines. 3) Finally, the list of lines is assembled to a string and written atomically to the public key file, thereby overriding it.
If the public key file does not exist, we create it. This is necessary for a smooth transition after an upgrade.
Parameters | |
target | identifier of the node whose key is added; in most cases this is the node's UUID, but in some it is the node's host name |
target | string containing a public SSH key (a complete line possibly including more parameters than just the key) |
key | filename of the file of public node keys (optional parameter for testing) |
error | Function that returns an exception, used to customize exception types depending on the calling context |
process | function to process one line of the public key file |
process | function to be called if no line of the key file matches the target uuid |
Parses a line of the public key file.
Parameters | |
line:string | line of the public key file |
error | function to process error messages |
Returns | |
tuple (string, string) | a tuple containing the UUID of the node and a string containing the SSH key and possible more parameters for the key |
Logs when we tried to remove a key that does not exist.
This is an auxiliary function for _ManipulatePublicKeyFile which is run after we have processed the complete public key file and did not find the key to be removed.
Parameters | |
target | the UUID of the node whose key was supposed to be removed |
_target | the key of the node which was supposed to be removed (not used) |
Returns | |
string | in this case, always None |
Processes a line in the public key file when aiming for removing a key.
This is an auxiliary function for _ManipulatePublicKeyFile when we are removing a key from the public key file. This particular function only checks if the current line contains the UUID of the node in question and writes the line to the temporary file otherwise.
Parameters | |
target | UUID of the node whose key is being removed |
_target | SSH key of the node (not used) |
line | UUID of the node whose line is processed in this call |
line | SSH key of the nodes whose line is processed in this call |
found:boolean | whether or not the UUID was already found. |
Returns | |
(boolean, string) | a tuple, indicating if the target line was found and the processed line; the line is 'None', if the original line is removed |
Logs a debug message when we try to replace a key that is not there.
This is an implementation of the auxiliary process_else_fn function for the _ManipulatePubKeyFile function when we use it to replace a line in the public key file that is indexed by the node's name instead of the node's UUID.
Parameters | |
node | the node's UUID |
node | the node's UUID |
_key:string (not used) | the node's SSH key (not used) |
Returns | |
string | in this case, always None |
Replaces a node's name with its UUID on a matching line in the key file.
This is an auxiliary function for _ManipulatePublicKeyFile which processes a line of the ganeti public key file. If the line in question matches the node's name, the name will be replaced by the node's UUID.
Parameters | |
node | name of the node to be replaced by the UUID |
_key:string | SSH key of the node (not used) |
line | an identifier of a node in a line of the public key file. This can be either a node name or a node UUID, depending on if it got replaced already or not. |
line | SSH key of the node whose line is processed |
found:boolean | whether or not the line matches the node's name |
node | the node's UUID which will replace the node name |
Returns | |
(boolean, string) | a tuple indicating whether the target line was found and the processed line |