Skip to content

fix(sdk): make sdk auto closeable #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading