Skip to content

Commit ac13a0a

Browse files
feat: Examples module (#202)
The examples that we provide in the docs repo Add a module to manage them here Compile in ci to ensure there are no breaking changes to docs
1 parent 4084f44 commit ac13a0a

18 files changed

+644
-0
lines changed

examples/buf.gen.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: v1
2+
managed:
3+
enabled: true
4+
java_package_prefix:
5+
default: io.opentdf.platform
6+
except:
7+
- buf.build/bufbuild/protovalidate
8+
- buf.build/googleapis/googleapis
9+
- buf.build/grpc-ecosystem/grpc-gateway
10+
plugins:
11+
- plugin: buf.build/protocolbuffers/java:v25.3
12+
out: ./
13+
- plugin: buf.build/grpc/java:v1.61.1
14+
out: ./

examples/buf.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by buf. DO NOT EDIT.
2+
version: v1
3+
deps:
4+
- remote: buf.build
5+
owner: bufbuild
6+
repository: protovalidate
7+
commit: f05a6f4403ce4327bae4f50f281c3ed0
8+
digest: shake256:668a0661b8df44d41839194896329330965fc215f3d2f88057fd60eeb759c2daf6cc6edfdd13b2a653d49fe2896ebedcb1a33c4c5b2dd10919f03ffb7fc52ae6
9+
- remote: buf.build
10+
owner: googleapis
11+
repository: googleapis
12+
commit: 7e6f6e774e29406da95bd61cdcdbc8bc
13+
digest: shake256:fe43dd2265ea0c07d76bd925eeba612667cf4c948d2ce53d6e367e1b4b3cb5fa69a51e6acb1a6a50d32f894f054a35e6c0406f6808a483f2752e10c866ffbf73
14+
- remote: buf.build
15+
owner: grpc-ecosystem
16+
repository: grpc-gateway
17+
commit: 3f42134f4c564983838425bc43c7a65f
18+
digest: shake256:3d11d4c0fe5e05fda0131afefbce233940e27f0c31c5d4e385686aea58ccd30f72053f61af432fa83f1fc11cda57f5f18ca3da26a29064f73c5a0d076bba8d92

examples/buf.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: v1
2+
deps:
3+
- buf.build/bufbuild/protovalidate
4+
- buf.build/googleapis/googleapis
5+
- buf.build/grpc-ecosystem/grpc-gateway
6+
breaking:
7+
use:
8+
- FILE
9+
- PACKAGE
10+
- WIRE_JSON
11+
- WIRE
12+
lint:
13+
allow_comment_ignores: true
14+
use:
15+
- DEFAULT
16+
except:
17+
- PACKAGE_VERSION_SUFFIX
18+
ignore_only:
19+
PACKAGE_VERSION_SUFFIX:
20+
- google/api/annotations.proto
21+
- google/api/http.proto
22+
- google/protobuf/wrappers.proto

examples/pom.xml

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<artifactId>sdk-pom</artifactId>
6+
<groupId>io.opentdf.platform</groupId>
7+
<version>0.7.4</version>
8+
</parent>
9+
10+
<groupId>io.opentdf.platform</groupId>
11+
<artifactId>examples</artifactId>
12+
<version>0.7.4</version>
13+
14+
<name>examples</name>
15+
<!-- FIXME change it to the project's website -->
16+
<url>http://www.example.com</url>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<maven.compiler.release>11</maven.compiler.release>
21+
</properties>
22+
23+
<dependencyManagement>
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.junit</groupId>
27+
<artifactId>junit-bom</artifactId>
28+
<version>5.11.0</version>
29+
<type>pom</type>
30+
<scope>import</scope>
31+
</dependency>
32+
</dependencies>
33+
</dependencyManagement>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-api</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
<!-- Optionally: parameterized tests support -->
42+
<dependency>
43+
<groupId>org.junit.jupiter</groupId>
44+
<artifactId>junit-jupiter-params</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>io.opentdf.platform</groupId>
49+
<artifactId>sdk</artifactId>
50+
<version>${project.version}</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.google.code.gson</groupId>
54+
<artifactId>gson</artifactId>
55+
<version>2.11.0</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.apache.logging.log4j</groupId>
59+
<artifactId>log4j-slf4j2-impl</artifactId>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.apache.logging.log4j</groupId>
63+
<artifactId>log4j-core</artifactId>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.apache.logging.log4j</groupId>
67+
<artifactId>log4j-api</artifactId>
68+
</dependency>
69+
<dependency>
70+
<groupId>javax.annotation</groupId>
71+
<artifactId>javax.annotation-api</artifactId>
72+
<version>1.3.2</version> <!-- Use the latest version available -->
73+
</dependency>
74+
</dependencies>
75+
76+
<build>
77+
<!-- <pluginManagement>lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
78+
<plugins>
79+
<plugin>
80+
<groupId>org.apache.maven.plugins</groupId>
81+
<artifactId>maven-shade-plugin</artifactId>
82+
<version>3.2.4</version>
83+
<executions>
84+
<execution>
85+
<phase>package</phase>
86+
<goals>
87+
<goal>shade</goal>
88+
</goals>
89+
<configuration>
90+
<createDependencyReducedPom>false</createDependencyReducedPom>
91+
<transformers>
92+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
93+
<mainClass>io.opentdf.platform.App</mainClass>
94+
<manifestEntries>
95+
<Implementation-Version>${project.version}</Implementation-Version>
96+
<Main-Class>io.opentdf.platform.App</Main-Class>
97+
</manifestEntries>
98+
</transformer>
99+
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
100+
</transformers>
101+
<filters>
102+
<filter>
103+
<artifact>*:*</artifact>
104+
<excludes>
105+
<exclude>META-INF/*.SF</exclude>
106+
<exclude>META-INF/*.DSA</exclude>
107+
<exclude>META-INF/*.RSA</exclude>
108+
</excludes>
109+
</filter>
110+
</filters>
111+
</configuration>
112+
</execution>
113+
</executions>
114+
</plugin>
115+
<plugin>
116+
<groupId>org.codehaus.mojo</groupId>
117+
<artifactId>exec-maven-plugin</artifactId>
118+
<version>3.0.0</version>
119+
<configuration>
120+
<mainClass>io.opentdf.platform.App</mainClass>
121+
</configuration>
122+
</plugin>
123+
<plugin>
124+
<groupId>org.apache.maven.plugins</groupId>
125+
<artifactId>maven-antrun-plugin</artifactId>
126+
<version>3.1.0</version>
127+
<executions>
128+
<!-- Generate Sources Execution under target/generated-sources -->
129+
<execution>
130+
<id>generateSources</id>
131+
<phase>generate-sources</phase>
132+
<configuration>
133+
<target>
134+
<!-- Generate OpenTDF Platform Protobuf -->
135+
<exec executable="buf" dir="." failOnError="true">
136+
<arg value="generate"/>
137+
<arg value="https://github.com/opentdf/platform.git#branch=main,subdir=service"/>
138+
<arg value="-o"/>
139+
<arg value="target/generated-sources"/>
140+
</exec>
141+
<!-- Generate gRPC Protobuf -->
142+
<!-- <exec executable="buf" dir="." failOnError="true">
143+
<arg value="generate"/>
144+
<arg value="buf.build/grpc-ecosystem/grpc-gateway"/>
145+
<arg value="-o"/>
146+
<arg value="target/generated-sources"/>
147+
</exec> -->
148+
</target>
149+
</configuration>
150+
<goals>
151+
<goal>run</goal>
152+
</goals>
153+
</execution>
154+
</executions>
155+
</plugin>
156+
<!-- Add Generate Sources Directory -->
157+
<plugin>
158+
<groupId>org.codehaus.mojo</groupId>
159+
<artifactId>build-helper-maven-plugin</artifactId>
160+
<version>3.5.0</version>
161+
<executions>
162+
<execution>
163+
<id>add-source</id>
164+
<phase>generate-sources</phase>
165+
<goals>
166+
<goal>add-source</goal>
167+
</goals>
168+
<configuration>
169+
<sources>
170+
<source>target/generated-sources</source>
171+
</sources>
172+
</configuration>
173+
</execution>
174+
</executions>
175+
</plugin>
176+
</plugins>
177+
<!-- </pluginManagement> -->
178+
</build>
179+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.opentdf.platform;
2+
3+
public class App {
4+
public static void main(String[] args) {
5+
System.out.println("opentdf examples");
6+
}
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.opentdf.platform;
2+
import io.opentdf.platform.sdk.*;
3+
4+
import java.util.concurrent.ExecutionException;
5+
6+
import io.opentdf.platform.policy.AttributeRuleTypeEnum;
7+
8+
import io.opentdf.platform.policy.attributes.*;
9+
import io.opentdf.platform.policy.Attribute;
10+
11+
import java.util.Arrays;
12+
13+
public class CreateAttribute {
14+
public static void main(String[] args) throws ExecutionException, InterruptedException{
15+
16+
String clientId = "opentdf";
17+
String clientSecret = "secret";
18+
String platformEndpoint = "localhost:8080";
19+
20+
SDKBuilder builder = new SDKBuilder();
21+
SDK sdk = builder.platformEndpoint(platformEndpoint)
22+
.clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true)
23+
.build();
24+
25+
CreateAttributeRequest request = CreateAttributeRequest.newBuilder()
26+
.setNamespaceId("877990d1-609b-42ab-a273-4253b8b321eb")
27+
.setName("test")
28+
.setRule(AttributeRuleTypeEnum.forNumber(AttributeRuleTypeEnum.ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF_VALUE))
29+
.addAllValues(Arrays.asList("test1", "test2")).build();
30+
31+
CreateAttributeResponse resp = sdk.getServices().attributes().createAttribute(request).get();
32+
33+
Attribute attribute = resp.getAttribute();
34+
35+
}
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.opentdf.platform;
2+
import io.opentdf.platform.sdk.*;
3+
4+
import java.util.concurrent.ExecutionException;
5+
6+
import io.opentdf.platform.policy.namespaces.*;
7+
8+
public class CreateNamespace {
9+
public static void main(String[] args) throws ExecutionException, InterruptedException{
10+
11+
String clientId = "opentdf";
12+
String clientSecret = "secret";
13+
String platformEndpoint = "localhost:8080";
14+
15+
SDKBuilder builder = new SDKBuilder();
16+
SDK sdk = builder.platformEndpoint(platformEndpoint)
17+
.clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true)
18+
.build();
19+
20+
CreateNamespaceRequest request = CreateNamespaceRequest.newBuilder().setName("mynamespace.com").build();
21+
22+
CreateNamespaceResponse resp = sdk.getServices().namespaces().createNamespace(request).get();
23+
24+
System.out.println(resp.getNamespace().getName());
25+
26+
}
27+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.opentdf.platform;
2+
import io.opentdf.platform.sdk.*;
3+
4+
import java.util.concurrent.ExecutionException;
5+
6+
import io.opentdf.platform.policy.subjectmapping.*;
7+
import io.opentdf.platform.policy.SubjectMapping;
8+
import io.opentdf.platform.policy.SubjectConditionSet;
9+
import io.opentdf.platform.policy.SubjectSet;
10+
import io.opentdf.platform.policy.ConditionGroup;
11+
import io.opentdf.platform.policy.Condition;
12+
import io.opentdf.platform.policy.ConditionBooleanTypeEnum;
13+
import io.opentdf.platform.policy.SubjectMappingOperatorEnum;
14+
15+
16+
public class CreateSubjectConditionSet {
17+
public static void main(String[] args) throws ExecutionException, InterruptedException{
18+
19+
String clientId = "opentdf";
20+
String clientSecret = "secret";
21+
String platformEndpoint = "localhost:8080";
22+
23+
SDKBuilder builder = new SDKBuilder();
24+
SDK sdk = builder.platformEndpoint(platformEndpoint)
25+
.clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true)
26+
.build();
27+
28+
var subjectset = SubjectSet.newBuilder().addConditionGroups(ConditionGroup.newBuilder()
29+
.setBooleanOperator(ConditionBooleanTypeEnum.CONDITION_BOOLEAN_TYPE_ENUM_AND)
30+
.addConditions(Condition.newBuilder()
31+
.setSubjectExternalSelectorValue(".myfield")
32+
.setOperator(SubjectMappingOperatorEnum.SUBJECT_MAPPING_OPERATOR_ENUM_IN)
33+
.addSubjectExternalValues("myvalue")
34+
));
35+
36+
CreateSubjectConditionSetRequest request = CreateSubjectConditionSetRequest.newBuilder()
37+
.setSubjectConditionSet(
38+
SubjectConditionSetCreate.newBuilder().addSubjectSets(subjectset))
39+
.build();
40+
41+
CreateSubjectConditionSetResponse resp = sdk.getServices().subjectMappings().createSubjectConditionSet(request).get();
42+
43+
SubjectConditionSet scs = resp.getSubjectConditionSet();
44+
45+
System.out.println(scs.getId());
46+
}
47+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.opentdf.platform;
2+
import io.opentdf.platform.sdk.*;
3+
4+
import java.util.concurrent.ExecutionException;
5+
6+
import io.opentdf.platform.policy.subjectmapping.*;
7+
import io.opentdf.platform.policy.SubjectMapping;
8+
import io.opentdf.platform.policy.Action;
9+
10+
public class CreateSubjectMapping {
11+
public static void main(String[] args) throws ExecutionException, InterruptedException{
12+
13+
String clientId = "opentdf";
14+
String clientSecret = "secret";
15+
String platformEndpoint = "localhost:8080";
16+
17+
SDKBuilder builder = new SDKBuilder();
18+
SDK sdk = builder.platformEndpoint(platformEndpoint)
19+
.clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true)
20+
.build();
21+
22+
CreateSubjectMappingRequest request = CreateSubjectMappingRequest.newBuilder()
23+
.setAttributeValueId("33c47777-f3b6-492d-bcd2-5329e0aab642")
24+
.addActions(Action.newBuilder().setStandard(Action.StandardAction.STANDARD_ACTION_DECRYPT))
25+
.setExistingSubjectConditionSetId("9009fde8-d22b-4dfb-a456-f9ce6943244a")
26+
.build();
27+
28+
CreateSubjectMappingResponse resp = sdk.getServices().subjectMappings().createSubjectMapping(request).get();
29+
30+
SubjectMapping sm = resp.getSubjectMapping();
31+
32+
System.out.println(sm.getId());
33+
}
34+
}

0 commit comments

Comments
 (0)