Skip to content

Commit

Permalink
fix(sdk): make sdk auto closeable (#63)
Browse files Browse the repository at this point in the history
it contains resources that should be cleaned up when one is done with it
  • Loading branch information
mkleene committed Jun 6, 2024
1 parent df20e6d commit c1bbbb4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sdk/src/main/java/io/opentdf/platform/sdk/KASClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String getPublicKey(Config.KASInfo kasInfo) {
}

@Override
public void close() {
public synchronized void close() {
var entries = new ArrayList<>(stubs.values());
stubs.clear();
for (var entry: entries) {
Expand Down
20 changes: 16 additions & 4 deletions sdk/src/main/java/io/opentdf/platform/sdk/SDK.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.opentdf.platform.sdk;

import io.grpc.Channel;
import io.grpc.ManagedChannel;
import io.opentdf.platform.policy.attributes.AttributesServiceGrpc;
import io.opentdf.platform.policy.attributes.AttributesServiceGrpc.AttributesServiceFutureStub;
import io.opentdf.platform.policy.namespaces.NamespaceServiceGrpc;
Expand All @@ -14,29 +15,40 @@
* The SDK class represents a software development kit for interacting with the opentdf platform. It
* provides various services and stubs for making API calls to the opentdf platform.
*/
public class SDK {
public class SDK implements AutoCloseable {
private final Services services;

public interface KAS {
@Override
public void close() throws Exception {
services.close();
}

public interface KAS extends AutoCloseable {
String getPublicKey(Config.KASInfo kasInfo);
byte[] unwrap(Manifest.KeyAccess keyAccess, String policy);
}

// TODO: add KAS
public interface Services {
public interface Services extends AutoCloseable {
AttributesServiceFutureStub attributes();
NamespaceServiceFutureStub namespaces();
SubjectMappingServiceFutureStub subjectMappings();
ResourceMappingServiceFutureStub resourceMappings();
KAS kas();

static Services newServices(Channel channel, KAS kas) {
static Services newServices(ManagedChannel channel, KAS kas) {
var attributeService = AttributesServiceGrpc.newFutureStub(channel);
var namespaceService = NamespaceServiceGrpc.newFutureStub(channel);
var subjectMappingService = SubjectMappingServiceGrpc.newFutureStub(channel);
var resourceMappingService = ResourceMappingServiceGrpc.newFutureStub(channel);

return new Services() {
@Override
public void close() throws Exception {
channel.shutdownNow();
kas.close();
}

@Override
public AttributesServiceFutureStub attributes() {
return attributeService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ void sdkServicesSetup(boolean useSSL) throws Exception{

Server platformServicesServer = null;
Server kasServer = null;
SDK.Services services = null;
// we use the HTTP server for two things:
// * it returns the OIDC configuration we use at bootstrapping time
// * it fakes out being an IDP and returns an access token when need to retrieve an access token
Expand Down Expand Up @@ -223,7 +224,7 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, Re
certificate()).build());
}

SDK.Services services = servicesBuilder
services = servicesBuilder
.buildServices();

assertThat(services).isNotNull();
Expand Down Expand Up @@ -288,6 +289,9 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, Re
if (kasServer != null) {
kasServer.shutdownNow();
}
if (services != null) {
services.close();
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions sdk/src/test/java/io/opentdf/platform/sdk/TDFTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
public class TDFTest {

private static SDK.KAS kas = new SDK.KAS() {
@Override
public void close() {}

@Override
public String getPublicKey(Config.KASInfo kasInfo) {
int index = Integer.parseInt(kasInfo.URL);
Expand Down

0 comments on commit c1bbbb4

Please sign in to comment.