InstanceZTSProvider implements InstanceProvider { boolean validateHostname(final String hostname, final String[] sanIps) { LOGGER.debug("Validating hostname: {}, sanIps: {}", hostname, sanIps); if (hostname == null || hostname.isEmpty()) { LOGGER.info("Request contains no hostname entry for validation"); if (sanIps != null && sanIps.length > 1) { LOGGER.error("SanIps:{} > 1, and hostname is empty", sanIps); return false; } return true; } if (sanIps == null || sanIps.length == 0) { LOGGER.error("Request contains no sanIp entry for hostname:{} validation", hostname); return false; } Set<String> hostIps = hostnameResolver.getAllByName(hostname); for (String sanIp: sanIps) { if (!hostIps.contains(sanIp)) { LOGGER.error("One of sanIp: {} is not present in HostIps: {}", hostIps, sanIps); return false; } } return true; }  @Override Scheme getProviderScheme(); @Override void initialize(String provider, String providerEndpoint, SSLContext sslContext,
            KeyStore keyStore); @Override void setHostnameResolver(HostnameResolver hostnameResolver); @Override InstanceConfirmation confirmInstance(InstanceConfirmation confirmation); @Override InstanceConfirmation refreshInstance(InstanceConfirmation confirmation); boolean validatePublicKeys(final String athenzPublicKey, final String csrPublicKey);  }
@Test public void testValidateHostname() { HostnameResolver hostnameResolver = Mockito.mock(HostnameResolver.class); Mockito.when(hostnameResolver.getAllByName("abc.athenz.com")) .thenReturn(new HashSet<String>(Arrays.asList("10.1.1.1", "2001:db8:a0b:12f0:0:0:0:1"))); InstanceZTSProvider provider = new InstanceZTSProvider(); provider.initialize("provider", "com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider", null, null); provider.setHostnameResolver(hostnameResolver); assertTrue(provider.validateHostname("abc.athenz.com", new String[]{"10.1.1.1"})); assertTrue(provider.validateHostname("abc.athenz.com", new String[]{"10.1.1.1", "2001:db8:a0b:12f0:0:0:0:1"})); assertFalse(provider.validateHostname("abc.athenz.com", new String[]{"10.1.1.2"})); assertFalse(provider.validateHostname("abc.athenz.com", new String[]{"10.1.1.1", "1:2:3:4:5:6:7:8"})); assertFalse(provider.validateHostname("abc.athenz.com", new String[]{"10.1.1.2", "1:2:3:4:5:6:7:8"})); assertFalse(provider.validateHostname("abc.athenz.com", null)); assertFalse(provider.validateHostname("abc.athenz.com", new String[]{})); assertFalse(provider.validateHostname("abc.athenz.com", new String[]{""})); assertTrue(provider.validateHostname("", new String[]{"10.1.1.1"})); assertTrue(provider.validateHostname(null, new String[]{"10.1.1.1"})); assertFalse(provider.validateHostname(null, new String[]{"10.1.1.1", "2001:db8:a0b:12f0:0:0:0:1"})); assertFalse(provider.validateHostname("", new String[]{"10.1.1.1", "2001:db8:a0b:12f0:0:0:0:1"})); provider.close(); }