MCPcopy Index your code
hub / github.com/cedar-policy/cedar-java

github.com/cedar-policy/cedar-java @v4.10.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v4.10.0 ↗ · + Follow
980 symbols 3,376 edges 113 files 387 documented · 39%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

cedar-java

Cedar Logo

This repository contains the source code for a Java package CedarJava that supports using the Cedar policy language. It also contains source code for a Rust crate CedarJavaFFI that enables calling Cedar library functions (written in Rust) from Java.

Cedar is a language for writing and enforcing authorization policies in your applications. Using Cedar, you can write policies that specify your applications' fine-grained permissions. Your applications then authorize access requests by calling Cedar's authorization engine. Because Cedar policies are separate from application code, they can be independently authored, updated, analyzed, and audited. You can use Cedar's validator to check that Cedar policies are consistent with a declared schema which defines your application's authorization model.

Getting Started

Import CedarJava to your application

Maven Package

CedarJava is available as a maven package. You can add CedarJava as a dependency to your build file.

Example (Gradle):

dependencies{
    implementation 'com.cedarpolicy:cedar-java:4.2.3:uber'
}

We highly recommend using the *-uber.jar as it also contains the shared library from CedarJavaFFI.

See https://central.sonatype.com/artifact/com.cedarpolicy/cedar-java for more details.

Build from Source

The CedarJavaFFI and CedarJava directories contain detailed instructions on building the individual modules.

The CedarJava module uses Gradle to build both modules and run integration tests. It stores the shared library from CedarJavaFFI in the -uber.jar. The following commands provide general usage for getting started.

cd CedarJava
./gradlew build

Perform an Authorization Request

Here is a small snippet on how to perform isAuthorized() call using CedarJava

package com.mypackage;

import com.cedarpolicy.AuthorizationEngine;
import com.cedarpolicy.BasicAuthorizationEngine;
import com.cedarpolicy.model.AuthorizationRequest;
import com.cedarpolicy.model.AuthorizationResponse;
import com.cedarpolicy.model.Context;
import com.cedarpolicy.model.entity.Entities;
import com.cedarpolicy.model.entity.Entity;
import com.cedarpolicy.model.policy.PolicySet;
import com.cedarpolicy.value.EntityUID;

public class SimpleAuthorization {
    public static void main(String[] args) throws Exception {

        // Build entities
        Entity principal = new Entity(EntityUID.parse("User::\"Alice\"").get());
        Entity action = new Entity(EntityUID.parse("Action::\"view\"").get());
        Entity resource = new Entity(EntityUID.parse("Photo::\"alice_photo\"").get());

        // Build policies
        PolicySet policySet = PolicySet.parsePolicies("""
            permit(
                principal == User::"Alice",
                action == Action::"view",
                resource == Photo::"alice_photo"
            );

            forbid(
                principal == User::"Alice",
                action == Action::"view",
                resource == Photo::"bob_photo"
            );
        """);

        // Authorization request
        AuthorizationEngine ae = new BasicAuthorizationEngine();
        Entities entities = new Entities();
        Context context = new Context();
        AuthorizationRequest request  = new AuthorizationRequest(principal, action, resource, context);
        AuthorizationResponse authorizationResponse = ae.isAuthorized(request, policySet, entities);
    }
}

Examples

Explore our sample applications in cedar-examples: * cedar-java-hello-world: Demonstrates basic authorization calls using Cedar-Java * cedar-java-partial-evaluation: Illustrates partial evaluation capabilities in Cedar-Java

Changelog

For a list of changes and version updates, see CHANGELOG.md.

Notes

CedarJava requires JDK 17 or later.

Cedar is primarily developed in Rust (in the cedar repository). As such, CedarJava typically lags behind the newest Cedar features.

The main branch of this repository is kept up-to-date with the development version of the Rust code (available in the main branch of cedar). Unless you plan to build the Rust code locally, please use the latest release/x.x.x branch instead.

Security

See CONTRIBUTING for more information.

License

This project is licensed under the Apache-2.0 License.

Extension points exported contracts — how you extend this code

Object (Interface)
General trait for anything that's a wrapper around a java object. This lets us dynamically cast from a Java Object to ou [6 …
CedarJavaFFI/src/objects.rs
AuthorizationEngine (Interface)
Implementations of the AuthorizationEngine interface invoke Cedar to respond to an authorization or validation request. [1 …
CedarJava/src/main/java/com/cedarpolicy/AuthorizationEngine.java

Core symbols most depended-on inside this repo

get
called by 220
CedarJava/src/main/java/com/cedarpolicy/model/Context.java
of
called by 148
CedarJava/src/main/java/com/cedarpolicy/value/EntityTypeName.java
add
called by 148
CedarJava/src/main/java/com/cedarpolicy/value/CedarList.java
parse
called by 104
CedarJava/src/main/java/com/cedarpolicy/value/EntityTypeName.java
put
called by 100
CedarJava/src/main/java/com/cedarpolicy/value/CedarMap.java
getEUID
called by 77
CedarJava/src/main/java/com/cedarpolicy/model/entity/Entity.java
toString
called by 66
CedarJava/src/main/java/com/cedarpolicy/model/Context.java
empty
called by 36
CedarJavaFFI/src/objects.rs

Shape

Method 691
Function 151
Class 125
Enum 11
Interface 2

Languages

Java79%
Rust21%

Modules by API surface

CedarJavaFFI/src/interface.rs104 symbols
CedarJava/src/jmh/java/com/cedarpolicy/AuthorizationBenchmark.java34 symbols
CedarJava/src/test/java/com/cedarpolicy/pbt/IntegrationTests.java27 symbols
CedarJavaFFI/src/tests.rs25 symbols
CedarJava/src/main/java/com/cedarpolicy/value/CedarList.java24 symbols
CedarJavaFFI/src/objects.rs21 symbols
CedarJava/src/main/java/com/cedarpolicy/model/policy/Policy.java21 symbols
CedarJava/src/test/java/com/cedarpolicy/ValidationTests.java20 symbols
CedarJava/src/main/java/com/cedarpolicy/model/schema/Schema.java20 symbols
CedarJavaFFI/src/jlist.rs19 symbols
CedarJava/src/main/java/com/cedarpolicy/model/policy/PolicySet.java19 symbols
CedarJava/src/test/java/com/cedarpolicy/SchemaTests.java18 symbols

For agents

$ claude mcp add cedar-java \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact