Install a p12 certificate file into the macOS keychain

    name
        The certificate to install

    password
        The password for the certificate being installed formatted in the way
        described for openssl command in the PASS PHRASE ARGUMENTS section

    keychain
        The keychain to install the certificate to, this defaults to
        /Library/Keychains/System.keychain

    allow_any
        Allow any application to access the imported certificate without warning

    keychain_password
        If your keychain is likely to be locked pass the password and it will be unlocked
        before running the import

def installed(name, password, keychain="/Library/Keychains/System.keychain", **kwargs):
    '''
    Install a p12 certificate file into the macOS keychain

    name
        The certificate to install

    password
        The password for the certificate being installed formatted in the way
        described for openssl command in the PASS PHRASE ARGUMENTS section

    keychain
        The keychain to install the certificate to, this defaults to
        /Library/Keychains/System.keychain

    allow_any
        Allow any application to access the imported certificate without warning

    keychain_password
        If your keychain is likely to be locked pass the password and it will be unlocked
        before running the import

    '''
    ret = {'name': name,
           'result': True,
           'comment': '',
           'changes': {}}

    if 'http' in name or 'salt' in name:
        name = __salt__['cp.cache_file'](name)

    certs = __salt__['keychain.list_certs'](keychain)
    friendly_name = __salt__['keychain.get_friendly_name'](name, password)

    if friendly_name in certs:
        file_hash = __salt__['keychain.get_hash'](name, password)
        keychain_hash = __salt__['keychain.get_hash'](friendly_name)

        if file_hash != keychain_hash:
            out = __salt__['keychain.uninstall'](friendly_name, keychain,
                                                 keychain_password=kwargs.get('keychain_password'))
            if "unable" not in out:
                ret['comment'] += "Found a certificate with the same name but different hash, removing it.\n"
                ret['changes']['uninstalled'] = friendly_name

                # Reset the certs found
                certs = __salt__['keychain.list_certs'](keychain)
            else:
                ret['result'] = False
                ret['comment'] += "Found an incorrect cert but was unable to uninstall it: {0}".format(friendly_name)
                return ret

    if friendly_name not in certs:
        out = __salt__['keychain.install'](name, password, keychain, **kwargs)
        if "imported" in out:
            ret['changes']['installed'] = friendly_name
        else:
            ret['result'] = False
            ret['comment'] += "Failed to install {0}".format(friendly_name)
    else:
        ret['comment'] += "{0} already installed.".format(friendly_name)

    return ret