Skip to content

Conversation

muditchaudhary
Copy link
Contributor

@muditchaudhary muditchaudhary commented Aug 12, 2025

Overview

Adds DateTime Extension Support (RFC 80)

Changes

  • Implements new com.cedarpolicy.value.DateTime class

    • Validates input dates against supported format specifications
    • Stores DateTime value in String representation
  • Integrates DateTime support in serialization layer

    • Extends ValueSerializer to handle DateTime serialization
    • Adds DateTime deserialization support in ValueDeserializer
  • Testing

    • Adds unit tests covering DateTime validation, serialization, and edge cases
    • Includes integration tests to verify end-to-end DateTime functionality
  • Updates CHANGELOG

Example Usage

Set<Entity> entities = new HashSet<>();
String principalId = "alice";
Map<String, Value> principalAttributes = new HashMap<>();
principalAttributes.put("DOB", new DateTime("2000-01-01"));
Set<EntityUID> principalParents = new HashSet<>();
Entity principal = new Entity(new EntityUID(principalType, principalId), principalAttributes, principalParents);
entities.add(principal);

String actionId = "view";
Map<String, Value> actionAttributes = new HashMap<>();
Set<EntityUID> actionParents = new HashSet<>();
Entity action = new Entity(new EntityUID(actionType, actionId), actionAttributes, actionParents);
entities.add(action);

String resourceId = "photo.jpg";
Map<String, Value> resourceAttributes = new HashMap<>();
Set<EntityUID> resourceParents = new HashSet<>();
var resource = new Entity(new EntityUID(resourceType, resourceId), resourceAttributes, resourceParents);
entities.add(resource);

String p = "permit(\n"
        + "principal==" + principal.getEUID().toString() + ",\n"
        + "action==" + action.getEUID().toString() + ",\n"
        + "resource==" + resource.getEUID().toString() + "\n"
        + ") when {principal.DOB > datetime(\"1999-01-01\")};";
                
final String policyId = "ID0";
Policy policy = new Policy(p, policyId);
Set<Policy> policies = new HashSet<>();
policies.add(policy);
PolicySet policySet = new PolicySet(policies);
Map<String, Value> currentContext = new HashMap<>();
AuthorizationRequest request = new AuthorizationRequest( principal, action, resource, currentContext);

Signed-off-by: Mudit Chaudhary <[email protected]>
Signed-off-by: Mudit Chaudhary <[email protected]>
Signed-off-by: Mudit Chaudhary <[email protected]>
Signed-off-by: Mudit Chaudhary <[email protected]>
@muditchaudhary muditchaudhary marked this pull request as ready for review August 12, 2025 18:43
/** Convert DateTime to Cedar expr that can be used in a Cedar policy. */
@Override
public String toCedarExpr() {
return "datetime(\"" + dateTime + "\")";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worried about this being valid, but should be fine since dateTime is validated on construction, cannot be mutated externally since it's private and it's set once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. It's final and only assigned after validation so should be ok. This is what we do for IpAddress as well

Copy link
Contributor

@mark-creamer-amazon mark-creamer-amazon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

Copy link
Contributor

@shaobo-he-aws shaobo-he-aws left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@muditchaudhary muditchaudhary merged commit 33a23e9 into cedar-policy:main Aug 15, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants