Search in sources :

Example 31 with GatewayServices

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"));
}
Also used : Cookie(javax.servlet.http.Cookie) GatewayServices(org.apache.knox.gateway.services.GatewayServices) ServletOutputStream(javax.servlet.ServletOutputStream) JWT(org.apache.knox.gateway.services.security.token.impl.JWT) HttpServletResponse(javax.servlet.http.HttpServletResponse) JWTToken(org.apache.knox.gateway.services.security.token.impl.JWTToken) HttpServletRequest(javax.servlet.http.HttpServletRequest) JWTokenAuthority(org.apache.knox.gateway.services.security.token.JWTokenAuthority) ServletContext(javax.servlet.ServletContext) Principal(java.security.Principal) Test(org.junit.Test)

Example 32 with GatewayServices

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);
}
Also used : Cookie(javax.servlet.http.Cookie) GatewayServices(org.apache.knox.gateway.services.GatewayServices) ServletOutputStream(javax.servlet.ServletOutputStream) JWT(org.apache.knox.gateway.services.security.token.impl.JWT) HttpServletResponse(javax.servlet.http.HttpServletResponse) JWTToken(org.apache.knox.gateway.services.security.token.impl.JWTToken) Date(java.util.Date) HttpServletRequest(javax.servlet.http.HttpServletRequest) JWTokenAuthority(org.apache.knox.gateway.services.security.token.JWTokenAuthority) ServletContext(javax.servlet.ServletContext) Principal(java.security.Principal) Test(org.junit.Test)

Example 33 with GatewayServices

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;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) AliasService(org.apache.knox.gateway.services.security.AliasService) AliasServiceException(org.apache.knox.gateway.services.security.AliasServiceException)

Example 34 with GatewayServices

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();
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) DefaultGatewayServices(org.apache.knox.gateway.services.DefaultGatewayServices) Topology(org.apache.knox.gateway.topology.Topology) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Test(org.junit.Test)

Example 35 with GatewayServices

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();
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) DefaultGatewayServices(org.apache.knox.gateway.services.DefaultGatewayServices) Topology(org.apache.knox.gateway.topology.Topology) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Test(org.junit.Test)

Aggregations

GatewayServices (org.apache.knox.gateway.services.GatewayServices)75 Test (org.junit.Test)37 HttpServletRequest (javax.servlet.http.HttpServletRequest)24 ServletContext (javax.servlet.ServletContext)22 Principal (java.security.Principal)21 JWTokenAuthority (org.apache.knox.gateway.services.security.token.JWTokenAuthority)21 AliasService (org.apache.knox.gateway.services.security.AliasService)20 HttpServletResponse (javax.servlet.http.HttpServletResponse)19 JWT (org.apache.knox.gateway.services.security.token.impl.JWT)18 Response (javax.ws.rs.core.Response)17 JWTToken (org.apache.knox.gateway.services.security.token.impl.JWTToken)17 TopologyService (org.apache.knox.gateway.services.topology.TopologyService)17 HashMap (java.util.HashMap)14 Path (javax.ws.rs.Path)12 File (java.io.File)11 PrintWriter (java.io.PrintWriter)11 StringWriter (java.io.StringWriter)11 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)11 PrimaryPrincipal (org.apache.knox.gateway.security.PrimaryPrincipal)11 TokenResource (org.apache.knox.gateway.service.knoxtoken.TokenResource)11