use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class GatewayServicesContextListener method contextInitialized.
@Override
public void contextInitialized(ServletContextEvent sce) {
GatewayServices gs = GatewayServer.getGatewayServices();
sce.getServletContext().setAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE, gs);
String topologyName = (String) sce.getServletContext().getAttribute("org.apache.knox.gateway.gateway.cluster");
TopologyService ts = gs.getService(GatewayServices.TOPOLOGY_SERVICE);
Topology topology = getTopology(ts, topologyName);
sce.getServletContext().setAttribute("org.apache.knox.gateway.topology", topology);
}
use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class ServiceTestResource method serviceTest.
@GET
@Produces({ APPLICATION_XML, APPLICATION_JSON })
public ServiceTestWrapper serviceTest(@QueryParam("username") String username, @QueryParam("password") String password) {
List<ServiceTest> tests = new ArrayList<>();
List<String> messages = new ArrayList<>();
String authString;
GatewayConfig config = (GatewayConfig) request.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
SSLContext ctx = null;
CloseableHttpClient client;
String id = getTopologyName();
Topology topology = getTopology(id);
// Create Authorization String
if (username != null && password != null) {
authString = "Basic " + Base64.encodeAsString((username + ":" + password).getBytes());
} else if (request.getHeader("Authorization") != null) {
authString = request.getHeader("Authorization");
} else {
authString = null;
}
// Attempt to build SSL context for HTTP client.
try {
ctx = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
} catch (Exception e) {
messages.add(e.getMessage());
}
// Initialize the HTTP client
if (ctx == null) {
client = HttpClients.createDefault();
} else {
client = HttpClients.custom().setSslcontext(ctx).build();
}
if (topology != null) {
for (Service s : topology.getServices()) {
List<String> urls = getServiceTestURLs(config, s.getRole(), topology);
// Make sure we handle a case where no URLs are found.
if (urls.size() <= 0) {
ServiceTest test = new ServiceTest(s);
test.setMessage("This service did not contain any test URLs");
}
for (String url : urls) {
HttpGet req = new HttpGet();
ServiceTest test = new ServiceTest(s, url);
if (authString != null) {
req.setHeader("Authorization", authString);
} else {
messages.add("No credentials provided. Expect HTTP 401 responses.");
}
try {
req.setURI(new URIBuilder(url).build());
CloseableHttpResponse res = client.execute(req);
String contentLength = "Content-Length:" + res.getEntity().getContentLength();
String contentType = (res.getEntity().getContentType() != null) ? res.getEntity().getContentType().toString() : "No-contenttype";
test.setResponseContent(contentLength + "," + contentType);
test.setHttpCode(res.getStatusLine().getStatusCode());
res.close();
} catch (IOException e) {
messages.add("Exception: " + e.getMessage());
test.setMessage(e.getMessage());
} catch (URISyntaxException e) {
test.setMessage(e.getMessage());
} catch (Exception e) {
messages.add(e.getMessage());
test.setMessage(e.getMessage());
} finally {
req.releaseConnection();
tests.add(test);
}
}
}
} else {
messages.add("Topology " + id + " not found");
}
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
ServiceTestWrapper stw = new ServiceTestWrapper();
stw.setTests(tests);
stw.setMessages(messages);
return stw;
}
use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class GatewayAdminTopologyFuncTest method testDeployTopology.
@Test(timeout = TestUtils.LONG_TIMEOUT)
public void testDeployTopology() throws Exception {
LOG_ENTER();
Topology testTopology = createTestTopology();
String user = "guest";
String password = "guest-password";
String url = gatewayUrl + "/" + testTopology.getName() + "/test-service-path/test-service-resource";
GatewayServices srvs = GatewayServer.getGatewayServices();
TopologyService ts = srvs.getService(GatewayServices.TOPOLOGY_SERVICE);
try {
ts.stopMonitor();
assertThat(testTopology, not(nullValue()));
assertThat(testTopology.getName(), is("test-topology"));
given().auth().preemptive().basic("admin", "admin-password").header("Accept", MediaType.APPLICATION_JSON).then().statusCode(HttpStatus.SC_OK).body(containsString("ServerVersion")).when().get(gatewayUrl + "/admin/api/v1/version");
given().auth().preemptive().basic(user, password).then().statusCode(HttpStatus.SC_NOT_FOUND).when().get(url);
ts.deployTopology(testTopology);
given().auth().preemptive().basic(user, password).then().statusCode(HttpStatus.SC_OK).contentType("text/plain").body(is("test-service-response")).when().get(url).getBody();
ts.deleteTopology(testTopology);
given().auth().preemptive().basic(user, password).then().statusCode(HttpStatus.SC_NOT_FOUND).when().get(url);
} finally {
ts.startMonitor();
}
LOG_EXIT();
}
use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class GatewayAdminTopologyFuncTest method createTestTopology.
private Topology createTestTopology() {
Topology topology = new Topology();
topology.setName("test-topology");
try {
topology.setUri(new URI(gatewayUrl + "/" + topology.getName()));
} catch (URISyntaxException ex) {
assertThat(topology.getUri(), not(nullValue()));
}
Provider identityProvider = new Provider();
identityProvider.setName("Default");
identityProvider.setRole("identity-assertion");
identityProvider.setEnabled(true);
Provider AuthenicationProvider = new Provider();
AuthenicationProvider.setName("ShiroProvider");
AuthenicationProvider.setRole("authentication");
AuthenicationProvider.setEnabled(true);
Param ldapMain = new Param();
ldapMain.setName("main.ldapRealm");
ldapMain.setValue("org.apache.knox.gateway.shirorealm.KnoxLdapRealm");
Param ldapGroupContextFactory = new Param();
ldapGroupContextFactory.setName("main.ldapGroupContextFactory");
ldapGroupContextFactory.setValue("org.apache.knox.gateway.shirorealm.KnoxLdapContextFactory");
Param ldapRealmContext = new Param();
ldapRealmContext.setName("main.ldapRealm.contextFactory");
ldapRealmContext.setValue("$ldapGroupContextFactory");
Param ldapURL = new Param();
ldapURL.setName("main.ldapRealm.contextFactory.url");
ldapURL.setValue(driver.getLdapUrl());
Param ldapUserTemplate = new Param();
ldapUserTemplate.setName("main.ldapRealm.userDnTemplate");
ldapUserTemplate.setValue("uid={0},ou=people,dc=hadoop,dc=apache,dc=org");
Param authcBasic = new Param();
authcBasic.setName("urls./**");
authcBasic.setValue("authcBasic");
AuthenicationProvider.addParam(ldapGroupContextFactory);
AuthenicationProvider.addParam(ldapMain);
AuthenicationProvider.addParam(ldapRealmContext);
AuthenicationProvider.addParam(ldapURL);
AuthenicationProvider.addParam(ldapUserTemplate);
AuthenicationProvider.addParam(authcBasic);
Service testService = new Service();
testService.setRole("test-service-role");
topology.addProvider(AuthenicationProvider);
topology.addProvider(identityProvider);
topology.addService(testService);
topology.setTimestamp(System.nanoTime());
return topology;
}
use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class GatewayAdminTopologyFuncTest method testDeleteTopology.
@Test(timeout = TestUtils.LONG_TIMEOUT)
public void testDeleteTopology() throws ClassNotFoundException {
LOG_ENTER();
Topology test = createTestTopology();
String username = "admin";
String password = "admin-password";
String url = clusterUrl + "/api/v1/topologies/" + test.getName();
GatewayServices gs = GatewayServer.getGatewayServices();
TopologyService ts = gs.getService(GatewayServices.TOPOLOGY_SERVICE);
ts.deployTopology(test);
given().auth().preemptive().basic(username, password).header("Accept", MediaType.APPLICATION_JSON).then().statusCode(HttpStatus.SC_OK).contentType(MediaType.APPLICATION_JSON).when().get(url);
given().auth().preemptive().basic(username, password).then().statusCode(HttpStatus.SC_OK).contentType(MediaType.APPLICATION_JSON).when().delete(url);
given().auth().preemptive().basic(username, password).then().statusCode(HttpStatus.SC_NO_CONTENT).when().get(url);
LOG_EXIT();
}
Aggregations