// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.athenz.tls;

import com.yahoo.vespa.athenz.api.AthenzIdentity;
import com.yahoo.vespa.athenz.api.AthenzRole;
import com.yahoo.vespa.athenz.utils.AthenzIdentities;

import java.security.cert.X509Certificate;
import java.util.List;

import static com.yahoo.security.SubjectAlternativeName.Type.RFC822_NAME;

/**
 * Utility methods for Athenz issued x509 certificates
 *
 * @author bjorncs
 */
public class AthenzX509CertificateUtils {

    private static final String COMMON_NAME_ROLE_DELIMITER = ":role.";

    private AthenzX509CertificateUtils() {}

    public static boolean isAthenzRoleCertificate(X509Certificate certificate) {
        return isAthenzIssuedCertificate(certificate) &&
                com.yahoo.security.X509CertificateUtils.getSubjectCommonNames(certificate).get(0).contains(COMMON_NAME_ROLE_DELIMITER);
    }

    public static boolean isAthenzIssuedCertificate(X509Certificate certificate) {
        return com.yahoo.security.X509CertificateUtils.getIssuerCommonNames(certificate).stream()
                .anyMatch(cn -> cn.equalsIgnoreCase("Yahoo Athenz CA") || cn.equalsIgnoreCase("Athenz AWS CA"));
    }

    public static AthenzIdentity getIdentityFromRoleCertificate(X509Certificate certificate) {
        List<com.yahoo.security.SubjectAlternativeName> sans = com.yahoo.security.X509CertificateUtils.getSubjectAlternativeNames(certificate);
        return sans.stream()
                .filter(san -> san.getType() == RFC822_NAME)
                .map(com.yahoo.security.SubjectAlternativeName::getValue)
                .map(AthenzX509CertificateUtils::getIdentityFromSanEmail)
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("Could not find identity in SAN: " + sans));
    }

    public static AthenzRole getRolesFromRoleCertificate(X509Certificate certificate) {
        String commonName = com.yahoo.security.X509CertificateUtils.getSubjectCommonNames(certificate).get(0);
        return AthenzRole.fromString(commonName);
    }

    private static AthenzIdentity getIdentityFromSanEmail(String email) {
        int separator = email.indexOf('@');
        if (separator == -1) throw new IllegalArgumentException("Invalid SAN email: " + email);
        return AthenzIdentities.from(email.substring(0, separator));
    }

}

@@ -586,692 +586,38 @@
 ate 
-static final String COMMON_NAME_ROLE_DELIMITER = %22:role.%22;%0A%0A    private AthenzX509CertificateUtils() %7B%7D%0A%0A    public static boolean isAthenzRoleCertificate(X509Certificate certificate) %7B%0A        return isAthenzIssuedCertificate(certificate) &&%0A                com.yahoo.security.X509CertificateUtils.getSubjectCommonNames(certificate).get(0).contains(COMMON_NAME_ROLE_DELIMITER);%0A    %7D%0A%0A    public static boolean isAthenzIssuedCertificate(X509Certificate certificate) %7B%0A        return com.yahoo.security.X509CertificateUtils.getIssuerCommonNames(certificate).stream()%0A                .anyMatch(cn -%3E cn.equalsIgnoreCase(%22Yahoo Athenz CA%22) %7C%7C cn.equalsIgnoreCase(%22Athenz AWS CA%22));%0A    
+AthenzX509CertificateUtils() %7B
 %7D%0A%0A 

Remove unused methods