Skip to content

feat: Examples module #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: v1
managed:
enabled: true
java_package_prefix:
default: io.opentdf.platform
except:
- buf.build/bufbuild/protovalidate
- buf.build/googleapis/googleapis
- buf.build/grpc-ecosystem/grpc-gateway
plugins:
- plugin: buf.build/protocolbuffers/java:v25.3
out: ./
- plugin: buf.build/grpc/java:v1.61.1
out: ./
18 changes: 18 additions & 0 deletions examples/buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by buf. DO NOT EDIT.
version: v1
deps:
- remote: buf.build
owner: bufbuild
repository: protovalidate
commit: f05a6f4403ce4327bae4f50f281c3ed0
digest: shake256:668a0661b8df44d41839194896329330965fc215f3d2f88057fd60eeb759c2daf6cc6edfdd13b2a653d49fe2896ebedcb1a33c4c5b2dd10919f03ffb7fc52ae6
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 7e6f6e774e29406da95bd61cdcdbc8bc
digest: shake256:fe43dd2265ea0c07d76bd925eeba612667cf4c948d2ce53d6e367e1b4b3cb5fa69a51e6acb1a6a50d32f894f054a35e6c0406f6808a483f2752e10c866ffbf73
- remote: buf.build
owner: grpc-ecosystem
repository: grpc-gateway
commit: 3f42134f4c564983838425bc43c7a65f
digest: shake256:3d11d4c0fe5e05fda0131afefbce233940e27f0c31c5d4e385686aea58ccd30f72053f61af432fa83f1fc11cda57f5f18ca3da26a29064f73c5a0d076bba8d92
22 changes: 22 additions & 0 deletions examples/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: v1
deps:
- buf.build/bufbuild/protovalidate
- buf.build/googleapis/googleapis
- buf.build/grpc-ecosystem/grpc-gateway
breaking:
use:
- FILE
- PACKAGE
- WIRE_JSON
- WIRE
lint:
allow_comment_ignores: true
use:
- DEFAULT
except:
- PACKAGE_VERSION_SUFFIX
ignore_only:
PACKAGE_VERSION_SUFFIX:
- google/api/annotations.proto
- google/api/http.proto
- google/protobuf/wrappers.proto
179 changes: 179 additions & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?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>
<parent>
<artifactId>sdk-pom</artifactId>
<groupId>io.opentdf.platform</groupId>
<version>0.7.4</version>
</parent>

<groupId>io.opentdf.platform</groupId>
<artifactId>examples</artifactId>
<version>0.7.4</version>

<name>examples</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<!-- Optionally: parameterized tests support -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentdf.platform</groupId>
<artifactId>sdk</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version> <!-- Use the latest version available -->
</dependency>
</dependencies>

<build>
<!-- <pluginManagement>lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.opentdf.platform.App</mainClass>
<manifestEntries>
<Implementation-Version>${project.version}</Implementation-Version>
<Main-Class>io.opentdf.platform.App</Main-Class>
</manifestEntries>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>io.opentdf.platform.App</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<!-- Generate Sources Execution under target/generated-sources -->
<execution>
<id>generateSources</id>
<phase>generate-sources</phase>
<configuration>
<target>
<!-- Generate OpenTDF Platform Protobuf -->
<exec executable="buf" dir="." failOnError="true">
<arg value="generate"/>
<arg value="https://github.com/opentdf/platform.git#branch=main,subdir=service"/>
<arg value="-o"/>
<arg value="target/generated-sources"/>
</exec>
<!-- Generate gRPC Protobuf -->
<!-- <exec executable="buf" dir="." failOnError="true">
<arg value="generate"/>
<arg value="buf.build/grpc-ecosystem/grpc-gateway"/>
<arg value="-o"/>
<arg value="target/generated-sources"/>
</exec> -->
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Add Generate Sources Directory -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<!-- </pluginManagement> -->
</build>
</project>
7 changes: 7 additions & 0 deletions examples/src/main/java/io/opentdf/platform/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.opentdf.platform;

public class App {
public static void main(String[] args) {
System.out.println("opentdf examples");
}
}
36 changes: 36 additions & 0 deletions examples/src/main/java/io/opentdf/platform/CreateAttribute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.opentdf.platform;
import io.opentdf.platform.sdk.*;

import java.util.concurrent.ExecutionException;

import io.opentdf.platform.policy.AttributeRuleTypeEnum;

import io.opentdf.platform.policy.attributes.*;
import io.opentdf.platform.policy.Attribute;

import java.util.Arrays;

