Claus Ibsen created CAMEL-27763:
-----------------------------------
Summary: camel-support - KeyValueRepository stores text as text
and binary as byte[], never Java serialization
Key: CAMEL-27763
URL: https://issues.apache.org/jira/browse/CAMEL-27763
Project: Camel
Issue Type: Improvement
Components: camel-support, camel-core
Reporter: Claus Ibsen
Fix For: 4.24.0
h3. Problem
The KeyValueRepository stores that keep data outside the JVM write every value
with plain Java serialization: KeyValueRepositoryHelper.serialize() uses an
ObjectOutputStream. This is used by RedisKeyValueRepository,
JdbcKeyValueRepository, JpaKeyValueRepository, CassandraKeyValueRepository,
KafkaKeyValueRepository, EhcacheKeyValueRepository, HazelcastKeyValueRepository
and JCacheKeyValueRepository.
Users do not want Java serialization in their data stores. It is a security
risk, and the data cannot be read by anything else than a Java application with
the same classes.
Two problems show with the Cache EIP (new in 4.23):
* A JSON or XML body is stored as Java serialization bytes (they start with AC
ED 00 05), not as the text. {{redis-cli GET}}, a SQL query or another
application cannot read it as JSON or XML.
* A stream body cannot be stored at all. After an HTTP call the body is an
InputStreamCache, which is not serializable. The Cache EIP logs {{WARN Cache
write failed for key 'C-1': Failed to serialize value}} and goes on. Nothing is
cached, and every message calls the slow service again. The route works, but it
quietly does not cache, and the warning does not say why.
h3. How to reproduce
Run {{camel infra run redis}}, then this route with {{camel run
cache.camel.yaml --dep=camel:redis}}:
{code:yaml}
- beans:
- name: customerCache
type: org.apache.camel.component.redis.RedisKeyValueRepository
properties:
endpoint: localhost:6379
- route:
from:
uri: platform-http:/customer
steps:
- log:
message: "calling customer service"
- setBody:
constant: '{"id":"C-1","name":"Acme"}'
- route:
from:
uri: timer:orders?period=1000&repeatCount=4
steps:
- cache:
expression:
simple: "C-1"
keyValueRepository: customerCache
ttl: 60s
steps:
- to:
uri: http://localhost:8080/customer
- log:
message: "order: ${body}"
{code}
Result: "calling customer service" is logged 4 times, each with the WARN above.
With {{convertBodyTo: String}} at the end of the cache block, the service is
called once, but the value in Redis is Java serialization bytes followed by the
JSON.
h3. What we want
* Text (a String, whether JSON, XML, CSV or plain text) is stored as is, as
UTF-8 text.
* Binary data (byte[]) is stored as byte[].
* Never Java serialization.
* A value is read back with the same type it was stored with (String or
byte[]). How each store keeps the type is part of this work.
* The Cache EIP turns a stream body (StreamCache) into byte[] or String before
it stores it, so a stream body is cached in every store. The in-memory store
keeps the same stream object today.
* Any other type fails with a clear message, for example: "The cache value must
be a String or byte[], but was InputStreamCache. Convert the body inside the
cache block (convertBodyTo String)."
h3. Other values in the stores
* KeyValueIdempotentRepository stores Boolean.TRUE, which is Java-serialized
today. A plain marker value is enough.
* KeyValueAggregationRepository stores DefaultExchangeHolder objects, which
needs a format without Java serialization (for example the body and headers as
JSON). This can be a follow-up ticket.
h3. Compatibility
The persistent KeyValueRepository stores are new in 4.23 (CAMEL-24463), so few
stored values exist. Values written by 4.23 cannot be read after this change;
the upgrade guide should say to clear the store or let the entries expire.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)