# Buggy code
def create_private_key(
    path=None,
    text=False,
    bits=2048,
    passphrase=None,
    cipher="aes_128_cbc",
    verbose=True,
):
    """
    Creates a private key in PEM format.

    path:
        The path to write the file to, either ``path`` or ``text``
        are required.

    text:
        If ``True``, return the PEM text without writing to a file.
        Default ``False``.

    bits:
        Length of the private key in bits. Default 2048

    passphrase:
        Passphrase for encryting the private key

    cipher:
        Cipher for encrypting the private key. Has no effect if passhprase is None.

    verbose:
        Provide visual feedback on stdout. Default True

        .. versionadded:: 2016.11.0

    CLI Example:

    .. code-block:: bash

        salt '*' x509.create_private_key path=/etc/pki/mykey.key
    """
    if not path and not text:
        raise salt.exceptions.SaltInvocationError(
            "Either path or text must be specified."
        )
    if path and text:
        raise salt.exceptions.SaltInvocationError(
            "Either path or text must be specified, not both."
        )

    if verbose:
        _callback_func = M2Crypto.RSA.keygen_callback
    else:
        _callback_func = _keygen_callback

    # pylint: disable=no-member
    rsa = M2Crypto.RSA.gen_key(bits, M2Crypto.m2.RSA_F4, _callback_func)
    # pylint: enable=no-member
    bio = M2Crypto.BIO.MemoryBuffer()
    if passphrase is None:
        cipher = None
    rsa.save_key_bio(bio, cipher=cipher, callback=_passphrase_callback(passphrase))

    if path:
        return write_pem(text=bio.read_all(), path=path, pem_type="RSA PRIVATE KEY")
    else:
        return bio.read_all()


# Traceback (SaltInvocationError)
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/cli/caller.py", line 197, in call
ret['return'] = func(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/salt/modules/x509.py", line 1509, in create_certificate
passphrase=kwargs['signing_private_key_passphrase']),
File "/usr/lib/python2.7/site-packages/salt/modules/x509.py", line 358, in _get_private_key_obj
private_key = get_pem_entry(private_key, pem_type='RSA PRIVATE KEY')
File "/usr/lib/python2.7/site-packages/salt/modules/x509.py", line 472, in get_pem_entry
raise salt.exceptions.SaltInvocationError(errmsg)
SaltInvocationError: PEM does not contain a single entry of type RSA PRIVATE KEY:
-----BEGIN PRIVATE KEY-----
[PRIVATE KEY GOES HERE]
-----END PRIVATE KEY-----

# Fixed code
def create_private_key(
    path=None,
    text=False,
    bits=2048,
    passphrase=None,
    cipher="aes_128_cbc",
    verbose=True,
):
    """
    Creates a private key in PEM format.

    path:
        The path to write the file to, either ``path`` or ``text``
        are required.

    text:
        If ``True``, return the PEM text without writing to a file.
        Default ``False``.

    bits:
        Length of the private key in bits. Default 2048

    passphrase:
        Passphrase for encryting the private key

    cipher:
        Cipher for encrypting the private key. Has no effect if passhprase is None.

    verbose:
        Provide visual feedback on stdout. Default True

        .. versionadded:: 2016.11.0

    CLI Example:

    .. code-block:: bash

        salt '*' x509.create_private_key path=/etc/pki/mykey.key
    """
    if not path and not text:
        raise salt.exceptions.SaltInvocationError(
            "Either path or text must be specified."
        )
    if path and text:
        raise salt.exceptions.SaltInvocationError(
            "Either path or text must be specified, not both."
        )

    if verbose:
        _callback_func = M2Crypto.RSA.keygen_callback
    else:
        _callback_func = _keygen_callback

    # pylint: disable=no-member
    rsa = M2Crypto.RSA.gen_key(bits, M2Crypto.m2.RSA_F4, _callback_func)
    # pylint: enable=no-member
    bio = M2Crypto.BIO.MemoryBuffer()
    if passphrase is None:
        cipher = None
    rsa.save_key_bio(bio, cipher=cipher, callback=_passphrase_callback(passphrase))

    if path:
        return write_pem(
            text=bio.read_all(), path=path, pem_type="(?:RSA )?PRIVATE KEY"
        )
    else:
        return bio.read_all()