Skip to content

Commit

Permalink
CAMEL-20306 - Camel-CassandraQL: Add ObjectInputFilter String pattern…
Browse files Browse the repository at this point in the history
… parameter in CassandraAggregationRepository to be used in unmarshall operations (#12759)

* CAMEL-20306 - Camel-CassandraQL: Add ObjectInputFilter String pattern parameter in CassandraAggregationRepository to be used in unmarshall operations

Signed-off-by: Andrea Cosentino <ancosen@gmail.com>

* CAMEL-20306 - Camel-CassandraQL: Add ObjectInputFilter String pattern parameter in CassandraAggregationRepository to be used in unmarshall operations - Docs

Signed-off-by: Andrea Cosentino <ancosen@gmail.com>

* CAMEL-20306 - Camel-CassandraQL: Add ObjectInputFilter String pattern parameter in CassandraAggregationRepository to be used in unmarshall operations - Migration Docs

Signed-off-by: Andrea Cosentino <ancosen@gmail.com>

---------

Signed-off-by: Andrea Cosentino <ancosen@gmail.com>
  • Loading branch information
oscerd committed Jan 11, 2024
1 parent fe81d6d commit c7aa49f
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 13 deletions.
4 changes: 4 additions & 0 deletions components/camel-cassandraql/src/main/docs/cql-component.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ Alternatively, the `CassandraAggregationRepository` does not have a
`LOCAL_QUORUM`…
|=======================================================================

While deserializing it's important to notice that the the unmarshallExchange method will allow only all java packages and subpackages
and org.apache.camel packages and subpackages. The remaining classes will be blacklisted. So you'll need to change the filter in case of need.
This could be accomplished by changing the deserializationFilter field on the repository.

== Examples

To insert something on a table you can use the following code:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ public class CassandraAggregationRepository extends ServiceSupport implements Re

private boolean allowSerializedHeaders;

/**
* Sets a deserialization filter while reading Object from Aggregation Repository. By default the filter will allow
* all java packages and subpackages and all org.apache.camel packages and subpackages, while the remaining will be
* blacklisted and not deserialized. This parameter should be customized if you're using classes you trust to be
* deserialized.
*/
private String deserializationFilter = "java.**;org.apache.camel.**;!*";

public CassandraAggregationRepository() {
}

Expand Down Expand Up @@ -211,7 +219,8 @@ public Exchange get(CamelContext camelContext, String key) {
Exchange exchange = null;
if (row != null) {
try {
exchange = exchangeCodec.unmarshallExchange(camelContext, row.getByteBuffer(exchangeColumn));
exchange = exchangeCodec.unmarshallExchange(camelContext, row.getByteBuffer(exchangeColumn),
deserializationFilter);
} catch (IOException iOException) {
throw new CassandraAggregationException("Failed to read exchange", exchange, iOException);
} catch (ClassNotFoundException classNotFoundException) {
Expand Down Expand Up @@ -468,4 +477,12 @@ public boolean isAllowSerializedHeaders() {
public void setAllowSerializedHeaders(boolean allowSerializedHeaders) {
this.allowSerializedHeaders = allowSerializedHeaders;
}

public String getDeserializationFilter() {
return deserializationFilter;
}

public void setDeserializationFilter(String deserializationFilter) {
this.deserializationFilter = deserializationFilter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
*/
package org.apache.camel.processor.aggregate.cassandra;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.*;
import java.nio.ByteBuffer;

import org.apache.camel.CamelContext;
Expand Down Expand Up @@ -62,9 +58,10 @@ public ByteBuffer marshallExchange(Exchange exchange, boolean allowSerializedHea
return ByteBuffer.wrap(serialize(pe));
}

public Exchange unmarshallExchange(CamelContext camelContext, ByteBuffer buffer)
public Exchange unmarshallExchange(CamelContext camelContext, ByteBuffer buffer, String deserializationFilter)
throws IOException, ClassNotFoundException {
DefaultExchangeHolder pe = (DefaultExchangeHolder) deserialize(camelContext, new ByteBufferInputStream(buffer));
DefaultExchangeHolder pe
= (DefaultExchangeHolder) deserialize(camelContext, new ByteBufferInputStream(buffer), deserializationFilter);
Exchange answer = new DefaultExchange(camelContext);
DefaultExchangeHolder.unmarshal(answer, pe);
// restore the from endpoint
Expand All @@ -86,9 +83,11 @@ private byte[] serialize(Object object) throws IOException {
return bytesOut.toByteArray();
}

private Object deserialize(CamelContext camelContext, InputStream bytes) throws IOException, ClassNotFoundException {
private Object deserialize(CamelContext camelContext, InputStream bytes, String deserializationFilter)
throws IOException, ClassNotFoundException {
ClassLoader classLoader = camelContext.getApplicationContextClassLoader();
ObjectInputStream objectIn = new ClassLoadingAwareObjectInputStream(classLoader, bytes);
objectIn.setObjectInputFilter(ObjectInputFilter.Config.createFilter(deserializationFilter));
Object object = objectIn.readObject();
objectIn.close();
return object;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.processor.aggregate.cassandra;

import java.io.*;
import java.nio.ByteBuffer;

import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.malicious.example.Employee;

public class CassandraCamelCodecTest extends CamelTestSupport {

CassandraCamelCodec codec;

@Override
protected void startCamelContext() throws Exception {
super.startCamelContext();
codec = new CassandraCamelCodec();
}

@Test
public void shouldFailWithRejected() throws IOException, ClassNotFoundException {
Employee emp = new Employee("Mickey", "Mouse");

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);

oos.writeObject(emp);

oos.flush();
oos.close();

InputStream is = new ByteArrayInputStream(baos.toByteArray());
InvalidClassException thrown = Assertions.assertThrows(InvalidClassException.class, () -> {
codec.unmarshallExchange(context, ByteBuffer.wrap(is.readAllBytes()), "java.**;org.apache.camel.**;!*");
});

Assertions.assertEquals("filter status: REJECTED", thrown.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.malicious.example;

import java.io.Serializable;

public class Employee implements Serializable {

String name;
String surname;

public Employee(String name, String surname) {
this.name = name;
this.surname = surname;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ With the AZURE_IDENTITY mode the user will be able to use the Default Azure Cred
With the SHARED_ACCOUNT_KEY mode the user could explicitly set the accessKey parameter.
This is part of the effort explained in CAMEL-18590.

=== camel-cassandraql

The NamedCassandraAggregationRepository now provides a deserializationFilter parameter. The default value for it is allowing all java packages and subpackages and all org.apache.camel packages and subpackages. If you plan to use particular classes and you want to expand the filter, you should change the value according to your needs. More details in CAMEL-20306.

=== camel-consul

This component has migrated from `com.orbitz.consul:consul-client` to `org.kiwiproject:consul-client` as the former is no longer maintained,
Expand Down Expand Up @@ -118,14 +122,14 @@ useful examples of how you might need to change your code to be compatible with

The component has been removed after deprecation in 4.3.0

=== camel-sql

The JdbcAggregationRepository now provides a deserializationFilter parameter. The default value for it is allowing all java packages and subpackages and all org.apache.camel packages and subpackages. If you plan to use particular classes and you want to expand the filter, you should change the value according to your needs. More details in CAMEL-20303.

=== camel-jsonata

Replaced the previous JSONata library with a new one that offers complete compatibility with the JSONata reference implementation's features.

=== camel-sql

The JdbcAggregationRepository now provides a deserializationFilter parameter. The default value for it is allowing all java packages and subpackages and all org.apache.camel packages and subpackages. If you plan to use particular classes and you want to expand the filter, you should change the value according to your needs. More details in CAMEL-20303.

== Camel Spring Boot

=== Auto Configuration
Expand Down

0 comments on commit c7aa49f

Please sign in to comment.