InstanceProviderManager { InstanceProvider getProvider(String provider, HostnameResolver hostnameResolver) { int idx = provider.lastIndexOf('.'); if (idx == -1) { LOGGER.error("getProviderClient: Invalid provider service name: {}", provider); return null; } final String domainName = provider.substring(0, idx); DataCache dataCache = dataStore.getDataCache(domainName); if (dataCache == null) { LOGGER.error("getProviderClient: Unknown domain: {}", domainName); return null; } String providerEndpoint = null; boolean validProviderName = false; List<com.yahoo.athenz.zms.ServiceIdentity> services = dataCache.getDomainData().getServices(); if (services == null) { LOGGER.error("getProviderClient: Unknown provider servicee: {}", provider); return null; } for (com.yahoo.athenz.zms.ServiceIdentity service : services) { if (service.getName().equals(provider)) { providerEndpoint = service.getProviderEndpoint(); validProviderName = true; break; } } if (providerEndpoint == null || providerEndpoint.isEmpty()) { if (validProviderName) { LOGGER.error("getProviderClient: Unknown provider service name: {}", provider); } else { LOGGER.error("getProviderClient: Provider service {} does not have endpoint defined", provider); } return null; } InstanceProvider instanceProvider = null; URI uri; try { uri = new URI(providerEndpoint); } catch (URISyntaxException ex) { LOGGER.error("getProviderClient: Unable to parse {}: {}", providerEndpoint, ex.getMessage()); return null; } ProviderScheme schemeType = getProviderEndpointScheme(uri); switch (schemeType) { case HTTPS: instanceProvider = new InstanceHttpProvider(); instanceProvider.initialize(provider, providerEndpoint, sslContext, keyStore); break; case CLASS: instanceProvider = getClassProvider(uri.getHost(), provider, hostnameResolver); break; default: break; } return instanceProvider; } InstanceProviderManager(DataStore dataStore, SSLContext sslContext, KeyStore keyStore);   }
@Test public void testGetHttpsProviderInvalidName() { SignedDomain signedDomain = createSignedDomainHttpsEndpoint("coretech", "weather", true, true); store.processDomain(signedDomain, false); InstanceProviderManager provider = new InstanceProviderManager(store, null, null); InstanceProvider client = provider.getProvider("coretech", null); assertNull(client); }
@Test public void testGetHttpsProvider() throws NoSuchAlgorithmException { SignedDomain signedDomain = createSignedDomainHttpsEndpoint("coretech", "weather", true, true); store.processDomain(signedDomain, false); InstanceProviderManager provider = new InstanceProviderManager(store, SSLContext.getDefault(), null); InstanceProvider client = provider.getProvider("coretech.weather", null); assertNotNull(client); client.close(); }
@Test public void testGetHttpsProviderUnknownProvider() throws NoSuchAlgorithmException { SignedDomain signedDomain = createSignedDomainHttpsEndpoint("coretech", "weather", true, true); store.processDomain(signedDomain, false); InstanceProviderManager provider = new InstanceProviderManager(store, SSLContext.getDefault(), null); InstanceProvider client = provider.getProvider("coretech.weather2", null); assertNull(client); }
@Test public void testGetClassProviderException() { SignedDomain signedDomain = createSignedDomainClassEndpoint("coretech", "weather", true, true); store.processDomain(signedDomain, false); System.setProperty("athenz.instance.test.class.exception", "true"); InstanceProviderManager provider = new InstanceProviderManager(store, null, null); InstanceProvider client = provider.getProvider("coretech.weather", new HostnameResolver(){}); assertNull(client); System.clearProperty("athenz.instance.test.class.exception"); }
@Test public void testGetProviderClientInvalidDomain() { SignedDomain signedDomain = createSignedDomainHttpsEndpoint("coretech", "weather", true, true); store.processDomain(signedDomain, false); InstanceProviderManager provider = new InstanceProviderManager(store, null, null); InstanceProvider client = provider.getProvider("coretech2.weather", null); assertNull(client); }
@Test public void testGetProviderClientInvalidService() { SignedDomain signedDomain = createSignedDomainHttpsEndpoint("coretech", "weather", true, true); store.processDomain(signedDomain, false); InstanceProviderManager provider = new InstanceProviderManager(store, null, null); InstanceProvider client = provider.getProvider("coretech.weather2", null); assertNull(client); }
@Test public void testGetProviderClientNoEndpoint() { SignedDomain signedDomain = createSignedDomainHttpsEndpoint("coretech", "weather", true, false); store.processDomain(signedDomain, false); InstanceProviderManager provider = new InstanceProviderManager(store, null, null); InstanceProvider client = provider.getProvider("coretech.weather", null); assertNull(client); }
@Test public void testGetProviderClientInvalidEndpoint() { SignedDomain signedDomain = createSignedDomain("coretech", "weather", true, true, "http: store.processDomain(signedDomain, false); InstanceProviderManager provider = new InstanceProviderManager(store, null, null); InstanceProvider client = provider.getProvider("coretech.weather", null); assertNull(client); }
@Test public void testGetProviderClientInvalidEndpointParse() { SignedDomain signedDomain = createSignedDomain("coretech", "weather", true, true, ": store.processDomain(signedDomain, false); InstanceProviderManager provider = new InstanceProviderManager(store, null, null); InstanceProvider client = provider.getProvider("coretech.weather", null); assertNull(client); }
@Test public void testGetProviderClientNoServices() { SignedDomain signedDomain = createSignedDomainHttpsEndpoint("coretech", "weather", false, true); store.processDomain(signedDomain, false); InstanceProviderManager provider = new InstanceProviderManager(store, null, null); InstanceProvider client = provider.getProvider("coretech.weather", null); assertNull(client); }