Skip to content

Latest commit

 

History

History
115 lines (86 loc) · 2.97 KB

File metadata and controls

115 lines (86 loc) · 2.97 KB

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Create Attribute
package main

import (
	"context"
	"crypto/rand"
	"log"

	"github.com/opentdf/platform/protocol/go/policy"
	"github.com/opentdf/platform/protocol/go/policy/attributes"
	"github.com/opentdf/platform/protocol/go/policy/namespaces"
	"github.com/opentdf/platform/sdk"
)

func main() {

	platformEndpoint := "http://localhost:8080"

	// Create a new client
	client, err := sdk.New(
		platformEndpoint,
		sdk.WithClientCredentials("opentdf", "secret", nil),
	)

	if err != nil {
		log.Fatal(err)
	}

	// List namespaces to get a namespace ID
	listResponse, err := client.Namespaces.ListNamespaces(context.Background(), &namespaces.ListNamespacesRequest{})
	if err != nil {
		log.Fatalf("failed to list namespaces: %s", err)
	}

	if len(listResponse.GetNamespaces()) == 0 {
		log.Fatal("no namespaces found")
	}

	namespaceID := listResponse.GetNamespaces()[0].GetId()

	// Create a new attribute
	attrRequest := &attributes.CreateAttributeRequest{
		NamespaceId: namespaceID,
		Name:        "role" + "-" + rand.Text()[:4],
		Rule:        policy.AttributeRuleTypeEnum_ATTRIBUTE_RULE_TYPE_ENUM_ANY_OF,
		Values:      []string{"admin", "developer", "guest"},
	}

	attribute, err := client.Attributes.CreateAttribute(context.Background(), attrRequest)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Created attribute: %s with ID: %s in namespace: %s\n", attribute.GetAttribute().Name, attribute.GetAttribute().GetId(), namespaceID)
}
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 = "http://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();

    }
}