Managing Policy
Creating a Namespace
- Go
- Java
- Javascript
package main
import (
"context"
"log"
"github.com/opentdf/platform/protocol/go/policy/namespaces"
"github.com/opentdf/platform/sdk"
)
func main() {
platformEndpoint := "https://opentdf.io"
// Create a new client
client, err := sdk.New(
platformEndpoint,
sdk.WithClientCredentials("opentdf", "secret", nil),
)
if err != nil {
log.Fatal(err)
}
// Create Namesapce
namespace := &namespaces.CreateNamespaceRequest{
Name: "opentdf.io",
}
_, err = client.Namespaces.CreateNamespace(context.Background(), namespace)
if err != nil {
log.Fatal(err)
}
}
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());
}
}
List Namespaces
- Go
- Java
- Javascript
package main
import (
"context"
"log"
"github.com/opentdf/platform/protocol/go/policy/namespaces"
"github.com/opentdf/platform/sdk"
)
func main() {
platformEndpoint := "http://localhost:9002"
// Create a new client
client, err := sdk.New(
platformEndpoint,
sdk.WithClientCredentials("opentdf", "secret", nil),
)
if err != nil {
log.Fatal(err)
}
// List All Namespaces
namespaces, err := client.Namespaces.ListNamespaces(context.Background(), &namespaces.ListNamespacesRequest{})
if err != nil {
log.Fatal(err)
}
for _, namespace := range namespaces.GetNamespaces() {
log.Printf("Namespace: %s\n", namespace.GetName())
}
}
package io.opentdf.platform;
import io.opentdf.platform.sdk.*;
import java.util.concurrent.ExecutionException;
import io.opentdf.platform.policy.namespaces.*;
import io.opentdf.platform.policy.Namespace;
public class ListNamespaces {
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();
ListNamespacesRequest request = ListNamespacesRequest.newBuilder().build();
ListNamespacesResponse resp = sdk.getServices().namespaces().listNamespaces(request).get();
java.util.List<Namespace> namespaces = resp.getNamespacesList();
}
}
Create Attribute
- Go
- Java
- Javascript
package main
import (
"context"
"log"
"github.com/opentdf/platform/protocol/go/policy"
"github.com/opentdf/platform/protocol/go/policy/attributes"
"github.com/opentdf/platform/sdk"
)
func main() {
platformEndpoint := "http://localhost:9002"
// Create a new client
client, err := sdk.New(
platformEndpoint,
sdk.WithClientCredentials("opentdf", "secret", nil),
)
if err != nil {
log.Fatal(err)
}
// Create a new attribute
namespaceID := "f9ac9403-a12f-4ed3-b3c9-a46910361b4d"
attrRequest := &attributes.CreateAttributeRequest{
NamespaceId: namespaceID,
Name: "role",
Rule: policy.AttributeRuleTypeEnum_ATTRIBUTE_RULE_TYPE_ENUM_ANY_OF,
Values: []string{"admin", "developer", "guest"},
}
_, err = client.Attributes.CreateAttribute(context.Background(), attrRequest)
if err != nil {
log.Fatal(err)
}
}
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();
}
}
List Attributes
- Go
- Java
- Javascript
package main
import (
"context"
"log"
"github.com/opentdf/platform/protocol/go/policy/attributes"
"github.com/opentdf/platform/sdk"
)
func main() {
platformEndpoint := "http://localhost:9002"
// Create a new client
client, err := sdk.New(
platformEndpoint,
sdk.WithClientCredentials("opentdf", "secret", nil),
)
if err != nil {
log.Fatal(err)
}
// List attributes
attrs, err := client.Attributes.ListAttributes(context.Background(), &attributes.ListAttributesRequest{})
if err != nil {
log.Fatal(err)
}
for _, attr := range attrs.GetAttributes() {
log.Printf("Attribute: %s, ID: %s, ", attr.GetName(), attr.GetId())
for _, value := range attr.Values {
log.Printf("Value: %s, ID: %s", value.GetValue(), value.GetId())
}
}
}
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.List;
public class ListAttributes {
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();
ListAttributesRequest request = ListAttributesRequest.newBuilder()
.setNamespace("mynamespace.com").build();
ListAttributesResponse resp = sdk.getServices().attributes().listAttributes(request).get();
List<Attribute> attributes = resp.getAttributesList();
System.out.println(resp.getAttributesCount());
}
}
Create Subject Condition Set
- Go
- Java
- Javascript
package main
import (
"context"
"log"
"github.com/opentdf/platform/protocol/go/policy"
"github.com/opentdf/platform/protocol/go/policy/subjectmapping"
"github.com/opentdf/platform/sdk"
)
func main() {
platformEndpoint := "http://localhost:9002"
// Create a new client
client, err := sdk.New(
platformEndpoint,
sdk.WithClientCredentials("opentdf", "secret", nil),
)
if err != nil {
log.Fatal(err)
}
// Create Subject Condition Set
coditionset := &subjectmapping.CreateSubjectConditionSetRequest{
SubjectConditionSet: &subjectmapping.SubjectConditionSetCreate{
SubjectSets: []*policy.SubjectSet{
{
ConditionGroups: []*policy.ConditionGroup{
{
BooleanOperator: policy.ConditionBooleanTypeEnum_CONDITION_BOOLEAN_TYPE_ENUM_AND,
Conditions: []*policy.Condition{
{
SubjectExternalSelectorValue: ".clientId",
Operator: policy.SubjectMappingOperatorEnum_SUBJECT_MAPPING_OPERATOR_ENUM_IN,
SubjectExternalValues: []string{"opentdf"},
},
},
},
},
},
},
},
}
_, err = client.SubjectMapping.CreateSubjectConditionSet(context.Background(), coditionset)
if err != nil {
log.Fatal(err)
}
}
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());
}
}
Create Subject Mapping
- Go
- Java
- Javascript
package main
import (
"context"
"log"
"github.com/opentdf/platform/protocol/go/policy"
"github.com/opentdf/platform/protocol/go/policy/subjectmapping"
"github.com/opentdf/platform/sdk"
)
func main() {
platformEndpoint := "http://localhost:9002"
// Create a new client
client, err := sdk.New(
platformEndpoint,
sdk.WithClientCredentials("opentdf", "secret", nil),
)
if err != nil {
log.Fatal(err)
}
// Create Subject Mapping
subjectMapping := &subjectmapping.CreateSubjectMappingRequest{
AttributeValueId: "224c9d29-2cd4-4a38-b6ad-5f025ca93a8c",
Actions: []*policy.Action{
{
Value: &policy.Action_Standard{
Standard: policy.Action_STANDARD_ACTION_DECRYPT,
},
},
},
ExistingSubjectConditionSetId: "890b26db-4ee4-447f-ae8a-2862d922eeef",
}
_, err = client.SubjectMapping.CreateSubjectMapping(context.Background(), subjectMapping)
if err != nil {
log.Fatal(err)
}
}
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());
}
}
List Subject Mappings
- Go
- Java
- Javascript
package main
import (
"context"
"log"
"github.com/opentdf/platform/protocol/go/policy/subjectmapping"
"github.com/opentdf/platform/sdk"
)
func main() {
platformEndpoint := "http://localhost:9002"
// Create a new client
client, err := sdk.New(
platformEndpoint,
sdk.WithClientCredentials("opentdf", "secret", nil),
)
if err != nil {
log.Fatal(err)
}
// List Subject Mapping
subjectmappings, err := client.SubjectMapping.ListSubjectMappings(context.Background(), &subjectmapping.ListSubjectMappingsRequest{})
if err != nil {
log.Fatal(err)
}
for _, sm := range subjectmappings.GetSubjectMappings() {
log.Printf("Subject Mapping: %s", sm.GetId())
}
}
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 java.util.List;
public class ListSubjectMappings {
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();
ListSubjectMappingsRequest request = ListSubjectMappingsRequest.newBuilder().build();
ListSubjectMappingsResponse resp = sdk.getServices().subjectMappings().listSubjectMappings(request).get();
List<SubjectMapping> sms = resp.getSubjectMappingsList();
System.out.println(sms.size());
System.out.println(sms.get(0).getId());
}
}