Skip to content

Commit

Permalink
work around path separator bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Daniels committed Oct 3, 2024
1 parent 5712e3b commit cc6ae25
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/styra/opa/OPAClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class OPAClient {
* suitable value for most sidecar style deployments of OPA.
*/
public OPAClient() {
this.sdk = OpaApiClient.builder().serverURL(sdkServerURL).build();
this.sdk = OpaApiClient.builder().client(new OPAHTTPClient()).serverURL(sdkServerURL).build();
}

/**
Expand All @@ -92,7 +92,7 @@ public OPAClient() {
*/
public OPAClient(String opaURL) {
this.sdkServerURL = opaURL;
this.sdk = OpaApiClient.builder().serverURL(opaURL).build();
this.sdk = OpaApiClient.builder().client(new OPAHTTPClient()).serverURL(opaURL).build();
}

/**
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/styra/opa/utils/OPAHTTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.net.URI;

Check failure on line 17 in src/main/java/com/styra/opa/utils/OPAHTTPClient.java

View workflow job for this annotation

GitHub Actions / test_and_lint

Wrong order for 'java.net.URI' import.

import static java.net.http.HttpRequest.Builder;

Expand Down Expand Up @@ -53,7 +54,6 @@ public OPAHTTPClient() {
public HttpResponse<InputStream> send(HttpRequest request)
throws IOException, InterruptedException, URISyntaxException {


// At this point, the HTTP request has already been built, so there
// is no way to add a new header, see:
//
Expand All @@ -62,7 +62,23 @@ public HttpResponse<InputStream> send(HttpRequest request)
// Consequentially, we need to make a new builder and copy all of the
// existing request data into it.

Builder b = HttpRequest.newBuilder(request.uri());
// Explicitly expand path separators in the URI, as the SE SDK
// sometimes escapes them as %2F. `/` is not a legal character to appear in
// a Rego package path or rule head, so we can simply re-expand those path
// elements.

URI oldURI = request.uri();
URI newURI = new URI(
oldURI.getScheme(),
oldURI.getUserInfo(),
oldURI.getHost(),
oldURI.getPort(),
oldURI.getPath().replaceAll("%2F", "/"),
oldURI.getQuery(),
oldURI.getFragment()
);

Builder b = HttpRequest.newBuilder(newURI);
b.method(
request.method(),
request.bodyPublisher().orElse(HttpRequest.BodyPublishers.noBody()));
Expand Down

0 comments on commit cc6ae25

Please sign in to comment.