Search in sources :

Example 1 with Query

use of org.apache.knox.gateway.util.urltemplate.Query in project knox by apache.

the class SecureQueryDecodeProcessor method process.

@Override
public UrlRewriteStepStatus process(UrlRewriteContext context) throws Exception {
    // TODO: Need some way to get a reference to the keystore service and the encryption key in particular.
    Template currUrl = context.getCurrentUrl();
    Builder newUrl = new Builder(currUrl);
    Map<String, Query> map = newUrl.getQuery();
    Query query = map.remove(ENCODED_PARAMETER_NAME);
    if (query != null) {
        String value = query.getFirstValue().getPattern();
        value = decode(value);
        StringTokenizer outerParser = new StringTokenizer(value, "&");
        while (outerParser.hasMoreTokens()) {
            String pair = outerParser.nextToken();
            StringTokenizer innerParser = new StringTokenizer(pair, "=");
            if (innerParser.hasMoreTokens()) {
                String paramName = innerParser.nextToken();
                if (innerParser.hasMoreTokens()) {
                    String paramValue = innerParser.nextToken();
                    // Need to take out any existing query param.
                    // If we don't then someone could override something in the encoded param.
                    map.remove(paramName);
                    newUrl.addQuery(paramName, "", paramValue, true);
                } else {
                    newUrl.addQuery(paramName, "", null, true);
                }
            }
        }
        context.setCurrentUrl(newUrl.build());
    }
    return UrlRewriteStepStatus.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) Query(org.apache.knox.gateway.util.urltemplate.Query) Builder(org.apache.knox.gateway.util.urltemplate.Builder) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 2 with Query

use of org.apache.knox.gateway.util.urltemplate.Query in project knox by apache.

the class SecureQueryDecryptProcessor method process.

@Override
public UrlRewriteStepStatus process(UrlRewriteContext context) throws Exception {
    // TODO: Need some way to get a reference to the keystore service and the encryption key in particular.
    Template currUrl = context.getCurrentUrl();
    Builder newUrl = new Builder(currUrl);
    Map<String, Query> map = newUrl.getQuery();
    Query query = map.remove(ENCRYPTED_PARAMETER_NAME);
    UrlRewriteStepStatus status = UrlRewriteStepStatus.FAILURE;
    status = getUrlRewriteStepStatus(context, newUrl, map, query, status);
    return status;
}
Also used : UrlRewriteStepStatus(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus) Query(org.apache.knox.gateway.util.urltemplate.Query) Builder(org.apache.knox.gateway.util.urltemplate.Builder) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 3 with Query

use of org.apache.knox.gateway.util.urltemplate.Query in project knox by apache.

the class SecureQueryEncryptDecryptProcessorTest method testEncryptBadDecrypt.

