Skip to content

Commit c1bbbb4

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

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
@@ -52,7 +52,7 @@ public String getPublicKey(Config.KASInfo kasInfo) {
5252
}
5353

5454
@Override
55-
public void close() {
55+
public synchronized void close() {
5656
var entries = new ArrayList<>(stubs.values());
5757
stubs.clear();
5858
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;
@@ -14,29 +15,40 @@
1415
* The SDK class represents a software development kit for interacting with the opentdf platform. It
1516
* provides various services and stubs for making API calls to the opentdf platform.
1617
*/
17-
public class SDK {
18+
public class SDK implements AutoCloseable {
1819
private final Services services;
1920

20-
public interface KAS {
21+
@Override
22+
public void close() throws Exception {
23+
services.close();
24+
}
25+
26+
public interface KAS extends AutoCloseable {
2127
String getPublicKey(Config.KASInfo kasInfo);
2228
byte[] unwrap(Manifest.KeyAccess keyAccess, String policy);
2329
}
2430

2531
// TODO: add KAS
26-
public interface Services {
32+
public interface Services extends AutoCloseable {
2733
AttributesServiceFutureStub attributes();
2834
NamespaceServiceFutureStub namespaces();
2935
SubjectMappingServiceFutureStub subjectMappings();
3036
ResourceMappingServiceFutureStub resourceMappings();
3137
KAS kas();
3238

33-
static Services newServices(Channel channel, KAS kas) {
39+
static Services newServices(ManagedChannel channel, KAS kas) {
3440
var attributeService = AttributesServiceGrpc.newFutureStub(channel);
3541
var namespaceService = NamespaceServiceGrpc.newFutureStub(channel);
3642
var subjectMappingService = SubjectMappingServiceGrpc.newFutureStub(channel);
3743
var resourceMappingService = ResourceMappingServiceGrpc.newFutureStub(channel);
3844

3945
return new Services() {
46+
@Override
47+
public void close() throws Exception {
48+
channel.shutdownNow();
49+
kas.close();
50+
}
51+
4052
@Override
4153
public AttributesServiceFutureStub attributes() {
4254
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
@@ -22,6 +22,9 @@
2222
public class TDFTest {
2323

2424
private static SDK.KAS kas = new SDK.KAS() {
25+
@Override
26+
public void close() {}
27+
2528
@Override
2629
public String getPublicKey(Config.KASInfo kasInfo) {
2730
int index = Integer.parseInt(kasInfo.URL);

0 commit comments

Comments
 (0)