diff --git a/.github/workflows/checks.yaml b/.github/workflows/checks.yaml index c1507f1e..d24e2275 100644 --- a/.github/workflows/checks.yaml +++ b/.github/workflows/checks.yaml @@ -42,11 +42,12 @@ jobs: revert # Scopes include: # - ci: anything related to ci - # - cmdline: changes to @opentdf/ctl + # - cmdline: changes to cmdline # - docs: anything related solely to documentation # - main: bot generated commits - # - sdk: changes to @opentdf/sdk (was lib) + # - sdk: changes to sdk # - tests: test only changes + # - examples: examples only changes scopes: | ci cmdline @@ -54,6 +55,7 @@ jobs: main sdk tests + examples mavenverify: runs-on: ubuntu-latest diff --git a/examples/src/main/java/io/opentdf/platform/CreateAttribute.java b/examples/src/main/java/io/opentdf/platform/CreateAttribute.java index e81f556b..bd38a02f 100644 --- a/examples/src/main/java/io/opentdf/platform/CreateAttribute.java +++ b/examples/src/main/java/io/opentdf/platform/CreateAttribute.java @@ -1,37 +1,80 @@ package io.opentdf.platform; + import com.connectrpc.ResponseMessageKt; -import io.opentdf.platform.policy.Attribute; import io.opentdf.platform.policy.AttributeRuleTypeEnum; +import io.opentdf.platform.policy.Namespace; import io.opentdf.platform.policy.attributes.CreateAttributeRequest; import io.opentdf.platform.policy.attributes.CreateAttributeResponse; +import io.opentdf.platform.policy.namespaces.GetNamespaceRequest; import io.opentdf.platform.sdk.*; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.Collections; -import java.util.concurrent.ExecutionException; import java.util.Arrays; +import java.util.Objects; public class CreateAttribute { - public static void main(String[] args) { - String clientId = "opentdf"; - String clientSecret = "secret"; - String platformEndpoint = "localhost:8080"; + private static final Logger logger = LogManager.getLogger(CreateAttribute.class); + + public static void main(String[] args) { + + String clientId = "opentdf"; + String clientSecret = "secret"; + String platformEndpoint = "localhost:8080"; + String namespaceName = "opentdf.io"; + + SDKBuilder builder = new SDKBuilder(); - SDKBuilder builder = new SDKBuilder(); - SDK sdk = builder.platformEndpoint(platformEndpoint) - .clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true) - .build(); + try (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(); + Namespace namespace = + ResponseMessageKt.getOrThrow( + sdk.getServices() + .namespaces() + .getNamespaceBlocking( + GetNamespaceRequest.newBuilder() + .setFqn("https://" + namespaceName) + .build(), + Collections.emptyMap()) + .execute()) + .getNamespace(); - CreateAttributeResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().attributes().createAttributeBlocking(request, Collections.emptyMap()).execute()); + CreateAttributeRequest createAttributeRequest = + CreateAttributeRequest.newBuilder() + .setNamespaceId(namespace.getId()) + .setName("test-attribute") + .setRule( + AttributeRuleTypeEnum.forNumber( + AttributeRuleTypeEnum.ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF_VALUE)) + .addAllValues(Arrays.asList("test1", "test2")) + .build(); - Attribute attribute = resp.getAttribute(); + CreateAttributeResponse createAttributeResponse = + ResponseMessageKt.getOrThrow( + sdk.getServices() + .attributes() + .createAttributeBlocking(createAttributeRequest, Collections.emptyMap()) + .execute()); + logger.info( + "Successfully created attribute with ID: {}", + createAttributeResponse.getAttribute().getId()); + } catch (Exception e) { + if (Objects.equals(e.getMessage(), "resource not found")) { + logger.error("Namespace '{}' not found", namespaceName, e); + } else if (Objects.equals(e.getMessage(), "resource unique field violation")) { + logger.error("Attribute already exists", e); + } else { + logger.error("Failed to create attribute", e); + } } + } } diff --git a/examples/src/main/java/io/opentdf/platform/CreateNamespace.java b/examples/src/main/java/io/opentdf/platform/CreateNamespace.java index ff9b2407..ee7ac939 100644 --- a/examples/src/main/java/io/opentdf/platform/CreateNamespace.java +++ b/examples/src/main/java/io/opentdf/platform/CreateNamespace.java @@ -1,29 +1,55 @@ package io.opentdf.platform; + import com.connectrpc.ResponseMessageKt; import io.opentdf.platform.policy.namespaces.CreateNamespaceRequest; import io.opentdf.platform.policy.namespaces.CreateNamespaceResponse; import io.opentdf.platform.sdk.*; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.Collections; -import java.util.concurrent.ExecutionException; +import java.util.Objects; public class CreateNamespace { - public static void main(String[] args) { - String clientId = "opentdf"; - String clientSecret = "secret"; - String platformEndpoint = "localhost:8080"; + private static final Logger logger = LogManager.getLogger(CreateNamespace.class); + + public static void main(String[] args) { + + String clientId = "opentdf"; + String clientSecret = "secret"; + String platformEndpoint = "localhost:8080"; + String namespaceName = "opentdf.io"; + + SDKBuilder builder = new SDKBuilder(); + + try (SDK sdk = + builder + .platformEndpoint(platformEndpoint) + .clientSecret(clientId, clientSecret) + .useInsecurePlaintextConnection(true) + .build()) { - SDKBuilder builder = new SDKBuilder(); - SDK sdk = builder.platformEndpoint(platformEndpoint) - .clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true) - .build(); + CreateNamespaceRequest createNamespaceRequest = + CreateNamespaceRequest.newBuilder().setName(namespaceName).build(); - CreateNamespaceRequest request = CreateNamespaceRequest.newBuilder().setName("mynamespace.com").build(); + CreateNamespaceResponse createNamespaceResponse = + ResponseMessageKt.getOrThrow( + sdk.getServices() + .namespaces() + .createNamespaceBlocking(createNamespaceRequest, Collections.emptyMap()) + .execute()); - CreateNamespaceResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().namespaces().createNamespaceBlocking(request, Collections.emptyMap()).execute()); + logger.info( + "Successfully created namespace with ID: {}", + createNamespaceResponse.getNamespace().getId()); - System.out.println(resp.getNamespace().getName()); - + } catch (Exception e) { + if (Objects.equals(e.getMessage(), "resource unique field violation")) { + logger.error("Namespace '{}' already exists", namespaceName, e); + } else { + logger.error("Failed to create namespace", e); + } } + } } diff --git a/examples/src/main/java/io/opentdf/platform/CreateSubjectConditionSet.java b/examples/src/main/java/io/opentdf/platform/CreateSubjectConditionSet.java index 2f2481b8..8d7241ea 100644 --- a/examples/src/main/java/io/opentdf/platform/CreateSubjectConditionSet.java +++ b/examples/src/main/java/io/opentdf/platform/CreateSubjectConditionSet.java @@ -1,4 +1,5 @@ package io.opentdf.platform; + import com.connectrpc.ResponseMessageKt; import io.opentdf.platform.policy.Condition; import io.opentdf.platform.policy.ConditionBooleanTypeEnum; @@ -10,40 +11,64 @@ import io.opentdf.platform.policy.subjectmapping.CreateSubjectConditionSetResponse; import io.opentdf.platform.policy.subjectmapping.SubjectConditionSetCreate; import io.opentdf.platform.sdk.*; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.Collections; -import java.util.concurrent.ExecutionException; - public class CreateSubjectConditionSet { - public static void main(String[] args) { - String clientId = "opentdf"; - String clientSecret = "secret"; - String platformEndpoint = "localhost:8080"; + private static final Logger logger = LogManager.getLogger(CreateSubjectConditionSet.class); + + public static void main(String[] args) { + + String clientId = "opentdf"; + String clientSecret = "secret"; + String platformEndpoint = "localhost:8080"; + + SDKBuilder builder = new SDKBuilder(); + + try (SDK sdk = + builder + .platformEndpoint(platformEndpoint) + .clientSecret(clientId, clientSecret) + .useInsecurePlaintextConnection(true) + .build()) { - SDKBuilder builder = new SDKBuilder(); - SDK sdk = builder.platformEndpoint(platformEndpoint) - .clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true) - .build(); + SubjectSet.Builder subjectSetBuilder = + 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"))); - 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 createSubjectConditionSetRequest = + CreateSubjectConditionSetRequest.newBuilder() + .setSubjectConditionSet( + SubjectConditionSetCreate.newBuilder().addSubjectSets(subjectSetBuilder)) + .build(); - CreateSubjectConditionSetRequest request = CreateSubjectConditionSetRequest.newBuilder() - .setSubjectConditionSet( - SubjectConditionSetCreate.newBuilder().addSubjectSets(subjectset)) - .build(); + CreateSubjectConditionSetResponse createSubjectConditionSetResponse = + ResponseMessageKt.getOrThrow( + sdk.getServices() + .subjectMappings() + .createSubjectConditionSetBlocking( + createSubjectConditionSetRequest, Collections.emptyMap()) + .execute()); - CreateSubjectConditionSetResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().subjectMappings().createSubjectConditionSetBlocking(request, Collections.emptyMap()).execute()); + SubjectConditionSet subjectConditionSet = + createSubjectConditionSetResponse.getSubjectConditionSet(); - SubjectConditionSet scs = resp.getSubjectConditionSet(); + logger.info( + "Successfully created subject condition set with ID: {}", subjectConditionSet.getId()); - System.out.println(scs.getId()); + } catch (Exception e) { + logger.error("Failed to create subject condition set", e); } + } } diff --git a/examples/src/main/java/io/opentdf/platform/CreateSubjectMapping.java b/examples/src/main/java/io/opentdf/platform/CreateSubjectMapping.java index 7425a7c2..9a1b6657 100644 --- a/examples/src/main/java/io/opentdf/platform/CreateSubjectMapping.java +++ b/examples/src/main/java/io/opentdf/platform/CreateSubjectMapping.java @@ -1,36 +1,146 @@ package io.opentdf.platform; + import com.connectrpc.ResponseMessageKt; import io.opentdf.platform.policy.Action; +import io.opentdf.platform.policy.Attribute; +import io.opentdf.platform.policy.Condition; +import io.opentdf.platform.policy.ConditionBooleanTypeEnum; +import io.opentdf.platform.policy.ConditionGroup; +import io.opentdf.platform.policy.Namespace; +import io.opentdf.platform.policy.SubjectConditionSet; import io.opentdf.platform.policy.SubjectMapping; +import io.opentdf.platform.policy.SubjectMappingOperatorEnum; +import io.opentdf.platform.policy.SubjectSet; +import io.opentdf.platform.policy.attributes.GetAttributeRequest; +import io.opentdf.platform.policy.namespaces.GetNamespaceRequest; +import io.opentdf.platform.policy.subjectmapping.CreateSubjectConditionSetRequest; +import io.opentdf.platform.policy.subjectmapping.CreateSubjectConditionSetResponse; import io.opentdf.platform.policy.subjectmapping.CreateSubjectMappingRequest; import io.opentdf.platform.policy.subjectmapping.CreateSubjectMappingResponse; -import io.opentdf.platform.sdk.*; +import io.opentdf.platform.policy.subjectmapping.SubjectConditionSetCreate; +import io.opentdf.platform.sdk.SDK; +import io.opentdf.platform.sdk.SDKBuilder; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.Collections; -import java.util.concurrent.ExecutionException; +import java.util.Objects; public class CreateSubjectMapping { - public static void main(String[] args) { - String clientId = "opentdf"; - String clientSecret = "secret"; - String platformEndpoint = "localhost:8080"; + private static final Logger logger = LogManager.getLogger(CreateSubjectMapping.class); + + public static void main(String[] args) { + String clientId = "opentdf"; + String clientSecret = "secret"; + String platformEndpoint = "localhost:8080"; + String namespaceName = "opentdf.io"; + String attributeName = "test-attribute"; + + SDKBuilder builder = new SDKBuilder(); + try (SDK sdk = + builder + .platformEndpoint(platformEndpoint) + .clientSecret(clientId, clientSecret) + .useInsecurePlaintextConnection(true) + .build()) { + + Namespace namespace; + + try { + namespace = + ResponseMessageKt.getOrThrow( + sdk.getServices() + .namespaces() + .getNamespaceBlocking( + GetNamespaceRequest.newBuilder() + .setFqn("https://" + namespaceName) + .build(), + Collections.emptyMap()) + .execute()) + .getNamespace(); + } catch (Exception e) { + if (Objects.equals(e.getMessage(), "resource not found")) { + logger.error("Namespace '{}' not found", namespaceName, e); + } else { + logger.error("Failed to retrieve namespace '{}'", namespaceName, e); + } + return; + } + + Attribute attribute; + String attributeFqn = namespace.getFqn() + "/attr/" + attributeName; + + try { + GetAttributeRequest getAttributeRequest = + GetAttributeRequest.newBuilder().setFqn(attributeFqn).build(); + + attribute = + ResponseMessageKt.getOrThrow( + sdk.getServices() + .attributes() + .getAttributeBlocking(getAttributeRequest, Collections.emptyMap()) + .execute()) + .getAttribute(); + + } catch (Exception e) { + if (Objects.equals(e.getMessage(), "resource not found")) { + logger.error("Attribute '{}' not found", attributeFqn, e); + } else { + logger.error("Failed to retrieve attribute '{}'", attributeFqn, e); + } + return; + } + + CreateSubjectConditionSetRequest subjectConditionSetRequest = + CreateSubjectConditionSetRequest.newBuilder() + .setSubjectConditionSet( + SubjectConditionSetCreate.newBuilder() + .addSubjectSets( + 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"))))) + .build(); + + CreateSubjectConditionSetResponse subjectConditionSetResponse = + ResponseMessageKt.getOrThrow( + sdk.getServices() + .subjectMappings() + .createSubjectConditionSetBlocking( + subjectConditionSetRequest, Collections.emptyMap()) + .execute()); - SDKBuilder builder = new SDKBuilder(); - SDK sdk = builder.platformEndpoint(platformEndpoint) - .clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true) - .build(); + SubjectConditionSet subjectConditionSet = + subjectConditionSetResponse.getSubjectConditionSet(); - 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(); + CreateSubjectMappingRequest request = + CreateSubjectMappingRequest.newBuilder() + .setAttributeValueId(attribute.getValues(0).getId()) + .addActions(Action.newBuilder().setName("read")) + .setExistingSubjectConditionSetId(subjectConditionSet.getId()) + .build(); - CreateSubjectMappingResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().subjectMappings().createSubjectMappingBlocking(request, Collections.emptyMap()).execute()); + CreateSubjectMappingResponse resp = + ResponseMessageKt.getOrThrow( + sdk.getServices() + .subjectMappings() + .createSubjectMappingBlocking(request, Collections.emptyMap()) + .execute()); - SubjectMapping sm = resp.getSubjectMapping(); + SubjectMapping subjectMapping = resp.getSubjectMapping(); - System.out.println(sm.getId()); + logger.info("Successfully created subject mapping with ID: {}", subjectMapping.getId()); + } catch (Exception e) { + logger.error("Failed to create subject mapping", e); } + } } diff --git a/examples/src/main/java/io/opentdf/platform/GetDecisions.java b/examples/src/main/java/io/opentdf/platform/GetDecisions.java index 9dbd533e..f9126f86 100644 --- a/examples/src/main/java/io/opentdf/platform/GetDecisions.java +++ b/examples/src/main/java/io/opentdf/platform/GetDecisions.java @@ -1,4 +1,5 @@ package io.opentdf.platform; + import com.connectrpc.ResponseMessageKt; import io.opentdf.platform.authorization.DecisionRequest; import io.opentdf.platform.authorization.DecisionResponse; @@ -9,36 +10,68 @@ import io.opentdf.platform.authorization.ResourceAttribute; import io.opentdf.platform.policy.Action; import io.opentdf.platform.sdk.*; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.Collections; -import java.util.concurrent.ExecutionException; import java.util.List; +import java.util.stream.Collectors; public class GetDecisions { - public static void main(String[] args) { - - 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(); - - GetDecisionsRequest request = GetDecisionsRequest.newBuilder() - .addDecisionRequests(DecisionRequest.newBuilder() - .addEntityChains(EntityChain.newBuilder().setId("ec1").addEntities(Entity.newBuilder().setId("entity-1").setClientId("opentdf"))) - .addActions(Action.newBuilder().setStandard(Action.StandardAction.STANDARD_ACTION_DECRYPT)) - .addResourceAttributes(ResourceAttribute.newBuilder().setResourceAttributesId("resource-attribute-1") - .addAttributeValueFqns("https://mynamespace.com/attr/test/value/test1")) - ).build(); - - GetDecisionsResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().authorization().getDecisionsBlocking(request, Collections.emptyMap()).execute()); - - List decisions = resp.getDecisionResponsesList(); - - System.out.println(DecisionResponse.Decision.forNumber(decisions.get(0).getDecisionValue())); + + private static final Logger logger = LogManager.getLogger(GetDecisions.class); + + public static void main(String[] args) { + + String clientId = "opentdf"; + String clientSecret = "secret"; + String platformEndpoint = "localhost:8080"; + String namespaceName = "opentdf.io"; + + SDKBuilder builder = new SDKBuilder(); + + try (SDK sdk = + builder + .platformEndpoint(platformEndpoint) + .clientSecret(clientId, clientSecret) + .useInsecurePlaintextConnection(true) + .build()) { + + GetDecisionsRequest request = + GetDecisionsRequest.newBuilder() + .addDecisionRequests( + DecisionRequest.newBuilder() + .addEntityChains( + EntityChain.newBuilder() + .setId("ec1") + .addEntities( + Entity.newBuilder().setId("entity-1").setClientId("opentdf"))) + .addActions(Action.newBuilder().setName("read")) + .addResourceAttributes( + ResourceAttribute.newBuilder() + .setResourceAttributesId("resource-attribute-1") + .addAttributeValueFqns( + "https://" + namespaceName + "/attr/test/value/test1"))) + .build(); + + GetDecisionsResponse getDecisionsResponse = + ResponseMessageKt.getOrThrow( + sdk.getServices() + .authorization() + .getDecisionsBlocking(request, Collections.emptyMap()) + .execute()); + + List decisions = getDecisionsResponse.getDecisionResponsesList(); + + logger.info( + "Successfully retrieved decisions: [{}]", + decisions.stream() + .map(DecisionResponse::getDecision) + .map(DecisionResponse.Decision::toString) + .collect(Collectors.joining(", "))); + } catch (Exception e) { + logger.error("Failed to get decisions", e); } + } } diff --git a/examples/src/main/java/io/opentdf/platform/GetEntitlements.java b/examples/src/main/java/io/opentdf/platform/GetEntitlements.java index f9479577..a1719626 100644 --- a/examples/src/main/java/io/opentdf/platform/GetEntitlements.java +++ b/examples/src/main/java/io/opentdf/platform/GetEntitlements.java @@ -1,35 +1,59 @@ package io.opentdf.platform; + import com.connectrpc.ResponseMessageKt; import io.opentdf.platform.authorization.Entity; import io.opentdf.platform.authorization.EntityEntitlements; import io.opentdf.platform.authorization.GetEntitlementsRequest; import io.opentdf.platform.authorization.GetEntitlementsResponse; import io.opentdf.platform.sdk.*; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; public class GetEntitlements { - public static void main(String[] args) { - - 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(); - - GetEntitlementsRequest request = GetEntitlementsRequest.newBuilder() - .addEntities(Entity.newBuilder().setId("entity-1").setClientId("opentdf")) - .build(); - - GetEntitlementsResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().authorization().getEntitlementsBlocking(request, Collections.emptyMap()).execute()); - - List entitlements = resp.getEntitlementsList(); - - System.out.println(entitlements.get(0).getAttributeValueFqnsList()); + private static final Logger logger = LogManager.getLogger(GetEntitlements.class); + + public static void main(String[] args) { + + String clientId = "opentdf"; + String clientSecret = "secret"; + String platformEndpoint = "localhost:8080"; + + SDKBuilder builder = new SDKBuilder(); + + try (SDK sdk = + builder + .platformEndpoint(platformEndpoint) + .clientSecret(clientId, clientSecret) + .useInsecurePlaintextConnection(true) + .build()) { + + GetEntitlementsRequest request = + GetEntitlementsRequest.newBuilder() + .addEntities(Entity.newBuilder().setId("entity-1").setClientId("opentdf")) + .build(); + + GetEntitlementsResponse getEntitlementsResponse = + ResponseMessageKt.getOrThrow( + sdk.getServices() + .authorization() + .getEntitlementsBlocking(request, Collections.emptyMap()) + .execute()); + + List entitlements = getEntitlementsResponse.getEntitlementsList(); + + logger.info( + "Successfully retrieved entitlements: [{}]", + entitlements.stream() + .map(EntityEntitlements::getAttributeValueFqnsList) + .map(List::toString) + .collect(Collectors.joining(", "))); + } catch (Exception e) { + logger.error("Failed to get entitlements", e); } + } } diff --git a/examples/src/main/java/io/opentdf/platform/ListAttributes.java b/examples/src/main/java/io/opentdf/platform/ListAttributes.java index 96b17f16..be7ee91f 100644 --- a/examples/src/main/java/io/opentdf/platform/ListAttributes.java +++ b/examples/src/main/java/io/opentdf/platform/ListAttributes.java @@ -1,34 +1,54 @@ package io.opentdf.platform; + import com.connectrpc.ResponseMessageKt; import io.opentdf.platform.policy.Attribute; import io.opentdf.platform.policy.attributes.ListAttributesRequest; import io.opentdf.platform.policy.attributes.ListAttributesResponse; import io.opentdf.platform.sdk.*; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.Collections; -import java.util.concurrent.ExecutionException; import java.util.List; +import java.util.stream.Collectors; public class ListAttributes { - public static void main(String[] args) { + private static final Logger logger = LogManager.getLogger(ListAttributes.class); + + public static void main(String[] args) { + + String clientId = "opentdf"; + String clientSecret = "secret"; + String platformEndpoint = "localhost:8080"; + String namespaceName = "opentdf.io"; - String clientId = "opentdf"; - String clientSecret = "secret"; - String platformEndpoint = "localhost:8080"; + SDKBuilder builder = new SDKBuilder(); - SDKBuilder builder = new SDKBuilder(); - SDK sdk = builder.platformEndpoint(platformEndpoint) - .clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true) - .build(); + try (SDK sdk = + builder + .platformEndpoint(platformEndpoint) + .clientSecret(clientId, clientSecret) + .useInsecurePlaintextConnection(true) + .build()) { - ListAttributesRequest request = ListAttributesRequest.newBuilder() - .setNamespace("mynamespace.com").build(); + ListAttributesRequest request = + ListAttributesRequest.newBuilder().setNamespace(namespaceName).build(); - ListAttributesResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().attributes().listAttributesBlocking(request, Collections.emptyMap()).execute()); + ListAttributesResponse listAttributesResponse = + ResponseMessageKt.getOrThrow( + sdk.getServices() + .attributes() + .listAttributesBlocking(request, Collections.emptyMap()) + .execute()); - List attributes = resp.getAttributesList(); + List attributes = listAttributesResponse.getAttributesList(); - System.out.println(resp.getAttributesCount()); + logger.info( + "Successfully retrieved attributes: [{}]", + attributes.stream().map(Attribute::getFqn).collect(Collectors.joining(", "))); + } catch (Exception e) { + logger.error("Failed to list attributes", e); } + } } diff --git a/examples/src/main/java/io/opentdf/platform/ListNamespaces.java b/examples/src/main/java/io/opentdf/platform/ListNamespaces.java index 61bc0d5f..f728ca63 100644 --- a/examples/src/main/java/io/opentdf/platform/ListNamespaces.java +++ b/examples/src/main/java/io/opentdf/platform/ListNamespaces.java @@ -1,30 +1,51 @@ package io.opentdf.platform; + import com.connectrpc.ResponseMessageKt; import io.opentdf.platform.policy.Namespace; import io.opentdf.platform.policy.namespaces.ListNamespacesRequest; import io.opentdf.platform.policy.namespaces.ListNamespacesResponse; import io.opentdf.platform.sdk.*; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.Collections; import java.util.List; -import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; public class ListNamespaces { - public static void main(String[] args) { + private static final Logger logger = LogManager.getLogger(ListNamespaces.class); + + public static void main(String[] args) { + + String clientId = "opentdf"; + String clientSecret = "secret"; + String platformEndpoint = "localhost:8080"; + + SDKBuilder builder = new SDKBuilder(); - String clientId = "opentdf"; - String clientSecret = "secret"; - String platformEndpoint = "localhost:8080"; + try (SDK sdk = + builder + .platformEndpoint(platformEndpoint) + .clientSecret(clientId, clientSecret) + .useInsecurePlaintextConnection(true) + .build()) { - SDKBuilder builder = new SDKBuilder(); - SDK sdk = builder.platformEndpoint(platformEndpoint) - .clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true) - .build(); + ListNamespacesRequest request = ListNamespacesRequest.newBuilder().build(); - ListNamespacesRequest request = ListNamespacesRequest.newBuilder().build(); + ListNamespacesResponse listNamespacesResponse = + ResponseMessageKt.getOrThrow( + sdk.getServices() + .namespaces() + .listNamespacesBlocking(request, Collections.emptyMap()) + .execute()); - ListNamespacesResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().namespaces().listNamespacesBlocking(request, Collections.emptyMap()).execute()); + List namespaces = listNamespacesResponse.getNamespacesList(); - List namespaces = resp.getNamespacesList(); + logger.info( + "Successfully retrieved namespaces: [{}]", + namespaces.stream().map(Namespace::getFqn).collect(Collectors.joining(", "))); + } catch (Exception e) { + logger.error("Failed to list namespaces", e); } + } } diff --git a/examples/src/main/java/io/opentdf/platform/ListSubjectMappings.java b/examples/src/main/java/io/opentdf/platform/ListSubjectMappings.java index 9a771be6..a69e1aae 100644 --- a/examples/src/main/java/io/opentdf/platform/ListSubjectMappings.java +++ b/examples/src/main/java/io/opentdf/platform/ListSubjectMappings.java @@ -1,34 +1,53 @@ package io.opentdf.platform; + import com.connectrpc.ResponseMessageKt; import io.opentdf.platform.policy.SubjectMapping; import io.opentdf.platform.policy.subjectmapping.ListSubjectMappingsRequest; import io.opentdf.platform.policy.subjectmapping.ListSubjectMappingsResponse; import io.opentdf.platform.sdk.*; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.Collections; -import java.util.concurrent.ExecutionException; import java.util.List; +import java.util.stream.Collectors; public class ListSubjectMappings { - public static void main(String[] args) { + private static final Logger logger = LogManager.getLogger(ListSubjectMappings.class); + + public static void main(String[] args) { + + String clientId = "opentdf"; + String clientSecret = "secret"; + String platformEndpoint = "localhost:8080"; + + SDKBuilder builder = new SDKBuilder(); - String clientId = "opentdf"; - String clientSecret = "secret"; - String platformEndpoint = "localhost:8080"; + try (SDK sdk = + builder + .platformEndpoint(platformEndpoint) + .clientSecret(clientId, clientSecret) + .useInsecurePlaintextConnection(true) + .build()) { - SDKBuilder builder = new SDKBuilder(); - SDK sdk = builder.platformEndpoint(platformEndpoint) - .clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true) - .build(); - - ListSubjectMappingsRequest request = ListSubjectMappingsRequest.newBuilder().build(); + ListSubjectMappingsRequest listSubjectMappingsRequest = + ListSubjectMappingsRequest.newBuilder().build(); - ListSubjectMappingsResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().subjectMappings().listSubjectMappingsBlocking(request, Collections.emptyMap()).execute()); + ListSubjectMappingsResponse listSubjectMappingsResponse = + ResponseMessageKt.getOrThrow( + sdk.getServices() + .subjectMappings() + .listSubjectMappingsBlocking(listSubjectMappingsRequest, Collections.emptyMap()) + .execute()); - List sms = resp.getSubjectMappingsList(); + List subjectMappings = listSubjectMappingsResponse.getSubjectMappingsList(); - System.out.println(sms.size()); - System.out.println(sms.get(0).getId()); + logger.info( + "Successfully retrieved subject mappings: [{}]", + subjectMappings.stream().map(SubjectMapping::getId).collect(Collectors.joining(", "))); + } catch (Exception e) { + logger.error("Failed to list subject mappings", e); } + } }