ProviderHostnameVerifier implements HostnameVerifier { @Override public boolean verify(String hostname, SSLSession session) { Certificate[] certs = null; try { certs = session.getPeerCertificates(); } catch (SSLPeerUnverifiedException ignored) { } if (certs == null) { return false; } for (Certificate cert : certs) { final X509Certificate x509Cert = (X509Certificate) cert; if (serviceName.equals(Crypto.extractX509CertCommonName(x509Cert))) { return true; } } return false; } ProviderHostnameVerifier(String serviceName); @Override boolean verify(String hostname, SSLSession session);  }
@Test public void testHostnameVerifier() throws IOException { SSLSession session = Mockito.mock(SSLSession.class); Path path = Paths.get("src/test/resources/athenz.instanceid.pem"); String pem = new String(Files.readAllBytes(path)); X509Certificate cert = Crypto.loadX509Certificate(pem); Certificate[] certs = new Certificate[1]; certs[0] = cert; Mockito.when(session.getPeerCertificates()).thenReturn(certs); ProviderHostnameVerifier verifier1 = new ProviderHostnameVerifier("athenz.production"); assertTrue(verifier1.verify("athenz", session)); ProviderHostnameVerifier verifier2 = new ProviderHostnameVerifier("athenz.production2"); assertFalse(verifier2.verify("athenz", session)); }
@Test public void testHostnameVerifierNullCerts() throws IOException { SSLSession session = Mockito.mock(SSLSession.class); Mockito.when(session.getPeerCertificates()).thenReturn(null); ProviderHostnameVerifier verifier1 = new ProviderHostnameVerifier("athenz.production"); assertFalse(verifier1.verify("athenz", session)); }