Andrea Cosentino created CAMEL-28022:
----------------------------------------
Summary: camel-cedar - reject fractional numbers in context and
entity attributes
Key: CAMEL-28022
URL: https://issues.apache.org/jira/browse/CAMEL-28022
Project: Camel
Issue Type: Bug
Reporter: Andrea Cosentino
Assignee: Andrea Cosentino
Fix For: 4.25.0
h3. Background
camel-cedar evaluates Cedar policies to produce an allow/deny decision for each
message. The {{context}} option is an Expression whose result (a Map or a POJO)
is converted into the Cedar request context that policies read. Cedar has no
floating-point number type; the option description already states "Cedar has no
floating-point numbers: use a whole number or a Decimal", and that "a result
that cannot be converted ... gives the ERROR outcome".
h3. Behaviour today
A fractional number in the {{context}} result is not rejected.
{{CedarEvaluator.evaluate()}} converts the value with
{{CedarJson.objectMapper().convertValue(value, Map<String, Value>)}}. The Cedar
Java {{Value}} deserializer represents any in-range JSON number as a Cedar
{{Long}}, using Jackson's range-only {{canConvertToLong()}} together with the
truncating {{asLong()}}. A fractional number is therefore converted (floored
toward zero) to a whole number, with a normal decision and no error - for
example {{2.5}} becomes {{2}}. A policy that compares the attribute then
evaluates against a different number than the one the Expression produced. This
is inconsistent with the documented behaviour of the option ("Cedar has no
floating-point numbers" and "a result that cannot be converted ... gives the
ERROR outcome").
The same conversion is reached on the entity path:
{{CedarEvaluator.entities(...)}} parses the JSON returned by a
{{CedarEntityProvider}} with {{Entities.parse(...)}}, which uses the same
{{Value}} deserializer, so a fractional entity attribute number is converted
the same way.
h3. Behaviour after the change
A fractional number in the {{context}} result, or in the JSON that a
{{CedarEntityProvider}} returns, gives the ERROR outcome instead of being
converted to a whole number. A whole-valued number such as {{2.0}} is still
accepted and evaluates as {{Long 2}}. A Cedar {{Decimal}} (an extension value
whose numeric argument is carried as a string) is unaffected and remains the
documented way to pass a fractional value.
h3. Classes involved
* {{org.apache.camel.component.cedar.CedarEvaluator}} - {{evaluate(Exchange)}}
(the {{context}} conversion) and {{entities(Snapshot, Exchange)}} (the
entity-provider JSON).
* The existing private {{Rejected}} signal in {{CedarEvaluator}} already maps
rejected input to the ERROR outcome in the {{catch}} block of {{evaluate()}};
the change reuses it.
h3. Code-level change
* In {{evaluate()}}, build a Jackson tree with the same mapper first
({{JsonNode tree = CedarJson.objectMapper().valueToTree(value)}}), walk it
recursively, and for any node where {{node.isNumber() &&
!node.canConvertToExactIntegral()}} throw {{Rejected}} (which yields the ERROR
outcome). Then convert the validated tree to {{Map<String, Value>}} and keep
the existing {{maxResourceSize}} check. Whole-valued numbers pass
{{canConvertToExactIntegral()}}; a Cedar {{Decimal}} extension carries its
number as a string, so it is not affected by the numeric scan.
* Apply the same non-integer scan to the JSON a {{CedarEntityProvider}} returns
in {{entities(...)}}, before {{Entities.parse(...)}}.
h3. Tests
New tests alongside the existing {{CedarEvaluatorTest}} (AssertJ,
{{camel-test-junit6}}; they reuse the gated native-engine fixture, so they run
on the same cells as the current tests):
* {{fractionalContextNumberGivesErrorOutcome}} - a fractional {{context}}
number gives {{Outcome.ERROR}} (today it is accepted as a whole number).
* {{wholeValuedDoubleContextStillEvaluates}} - a whole-valued double behaves as
{{Long}} (a normal decision, not ERROR).
* {{bigDecimalFractionalContextGivesErrorOutcome}} - a fractional
{{BigDecimal}} gives {{Outcome.ERROR}}.
* a nested or array fractional number gives {{Outcome.ERROR}}.
* a {{CedarEntityProvider}} that returns a fractional attribute gives
{{Outcome.ERROR}}.
h3. Documentation
Clarify the {{context}} option description in
{{components/camel-cedar/src/main/docs/cedar-component.adoc}} to state that a
fractional number gives the ERROR outcome (it is not converted to a whole
number), and regenerate the catalog doc mirror under {{catalog/camel-catalog/}}
so the "uncommitted changes" check stays green.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)