use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class CryptoServiceTest method testCryptoServiceDES.
@Test
public void testCryptoServiceDES() throws Exception {
GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(config.getAlgorithm()).andReturn("DES");
EasyMock.expect(config.getPBEAlgorithm()).andReturn("PBKDF2WithHmacSHA1");
EasyMock.expect(config.getSaltSize()).andReturn("16");
EasyMock.expect(config.getIterationCount()).andReturn("65536");
EasyMock.expect(config.getKeyLength()).andReturn("128");
EasyMock.expect(config.getTransformation()).andReturn("DES");
EasyMock.replay(config);
// password to create key - same Encryptor
String queryString = "url=http://localhost:50070/api/v1/blahblah";
ConfigurableEncryptor aes0 = new ConfigurableEncryptor("password");
aes0.init(config);
cs.init(config, new HashMap<String, String>());
EncryptionResult result0 = cs.encryptForCluster("Test", "encrypt_url", queryString.getBytes("UTF8"));
byte[] decrypted0 = cs.decryptForCluster("Test", "encrypt_url", result0.cipher, result0.iv, result0.salt);
assertEquals(queryString, new String(decrypted0, "UTF8"));
assertEquals(queryString.getBytes("UTF8").length, decrypted0.length);
assertEquals(queryString.getBytes("UTF8").length, new String(decrypted0, "UTF8").toCharArray().length);
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class TopologiesResource method getTopology.
@GET
@Produces({ APPLICATION_JSON, APPLICATION_XML })
@Path(SINGLE_TOPOLOGY_API_PATH)
public Topology getTopology(@PathParam("id") String id) {
GatewayServices services = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
GatewayConfig config = (GatewayConfig) request.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
TopologyService ts = services.getService(GatewayServices.TOPOLOGY_SERVICE);
for (org.apache.knox.gateway.topology.Topology t : ts.getTopologies()) {
if (t.getName().equals(id)) {
try {
t.setUri(new URI(buildURI(t, config, request)));
} catch (URISyntaxException se) {
t.setUri(null);
}
return BeanConverter.getTopology(t);
}
}
return null;
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class TopologyResourceTest method testTopologyURLMethods.
@Test
public void testTopologyURLMethods() {
// Note: had to change method signature due to these tests. Changed methods to public and added
// HttpServletRequest argument.
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Host, host);
setMockRequestHeader(request, X_Forwarded_Port, port);
setMockRequestHeader(request, X_Forwarded_Proto, proto);
setMockRequestHeader(request, X_Forwarded_Context, fullContext);
setMockRequestHeader(request, X_Forwarded_Server, server);
Topology t = EasyMock.createNiceMock(Topology.class);
EasyMock.expect(t.getName()).andReturn(topologyName).anyTimes();
GatewayConfig conf = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(conf.getGatewayPath()).andReturn(gatewayPath).anyTimes();
EasyMock.replay(request, t, conf);
TopologiesResource res = new TopologiesResource();
String href = res.buildHref(t, request);
String uri = res.buildURI(t, conf, request);
assertThat(uri, containsString(proto));
assertThat(uri, containsString(host));
assertThat(uri, containsString(server));
assertThat(uri, containsString(port));
assertThat(uri, containsString(topologyName));
assert (uri.equals(expectedURI));
assertThat(href, containsString(proto));
assertThat(href, containsString(host));
assertThat(href, containsString(server));
assertThat(href, containsString(port));
assertThat(href, containsString(fullContext));
assert (href.equals(expectedHref));
// Test 2 - No Protocol Header
EasyMock.reset(request);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Host, host);
setMockRequestHeader(request, X_Forwarded_Port, port);
setMockRequestHeader(request, X_Forwarded_Context, fullContext);
setMockRequestHeader(request, X_Forwarded_Server, server);
EasyMock.replay(request);
String test2URI = expectedURI.replace(proto, reqProto);
String test2href = expectedHref.replace(proto, reqProto);
assert (res.buildURI(t, conf, request).equals(test2URI));
assert (res.buildHref(t, request).equals(test2href));
// Test 3 - No port in host Header
EasyMock.reset(request);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Host, server);
setMockRequestHeader(request, X_Forwarded_Port, port);
setMockRequestHeader(request, X_Forwarded_Context, fullContext);
setMockRequestHeader(request, X_Forwarded_Server, server);
setMockRequestHeader(request, X_Forwarded_Proto, proto);
EasyMock.replay(request);
assert (res.buildURI(t, conf, request).equals(expectedURI));
assert (res.buildHref(t, request).equals(expectedHref));
// Test 4 - server & no host Header
EasyMock.reset(request);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Server, server);
setMockRequestHeader(request, X_Forwarded_Port, port);
setMockRequestHeader(request, X_Forwarded_Context, fullContext);
setMockRequestHeader(request, X_Forwarded_Proto, proto);
EasyMock.replay(request);
assert (res.buildURI(t, conf, request).equals(expectedURI));
assert (res.buildHref(t, request).equals(expectedHref));
// Test 5 - no server & no host Header
EasyMock.reset(request);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Port, port);
setMockRequestHeader(request, X_Forwarded_Context, fullContext);
setMockRequestHeader(request, X_Forwarded_Proto, proto);
EasyMock.replay(request);
String test5URI = expectedURI.replace(server, reqServer);
String test5href = expectedHref.replace(server, reqServer);
assertEquals(res.buildURI(t, conf, request), test5URI);
assertEquals(res.buildHref(t, request), test5href);
// Test 6 - no port, no server & no host Header
EasyMock.reset(request);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Context, fullContext);
setMockRequestHeader(request, X_Forwarded_Proto, proto);
EasyMock.replay(request);
String test6URI = expectedURI.replace(server, reqServer).replace(port, reqPort);
String test6href = expectedHref.replace(server, reqServer).replace(port, reqPort);
assertEquals(res.buildURI(t, conf, request), (test6URI));
assertEquals(res.buildHref(t, request), (test6href));
// Test 7 - No Context
EasyMock.reset(request);
setDefaultExpectations(request);
setMockRequestHeader(request, X_Forwarded_Host, host);
setMockRequestHeader(request, X_Forwarded_Port, port);
setMockRequestHeader(request, X_Forwarded_Proto, proto);
setMockRequestHeader(request, X_Forwarded_Server, server);
EasyMock.replay(request);
String test7URI = expectedURI.replace(startContext, "");
String test7href = expectedHref.replace(startContext, "");
assertEquals(res.buildURI(t, conf, request), test7URI);
assertEquals(res.buildHref(t, request), test7href);
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class DefaultRemoteConfigurationRegistriesTest method mockGatewayConfig.
/**
* Create a mock GatewayConfig based on the specified test properties.
*
* @param testProperties The test properties to set on the config
*/
private GatewayConfig mockGatewayConfig(Map<String, Properties> testProperties) {
// Mock gateway config
GatewayConfig gc = EasyMock.createNiceMock(GatewayConfig.class);
List<String> configNames = new ArrayList<>();
for (String registryName : testProperties.keySet()) {
configNames.add(registryName);
String propertyValueString = "";
Properties props = testProperties.get(registryName);
Enumeration names = props.propertyNames();
while (names.hasMoreElements()) {
String propertyName = (String) names.nextElement();
propertyValueString += propertyName + "=" + props.get(propertyName);
if (names.hasMoreElements()) {
propertyValueString += ";";
}
}
EasyMock.expect(gc.getRemoteRegistryConfiguration(registryName)).andReturn(propertyValueString).anyTimes();
}
EasyMock.expect(gc.getRemoteRegistryConfigurationNames()).andReturn(configNames).anyTimes();
EasyMock.replay(gc);
return gc;
}
use of org.apache.knox.gateway.config.GatewayConfig 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();
}
}
Aggregations