AthenzUtils { public static String extractServicePrincipal(X509Certificate x509Cert) { String principal = Crypto.extractX509CertCommonName(x509Cert); if (principal == null) { return null; } if (principal.contains(AuthorityConsts.ROLE_SEP)) { final List<String> emails = Crypto.extractX509CertEmails(x509Cert); if (emails.size() != 1) { return null; } final String email = emails.get(0); int idx = email.indexOf('@'); if (idx == -1) { return null; } principal = email.substring(0, idx); } return principal; } private  AthenzUtils(); static String extractServicePrincipal(X509Certificate x509Cert); static boolean isRoleCertificate(X509Certificate x509Cert); static String extractRoleName(final String roleName); static String extractRoleDomainName(final String roleName); static String extractPrincipalDomainName(final String principalName); static String extractPrincipalServiceName(final String principalName); static String[] splitPrincipalName(String name); static String getPrincipalName(String domain, String service); static List<String> splitCommaSeperatedSystemProperty(String property);  }
@Test public void testExtractServicePrincipal() throws Exception { try (InputStream inStream = new FileInputStream("src/test/resources/x509_altnames_singleip.cert")) { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream); assertEquals("athenz.production", AthenzUtils.extractServicePrincipal(cert)); } try (InputStream inStream = new FileInputStream("src/test/resources/ec_public_x509.cert")) { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream); assertEquals("athenz.syncer", AthenzUtils.extractServicePrincipal(cert)); } }
@Test public void testExtractServicePrincipalRoleCert() throws Exception { try (InputStream inStream = new FileInputStream("src/test/resources/valid_email_x509.cert")) { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream); assertEquals("athens.zts", AthenzUtils.extractServicePrincipal(cert)); } }
@Test public void testExtractServicePrincipalNoCn() throws Exception { try (InputStream inStream = new FileInputStream("src/test/resources/no_cn_x509.cert")) { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream); assertNull(AthenzUtils.extractServicePrincipal(cert)); } }
@Test public void testExtractServicePrincipalInvalidEmailCount() throws Exception { try (InputStream inStream = new FileInputStream("src/test/resources/no_email_x509.cert")) { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream); assertNull(AthenzUtils.extractServicePrincipal(cert)); } try (InputStream inStream = new FileInputStream("src/test/resources/multiple_email_x509.cert")) { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream); assertNull(AthenzUtils.extractServicePrincipal(cert)); } }
@Test public void testExtractServicePrincipalInvalidEmailFormat() throws Exception { try (InputStream inStream = new FileInputStream("src/test/resources/invalid_email_x509.cert")) { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream); assertNull(AthenzUtils.extractServicePrincipal(cert)); } }