Search in sources :

Example 6 with UrlRewriteStepStatus

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus 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 7 with UrlRewriteStepStatus

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus 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 8 with UrlRewriteStepStatus

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

the class UrlRewriteStepProcessorHolder method processOrFlow.

// At least one condition proceeding a set of one or more actions must succedd for the actions to be executed.
private UrlRewriteStepStatus processOrFlow(UrlRewriteContext context) throws Exception {
    UrlRewriteStepProcessorState state = new UrlRewriteStepProcessorState(childProcessors.iterator());
    UrlRewriteStepStatus status = UrlRewriteStepStatus.SUCCESS;
    UrlRewriteStepProcessorHolder step;
    while (state.hasNext()) {
        UrlRewriteStepStatus flowStatus = UrlRewriteStepStatus.FAILURE;
        while (state.hasNextCondition()) {
            step = state.nextCondition(status);
            if (flowStatus == UrlRewriteStepStatus.FAILURE) {
                status = step.process(context);
                switch(status) {
                    case SUCCESS:
                        flowStatus = UrlRewriteStepStatus.SUCCESS;
                        continue;
                    case FINISHED:
                        return status;
                }
            }
        }
        status = processActions(context, state);
        if (status != UrlRewriteStepStatus.SUCCESS) {
            return status;
        }
    }
    return UrlRewriteStepStatus.SUCCESS;
}
Also used : UrlRewriteStepStatus(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)

Example 9 with UrlRewriteStepStatus

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus 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 10 with UrlRewriteStepStatus

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

the class UrlRewriteMatchProcessorExt method process.

@Override
public UrlRewriteStepStatus process(UrlRewriteContext context) throws Exception {
    UrlRewriteStepStatus status = UrlRewriteStepStatus.SUCCESS;
    if (matcher != null) {
        status = UrlRewriteStepStatus.FAILURE;
        Matcher.Match match = matcher.match(context.getCurrentUrl());
        if (match != null) {
            context.addParameters(match.getParams());
            status = UrlRewriteStepStatus.SUCCESS;
        }
    }
    return status;
}
Also used : UrlRewriteStepStatus(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus) Matcher(org.apache.knox.gateway.util.urltemplate.Matcher)

Aggregations

UrlRewriteStepStatus (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)10 UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)3 Template (org.apache.knox.gateway.util.urltemplate.Template)3 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)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 Matcher (org.apache.knox.gateway.util.urltemplate.Matcher)2 Query (org.apache.knox.gateway.util.urltemplate.Query)2 Capture (org.easymock.Capture)2 Test (org.junit.Test)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 UrlRewriteFlowDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFlowDescriptor)1 UrlRewriteStepFlow (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteStepFlow)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