public class CreateAttribute {
public static void main(String[] args) throws ExecutionException, InterruptedException{

String clientId = "opentdf";
String clientSecret = "secret";
String platformEndpoint = "localhost:8080";

SDKBuilder builder = new SDKBuilder();
SDK sdk = builder.platformEndpoint(platformEndpoint)
.clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true)
.build();

CreateAttributeRequest request = CreateAttributeRequest.newBuilder()
.setNamespaceId("877990d1-609b-42ab-a273-4253b8b321eb")
.setName("test")
.setRule(AttributeRuleTypeEnum.forNumber(AttributeRuleTypeEnum.ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF_VALUE))
.addAllValues(Arrays.asList("test1", "test2")).build();

CreateAttributeResponse resp = sdk.getServices().attributes().createAttribute(request).get();

Attribute attribute = resp.getAttribute();

}
}
27 changes: 27 additions & 0 deletions examples/src/main/java/io/opentdf/platform/CreateNamespace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.opentdf.platform;
import io.opentdf.platform.sdk.*;

import java.util.concurrent.ExecutionException;

import io.opentdf.platform.policy.namespaces.*;

public class CreateNamespace {
public static void main(String[] args) throws ExecutionException, InterruptedException{

String clientId = "opentdf";
String clientSecret = "secret";
String platformEndpoint = "localhost:8080";

SDKBuilder builder = new SDKBuilder();
SDK sdk = builder.platformEndpoint(platformEndpoint)
.clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true)
.build();

CreateNamespaceRequest request = CreateNamespaceRequest.newBuilder().setName("mynamespace.com").build();

CreateNamespaceResponse resp = sdk.getServices().namespaces().createNamespace(request).get();

System.out.println(resp.getNamespace().getName());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.opentdf.platform;
import io.opentdf.platform.sdk.*;

import java.util.concurrent.ExecutionException;

import io.opentdf.platform.policy.subjectmapping.*;
import io.opentdf.platform.policy.SubjectMapping;
import io.opentdf.platform.policy.SubjectConditionSet;
import io.opentdf.platform.policy.SubjectSet;
import io.opentdf.platform.policy.ConditionGroup;
import io.opentdf.platform.policy.Condition;
import io.opentdf.platform.policy.ConditionBooleanTypeEnum;
import io.opentdf.platform.policy.SubjectMappingOperatorEnum;


public class CreateSubjectConditionSet {
public static void main(String[] args) throws ExecutionException, InterruptedException{

String clientId = "opentdf";
String clientSecret = "secret";
String platformEndpoint = "localhost:8080";

SDKBuilder builder = new SDKBuilder();
SDK sdk = builder.platformEndpoint(platformEndpoint)
.clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true)
.build();

var subjectset = SubjectSet.newBuilder().addConditionGroups(ConditionGroup.newBuilder()
.setBooleanOperator(ConditionBooleanTypeEnum.CONDITION_BOOLEAN_TYPE_ENUM_AND)
.addConditions(Condition.newBuilder()
.setSubjectExternalSelectorValue(".myfield")
.setOperator(SubjectMappingOperatorEnum.SUBJECT_MAPPING_OPERATOR_ENUM_IN)
.addSubjectExternalValues("myvalue")
));

CreateSubjectConditionSetRequest request = CreateSubjectConditionSetRequest.newBuilder()
.setSubjectConditionSet(
SubjectConditionSetCreate.newBuilder().addSubjectSets(subjectset))
.build();

CreateSubjectConditionSetResponse resp = sdk.getServices().subjectMappings().createSubjectConditionSet(request).get();

SubjectConditionSet scs = resp.getSubjectConditionSet();

System.out.println(scs.getId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.opentdf.platform;
import io.opentdf.platform.sdk.*;

import java.util.concurrent.ExecutionException;

import io.opentdf.platform.policy.subjectmapping.*;
import io.opentdf.platform.policy.SubjectMapping;
import io.opentdf.platform.policy.Action;

public class CreateSubjectMapping {
public static void main(String[] args) throws ExecutionException, InterruptedException{

String clientId = "opentdf";
String clientSecret = "secret";
String platformEndpoint = "localhost:8080";

SDKBuilder builder = new SDKBuilder();
SDK sdk = builder.platformEndpoint(platformEndpoint)
.clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true)
.build();

CreateSubjectMappingRequest request = CreateSubjectMappingRequest.newBuilder()
.setAttributeValueId("33c47777-f3b6-492d-bcd2-5329e0aab642")
.addActions(Action.newBuilder().setStandard(Action.StandardAction.STANDARD_ACTION_DECRYPT))
.setExistingSubjectConditionSetId("9009fde8-d22b-4dfb-a456-f9ce6943244a")
.build();

CreateSubjectMappingResponse resp = sdk.getServices().subjectMappings().createSubjectMapping(request).get();

SubjectMapping sm = resp.getSubjectMapping();

System.out.println(sm.getId());
}
}
Loading
Loading