cedar-policy / cedar-java

Java bindings for the Cedar language
https://www.cedarpolicy.com
Apache License 2.0
42 stars 19 forks source link

Add documentation for using 3.1.2 library #176

Closed modigopal closed 3 weeks ago

modigopal commented 3 months ago

Category

Documentation and code comments

Describe the feature you'd like to request

I was exploring the cedar-java and was trying simple policy execution as well.

I have updated this sampleMethod to use 3.1.2 version. And i am facing issue with EntityTypeName.parse("User")

`public boolean sampleMethod() throws AuthException { AuthorizationEngine ae = new BasicAuthorizationEngine();

    Optional<EntityTypeName> principleEntityTypeName = EntityTypeName.parse("User");
    EntityUID principleEntityUID = new EntityUID(principleEntityTypeName.get(),"Alice");
    Entity principleEntity = new Entity(principleEntityUID,null, null);

    Optional<EntityTypeName> actionEntityTypeName = EntityTypeName.parse("Action");
    EntityUID actionEntityUID = new EntityUID(actionEntityTypeName.get(),"View_Photo");
    Entity actionEntity = new Entity(actionEntityUID,null, null);

    Optional<EntityTypeName> resourceEntityTypeName = EntityTypeName.parse("Photo");
    EntityUID resourceEntityUID = new EntityUID(resourceEntityTypeName.get(),"pic01");
    Entity resourceEntity = new Entity(resourceEntityUID,null, null);

    AuthorizationRequest r = new AuthorizationRequest(principleEntity,
            actionEntity,
            resourceEntity, new HashMap<>());
    return ae.isAuthorized(r, buildSlice()).isAllowed();
}`.      

Error: Method threw 'java.lang.UnsatisfiedLinkError' exception.

Q1. Anything am i missing here? Q2. is 3.1.2 stable version ? Q3. why 3.2.0 is not available in maven repo?

Describe alternatives you've considered

NA

Additional context

I am planning to use cedar-java for limit policy in my org. If i get this answer it will unblock me.

Is this something that you'd be interested in working on?

mark-creamer-amazon commented 3 months ago

Q1. Anything am i missing here?

If we see Error: Method threw 'java.lang.UnsatisfiedLinkError' exception., it's because the CedarJavaFFI library doesn't have the JNI methods referenced from CedarJava3.1.2. One thing I would call out is checking your CEDAR_JAVA_FFI_LIB environment variable which points to which CedarJavaFFI library that's used for the JNI methods and confirming it points to a library built from CedarJavaFFI source from 3.1.x.

You can also use the uber jar from the 3.1.2 Maven release and avoid CEDAR_JAVA_FFI_LIB entirely and have nothing built locally. It appears you're referring to the cedar-java-hello-world example, so here are the steps I took in my environment to build your source changes for 3.1.x:

# Setup environment
git clone https://github.com/cedar-policy/cedar-examples.git -b release/3.1.x
cd cedar-examples/cedar-java-hello-world

# Change build.gradle to use the cedar-java:3.1.2 uber jar from Maven
sed -i '' 's/'com.cedarpolicy:cedar-java:2.3.3'/'com.cedarpolicy:cedar-java:3.1.2:uber'/' build.gradle

# Update the cedar-java-hello-world sampleMethod source code to be 3.1.x compliant

# Build cedar-java-hello-world
./gradlew clean && ./gradlew build

Q2. is 3.1.2 stable version ?

Yes, 3.1.2 is a stable version

Q3. why 3.2.0 is not available in maven repo?

We haven't published 3.2.0 to Maven yet.

Note, I have a pending change to update the cedar-java-hello-world example for 3.2.x, and then we can do the same for the 3.1.x branch as well.

shaobo-he-aws commented 3 weeks ago

Close it for now. Please reopen it if you have further questions.