Skip to content

Commit

Permalink
Added Library Loader using Fizzed Java Native Environment
Browse files Browse the repository at this point in the history
- Added Maven configuration with exec-maven-plugin
- Added Maven build for AArch64 and AMD64 Architectures
- Added GitHub Actions build workflow using Maven on macos-14
- Upgraded Gradle Wrapper from 8.1.1 to 8.6
- Set cedar-policy library version to 3.0.1
- Disabled unstable integration tests
  • Loading branch information
exceptionfactory committed Feb 7, 2024
1 parent 2e5fd1e commit cc97500
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 10 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: build

on:
workflow_dispatch:

permissions:
contents: read
packages: write

jobs:
build:
runs-on: macos-14
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout Sources
uses: actions/checkout@v4
- name: Setup Java JDK
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'zulu'
cache: 'maven'
- name: Install Zig
run: brew install zig
- name: Add Rust Build Targets
run: rustup target add x86_64-unknown-linux-gnu
- name: Maven Build
working-directory: CedarJava
run: mvn clean deploy -DdistributionOwner=${{ github.repository_owner }}
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.gradle/
.idea/
/build
/buildSrc/build/
/gradle/
/gradlew
/gradlew.bat
/wrapper/
.DS_Store
.jqwik-database
*.iml
.classpath
.factorypath
.project
.settings/
/gradle.properties
.idea/
target/
1 change: 1 addition & 0 deletions CedarJava/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.0'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.15.0'
implementation 'org.slf4j:slf4j-api:2.0.7'
implementation 'com.fizzed:jne:4.1.1'
compileOnly 'com.google.code.findbugs:findbugs:3.0.1'
testImplementation 'org.apache.logging.log4j:log4j-core:2.20.0'
testImplementation 'org.apache.logging.log4j:log4j-to-slf4j:2.20.0'
Expand Down
2 changes: 1 addition & 1 deletion CedarJava/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
204 changes: 204 additions & 0 deletions CedarJava/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.cedarpolicy</groupId>
<artifactId>cedar-java</artifactId>
<version>3.0.0-SNAPSHOT</version>

<properties>
<maven.compiler.release>17</maven.compiler.release>
<foreignFunctionModulePath>../CedarJavaFFI</foreignFunctionModulePath>
<foreignFunctionLibraryName>libcedar_java_ffi</foreignFunctionLibraryName>
<aarch64AppleTarget>aarch64-apple-darwin</aarch64AppleTarget>
<aarch64AppleOutputDirectory>${project.build.outputDirectory}/jne/macos/aarch64</aarch64AppleOutputDirectory>
<amd64LinuxTarget>x86_64-unknown-linux-gnu</amd64LinuxTarget>
<amd64LinuxOutputDirectory>${project.build.outputDirectory}/jne/linux/amd64</amd64LinuxOutputDirectory>
<distributionOwner>cedarpolicy</distributionOwner>
</properties>

<distributionManagement>
<repository>
<id>github</id>
<name>GitHub</name>
<url>https://maven.pkg.github.com/${distributionOwner}/cedar-java</url>
</repository>
</distributionManagement>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.11</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>com.fizzed</groupId>
<artifactId>jne</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>findbugs</artifactId>
<version>3.0.1</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>1.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.jqwik</groupId>
<artifactId>jqwik</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>cargo-build</id>
<phase>process-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cargo</executable>
<arguments>
<argument>build</argument>
<argument>--release</argument>
<argument>--target=${aarch64AppleTarget}</argument>
<argument>--manifest-path</argument>
<argument>${foreignFunctionModulePath}/Cargo.toml</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>create-target-directory-aarch64</id>
<phase>process-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mkdir</executable>
<arguments>
<argument>-p</argument>
<argument>${aarch64AppleOutputDirectory}</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>copy-release-library-aarch64</id>
<phase>process-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cp</executable>
<arguments>
<argument>${foreignFunctionModulePath}/target/${aarch64AppleTarget}/release/${foreignFunctionLibraryName}.dylib</argument>
<argument>${aarch64AppleOutputDirectory}</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>cargo-install-zigbuild</id>
<phase>process-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cargo</executable>
<arguments>
<argument>install</argument>
<argument>cargo-zigbuild</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>cargo-zigbuild</id>
<phase>process-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cargo</executable>
<arguments>
<argument>zigbuild</argument>
<argument>--release</argument>
<argument>--target=${amd64LinuxTarget}</argument>
<argument>--manifest-path</argument>
<argument>${foreignFunctionModulePath}/Cargo.toml</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>create-target-directory-amd64</id>
<phase>process-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mkdir</executable>
<arguments>
<argument>-p</argument>
<argument>${amd64LinuxOutputDirectory}</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>copy-release-library-amd64</id>
<phase>process-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cp</executable>
<arguments>
<argument>${foreignFunctionModulePath}/target/${amd64LinuxTarget}/release/${foreignFunctionLibraryName}.so</argument>
<argument>${amd64LinuxOutputDirectory}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.IOException;

