X509CertUtils { public static String extractRequestInstanceId(X509Certificate cert) { if (cert == null) { return null; } final List<String> uriList = Crypto.extractX509CertURIs(cert); final String instanceId = extractRequestInstanceIdFromURI(uriList); if (instanceId != null) { return instanceId; } final List<String> dnsNames = Crypto.extractX509CertDnsNames(cert); return extractRequestInstanceIdFromDnsNames(dnsNames); }  static String extractRequestInstanceIdFromURI(final List<String> uriList); static String extractItemFromURI(final List<String> uriList, final String item); static String extractRequestInstanceIdFromDnsNames(final List<String> dnsNames); static String extractRequestInstanceId(X509Certificate cert); static void logCert(final Logger certLogger, final Principal principal,
                final String ip, final String provider, final String instanceId,
                final X509Certificate x509Cert); static void logSSH(final Logger certLogger, final Principal principal, final String ip,
            final String service, final String instanceId); static String logRecord(final Principal principal, final String ip, final String provider,
            final String instanceId, final X509Certificate x509Cert);  }
@Test public void testExtractRequestInstanceId() throws CertificateParsingException { assertNull(X509CertUtils.extractRequestInstanceId(null)); X509Certificate cert = Mockito.mock(X509Certificate.class); Collection<List<?>> dnsNames = new ArrayList<>(); ArrayList<Object> item1 = new ArrayList<>(); item1.add(2); item1.add("host1.domain.athenz"); dnsNames.add(item1); Mockito.when(cert.getSubjectAlternativeNames()).thenReturn(dnsNames); assertNull(X509CertUtils.extractRequestInstanceId(cert)); ArrayList<Object> item2 = new ArrayList<>(); item2.add(2); item2.add("instanceid1.instanceid.athenz.test"); dnsNames.add(item2); assertEquals("instanceid1", X509CertUtils.extractRequestInstanceId(cert)); }
@Test public void textExtractRequestInstanceIdURI() throws IOException { Path path = Paths.get("src/test/resources/athenz.instanceid.uri.pem"); String pem = new String(Files.readAllBytes(path)); X509Certificate cert = Crypto.loadX509Certificate(pem); assertEquals("id-001", X509CertUtils.extractRequestInstanceId(cert)); }