def get_key_pair(user_host, logger=None,
                 overwrite_cache=False, ignore_ssh=False):
    """
    Returns RSA key containing both public and private keys for the user
    identified in `user_host`.  This can be an expensive operation, so
    we avoid generating a new key pair whenever possible.
    If ``~/.ssh/id_rsa`` exists and is private, that key is returned.

    user_host: string
        Format ``user@host``.

    logger: :class:`logging.Logger`
        Used for debug messages.

    overwrite_cache: bool
        If True, a new key is generated and forced into the cache of existing
        known keys.  Used for testing.

    ignore_ssh: bool
        If True, ignore any existing ssh id_rsa key file.  Used for testing.

    .. note::

        To avoid unnecessary key generation, the public/private key pair for
        the current user is stored in the private file ``~/.openmdao/keys``.
        On Windows this requires the pywin32 extension.  Also, the public
        key is stored in ssh form in ``~/.openmdao/id_rsa.pub``.

    """
    logger = logger or NullLogger()

    with _KEY_CACHE_LOCK:
        if overwrite_cache:
            key_pair = _generate(user_host, logger)
            _KEY_CACHE[user_host] = key_pair
            return key_pair

        # Look in previously generated keys.
        try:
            key_pair = _KEY_CACHE[user_host]
        except __HOLE__:
            # If key for current user (typical), check filesystem.
# TODO: file lock to protect from separate processes.
            user, host = user_host.split('@')
            if user == getpass.getuser():
                current_user = True
                key_pair = None

                # Try to re-use SSH key. Exceptions should *never* be exercised!
                if not ignore_ssh:
                    id_rsa = \
                        os.path.expanduser(os.path.join('~', '.ssh', 'id_rsa'))
                    if is_private(id_rsa):
                        try:
                            with open(id_rsa, 'r') as inp:
                                key_pair = RSA.importKey(inp.read())
                        except Exception as exc:  #pragma no cover
                            logger.warning('ssh id_rsa import: %r', exc)
                        else:
                            generate = False
                    else:  #pragma no cover
                        logger.warning('Ignoring insecure ssh id_rsa.')

                if key_pair is None:
                    # Look for OpenMDAO key.
                    key_file = \
                        os.path.expanduser(os.path.join('~', '.openmdao', 'keys'))
                    if is_private(key_file):
                        try:
                            with open(key_file, 'rb') as inp:
                                key_pair = cPickle.load(inp)
                        except Exception:
                            generate = True
                        else:
                            generate = False
                    else:
                        logger.warning('Insecure keyfile! Regenerating keys.')
                        os.remove(key_file)
                        generate = True

            # Difficult to run test as non-current user.
            else:  #pragma no cover
                current_user = False
                generate = True

            if generate:
                key_pair = _generate(user_host, logger)
                if current_user:
                    key_dir = os.path.dirname(key_file)
                    if not os.path.exists(key_dir):
                        os.mkdir(key_dir)

                    # Save key pair in protected file.
                    if sys.platform == 'win32' and not HAVE_PYWIN32: #pragma no cover
                        logger.debug('No pywin32, not saving keyfile')
                    else:
                        make_private(key_dir)  # Private while writing keyfile.
                        with open(key_file, 'wb') as out:
                            cPickle.dump(key_pair, out,
                                         cPickle.HIGHEST_PROTOCOL)
                        try:
                            make_private(key_file)
                        # Hard to cause (recoverable) error here.
                        except Exception:  #pragma no cover
                            os.remove(key_file)  # Remove unsecured file.
                            raise

                    # Save public key in ssh form.
                    users = {user_host: key_pair.publickey()}
                    filename = os.path.join(key_dir, 'id_rsa.pub')
                    write_authorized_keys(users, filename, logger)

            _KEY_CACHE[user_host] = key_pair

    return key_pair
dataset/ETHPy150Open OpenMDAO/OpenMDAO-Framework/openmdao.util/src/openmdao/util/publickey.py/get_key_pair