diff --git a/src/main/java/com/styra/opa/OPAClient.java b/src/main/java/com/styra/opa/OPAClient.java index d97233d4..fd08ce14 100644 --- a/src/main/java/com/styra/opa/OPAClient.java +++ b/src/main/java/com/styra/opa/OPAClient.java @@ -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(); } /** @@ -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(); } /** diff --git a/src/main/java/com/styra/opa/utils/OPAHTTPClient.java b/src/main/java/com/styra/opa/utils/OPAHTTPClient.java index 5494df08..d3356e80 100644 --- a/src/main/java/com/styra/opa/utils/OPAHTTPClient.java +++ b/src/main/java/com/styra/opa/utils/OPAHTTPClient.java @@ -14,6 +14,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.net.URI; import static java.net.http.HttpRequest.Builder; @@ -53,7 +54,6 @@ public OPAHTTPClient() { public HttpResponse 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: // @@ -62,7 +62,23 @@ public HttpResponse 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()));