Skip to content

Commit 1d3aeee

Browse files
mkleenesujankota
authored andcommitted
fix(sdk): make sdk auto closeable (#63)
it contains resources that should be cleaned up when one is done with it
1 parent 9abe7b1 commit 1d3aeee

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

sdk/src/main/java/io/opentdf/platform/sdk/KASClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public String getPublicKey(Config.KASInfo kasInfo) {
6868
}
6969

7070
@Override
71-
public void close() {
71+
public synchronized void close() {
7272
var entries = new ArrayList<>(stubs.values());
7373
stubs.clear();
7474
for (var entry: entries) {

sdk/src/main/java/io/opentdf/platform/sdk/SDK.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.opentdf.platform.sdk;
22

33
import io.grpc.Channel;
4+
import io.grpc.ManagedChannel;
45
import io.opentdf.platform.policy.attributes.AttributesServiceGrpc;
56
import io.opentdf.platform.policy.attributes.AttributesServiceGrpc.AttributesServiceFutureStub;
67
import io.opentdf.platform.policy.namespaces.NamespaceServiceGrpc;
@@ -15,31 +16,42 @@
1516
* The SDK class represents a software development kit for interacting with the opentdf platform. It
1617
* provides various services and stubs for making API calls to the opentdf platform.
1718
*/
18-
public class SDK {
19+
public class SDK implements AutoCloseable {
1920
private final Services services;
2021

21-
public interface KAS {
22+
@Override
23+
public void close() throws Exception {
24+
services.close();
25+
}
26+
27+
public interface KAS extends AutoCloseable {
2228
String getPublicKey(Config.KASInfo kasInfo);
2329
String getECPublicKey(Config.KASInfo kasInfo, NanoTDFType.ECCurve curve);
2430
byte[] unwrap(Manifest.KeyAccess keyAccess, String policy);
2531
byte[] unwrapNanoTDF(NanoTDFType.ECCurve curve, String header, String kasURL);
2632
}
2733

2834
// TODO: add KAS
29-
public interface Services {
35+
public interface Services extends AutoCloseable {
3036
AttributesServiceFutureStub attributes();
3137
NamespaceServiceFutureStub namespaces();
3238
SubjectMappingServiceFutureStub subjectMappings();
3339
ResourceMappingServiceFutureStub resourceMappings();
3440
KAS kas();
3541

36-
static Services newServices(Channel channel, KAS kas) {
42+
static Services newServices(ManagedChannel channel, KAS kas) {
3743
var attributeService = AttributesServiceGrpc.newFutureStub(channel);
3844
var namespaceService = NamespaceServiceGrpc.newFutureStub(channel);
3945
var subjectMappingService = SubjectMappingServiceGrpc.newFutureStub(channel);
4046
var resourceMappingService = ResourceMappingServiceGrpc.newFutureStub(channel);
4147

4248
return new Services() {
49+
@Override
50+
public void close() throws Exception {
51+
channel.shutdownNow();
52+
kas.close();
53+
}
54+
4355
@Override
4456
public AttributesServiceFutureStub attributes() {
4557
return attributeService;

sdk/src/test/java/io/opentdf/platform/sdk/SDKBuilderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ void sdkServicesSetup(boolean useSSL) throws Exception{
114114

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

226-
SDK.Services services = servicesBuilder
227+
services = servicesBuilder
227228
.buildServices();
228229

229230
assertThat(services).isNotNull();
@@ -288,6 +289,9 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, Re
288289
if (kasServer != null) {
289290
kasServer.shutdownNow();
290291
}
292+
if (services != null) {
293+
services.close();
294+
}
291295
}
292296
}
293297

sdk/src/test/java/io/opentdf/platform/sdk/TDFTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
public class TDFTest {
2626

2727
private static SDK.KAS kas = new SDK.KAS() {
28+
@Override
29+
public void close() {}
30+
2831
@Override
2932
public String getPublicKey(Config.KASInfo kasInfo) {
3033
int index = Integer.parseInt(kasInfo.URL);

0 commit comments

Comments
 (0)