Search in sources :

Example 1 with UrlRewriteContext

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext in project knox by apache.

the class UrlRewriteProcessor method rewrite.

@Override
public Template rewrite(Resolver resolver, Template inputUri, Direction direction, String ruleName) {
    Template outputUri = inputUri;
    String serviceRole = null;
    if (resolver != null) {
        List<String> serviceRoles = resolver.resolve("service.role");
        if (serviceRoles != null && !serviceRoles.isEmpty()) {
            serviceRole = serviceRoles.get(0);
        }
    }
    UrlRewriteStepProcessorHolder stepHolder = null;
    String effectiveRuleName = null;
    if (ruleName == null || "*".equals(ruleName)) {
        // Used for logging later.
        ruleName = null;
        Matcher<UrlRewriteRuleProcessorHolder>.Match match = null;
        switch(direction) {
            case IN:
                match = inbound.match(outputUri, serviceRole);
                break;
            case OUT:
                match = outbound.match(outputUri, serviceRole);
                break;
        }
        if (match != null) {
            stepHolder = match.getValue();
            effectiveRuleName = match.getValue().getRuleName();
        }
    } else if (!ruleName.isEmpty()) {
        stepHolder = rules.get(ruleName);
        effectiveRuleName = ruleName;
    }
    if (stepHolder != null) {
        UrlRewriteContext context = new UrlRewriteContextImpl(environment, resolver, functions, direction, inputUri);
        try {
            UrlRewriteStepStatus stepStatus = stepHolder.process(context);
            if (UrlRewriteStepStatus.SUCCESS == stepStatus) {
                outputUri = context.getCurrentUrl();
                if (ruleName == null) {
                    LOG.rewroteUrlViaImplicitRule(inputUri, direction, effectiveRuleName, outputUri);
                } else {
                    LOG.rewroteUrlViaExplicitRule(inputUri, direction, effectiveRuleName, outputUri);
                }
            } else {
                LOG.failedToRewriteUrl(inputUri, direction, effectiveRuleName, stepStatus);
                outputUri = null;
            }
        } catch (Exception e) {
            LOG.failedToRewriteUrlDueToException(inputUri, direction, effectiveRuleName, e);
            outputUri = null;
        }
    } else {
        LOG.noRuleMatchingUrl(inputUri, direction);
    }
    return outputUri;
}
Also used : UrlRewriteStepStatus(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus) Matcher(org.apache.knox.gateway.util.urltemplate.Matcher) ScopedMatcher(org.apache.knox.gateway.filter.rewrite.ext.ScopedMatcher) UrlRewriteStepProcessorHolder(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteStepProcessorHolder) UrlRewriteContextImpl(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteContextImpl) UrlRewriteContext(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 2 with UrlRewriteContext

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext in project knox by apache.

the class EncryptDecryptUriProcessorTest method testEncryptDecrypt.

@Test
public void testEncryptDecrypt() throws Exception {
    String encryptedValueParamName = "address";
    String clusterName = "test-cluster-name";
    String passwordAlias = "encryptQueryString";
    // Test encryption.  Result is in encryptedAdrress
    AliasService as = EasyMock.createNiceMock(AliasService.class);
    String secret = "asdf";
    EasyMock.expect(as.getPasswordFromAliasForCluster(clusterName, passwordAlias)).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(clusterName).anyTimes();
    UrlRewriteContext encContext = EasyMock.createNiceMock(UrlRewriteContext.class);
    EncryptStepContextParams hostPortParams = new EncryptStepContextParams();
    hostPortParams.addParam("host", Arrays.asList("host.yarn.com"));
    hostPortParams.addParam("port", Arrays.asList("8088"));
    EasyMock.expect(encContext.getParameters()).andReturn(hostPortParams);
    Capture<EncryptStepContextParams> encodedValue = new Capture<EncryptStepContextParams>();
    encContext.addParameters(EasyMock.capture(encodedValue));
    EasyMock.replay(gatewayServices, as, encEnvironment, encContext);
    EncryptUriDescriptor descriptor = new EncryptUriDescriptor();
    descriptor.setTemplate("{host}:{port}");
    descriptor.setParam(encryptedValueParamName);
    EncryptUriProcessor processor = new EncryptUriProcessor();
    processor.initialize(encEnvironment, descriptor);
    UrlRewriteStepStatus encStatus = processor.process(encContext);
    assertThat(encStatus, is(UrlRewriteStepStatus.SUCCESS));
    assertThat(encodedValue.getValue(), notNullValue());
    assertThat(encodedValue.getValue().resolve(encryptedValueParamName).size(), is(1));
    String encryptedAdrress = encodedValue.getValue().resolve(encryptedValueParamName).get(0);
    assertThat(encryptedAdrress, not(isEmptyOrNullString()));
    assertThat(encryptedAdrress, not("{host}:{port}"));
    assertThat(encryptedAdrress, not("hdp:8088"));
    // Test decryption.  Result is in dectryptedAdrress.
    String decParam = "foo";
    gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(gatewayServices.getService(GatewayServices.CRYPTO_SERVICE)).andReturn(cryptoService);
    as = EasyMock.createNiceMock(AliasService.class);
    EasyMock.expect(as.getPasswordFromAliasForCluster(clusterName, passwordAlias)).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(clusterName).anyTimes();
    UrlRewriteContext decContext = EasyMock.createNiceMock(UrlRewriteContext.class);
    EncryptStepContextParams encryptedParams = new EncryptStepContextParams();
    // Value was encrypted by EncryptUriProcessor
    encryptedParams.addParam(decParam, Arrays.asList(encryptedAdrress));
    encryptedParams.addParam("foo1", Arrays.asList("test"));
    EasyMock.expect(decContext.getParameters()).andReturn(encryptedParams);
    Capture<EncryptStepContextParams> decodedValue = new Capture<EncryptStepContextParams>();
    decContext.addParameters(EasyMock.capture(decodedValue));
    EasyMock.replay(gatewayServices, as, decEnvironment, decContext);
    DecryptUriDescriptor decDescriptor = new DecryptUriDescriptor();
    decDescriptor.setParam(decParam);
    DecryptUriProcessor decProcessor = new DecryptUriProcessor();
    decProcessor.initialize(decEnvironment, decDescriptor);
    UrlRewriteStepStatus decStatus = decProcessor.process(decContext);
    assertThat(decStatus, is(UrlRewriteStepStatus.SUCCESS));
    assertThat(decodedValue.getValue(), notNullValue());
    assertThat(decodedValue.getValue().resolve(decParam).size(), is(1));
    String dectryptedAdrress = decodedValue.getValue().resolve(decParam).get(0);
    assertThat(dectryptedAdrress, is("host.yarn.com:8088"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) EncryptStepContextParams(org.apache.knox.gateway.encrypturi.EncryptStepContextParams) AliasService(org.apache.knox.gateway.services.security.AliasService) EncryptUriDescriptor(org.apache.knox.gateway.encrypturi.api.EncryptUriDescriptor) IsEmptyString.isEmptyOrNullString(org.hamcrest.text.IsEmptyString.isEmptyOrNullString) UrlRewriteContext(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext) Capture(org.easymock.Capture) 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) DecryptUriDescriptor(org.apache.knox.gateway.encrypturi.api.DecryptUriDescriptor) DefaultCryptoService(org.apache.knox.gateway.services.security.impl.DefaultCryptoService) Test(org.junit.Test)

Example 3 with UrlRewriteContext

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext in project knox by apache.

the class SecureQueryDecodeProcessorTest method testSimpleQueryDecode.

@Test
public void testSimpleQueryDecode() throws Exception {
    UrlRewriteEnvironment environment = new UrlRewriteEnvironment() {

        @Override
        public URL getResource(String name) throws IOException {
            return null;
        }

        @Override
        public <T> T getAttribute(String name) {
            return null;
        }

        @Override
        public List<String> resolve(String name) {
            return null;
        }
    };
    BASE64Encoder encoder = new BASE64Encoder();
    String encQuery = encoder.encode("test-query".getBytes("utf-8"));
    encQuery = encQuery.replaceAll("\\=", "");
    String inString = "http://host:0/root/path?_=" + encQuery;
    Template inTemplate = Parser.parseLiteral(inString);
    UrlRewriteContext context = EasyMock.createNiceMock(UrlRewriteContext.class);
    EasyMock.expect(context.getCurrentUrl()).andReturn(inTemplate);
    Capture<Template> outTemplate = new Capture<Template>();
    context.setCurrentUrl(EasyMock.capture(outTemplate));
    EasyMock.replay(context);
    SecureQueryDecodeDescriptor descriptor = new SecureQueryDecodeDescriptor();
    SecureQueryDecodeProcessor processor = new SecureQueryDecodeProcessor();
    processor.initialize(environment, descriptor);
    processor.process(context);
    String outActual = outTemplate.getValue().toString();
    assertThat(outActual, is("http://host:0/root/path?test-query"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) BASE64Encoder(sun.misc.BASE64Encoder) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) UrlRewriteContext(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext) Capture(org.easymock.Capture) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 4 with UrlRewriteContext

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext in project knox by apache.

the class SecureQueryDecodeProcessorTest method testDecodeQueryWithNonEncodedParams.

@Test
public void testDecodeQueryWithNonEncodedParams() throws Exception {
    UrlRewriteEnvironment environment = new UrlRewriteEnvironment() {

        @Override
        public URL getResource(String name) throws IOException {
            return null;
        }

        @Override
        public <T> T getAttribute(String name) {
            return null;
        }

        @Override
        public List<String> resolve(String name) {
            return null;
        }
    };
    BASE64Encoder encoder = new BASE64Encoder();
    String inQuery = "test-query=test-value";
    String encQuery = encoder.encode(inQuery.getBytes("utf-8"));
    encQuery = encQuery.replaceAll("\\=", "");
    String inString = "http://host:0/root/path?_=" + encQuery + "&clear-param=clear-value";
    Template inTemplate = Parser.parseLiteral(inString);
    UrlRewriteContext context = EasyMock.createNiceMock(UrlRewriteContext.class);
    EasyMock.expect(context.getCurrentUrl()).andReturn(inTemplate);
    Capture<Template> outTemplate = new Capture<Template>();
    context.setCurrentUrl(EasyMock.capture(outTemplate));
    EasyMock.replay(context);
    SecureQueryDecodeDescriptor descriptor = new SecureQueryDecodeDescriptor();
    SecureQueryDecodeProcessor processor = new SecureQueryDecodeProcessor();
    processor.initialize(environment, descriptor);
    processor.process(context);
    String outActual = outTemplate.getValue().toString();
    assertThat(outActual, containsString("http://host:0/root/path?"));
    assertThat(outActual, containsString("test-query=test-value"));
    assertThat(outActual, containsString("clear-param=clear-value"));
    assertThat(outActual, not(containsString(encQuery)));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) BASE64Encoder(sun.misc.BASE64Encoder) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) UrlRewriteContext(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext) Capture(org.easymock.Capture) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 5 with UrlRewriteContext

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext in project knox by apache.

the class SecureQueryEncodeProcessorTest method testSimpleQueryEncoding.

@Test
public void testSimpleQueryEncoding() throws Exception {
    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 environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(environment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
    EasyMock.expect(environment.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn(Arrays.asList("test-cluster-name")).anyTimes();
    Template inTemplate = Parser.parseLiteral("http://host:0/root/path?query");
    UrlRewriteContext context = EasyMock.createNiceMock(UrlRewriteContext.class);
    EasyMock.expect(context.getCurrentUrl()).andReturn(inTemplate);
    Capture<Template> outTemplate = new Capture<Template>();
    context.setCurrentUrl(EasyMock.capture(outTemplate));
    EasyMock.replay(environment, context);
    SecureQueryEncodeDescriptor descriptor = new SecureQueryEncodeDescriptor();
    SecureQueryEncodeProcessor processor = new SecureQueryEncodeProcessor();
    processor.initialize(environment, descriptor);
    processor.process(context);
    BASE64Encoder encoder = new BASE64Encoder();
    String encQuery = encoder.encode("query".getBytes("utf-8"));
    encQuery = encQuery.replaceAll("\\=", "");
    String outExpect = "http://host:0/root/path?_=" + encQuery;
    String outActual = outTemplate.getValue().toString();
    assertThat(outActual, is(outExpect));
}
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) BASE64Encoder(sun.misc.BASE64Encoder) 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

UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)7 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)6 Template (org.apache.knox.gateway.util.urltemplate.Template)6 Capture (org.easymock.Capture)6 Test (org.junit.Test)6 GatewayServices (org.apache.knox.gateway.services.GatewayServices)4 AliasService (org.apache.knox.gateway.services.security.AliasService)4 CryptoService (org.apache.knox.gateway.services.security.CryptoService)4 DefaultCryptoService (org.apache.knox.gateway.services.security.impl.DefaultCryptoService)4 UrlRewriteStepStatus (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)3 BASE64Encoder (sun.misc.BASE64Encoder)3 Params (org.apache.knox.gateway.util.urltemplate.Params)2 Query (org.apache.knox.gateway.util.urltemplate.Query)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 EncryptStepContextParams (org.apache.knox.gateway.encrypturi.EncryptStepContextParams)1 DecryptUriDescriptor (org.apache.knox.gateway.encrypturi.api.DecryptUriDescriptor)1 EncryptUriDescriptor (org.apache.knox.gateway.encrypturi.api.EncryptUriDescriptor)1 ScopedMatcher (org.apache.knox.gateway.filter.rewrite.ext.ScopedMatcher)1 UrlRewriteContextImpl (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteContextImpl)1 UrlRewriteStepProcessorHolder (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteStepProcessorHolder)1