import com.cedarpolicy.library.NativeLibraryLoader;
import com.cedarpolicy.model.AuthorizationResponse;
import com.cedarpolicy.model.ValidationRequest;
import com.cedarpolicy.model.ValidationResponse;
Expand All @@ -40,7 +41,7 @@ public final class BasicAuthorizationEngine implements AuthorizationEngine {
private static final Logger LOG = LoggerFactory.getLogger(BasicAuthorizationEngine.class);

static {
System.load(System.getenv("CEDAR_JAVA_FFI_LIB"));
NativeLibraryLoader.loadLibrary();
}

/** Construct a basic authorization engine. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2022-2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cedarpolicy.library;

import com.fizzed.jne.JNE;

/**
* Native Library Loader encapsulates runtime loading of the Cedar Java FFI library using Java Native Environment
*/
public class NativeLibraryLoader {
private static final String LIBRARY_PATH_VARIABLE_NAME = "CEDAR_JAVA_FFI_LIB";

private static final String LIBRARY_NAME = "cedar_java_ffi";

/**
* Load Cedar Java FFI library based on runtime operating system and architecture of the Java Virtual Machine
*/
public static void loadLibrary() {
final String libraryPath = System.getenv(LIBRARY_PATH_VARIABLE_NAME);
if (libraryPath == null || libraryPath.isEmpty()) {
JNE.loadLibrary(LIBRARY_NAME);
} else {
System.load(libraryPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.cedarpolicy.model.schema;

import com.cedarpolicy.library.NativeLibraryLoader;
import com.cedarpolicy.model.exception.InternalException;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -29,7 +30,7 @@ public final class Schema {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

static {
System.load(System.getenv("CEDAR_JAVA_FFI_LIB"));
NativeLibraryLoader.loadLibrary();
}

// The schema after being parsed as a JSON object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.cedarpolicy.model.slice;

import com.cedarpolicy.library.NativeLibraryLoader;
import com.cedarpolicy.model.exception.InternalException;
import com.cedarpolicy.value.EntityUID;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -31,7 +32,7 @@ public class Policy {
private static final Logger LOG = LoggerFactory.getLogger(Policy.class);
private static final AtomicInteger idCounter = new AtomicInteger(0);
static {
System.load(System.getenv("CEDAR_JAVA_FFI_LIB"));
NativeLibraryLoader.loadLibrary();
}

/** Policy string. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.cedarpolicy.value;


import com.cedarpolicy.library.NativeLibraryLoader;

/**
* Class representing Entity Identifiers.
* All strings are valid Entity Identifiers
*/
public final class EntityIdentifier {
private String id;

static {
System.load(System.getenv("CEDAR_JAVA_FFI_LIB"));
static {
NativeLibraryLoader.loadLibrary();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

package com.cedarpolicy.value;

import com.cedarpolicy.library.NativeLibraryLoader;

import java.util.List;
import java.util.Optional;
import java.util.Objects;
Expand All @@ -17,7 +19,7 @@ public final class EntityTypeName {
private List<String> namespace;
private String basename;
static {
System.load(System.getenv("CEDAR_JAVA_FFI_LIB"));
NativeLibraryLoader.loadLibrary();
}

/**
Expand Down
Loading

0 comments on commit cc97500

Please sign in to comment.