Skip to content

Commit 4ef5636

Browse files
committed
normalize the platform endpoint
1 parent cb472ef commit 4ef5636

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

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

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323

2424
import kotlin.collections.MapsKt;
2525

26-
import java.net.MalformedURLException;
27-
import java.net.URI;
28-
import java.net.URISyntaxException;
29-
import java.net.URL;
3026
import java.security.MessageDigest;
3127
import java.security.NoSuchAlgorithmException;
3228
import java.time.Duration;
@@ -128,38 +124,6 @@ public KASKeyCache getKeyCache() {
128124
return this.kasKeyCache;
129125
}
130126

131-
private String normalizeAddress(String urlString) {
132-
URL url;
133-
try {
134-
url = new URL(urlString);
135-
} catch (MalformedURLException e) {
136-
url = tryParseHostAndPort(urlString);
137-
}
138-
final int port = url.getPort() == -1 ? ("http".equals(url.getProtocol()) ? 80 : 443 ) : url.getPort();
139-
final String protocol = usePlaintext && "http".equals(url.getProtocol()) ? "http" : "https";
140-
141-
try {
142-
return new URL(protocol, url.getHost(), port, "").toString();
143-
} catch (MalformedURLException e) {
144-
throw new SDKException("error creating KAS address", e);
145-
}
146-
}
147-
148-
private URL tryParseHostAndPort(String urlString) {
149-
URI uri;
150-
try {
151-
uri = new URI(null, urlString, null, null, null).parseServerAuthority();
152-
} catch (URISyntaxException e) {
153-
throw new SDKException("error trying to parse host and port", e);
154-
}
155-
156-
try {
157-
return new URL(uri.getPort() == 80 ? "http" : "https", uri.getHost(), uri.getPort(), "");
158-
} catch (MalformedURLException e) {
159-
throw new SDKException("error trying to create URL from host and port", e);
160-
}
161-
}
162-
163127
@Override
164128
public synchronized void close() {
165129
}
@@ -329,6 +293,6 @@ public byte[] unwrapNanoTDF(NanoTDFType.ECCurve curve, String header, String kas
329293

330294
// make this protected so we can test the address normalization logic
331295
synchronized AccessServiceClient getStub(String url) {
332-
return stubs.computeIfAbsent(normalizeAddress(url), channelFactory);
296+
return stubs.computeIfAbsent(SDKBuilder.normalizeAddress(url, usePlaintext), channelFactory);
333297
}
334298
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@
4444
import java.io.FileInputStream;
4545
import java.io.IOException;
4646
import java.io.InputStream;
47+
import java.net.MalformedURLException;
48+
import java.net.URI;
49+
import java.net.URISyntaxException;
50+
import java.net.URL;
4751
import java.nio.file.Path;
4852
import java.util.ArrayList;
4953
import java.util.Collections;
5054
import java.util.List;
5155
import java.util.UUID;
5256
import java.util.function.Function;
5357

58+
import static io.opentdf.platform.sdk.TDF.logger;
59+
5460
/**
5561
* A builder class for creating instances of the SDK class.
5662
*/
@@ -74,6 +80,40 @@ public static SDKBuilder newBuilder() {
7480
return builder;
7581
}
7682

83+
static String normalizeAddress(String urlString, boolean usePlaintext) {
84+
URL url;
85+
try {
86+
url = new URL(urlString);
87+
} catch (MalformedURLException e) {
88+
url = tryParseHostAndPort(urlString);
89+
}
90+
final int port = url.getPort() == -1 ? ("http".equals(url.getProtocol()) ? 80 : 443 ) : url.getPort();
91+
final String protocol = usePlaintext && "http".equals(url.getProtocol()) ? "http" : "https";
92+
93+
try {
94+
var returnUrl = new URL(protocol, url.getHost(), port, "").toString();
95+
TDF.logger.debug("normalized url [{}] to [{}]", urlString, returnUrl);
96+
return returnUrl;
97+
} catch (MalformedURLException e) {
98+
throw new SDKException("error creating KAS address", e);
99+
}
100+
}
101+
102+
private static URL tryParseHostAndPort(String urlString) {
103+
URI uri;
104+
try {
105+
uri = new URI(null, urlString, null, null, null).parseServerAuthority();
106+
} catch (URISyntaxException e) {
107+
throw new SDKException("error trying to parse host and port", e);
108+
}
109+
110+
try {
111+
return new URL(uri.getPort() == 80 ? "http" : "https", uri.getHost(), uri.getPort(), "");
112+
} catch (MalformedURLException e) {
113+
throw new SDKException("error trying to create URL from host and port", e);
114+
}
115+
}
116+
77117
public SDKBuilder sslFactory(SSLFactory sslFactory) {
78118
this.sslFactory = sslFactory;
79119
return this;
@@ -237,6 +277,7 @@ ServicesAndInternals buildServices() {
237277
throw new SDKException("Error generating DPoP key", e);
238278
}
239279

280+
this.platformEndpoint = normalizeAddress(this.platformEndpoint, this.usePlainText);
240281
var authInterceptor = getAuthInterceptor(dpopKey);
241282
var kasClient = getKASClient(dpopKey, authInterceptor);
242283
var protocolClient = getProtocolClient(platformEndpoint, authInterceptor);

0 commit comments

Comments
 (0)