@Test
public void testEncryptBadDecrypt() throws Exception {
    Query query;
    Template origTemplate = Parser.parseLiteral("http://host:0/path/file?query-param-name=query-param-value");
    // Test encryption.  Results are left in encTemplate
    AliasService as = EasyMock.createNiceMock(AliasService.class);
    String secret = "sdkjfhsdkjfhsdfs";
    EasyMock.expect(as.getPasswordFromAliasForCluster("test-cluster-name", "encryptQueryString")).andReturn(secret.toCharArray()).anyTimes();
    CryptoService cryptoService = new DefaultCryptoService();
    ((DefaultCryptoService) cryptoService).setAliasService(as);
    GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(gatewayServices.getService(GatewayServices.CRYPTO_SERVICE)).andReturn(cryptoService);
    UrlRewriteEnvironment encEnvironment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(encEnvironment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
    EasyMock.expect(encEnvironment.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster-name").anyTimes();
    UrlRewriteContext encContext = EasyMock.createNiceMock(UrlRewriteContext.class);
    EasyMock.expect(encContext.getCurrentUrl()).andReturn(origTemplate);
    Capture<Template> encTemplate = new Capture<Template>();
    encContext.setCurrentUrl(EasyMock.capture(encTemplate));
    EasyMock.replay(gatewayServices, as, encEnvironment, encContext);
    SecureQueryEncryptDescriptor descriptor = new SecureQueryEncryptDescriptor();
    SecureQueryEncryptProcessor processor = new SecureQueryEncryptProcessor();
    processor.initialize(encEnvironment, descriptor);
    processor.process(encContext);
    assertThat(encTemplate, notNullValue());
    query = encTemplate.getValue().getQuery().get("_");
    assertThat(query.getFirstValue().getPattern().length(), greaterThan(1));
    query = encTemplate.getValue().getQuery().get("query-param-name");
    assertThat(query, nullValue());
    // Test decryption with decode returning null
    gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(gatewayServices.getService(GatewayServices.CRYPTO_SERVICE)).andReturn(cryptoService);
    as = EasyMock.createNiceMock(AliasService.class);
    EasyMock.expect(as.getPasswordFromAliasForCluster("test-cluster-name", "encryptQueryString")).andReturn(secret.toCharArray()).anyTimes();
    UrlRewriteEnvironment decEnvironment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(decEnvironment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
    EasyMock.expect(decEnvironment.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster-name").anyTimes();
    Params decParams = EasyMock.createNiceMock(Params.class);
    EasyMock.expect(decParams.resolve(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn(Arrays.asList("test-cluster-name")).anyTimes();
    UrlRewriteContext decContext = EasyMock.createNiceMock(UrlRewriteContext.class);
    EasyMock.expect(decContext.getCurrentUrl()).andReturn(encTemplate.getValue());
    EasyMock.expect(decContext.getParameters()).andReturn(decParams);
    Capture<Template> decTemplate = new Capture<Template>();
    decContext.setCurrentUrl(EasyMock.capture(decTemplate));
    SecureQueryDecryptDescriptor descriptor1 = new SecureQueryDecryptDescriptor();
    SecureQueryDecryptProcessor decProcessor = EasyMock.createMockBuilder(SecureQueryDecryptProcessor.class).addMockedMethod(SecureQueryDecryptProcessor.class.getDeclaredMethod("decode", String.class)).createMock();
    EasyMock.expect(decProcessor.decode(EasyMock.anyObject(String.class))).andReturn(null);
    EasyMock.replay(gatewayServices, as, decEnvironment, decParams, decContext, decProcessor);
    decProcessor.initialize(decEnvironment, descriptor1);
    UrlRewriteStepStatus status = decProcessor.process(decContext);
    Assert.assertTrue((status == UrlRewriteStepStatus.FAILURE));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) AliasService(org.apache.knox.gateway.services.security.AliasService) Query(org.apache.knox.gateway.util.urltemplate.Query) Params(org.apache.knox.gateway.util.urltemplate.Params) UrlRewriteContext(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext) Capture(org.easymock.Capture) Template(org.apache.knox.gateway.util.urltemplate.Template) UrlRewriteStepStatus(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus) DefaultCryptoService(org.apache.knox.gateway.services.security.impl.DefaultCryptoService) CryptoService(org.apache.knox.gateway.services.security.CryptoService) DefaultCryptoService(org.apache.knox.gateway.services.security.impl.DefaultCryptoService) Test(org.junit.Test)

Example 4 with Query

use of org.apache.knox.gateway.util.urltemplate.Query in project knox by apache.

the class SecureQueryEncryptDecryptProcessorTest method testEncryptDecrypt.

@Test
public void testEncryptDecrypt() throws Exception {
    Query query;
    Template origTemplate = Parser.parseLiteral("http://host:0/path/file?query-param-name=query-param-value");
    // Test encryption.  Results are left in encTemplate
    AliasService as = EasyMock.createNiceMock(AliasService.class);
    String secret = "sdkjfhsdkjfhsdfs";
    EasyMock.expect(as.getPasswordFromAliasForCluster("test-cluster-name", "encryptQueryString")).andReturn(secret.toCharArray()).anyTimes();
    CryptoService cryptoService = new DefaultCryptoService();
    ((DefaultCryptoService) cryptoService).setAliasService(as);
    GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(gatewayServices.getService(GatewayServices.CRYPTO_SERVICE)).andReturn(cryptoService);
    UrlRewriteEnvironment encEnvironment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(encEnvironment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
    EasyMock.expect(encEnvironment.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster-name").anyTimes();
    UrlRewriteContext encContext = EasyMock.createNiceMock(UrlRewriteContext.class);
    EasyMock.expect(encContext.getCurrentUrl()).andReturn(origTemplate);
    Capture<Template> encTemplate = new Capture<Template>();
    encContext.setCurrentUrl(EasyMock.capture(encTemplate));
    EasyMock.replay(gatewayServices, as, encEnvironment, encContext);
    SecureQueryEncryptDescriptor descriptor = new SecureQueryEncryptDescriptor();
    SecureQueryEncryptProcessor processor = new SecureQueryEncryptProcessor();
    processor.initialize(encEnvironment, descriptor);
    processor.process(encContext);
    assertThat(encTemplate, notNullValue());
    query = encTemplate.getValue().getQuery().get("_");
    assertThat(query.getFirstValue().getPattern().length(), greaterThan(1));
    query = encTemplate.getValue().getQuery().get("query-param-name");
    assertThat(query, nullValue());
    // Test decryption.  Results are left in decTemplate.
    gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(gatewayServices.getService(GatewayServices.CRYPTO_SERVICE)).andReturn(cryptoService);
    as = EasyMock.createNiceMock(AliasService.class);
    EasyMock.expect(as.getPasswordFromAliasForCluster("test-cluster-name", "encryptQueryString")).andReturn(secret.toCharArray()).anyTimes();
    UrlRewriteEnvironment decEnvironment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(decEnvironment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
    EasyMock.expect(decEnvironment.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster-name").anyTimes();
    Params decParams = EasyMock.createNiceMock(Params.class);
    EasyMock.expect(decParams.resolve(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn(Arrays.asList("test-cluster-name")).anyTimes();
    UrlRewriteContext decContext = EasyMock.createNiceMock(UrlRewriteContext.class);
    EasyMock.expect(decContext.getCurrentUrl()).andReturn(encTemplate.getValue());
    EasyMock.expect(decContext.getParameters()).andReturn(decParams);
    Capture<Template> decTemplate = new Capture<Template>();
    decContext.setCurrentUrl(EasyMock.capture(decTemplate));
    EasyMock.replay(gatewayServices, as, decEnvironment, decParams, decContext);
    SecureQueryDecryptDescriptor descriptor1 = new SecureQueryDecryptDescriptor();
    SecureQueryDecryptProcessor decProcessor = new SecureQueryDecryptProcessor();
    decProcessor.initialize(decEnvironment, descriptor1);
    decProcessor.process(decContext);
    assertThat(decTemplate, notNullValue());
    assertThat(decTemplate.getValue(), notNullValue());
    query = decTemplate.getValue().getQuery().get("query-param-name");
    assertThat(query.getFirstValue().getPattern(), is("query-param-value"));
    query = decTemplate.getValue().getQuery().get("_");
    assertThat(query, nullValue());
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) AliasService(org.apache.knox.gateway.services.security.AliasService) Query(org.apache.knox.gateway.util.urltemplate.Query) Params(org.apache.knox.gateway.util.urltemplate.Params) UrlRewriteContext(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext) Capture(org.easymock.Capture) Template(org.apache.knox.gateway.util.urltemplate.Template) DefaultCryptoService(org.apache.knox.gateway.services.security.impl.DefaultCryptoService) CryptoService(org.apache.knox.gateway.services.security.CryptoService) DefaultCryptoService(org.apache.knox.gateway.services.security.impl.DefaultCryptoService) Test(org.junit.Test)

Aggregations

Query (org.apache.knox.gateway.util.urltemplate.Query)4 Template (org.apache.knox.gateway.util.urltemplate.Template)4 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)2 UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)2 UrlRewriteStepStatus (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)2 GatewayServices (org.apache.knox.gateway.services.GatewayServices)2 AliasService (org.apache.knox.gateway.services.security.AliasService)2 CryptoService (org.apache.knox.gateway.services.security.CryptoService)2 DefaultCryptoService (org.apache.knox.gateway.services.security.impl.DefaultCryptoService)2 Builder (org.apache.knox.gateway.util.urltemplate.Builder)2 Params (org.apache.knox.gateway.util.urltemplate.Params)2 Capture (org.easymock.Capture)2 Test (org.junit.Test)2 StringTokenizer (java.util.StringTokenizer)1