use of org.apache.knox.gateway.services.config.client.RemoteConfigurationRegistryClient in project knox by apache.
the class RemoteConfigurationRegistryClientServiceTest method testMultipleUnsecuredZooKeeperWithSimpleRegistryConfig.
/**
* Test multiple configurations for an unsecured remote registry.
*/
@Test
public void testMultipleUnsecuredZooKeeperWithSimpleRegistryConfig() throws Exception {
final String REGISTRY_CLIENT_NAME_1 = "zkclient1";
final String REGISTRY_CLIENT_NAME_2 = "zkclient2";
final String PRINCIPAL = null;
final String PWD = null;
final String CRED_ALIAS = null;
// Configure and start a secure ZK cluster
TestingCluster zkCluster = setupAndStartSecureTestZooKeeper(PRINCIPAL, PWD);
try {
// Create the setup client for the test cluster, and initialize the test znodes
CuratorFramework setupClient = initializeTestClientAndZNodes(zkCluster, PRINCIPAL);
// Mock configuration
GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
final String registryConfigValue1 = GatewayConfig.REMOTE_CONFIG_REGISTRY_TYPE + "=" + ZooKeeperClientService.TYPE + ";" + GatewayConfig.REMOTE_CONFIG_REGISTRY_ADDRESS + "=" + zkCluster.getConnectString();
EasyMock.expect(config.getRemoteRegistryConfiguration(REGISTRY_CLIENT_NAME_1)).andReturn(registryConfigValue1).anyTimes();
final String registryConfigValue2 = GatewayConfig.REMOTE_CONFIG_REGISTRY_TYPE + "=" + ZooKeeperClientService.TYPE + ";" + GatewayConfig.REMOTE_CONFIG_REGISTRY_ADDRESS + "=" + zkCluster.getConnectString();
EasyMock.expect(config.getRemoteRegistryConfiguration(REGISTRY_CLIENT_NAME_2)).andReturn(registryConfigValue2).anyTimes();
EasyMock.expect(config.getRemoteRegistryConfigurationNames()).andReturn(Arrays.asList(REGISTRY_CLIENT_NAME_1, REGISTRY_CLIENT_NAME_2)).anyTimes();
EasyMock.replay(config);
// Create the client service instance
RemoteConfigurationRegistryClientService clientService = RemoteConfigurationRegistryClientServiceFactory.newInstance(config);
assertEquals("Wrong registry client service type.", clientService.getClass(), CuratorClientService.class);
clientService.setAliasService(null);
clientService.init(config, null);
clientService.start();
RemoteConfigurationRegistryClient client1 = clientService.get(REGISTRY_CLIENT_NAME_1);
assertNotNull(client1);
RemoteConfigurationRegistryClient client2 = clientService.get(REGISTRY_CLIENT_NAME_2);
assertNotNull(client2);
doTestZooKeeperClient(setupClient, REGISTRY_CLIENT_NAME_1, clientService, false);
doTestZooKeeperClient(setupClient, REGISTRY_CLIENT_NAME_2, clientService, false);
} finally {
zkCluster.stop();
}
}
use of org.apache.knox.gateway.services.config.client.RemoteConfigurationRegistryClient in project knox by apache.
the class RemoteConfigurationRegistryClientServiceTest method doTestZooKeeperClient.
/**
* Test secure ZooKeeper client interactions.
*
* @param setupClient The client used for interacting with ZooKeeper independent from the registry client service.
* @param testClientName The name of the client to use from the registry client service.
* @param clientService The RemoteConfigurationRegistryClientService
* @param isSecureTest Flag to indicate whether this is a secure interaction test
*/
private void doTestZooKeeperClient(final CuratorFramework setupClient, final String testClientName, final RemoteConfigurationRegistryClientService clientService, boolean isSecureTest) throws Exception {
RemoteConfigurationRegistryClient client = clientService.get(testClientName);
assertNotNull(client);
List<String> descriptors = client.listChildEntries("/knox/config/descriptors");
assertNotNull(descriptors);
for (String descriptor : descriptors) {
System.out.println("Descriptor: " + descriptor);
}
List<String> providerConfigs = client.listChildEntries("/knox/config/shared-providers");
assertNotNull(providerConfigs);
for (String providerConfig : providerConfigs) {
System.out.println("Provider config: " + providerConfig);
}
List<String> someotherConfig = client.listChildEntries("/someotherconfig");
if (isSecureTest) {
assertNull("Expected null because of the ACL mismatch.", someotherConfig);
} else {
assertNotNull(someotherConfig);
}
// Test listeners
final String MY_NEW_ZNODE = "/clientServiceTestNode";
final String MY_NEW_DATA_ZNODE = MY_NEW_ZNODE + "/mydata";
if (setupClient.checkExists().forPath(MY_NEW_ZNODE) != null) {
setupClient.delete().deletingChildrenIfNeeded().forPath(MY_NEW_ZNODE);
}
final List<String> listenerLog = new ArrayList<>();
client.addChildEntryListener(MY_NEW_ZNODE, (c, type, path) -> {
listenerLog.add("EXTERNAL: " + type.toString() + ":" + path);
if (RemoteConfigurationRegistryClient.ChildEntryListener.Type.ADDED.equals(type)) {
try {
c.addEntryListener(path, (cc, p, d) -> listenerLog.add("EXTERNAL: " + p + ":" + (d != null ? new String(d) : "null")));
} catch (Exception e) {
e.printStackTrace();
}
}
});
client.createEntry(MY_NEW_ZNODE);
client.createEntry(MY_NEW_DATA_ZNODE, "more test data");
String testData = client.getEntryData(MY_NEW_DATA_ZNODE);
assertNotNull(testData);
assertEquals("more test data", testData);
assertTrue(client.entryExists(MY_NEW_DATA_ZNODE));
client.setEntryData(MY_NEW_DATA_ZNODE, "still more data");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//
}
client.setEntryData(MY_NEW_DATA_ZNODE, "changed completely");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//
}
client.deleteEntry(MY_NEW_DATA_ZNODE);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//
}
assertFalse(listenerLog.isEmpty());
}
use of org.apache.knox.gateway.services.config.client.RemoteConfigurationRegistryClient in project knox by apache.
the class DefaultTopologyService method deployProviderConfiguration.
@Override
public boolean deployProviderConfiguration(String name, String content) {
boolean result;
// Whether the remote configuration registry is being employed or not, write the file locally
result = writeConfig(sharedProvidersDirectory, name, content);
// If the remote configuration registry is being employed, persist it there also
if (remoteMonitor != null) {
RemoteConfigurationRegistryClient client = remoteMonitor.getClient();
if (client != null) {
String entryPath = "/knox/config/shared-providers/" + name;
client.createEntry(entryPath, content);
result = (client.getEntryData(entryPath) != null);
}
}
return result;
}
use of org.apache.knox.gateway.services.config.client.RemoteConfigurationRegistryClient in project knox by apache.
the class DefaultTopologyService method deleteRemoteEntry.
/**
* Delete the entry in the remote configuration registry, which matches the specified resource name.
*
* @param entryParent The remote registry path in which the entry exists.
* @param name The name of the entry (typically without any file extension).
*
* @return true, if the entry is deleted, or did not exist; otherwise, false.
*/
private boolean deleteRemoteEntry(String entryParent, String name) {
boolean result = true;
if (remoteMonitor != null) {
RemoteConfigurationRegistryClient client = remoteMonitor.getClient();
if (client != null) {
List<String> existingProviderConfigs = client.listChildEntries(entryParent);
for (String entryName : existingProviderConfigs) {
if (FilenameUtils.getBaseName(entryName).equals(name)) {
String entryPath = entryParent + "/" + entryName;
client.deleteEntry(entryPath);
result = !client.entryExists(entryPath);
if (!result) {
log.failedToDeletedRemoteConfigFile("descriptor", name);
}
break;
}
}
}
}
return result;
}
Aggregations