Search in sources :

Example 16 with GatewayServices

use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.

the class TopologiesResource method getProviderConfigurations.

@GET
@Produces({ APPLICATION_JSON })
@Path(PROVIDERCONFIG_API_PATH)
public HrefListing getProviderConfigurations() {
    HrefListing listing = new HrefListing();
    listing.setHref(buildHref(request));
    GatewayServices services = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
    List<HrefListItem> configs = new ArrayList<>();
    TopologyService ts = services.getService(GatewayServices.TOPOLOGY_SERVICE);
    // Get all the simple descriptor file names
    for (File providerConfig : ts.getProviderConfigurations()) {
        String id = FilenameUtils.getBaseName(providerConfig.getName());
        configs.add(new HrefListItem(buildHref(id, request), providerConfig.getName()));
    }
    listing.setItems(configs);
    return listing;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) ArrayList(java.util.ArrayList) File(java.io.File) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 17 with GatewayServices

use of org.apache.knox.gateway.services.GatewayServices 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;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 18 with GatewayServices

use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.

the class TopologiesResource method uploadTopology.

@PUT
@Consumes({ APPLICATION_JSON, APPLICATION_XML })
@Path(SINGLE_TOPOLOGY_API_PATH)
public Topology uploadTopology(@PathParam("id") String id, Topology t) {
    Topology result = null;
    GatewayServices gs = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
    t.setName(id);
    TopologyService ts = gs.getService(GatewayServices.TOPOLOGY_SERVICE);
    // Check for existing topology with the same name, to see if it had been generated
    boolean existingGenerated = false;
    for (org.apache.knox.gateway.topology.Topology existingTopology : ts.getTopologies()) {
        if (existingTopology.getName().equals(id)) {
            existingGenerated = existingTopology.isGenerated();
            break;
        }
    }
    // out of sync with the source descriptor. Otherwise, deploy the updated version.
    if (!existingGenerated) {
        ts.deployTopology(BeanConverter.getTopology(t));
        result = getTopology(id);
    } else {
        log.disallowedOverwritingGeneratedTopology(id);
    }
    return result;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) Topology(org.apache.knox.gateway.service.admin.beans.Topology) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 19 with GatewayServices

use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.

the class SimpleDescriptorHandler method provisionQueryParamEncryptionCredential.

/**
 * KNOX-1136
 *
 * Provision the query string encryption password prior to it being randomly generated during the topology
 * deployment.
 *
 * @param topologyName The name of the topology for which the credential will be provisioned.
 *
 * @return true if the credential was successfully provisioned; otherwise, false.
 */
private static boolean provisionQueryParamEncryptionCredential(final String topologyName) {
    boolean result = false;
    try {
        GatewayServices services = GatewayServer.getGatewayServices();
        if (services != null) {
            MasterService ms = services.getService("MasterService");
            if (ms != null) {
                KeystoreService ks = services.getService(GatewayServices.KEYSTORE_SERVICE);
                if (ks != null) {
                    if (!ks.isCredentialStoreForClusterAvailable(topologyName)) {
                        ks.createCredentialStoreForCluster(topologyName);
                    }
                    // If the credential store existed, or it was just successfully created
                    if (ks.getCredentialStoreForCluster(topologyName) != null) {
                        AliasService aliasService = services.getService(GatewayServices.ALIAS_SERVICE);
                        if (aliasService != null) {
                            // Derive and set the query param encryption password
                            String queryEncryptionPass = new String(ms.getMasterSecret()) + topologyName;
                            aliasService.addAliasForCluster(topologyName, "encryptQueryString", queryEncryptionPass);
                            result = true;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        log.exceptionCreatingPasswordForEncryption(topologyName, e);
    }
    return result;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) AliasService(org.apache.knox.gateway.services.security.AliasService) KeystoreService(org.apache.knox.gateway.services.security.KeystoreService) MasterService(org.apache.knox.gateway.services.security.MasterService) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 20 with GatewayServices

use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.

the class TokenServiceResourceTest method testDefaultTTL.

@Test
public void testDefaultTTL() throws Exception {
    ServletContext context = EasyMock.createNiceMock(ServletContext.class);
    EasyMock.expect(context.getInitParameter("knox.token.audiences")).andReturn("recipient1,recipient2");
    EasyMock.expect(context.getInitParameter("knox.token.ttl")).andReturn(null);
    EasyMock.expect(context.getInitParameter("knox.token.target.url")).andReturn(null);
    EasyMock.expect(context.getInitParameter("knox.token.client.data")).andReturn(null);
    HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
    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);
    StringWriter writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.expect(response.getWriter()).andReturn(printWriter);
    EasyMock.replay(principal, services, context, request, response);
    TokenResource tr = new TokenResource();
    tr.request = request;
    tr.response = response;
    tr.context = context;
    tr.init();
    // Issue a token
    Response retResponse = tr.doGet();
    assertEquals(200, retResponse.getStatus());
    // Parse the response
    String retString = writer.toString();
    String accessToken = getTagValue(retString, "access_token");
    assertNotNull(accessToken);
    String expiry = getTagValue(retString, "expires_in");
    assertNotNull(expiry);
    // Verify the token
    JWT parsedToken = new JWTToken(accessToken);
    assertEquals("alice", parsedToken.getSubject());
    assertTrue(authority.verifyToken(parsedToken));
    Date expiresDate = parsedToken.getExpiresDate();
    Date now = new Date();
    assertTrue(expiresDate.after(now));
    assertTrue((expiresDate.getTime() - now.getTime()) < 30000L);
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) TokenResource(org.apache.knox.gateway.service.knoxtoken.TokenResource) 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) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) StringWriter(java.io.StringWriter) JWTokenAuthority(org.apache.knox.gateway.services.security.token.JWTokenAuthority) ServletContext(javax.servlet.ServletContext) PrimaryPrincipal(org.apache.knox.gateway.security.PrimaryPrincipal) Principal(java.security.Principal) PrintWriter(java.io.PrintWriter) 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