def RenewSshKeys(node_uuids, node_names, master_candidate_uuids,
                 potential_master_candidates, old_key_type, new_key_type,
                 new_key_bits,
                 ganeti_pub_keys_file=pathutils.SSH_PUB_KEYS,
                 ssconf_store=None,
                 noded_cert_file=pathutils.NODED_CERT_FILE,
                 run_cmd_fn=ssh.RunSshCmdWithStdin,
                 ssh_update_debug=False,
                 ssh_update_verbose=False):
  if not ssconf_store:
    ssconf_store = ssconf.SimpleStore()
  cluster_name = ssconf_store.GetClusterName()

  if not len(node_uuids) == len(node_names):
    raise errors.ProgrammerError("List of nodes UUIDs and node names"
                                 " does not match in length.")

  old_pub_keyfile = ssh.GetSshPubKeyFilename(old_key_type)
  new_pub_keyfile = ssh.GetSshPubKeyFilename(new_key_type)
  old_master_key = ssh.ReadLocalSshPubKeys([old_key_type])

  node_uuid_name_map = zip(node_uuids, node_names)

  master_node_name = ssconf_store.GetMasterNode()
  master_node_uuid = _GetMasterNodeUUID(node_uuid_name_map, master_node_name)
  ssh_port_map = ssconf_store.GetSshPortMap()
  # List of all node errors that happened, but which did not abort the
  # procedure as a whole. It is important that this is a list to have a
  # somewhat chronological history of events.
  all_node_errors = []

  # process non-master nodes

  # keys to add in bulk at the end
  node_keys_to_add = []

  # list of all nodes
  node_list = []

  # list of keys to be removed before generating new keys
  node_info_to_remove = []

  for node_uuid, node_name in node_uuid_name_map:
    if node_name == master_node_name:
      continue
    master_candidate = node_uuid in master_candidate_uuids
    potential_master_candidate = node_name in potential_master_candidates
    node_list.append((node_uuid, node_name, master_candidate,
                      potential_master_candidate))

    if master_candidate:
      logging.debug("Fetching old SSH key from node '%s'.", node_name)
      old_pub_key = ssh.ReadRemoteSshPubKey(old_pub_keyfile,
                                            node_name, cluster_name,
                                            ssh_port_map[node_name],
                                            False, # ask_key
                                            False) # key_check
      if old_pub_key != old_master_key:
        # If we are already in a multi-key setup (that is past Ganeti 2.12),
        # we can safely remove the old key of the node. Otherwise, we cannot
        # remove that node's key, because it is also the master node's key
        # and that would terminate all communication from the master to the
        # node.
        node_info_to_remove.append(SshRemoveNodeInfo(
            uuid=node_uuid,
            name=node_name,
            from_authorized_keys=master_candidate,
            from_public_keys=False,
            clear_authorized_keys=False,
            clear_public_keys=False))
      else:
        logging.debug("Old key of node '%s' is the same as the current master"
                      " key. Not deleting that key on the node.", node_name)

  logging.debug("Removing old SSH keys of all master candidates.")
  if node_info_to_remove:
    node_errors = RemoveNodeSshKeyBulk(
        node_info_to_remove,
        master_candidate_uuids,
        potential_master_candidates,
        master_uuid=master_node_uuid,
        pub_key_file=ganeti_pub_keys_file,
        ssconf_store=ssconf_store,
        noded_cert_file=noded_cert_file,
        run_cmd_fn=run_cmd_fn,
        ssh_update_debug=ssh_update_debug,
        ssh_update_verbose=ssh_update_verbose)
    if node_errors:
      all_node_errors = all_node_errors + node_errors

  for (node_uuid, node_name, master_candidate, potential_master_candidate) \
      in node_list:

    logging.debug("Generating new SSH key for node '%s'.", node_name)
    _GenerateNodeSshKey(node_name, ssh_port_map, new_key_type, new_key_bits,
                        ssconf_store=ssconf_store,
                        noded_cert_file=noded_cert_file,
                        run_cmd_fn=run_cmd_fn,
                        ssh_update_verbose=ssh_update_verbose,
                        ssh_update_debug=ssh_update_debug)

    try:
      logging.debug("Fetching newly created SSH key from node '%s'.", node_name)
      pub_key = ssh.ReadRemoteSshPubKey(new_pub_keyfile,
                                        node_name, cluster_name,
                                        ssh_port_map[node_name],
                                        False, # ask_key
                                        False) # key_check
    except:
      raise errors.SshUpdateError("Could not fetch key of node %s"
                                  " (UUID %s)" % (node_name, node_uuid))

    if potential_master_candidate:
      ssh.RemovePublicKey(node_uuid, key_file=ganeti_pub_keys_file)
      ssh.AddPublicKey(node_uuid, pub_key, key_file=ganeti_pub_keys_file)

    node_info = SshAddNodeInfo(name=node_name,
                               uuid=node_uuid,
                               to_authorized_keys=master_candidate,
                               to_public_keys=potential_master_candidate,
                               get_public_keys=True)
    node_keys_to_add.append(node_info)

  node_errors = AddNodeSshKeyBulk(
      node_keys_to_add, potential_master_candidates,
      pub_key_file=ganeti_pub_keys_file, ssconf_store=ssconf_store,
      noded_cert_file=noded_cert_file,
      run_cmd_fn=run_cmd_fn,
      ssh_update_debug=ssh_update_debug,
      ssh_update_verbose=ssh_update_verbose)
  if node_errors:
    all_node_errors = all_node_errors + node_errors

  # Renewing the master node's key

  # Preserve the old keys for now
  old_master_keys_by_uuid = _GetOldMasterKeys(master_node_uuid,
                                              ganeti_pub_keys_file)

  # Generate a new master key with a suffix, don't touch the old one for now
  logging.debug("Generate new ssh key of master.")
  _GenerateNodeSshKey(master_node_name, ssh_port_map,
                      new_key_type, new_key_bits,
                      ssconf_store=ssconf_store,
                      noded_cert_file=noded_cert_file,
                      run_cmd_fn=run_cmd_fn,
                      suffix=constants.SSHS_MASTER_SUFFIX,
                      ssh_update_debug=ssh_update_debug,
                      ssh_update_verbose=ssh_update_verbose)
  # Read newly created master key
  new_master_keys = ssh.ReadLocalSshPubKeys(
      [new_key_type], suffix=constants.SSHS_MASTER_SUFFIX)

  # Replace master key in the master nodes' public key file
  ssh.RemovePublicKey(master_node_uuid, key_file=ganeti_pub_keys_file)
  for pub_key in new_master_keys:
    ssh.AddPublicKey(master_node_uuid, pub_key, key_file=ganeti_pub_keys_file)

  # Add new master key to all node's public and authorized keys
  logging.debug("Add new master key to all nodes.")
  node_errors = AddNodeSshKey(
      master_node_uuid, master_node_name, potential_master_candidates,
      to_authorized_keys=True, to_public_keys=True,
      get_public_keys=False, pub_key_file=ganeti_pub_keys_file,
      ssconf_store=ssconf_store, noded_cert_file=noded_cert_file,
      run_cmd_fn=run_cmd_fn,
      ssh_update_debug=ssh_update_debug,
      ssh_update_verbose=ssh_update_verbose)
  if node_errors:
    all_node_errors = all_node_errors + node_errors

  # Remove the old key file and rename the new key to the non-temporary filename
  ssh.ReplaceSshKeys(new_key_type, new_key_type,
                     src_key_suffix=constants.SSHS_MASTER_SUFFIX)

  # Remove old key from authorized keys
  (auth_key_file, _) = \
      ssh.GetAllUserFiles(constants.SSH_LOGIN_USER, mkdir=False, dircheck=False)
  ssh.RemoveAuthorizedKeys(auth_key_file,
                           old_master_keys_by_uuid[master_node_uuid])

  # Remove the old key from all node's authorized keys file
  logging.debug("Remove the old master key from all nodes.")
  node_errors = RemoveNodeSshKey(
      master_node_uuid, master_node_name, master_candidate_uuids,
      potential_master_candidates,
      keys_to_remove=old_master_keys_by_uuid, from_authorized_keys=True,
      from_public_keys=False, clear_authorized_keys=False,
      clear_public_keys=False,
      pub_key_file=ganeti_pub_keys_file,
      ssconf_store=ssconf_store,
      noded_cert_file=noded_cert_file,
      run_cmd_fn=run_cmd_fn,
      ssh_update_debug=ssh_update_debug,
      ssh_update_verbose=ssh_update_verbose)
  if node_errors:
    all_node_errors = all_node_errors + node_errors

  return all_node_errors
Start a recording for the provided hash
dataset/ETHPy150Open ganeti/ganeti/lib/backend.py/RenewSshKeys--crossover with--dataset/ETHPy150Open datasift/datasift-python/datasift/pylon.py/Pylon.start