# Buggy code
def get_public_key(key, passphrase=None, asObj=False):
    """
    Returns a string containing the public key in PEM format.

    key:
        A path or PEM encoded string containing a CSR, Certificate or
        Private Key from which a public key can be retrieved.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.get_public_key /etc/pki/mycert.cer
    """

    if isinstance(key, M2Crypto.X509.X509):
        rsa = key.get_pubkey().get_rsa()
        text = ""
    else:
        text = _text_or_file(key)
        text = get_pem_entry(text)

    if text.startswith("-----BEGIN PUBLIC KEY-----"):
        if not asObj:
            return text
        bio = M2Crypto.BIO.MemoryBuffer()
        bio.write(text)
        rsa = M2Crypto.RSA.load_pub_key_bio(bio)

    bio = M2Crypto.BIO.MemoryBuffer()
    if text.startswith("-----BEGIN CERTIFICATE-----"):
        cert = M2Crypto.X509.load_cert_string(text)
        rsa = cert.get_pubkey().get_rsa()
    if text.startswith("-----BEGIN CERTIFICATE REQUEST-----"):
        csr = M2Crypto.X509.load_request_string(text)
        rsa = csr.get_pubkey().get_rsa()
    if text.startswith("-----BEGIN PRIVATE KEY-----") or text.startswith(
        "-----BEGIN RSA PRIVATE KEY-----"
    ):
        rsa = M2Crypto.RSA.load_key_string(
            text, callback=_passphrase_callback(passphrase)
        )

    if asObj:
        evppubkey = M2Crypto.EVP.PKey()
        evppubkey.assign_rsa(rsa)
        return evppubkey

    rsa.save_pub_key_bio(bio)
    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 get_public_key(key, passphrase=None, asObj=False):
    """
    Returns a string containing the public key in PEM format.

    key:
        A path or PEM encoded string containing a CSR, Certificate or
        Private Key from which a public key can be retrieved.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.get_public_key /etc/pki/mycert.cer
    """

    if isinstance(key, M2Crypto.X509.X509):
        rsa = key.get_pubkey().get_rsa()
        text = b""
    else:
        text = _text_or_file(key)
        text = get_pem_entry(text)

    if text.startswith(b"-----BEGIN PUBLIC KEY-----"):
        if not asObj:
            return text
        bio = M2Crypto.BIO.MemoryBuffer()
        bio.write(text)
        rsa = M2Crypto.RSA.load_pub_key_bio(bio)

    bio = M2Crypto.BIO.MemoryBuffer()
    if text.startswith(b"-----BEGIN CERTIFICATE-----"):
        cert = M2Crypto.X509.load_cert_string(text)
        rsa = cert.get_pubkey().get_rsa()
    if text.startswith(b"-----BEGIN CERTIFICATE REQUEST-----"):
        csr = M2Crypto.X509.load_request_string(text)
        rsa = csr.get_pubkey().get_rsa()
    if text.startswith(b"-----BEGIN PRIVATE KEY-----") or text.startswith(
        b"-----BEGIN RSA PRIVATE KEY-----"
    ):
        rsa = M2Crypto.RSA.load_key_string(
            text, callback=_passphrase_callback(passphrase)
        )

    if asObj:
        evppubkey = M2Crypto.EVP.PKey()
        evppubkey.assign_rsa(rsa)
        return evppubkey

    rsa.save_pub_key_bio(bio)
    return bio.read_all()