# 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 (TypeError)
An exception occurred in this state: Traceback (most recent call last):
File "/root/salt/salt/state.py", line 1878, in call
**cdata['kwargs'])
File "/root/salt/salt/loader.py", line 1823, in wrapper
return f(*args, **kwargs)
File "/root/salt/salt/states/x509.py", line 311, in private_key_managed
ret = __states__['file.managed'](**file_args)
File "/root/salt/salt/loader.py", line 1823, in wrapper
return f(*args, **kwargs)
File "/root/salt/salt/states/file.py", line 2267, in managed
isinstance(use_contents, six.string_types) and '\0' in use_contents
TypeError: 'str' does not support the buffer interface

# 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 salt.utils.stringutils.to_str(bio.read_all())