Skip to content

Commit 205d834

Browse files
committed
feat: ability to set connect-rpc protocol
1 parent d337704 commit 205d834

4 files changed

Lines changed: 113 additions & 2 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.opentdf.platform.sdk;
2+
3+
import com.connectrpc.protocols.NetworkProtocol;
4+
5+
/**
6+
* Enumeration of supported network protocols for SDK communication.
7+
*
8+
* This enum provides a mapping between SDK protocol types and the underlying
9+
* Connect-RPC NetworkProtocol values, allowing flexible configuration of the
10+
* communication protocol used for platform services.
11+
*/
12+
public enum ProtocolType {
13+
/**
14+
* Connect's native protocol - HTTP-based with support for HTTP/1.1, HTTP/2, and HTTP/3.
15+
* Supports both JSON and binary Protobuf encoding with streaming capabilities.
16+
* This is the recommended default for new applications.
17+
*/
18+
CONNECT(NetworkProtocol.CONNECT),
19+
20+
/**
21+
* Standard gRPC protocol - requires HTTP/2 and uses binary Protobuf encoding.
22+
* Provides full gRPC compatibility including streaming, trailers, and error details.
23+
*/
24+
GRPC(NetworkProtocol.GRPC),
25+
26+
/**
27+
* gRPC-Web protocol - designed for web browsers, works over HTTP/1.1 and HTTP/2.
28+
* Eliminates the need for a translating proxy like Envoy.
29+
*/
30+
GRPC_WEB(NetworkProtocol.GRPC_WEB);
31+
32+
private final NetworkProtocol networkProtocol;
33+
34+
ProtocolType(NetworkProtocol networkProtocol) {
35+
this.networkProtocol = networkProtocol;
36+
}
37+
38+
/**
39+
* Get the underlying Connect-RPC NetworkProtocol value.
40+
*
41+
* @return the NetworkProtocol corresponding to this ProtocolType
42+
*/
43+
public NetworkProtocol getNetworkProtocol() {
44+
return networkProtocol;
45+
}
46+
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class SDKBuilder {
6565
private Boolean usePlainText;
6666
private SSLFactory sslFactory;
6767
private AuthorizationGrant authzGrant;
68+
private ProtocolType protocolType = ProtocolType.CONNECT;
6869

6970
private static final Logger logger = LoggerFactory.getLogger(SDKBuilder.class);
7071

@@ -160,6 +161,18 @@ public SDKBuilder useInsecurePlaintextConnection(Boolean usePlainText) {
160161
return this;
161162
}
162163

164+
/**
165+
* Set the network protocol to use for communication with platform services.
166+
*
167+
* @param protocolType the protocol type to use (CONNECT, GRPC, or GRPC_WEB)
168+
* @return this builder instance for method chaining
169+
* @see ProtocolType for available protocol options
170+
*/
171+
public SDKBuilder protocol(ProtocolType protocolType) {
172+
this.protocolType = protocolType;
173+
return this;
174+
}
175+
163176
private Interceptor getAuthInterceptor(RSAKey rsaKey) {
164177
if (platformEndpoint == null) {
165178
throw new SDKException("cannot build an SDK without specifying the platform endpoint");
@@ -329,7 +342,7 @@ private ProtocolClient getProtocolClient(String endpoint, OkHttpClient httpClien
329342
var protocolClientConfig = new ProtocolClientConfig(
330343
endpoint,
331344
new GoogleJavaProtobufStrategy(),
332-
NetworkProtocol.GRPC,
345+
protocolType.getNetworkProtocol(),
333346
null,
334347
GETConfiguration.Enabled.INSTANCE,
335348
authInterceptor == null ? Collections.emptyList() : List.of(ignoredConfig -> authInterceptor)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class KASClientTest {
4545
BiFunction<OkHttpClient, String, ProtocolClient> aclientFactory = (OkHttpClient client, String endpoint) -> {
4646
return new ProtocolClient(
4747
new ConnectOkHttpClient(httpClient),
48-
new ProtocolClientConfig(endpoint, new GoogleJavaProtobufStrategy(), NetworkProtocol.GRPC, null, GETConfiguration.Enabled.INSTANCE)
48+
new ProtocolClientConfig(endpoint, new GoogleJavaProtobufStrategy(), ProtocolType.GRPC.getNetworkProtocol(), null, GETConfiguration.Enabled.INSTANCE)
4949
);
5050
};
5151

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,58 @@ public void getNamespace(GetNamespaceRequest request,
412412
}
413413
}
414414

415+
@Test
416+
void testProtocolConfiguration() {
417+
// Test that protocol configuration works and defaults to CONNECT
418+
419+
// Test default protocol setting
420+
SDKBuilder.ServicesAndInternals services1 = SDKBuilder.newBuilder()
421+
.clientSecret("user", "password")
422+
.platformEndpoint("http://localhost:8080")
423+
.useInsecurePlaintextConnection(true)
424+
.buildServices();
425+
426+
// Test explicit GRPC protocol setting
427+
SDKBuilder.ServicesAndInternals services2 = SDKBuilder.newBuilder()
428+
.clientSecret("user", "password")
429+
.platformEndpoint("http://localhost:8080")
430+
.useInsecurePlaintextConnection(true)
431+
.protocol(ProtocolType.GRPC)
432+
.buildServices();
433+
434+
// Test GRPC_WEB protocol setting
435+
SDKBuilder.ServicesAndInternals services3 = SDKBuilder.newBuilder()
436+
.clientSecret("user", "password")
437+
.platformEndpoint("http://localhost:8080")
438+
.useInsecurePlaintextConnection(true)
439+
.protocol(ProtocolType.GRPC_WEB)
440+
.buildServices();
441+
442+
// Test CONNECT protocol setting
443+
SDKBuilder.ServicesAndInternals services4 = SDKBuilder.newBuilder()
444+
.clientSecret("user", "password")
445+
.platformEndpoint("http://localhost:8080")
446+
.useInsecurePlaintextConnection(true)
447+
.protocol(ProtocolType.CONNECT)
448+
.buildServices();
449+
450+
// Verify that all configurations create services successfully
451+
assertThat(services1.services).isNotNull();
452+
assertThat(services2.services).isNotNull();
453+
assertThat(services3.services).isNotNull();
454+
assertThat(services4.services).isNotNull();
455+
456+
// Clean up
457+
try {
458+
services1.services.close();
459+
services2.services.close();
460+
services3.services.close();
461+
services4.services.close();
462+
} catch (Exception ignored) {
463+
// ignore cleanup errors in test
464+
}
465+
}
466+
415467
public static int getRandomPort() throws IOException {
416468
int randomPort;
417469
try (ServerSocket socket = new ServerSocket(0)) {

0 commit comments

Comments
 (0)