use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.
the class WebSSOResourceTest method testAudiences.
@Test
public void testAudiences() throws Exception {
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(context.getInitParameter("knoxsso.cookie.name")).andReturn(null);
EasyMock.expect(context.getInitParameter("knoxsso.cookie.secure.only")).andReturn(null);
EasyMock.expect(context.getInitParameter("knoxsso.cookie.max.age")).andReturn(null);
EasyMock.expect(context.getInitParameter("knoxsso.cookie.domain.suffix")).andReturn(null);
EasyMock.expect(context.getInitParameter("knoxsso.redirect.whitelist.regex")).andReturn(null);
EasyMock.expect(context.getInitParameter("knoxsso.token.audiences")).andReturn("recipient1,recipient2");
EasyMock.expect(context.getInitParameter("knoxsso.token.ttl")).andReturn(null);
EasyMock.expect(context.getInitParameter("knoxsso.enable.session")).andReturn(null);
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(request.getParameter("originalUrl")).andReturn("http://localhost:9080/service");
EasyMock.expect(request.getParameterMap()).andReturn(Collections.<String, String[]>emptyMap());
EasyMock.expect(request.getServletContext()).andReturn(context).anyTimes();
Principal principal = EasyMock.createNiceMock(Principal.class);
EasyMock.expect(principal.getName()).andReturn("alice").anyTimes();
EasyMock.expect(request.getUserPrincipal()).andReturn(principal).anyTimes();
GatewayServices services = EasyMock.createNiceMock(GatewayServices.class);
EasyMock.expect(context.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services);
JWTokenAuthority authority = new TestJWTokenAuthority(publicKey, privateKey);
EasyMock.expect(services.getService(GatewayServices.TOKEN_SERVICE)).andReturn(authority);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
ServletOutputStream outputStream = EasyMock.createNiceMock(ServletOutputStream.class);
CookieResponseWrapper responseWrapper = new CookieResponseWrapper(response, outputStream);
EasyMock.replay(principal, services, context, request);
WebSSOResource webSSOResponse = new WebSSOResource();
webSSOResponse.request = request;
webSSOResponse.response = responseWrapper;
webSSOResponse.context = context;
webSSOResponse.init();
// Issue a token
webSSOResponse.doGet();
// Check the cookie
Cookie cookie = responseWrapper.getCookie("hadoop-jwt");
assertNotNull(cookie);
JWT parsedToken = new JWTToken(cookie.getValue());
assertEquals("alice", parsedToken.getSubject());
assertTrue(authority.verifyToken(parsedToken));
// Verify the audiences
List<String> audiences = Arrays.asList(parsedToken.getAudienceClaims());
assertEquals(2, audiences.size());
assertTrue(audiences.contains("recipient1"));
assertTrue(audiences.contains("recipient2"));
}
use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.
the class WebSSOResourceTest method testCustomTTL.
@Test
public void testCustomTTL() throws Exception {
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(context.getInitParameter("knoxsso.cookie.name")).andReturn(null);
EasyMock.expect(context.getInitParameter("knoxsso.cookie.secure.only")).andReturn(null);
EasyMock.expect(context.getInitParameter("knoxsso.cookie.max.age")).andReturn(null);
EasyMock.expect(context.getInitParameter("knoxsso.cookie.domain.suffix")).andReturn(null);
EasyMock.expect(context.getInitParameter("knoxsso.redirect.whitelist.regex")).andReturn(null);
EasyMock.expect(context.getInitParameter("knoxsso.token.audiences")).andReturn(null);
EasyMock.expect(context.getInitParameter("knoxsso.token.ttl")).andReturn("60000");
EasyMock.expect(context.getInitParameter("knoxsso.enable.session")).andReturn(null);
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(request.getParameter("originalUrl")).andReturn("http://localhost:9080/service");
EasyMock.expect(request.getParameterMap()).andReturn(Collections.<String, String[]>emptyMap());
EasyMock.expect(request.getServletContext()).andReturn(context).anyTimes();
Principal principal = EasyMock.createNiceMock(Principal.class);
EasyMock.expect(principal.getName()).andReturn("alice").anyTimes();
EasyMock.expect(request.getUserPrincipal()).andReturn(principal).anyTimes();
GatewayServices services = EasyMock.createNiceMock(GatewayServices.class);
EasyMock.expect(context.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services);
JWTokenAuthority authority = new TestJWTokenAuthority(publicKey, privateKey);
EasyMock.expect(services.getService(GatewayServices.TOKEN_SERVICE)).andReturn(authority);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
ServletOutputStream outputStream = EasyMock.createNiceMock(ServletOutputStream.class);
CookieResponseWrapper responseWrapper = new CookieResponseWrapper(response, outputStream);
EasyMock.replay(principal, services, context, request);
WebSSOResource webSSOResponse = new WebSSOResource();
webSSOResponse.request = request;
webSSOResponse.response = responseWrapper;
webSSOResponse.context = context;
webSSOResponse.init();
// Issue a token
webSSOResponse.doGet();
// Check the cookie
Cookie cookie = responseWrapper.getCookie("hadoop-jwt");
assertNotNull(cookie);
JWT parsedToken = new JWTToken(cookie.getValue());
assertEquals("alice", parsedToken.getSubject());
assertTrue(authority.verifyToken(parsedToken));
Date expiresDate = parsedToken.getExpiresDate();
Date now = new Date();
assertTrue(expiresDate.after(now));
long diff = expiresDate.getTime() - now.getTime();
assertTrue(diff < 60000L && diff > 30000L);
}
use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.
the class CredentialResource method getCredentialsList.
/**
* @return
*/
private List<String> getCredentialsList() {
GatewayServices services = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
String clusterName = (String) request.getServletContext().getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE);
AliasService as = services.getService(GatewayServices.ALIAS_SERVICE);
List<String> aliases = null;
try {
aliases = as.getAliasesForCluster(clusterName);
} catch (AliasServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return aliases;
}
use of org.apache.knox.gateway.services.GatewayServices 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.services.GatewayServices 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