Skip to content

Commit 931a64f

Browse files
committed
cleanup
1 parent bc3d212 commit 931a64f

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

keys/kas-cert.pem

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIC/TCCAeWgAwIBAgIUUCu5Zu5b8PIRhAFJ5VpaZFP35HQwDQYJKoZIhvcNAQEL
3+
BQAwDjEMMAoGA1UEAwwDa2FzMB4XDTI0MDgxNTExMzYxN1oXDTI1MDgxNTExMzYx
4+
N1owDjEMMAoGA1UEAwwDa2FzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
5+
AQEAx4PgRz+H/U4X5hf3Uj2JgqqyG7fX0QDSoxI3Ziu1Q+gbxElVppOqJQMA88Yd
6+
8uug2Px/vtmkFwGXGi5DzKq6bLcpCJEXc1TOtJH5o+2Wcu1Ahfp9MxkOXpbJvoH8
7+
JRkjOp6ZPIiIM/IOQDq3eHpLVAB6ihDFDwzhJsqBMVMejmkDRNj8qx5AkZSrE4zi
8+
AL7hV/TWWyCiq8rLiWVnZOFXNHyRtPmTgmerRg5Ad1lP9muMrLJ/1ziq1lILk7fB
9+
a31yOmS3g25MGYYwX+7PCNMWkhX0eCLAyosYfIp/K0SOJ3WO9G4eiq9keb4xRSbB
10+
jFKmadNBITEWIhPzCAzT8nDlFwIDAQABo1MwUTAdBgNVHQ4EFgQUFc94TI8PUU+u
11+
uASVIyQgQm4tHRswHwYDVR0jBBgwFoAUFc94TI8PUU+uuASVIyQgQm4tHRswDwYD
12+
VR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAjvzUeU9otYOM0UpwhZ/j
13+
nBxNcP0J7fAODokL1as8zXnv7t8mk327b8HSORkfGdbCsf5be10W9xGK1DoRhscA
14+
3wkASiWOk2wsIq9l12tR247EJXk7VMcDXQfGuVhzMhN1gp25bxX2FsmKvxnOFuN2
15+
Qv4BY61dQSHjoDQDRhYb9naYTqsppiHjj11nayfEY6nVivs9Hu9jXqakcE4wSksX
16+
DRRgxAs2KBcbQ0/rfOZs7yPs8jlqpmPk09M+yV7Tn1943EaAnWyiuavW7g5Zn/dM
17+
szuJrlIzmgRdUyHTD4tS6ebTqGo+hziYNdfHUdjQF8JDMiRFwx/xoqC5/1jP18kX
18+
MQ==
19+
-----END CERTIFICATE-----
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.opentdf.platform.sdk;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.net.MalformedURLException;
7+
import java.net.URI;
8+
import java.net.URISyntaxException;
9+
import java.net.URL;
10+
11+
class AddressNormalizer {
12+
private static final Logger logger = LoggerFactory.getLogger(AddressNormalizer.class);
13+
14+
static String normalizeAddress(String urlString, boolean usePlaintext) {
15+
URL url;
16+
try {
17+
url = new URL(urlString);
18+
} catch (MalformedURLException e) {
19+
url = tryParseHostAndPort(urlString);
20+
}
21+
final int port;
22+
if (url.getPort() == -1) {
23+
port = "http".equals(url.getProtocol()) ? 80 : 443;
24+
} else {
25+
port = url.getPort();
26+
}
27+
final String protocol = usePlaintext && "http".equals(url.getProtocol()) ? "http" : "https";
28+
29+
try {
30+
var returnUrl = new URL(protocol, url.getHost(), port, "").toString();
31+
logger.debug("normalized url [{}] to [{}]", urlString, returnUrl);
32+
return returnUrl;
33+
} catch (MalformedURLException e) {
34+
throw new SDKException("error creating KAS address", e);
35+
}
36+
}
37+
38+
private static URL tryParseHostAndPort(String urlString) {
39+
URI uri;
40+
try {
41+
uri = new URI(null, urlString, null, null, null).parseServerAuthority();
42+
} catch (URISyntaxException e) {
43+
throw new SDKException("error trying to parse host and port", e);
44+
}
45+
46+
try {
47+
return new URL(uri.getPort() == 443 ? "https" : "http", uri.getHost(), uri.getPort(), "");
48+
} catch (MalformedURLException e) {
49+
throw new SDKException("error trying to create URL from host and port", e);
50+
}
51+
}
52+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ ServicesAndInternals buildServices() {
239239
this.platformEndpoint = AddressNormalizer.normalizeAddress(this.platformEndpoint, this.usePlainText);
240240
var authInterceptor = getAuthInterceptor(dpopKey);
241241
var kasClient = getKASClient(dpopKey, authInterceptor);
242-
var protocolClient = getUnauthenticatedProtocolClient(platformEndpoint, authInterceptor);
242+
var protocolClient = getProtocolClient(platformEndpoint, authInterceptor);
243243

244244
return new ServicesAndInternals(
245245
authInterceptor,
@@ -249,7 +249,7 @@ ServicesAndInternals buildServices() {
249249

250250
@Nonnull
251251
private KASClient getKASClient(RSAKey dpopKey, Interceptor interceptor) {
252-
return new KASClient((String endpoint) -> new AccessServiceClient(getUnauthenticatedProtocolClient(endpoint, interceptor)), dpopKey, usePlainText);
252+
return new KASClient((String endpoint) -> new AccessServiceClient(getProtocolClient(endpoint, interceptor)), dpopKey, usePlainText);
253253
}
254254

255255
public SDK build() {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.opentdf.platform.sdk;
2+
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
import static io.opentdf.platform.sdk.AddressNormalizer.normalizeAddress;
7+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
8+
9+
class AddressNormalizerTest {
10+
11+
@Test
12+
public void testAddressNormalizationWithHTTPSClient() {
13+
assertThat(normalizeAddress("http://example.org", false)).isEqualTo("https://example.org:80");
14+
// default to https if no scheme is provided
15+
assertThat(normalizeAddress("example.org:1234", false)).isEqualTo("https://example.org:1234");
16+
}
17+
18+
@Test
19+
public void testAddressNormaliationWithInsecureHTTPClient() {
20+
assertThat(normalizeAddress("http://localhost:8080", true)).isEqualTo("http://localhost:8080");
21+
assertThat(normalizeAddress("https://example.org", true)).isEqualTo("https://example.org:443");
22+
// default to http if no scheme is provided
23+
assertThat(normalizeAddress("example.org:1234", true)).isEqualTo("http://example.org:1234");
24+
}
25+
}

0 commit comments

Comments
 (0)