# Buggy code
def deploy_ssh_pubkey(self, username, pubkey):
    """
    Deploy authorized_key
    """
    path, thumbprint, value = pubkey
    if path is None:
        raise OSUtilError("Publich key path is None")

    crytputil = CryptUtil(conf.get_openssl_cmd())

    path = self._norm_path(path)
    dir_path = os.path.dirname(path)
    fileutil.mkdir(dir_path, mode=0o700, owner=username)
    if value is not None:
        if not value.startswith("ssh-"):
            raise OSUtilError("Bad public key: {0}".format(value))
        fileutil.write_file(path, value)
    elif thumbprint is not None:
        lib_dir = conf.get_lib_dir()
        crt_path = os.path.join(lib_dir, thumbprint + ".crt")
        if not os.path.isfile(crt_path):
            raise OSUtilError("Can't find {0}.crt".format(thumbprint))
        pub_path = os.path.join(lib_dir, thumbprint + ".pub")
        pub = crytputil.get_pubkey_from_crt(crt_path)
        fileutil.write_file(pub_path, pub)
        self.set_selinux_context(pub_path, "unconfined_u:object_r:ssh_home_t:s0")
        self.openssl_to_openssh(pub_path, path)
        fileutil.chmod(pub_path, 0o600)
    else:
        raise OSUtilError("SSH public key Fingerprint and Value are None")

    self.set_selinux_context(path, "unconfined_u:object_r:ssh_home_t:s0")
    fileutil.chowner(path, username)
    fileutil.chmod(path, 0o644)


# Traceback (CalledProcessError)
2015/08/19 12:35:10 Created user account: azureuser
2015/08/19 12:35:10 Deploy public key:28EC0E2C8F27A2A0D081742A97D517CBD1FFFFFF
2015/08/19 12:35:10 ERROR:Failed: 28EC0E2C8F27A2A0D081742A97D517CBD1FFFFFF.pub.crt -> /home/azureuser/.ssh/authorized_keys
2015/08/19 12:35:10 ERROR:CalledProcessError.  Error Code is 1
2015/08/19 12:35:10 ERROR:CalledProcessError.  Command string was chcon unconfined_u:object_r:ssh_home_t:s0 /home/azureuser/.ssh/authorized_keys
2015/08/19 12:35:10 ERROR:CalledProcessError.  Command result was chcon: cannot access /home/azureuser/.ssh/authorized_keys: No such file or directory
2015/08/19 12:35:10 ERROR:Traceback (most recent call last):
2015/08/19 12:35:10 ERROR:  File "/usr/sbin/waagent", line 6048, in main
2015/08/19 12:35:10 ERROR:    WaAgent.Run()
2015/08/19 12:35:10 ERROR:  File "/usr/sbin/waagent", line 5571, in Run
2015/08/19 12:35:10 ERROR:    provisionError = self.Provision()
2015/08/19 12:35:10 ERROR:  File "/usr/sbin/waagent", line 5457, in Provision
2015/08/19 12:35:10 ERROR:    error = ovfobj.Process()
2015/08/19 12:35:10 ERROR:  File "/usr/sbin/waagent", line 4566, in Process
2015/08/19 12:35:10 ERROR:    ChangeOwner(path, self.UserName)
2015/08/19 12:35:10 ERROR:  File "/usr/sbin/waagent", line 2242, in ChangeOwner
2015/08/19 12:35:10 ERROR:    os.chown(filepath, p[2], p[3])
2015/08/19 12:35:10 ERROR:OSError: [Errno 2] No such file or directory: '/home/azureuser/.ssh/authorized_keys'
2015/08/19 12:35:10 ERROR:
2015/08/19 12:35:10 ERROR:Exception: [Errno 2] No such file or directory: '/home/azureuser/.ssh/authorized_keys'

# Fixed code
def deploy_ssh_pubkey(self, username, pubkey):
    """
    Deploy authorized_key
    """
    path, thumbprint, value = pubkey
    if path is None:
        raise OSUtilError("Public key path is None")

    crytputil = CryptUtil(conf.get_openssl_cmd())

    path = self._norm_path(path)
    dir_path = os.path.dirname(path)
    fileutil.mkdir(dir_path, mode=0o700, owner=username)
    if value is not None:
        if not value.startswith("ssh-"):
            raise OSUtilError("Bad public key: {0}".format(value))
        fileutil.write_file(path, value)
    elif thumbprint is not None:
        lib_dir = conf.get_lib_dir()
        crt_path = os.path.join(lib_dir, thumbprint + ".crt")
        if not os.path.isfile(crt_path):
            raise OSUtilError("Can't find {0}.crt".format(thumbprint))
        pub_path = os.path.join(lib_dir, thumbprint + ".pub")
        pub = crytputil.get_pubkey_from_crt(crt_path)
        fileutil.write_file(pub_path, pub)
        self.set_selinux_context(pub_path, "unconfined_u:object_r:ssh_home_t:s0")
        self.openssl_to_openssh(pub_path, path)
        fileutil.chmod(pub_path, 0o600)
    else:
        raise OSUtilError("SSH public key Fingerprint and Value are None")

    self.set_selinux_context(path, "unconfined_u:object_r:ssh_home_t:s0")
    fileutil.chowner(path, username)
    fileutil.chmod(path, 0o644)