From 0a556640ef8818b8ab76e49c7fbbc5be3cfe6068 Mon Sep 17 00:00:00 2001 From: Chandrahas Reddy Pola Date: Wed, 12 Aug 2026 14:24:18 -0700 Subject: [PATCH 1/2] Removed the deadcode, converted eTag and lastModifiedTime to volatile and closed the stream. Signed-off-by: Chandrahas Reddy Pola --- .../opa/plugins/BundleDownloader.java | 102 +++++++++--------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/opa-services/src/main/java/io/github/open_policy_agent/opa/plugins/BundleDownloader.java b/opa-services/src/main/java/io/github/open_policy_agent/opa/plugins/BundleDownloader.java index 7d52935..96218bb 100644 --- a/opa-services/src/main/java/io/github/open_policy_agent/opa/plugins/BundleDownloader.java +++ b/opa-services/src/main/java/io/github/open_policy_agent/opa/plugins/BundleDownloader.java @@ -50,8 +50,8 @@ public abstract class BundleDownloader { protected String service; protected String resource; protected Config.PollingConfig polling; - protected String etag; - protected long lastModifiedTime = 0; + protected volatile String etag; + protected volatile long lastModifiedTime = 0; protected long maxSizeBytes = Config.BundleConfig.DEFAULT_MAX_SIZE_BYTES; // Fallback when no service config is available; matches the response_header_timeout_seconds @@ -349,14 +349,10 @@ private CompletableFuture handleHttpDownload(URI uri) { HttpRequest request = requestBuilder.build(); - CompletableFuture> exchange = - httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()); - return exchange + return httpClient + .sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()) .handle( (response, throwable) -> { - if (throwable != null) { - exchange.cancel(true); - } handleHttpResponse(response, throwable); return null; }); @@ -378,54 +374,60 @@ private void handleHttpResponse(HttpResponse response, Throwable th return; } - if (response.statusCode() == 304) { - manager.getLogger().debug("Bundle '%s': Not modified (ETag match)", name); - if (!initialActivation.isDone()) { - initialActivation.complete(null); - } - return; - } - - if (response.statusCode() == 200) { - String contentType = response.headers().firstValue("Content-Type").orElse(""); - if (!isAcceptableContentType(contentType)) { - String errorMsg = "Unexpected Content-Type: '" + contentType + "'"; - manager.getLogger().error("Bundle '%s': %s", name, errorMsg); + try (InputStream bodyStream = response.body()) { + if (response.statusCode() == 304) { + manager.getLogger().debug("Bundle '%s': Not modified (ETag match)", name); if (!initialActivation.isDone()) { - initialActivation.completeExceptionally(new RuntimeException(errorMsg)); + initialActivation.complete(null); } return; } - OptionalLong contentLength = response.headers().firstValueAsLong("Content-Length"); - byte[] body; - try { - body = readBodyWithLimit(response.body(), contentLength); - } catch (Exception e) { - manager.getLogger().error("Bundle '%s': Download error: %s", name, e.getMessage()); + + if (response.statusCode() == 200) { + String contentType = response.headers().firstValue("Content-Type").orElse(""); + if (!isAcceptableContentType(contentType)) { + String errorMsg = "Unexpected Content-Type: '" + contentType + "'"; + manager.getLogger().error("Bundle '%s': %s", name, errorMsg); + if (!initialActivation.isDone()) { + initialActivation.completeExceptionally(new RuntimeException(errorMsg)); + } + return; + } + OptionalLong contentLength = response.headers().firstValueAsLong("Content-Length"); + byte[] body; + try { + body = readBodyWithLimit(bodyStream, contentLength); + } catch (Exception e) { + manager.getLogger().error("Bundle '%s': Download error: %s", name, e.getMessage()); + if (!initialActivation.isDone()) { + initialActivation.completeExceptionally(e); + } + return; + } + response.headers().firstValue("ETag").ifPresent(newEtag -> this.etag = newEtag); + try { + activateBundle(body); + } catch (Exception e) { + manager.getLogger().error("Bundle '%s': Activation failed: %s", name, e.getMessage()); + if (!initialActivation.isDone()) { + initialActivation.completeExceptionally(e); + } + return; + } if (!initialActivation.isDone()) { - initialActivation.completeExceptionally(e); + initialActivation.complete(null); } - return; - } - response.headers().firstValue("ETag").ifPresent(newEtag -> this.etag = newEtag); - try { - activateBundle(body); - } catch (Exception e) { - manager.getLogger().error("Bundle '%s': Activation failed: %s", name, e.getMessage()); + } else { + String errorMsg = "Download failed with status " + response.statusCode(); + manager.getLogger().error("Bundle '%s': %s", name, errorMsg); if (!initialActivation.isDone()) { - initialActivation.completeExceptionally(e); + initialActivation.completeExceptionally(new RuntimeException(errorMsg)); } - return; - } - if (!initialActivation.isDone()) { - initialActivation.complete(null); - } - } else { - String errorMsg = "Download failed with status " + response.statusCode(); - manager.getLogger().error("Bundle '%s': %s", name, errorMsg); - if (!initialActivation.isDone()) { - initialActivation.completeExceptionally(new RuntimeException(errorMsg)); } + } catch (IOException e) { + manager + .getLogger() + .debug("Bundle '%s': Error closing response body: %s", name, e.getMessage()); } } @@ -451,11 +453,9 @@ private static boolean isAcceptableContentType(String contentType) { private byte[] readBodyWithLimit(InputStream body, OptionalLong contentLength) throws IOException { long limit = Math.min(maxSizeBytes, Integer.MAX_VALUE); - try (InputStream in = body) { - rejectIfContentLengthExceedsLimit(contentLength, limit); - return readUpTo(in, limit); + rejectIfContentLengthExceedsLimit(contentLength, limit); + return readUpTo(body, limit); } - } /** * Rejects a response whose advertised Content-Length already exceeds the limit, so an oversized From f8ee24ac7f09c2b392770ec92a2807f2dd98ce5e Mon Sep 17 00:00:00 2001 From: Chandrahas Reddy Pola Date: Wed, 12 Aug 2026 14:52:14 -0700 Subject: [PATCH 2/2] Reverted the closing stream code because we are handling for multiple status codes. Signed-off-by: Chandrahas Reddy Pola --- .../opa/plugins/BundleDownloader.java | 90 +++++++++---------- 1 file changed, 43 insertions(+), 47 deletions(-) diff --git a/opa-services/src/main/java/io/github/open_policy_agent/opa/plugins/BundleDownloader.java b/opa-services/src/main/java/io/github/open_policy_agent/opa/plugins/BundleDownloader.java index 96218bb..1b5d43c 100644 --- a/opa-services/src/main/java/io/github/open_policy_agent/opa/plugins/BundleDownloader.java +++ b/opa-services/src/main/java/io/github/open_policy_agent/opa/plugins/BundleDownloader.java @@ -374,60 +374,54 @@ private void handleHttpResponse(HttpResponse response, Throwable th return; } - try (InputStream bodyStream = response.body()) { - if (response.statusCode() == 304) { - manager.getLogger().debug("Bundle '%s': Not modified (ETag match)", name); + if (response.statusCode() == 304) { + manager.getLogger().debug("Bundle '%s': Not modified (ETag match)", name); + if (!initialActivation.isDone()) { + initialActivation.complete(null); + } + return; + } + + if (response.statusCode() == 200) { + String contentType = response.headers().firstValue("Content-Type").orElse(""); + if (!isAcceptableContentType(contentType)) { + String errorMsg = "Unexpected Content-Type: '" + contentType + "'"; + manager.getLogger().error("Bundle '%s': %s", name, errorMsg); if (!initialActivation.isDone()) { - initialActivation.complete(null); + initialActivation.completeExceptionally(new RuntimeException(errorMsg)); } return; } - - if (response.statusCode() == 200) { - String contentType = response.headers().firstValue("Content-Type").orElse(""); - if (!isAcceptableContentType(contentType)) { - String errorMsg = "Unexpected Content-Type: '" + contentType + "'"; - manager.getLogger().error("Bundle '%s': %s", name, errorMsg); - if (!initialActivation.isDone()) { - initialActivation.completeExceptionally(new RuntimeException(errorMsg)); - } - return; - } - OptionalLong contentLength = response.headers().firstValueAsLong("Content-Length"); - byte[] body; - try { - body = readBodyWithLimit(bodyStream, contentLength); - } catch (Exception e) { - manager.getLogger().error("Bundle '%s': Download error: %s", name, e.getMessage()); - if (!initialActivation.isDone()) { - initialActivation.completeExceptionally(e); - } - return; - } - response.headers().firstValue("ETag").ifPresent(newEtag -> this.etag = newEtag); - try { - activateBundle(body); - } catch (Exception e) { - manager.getLogger().error("Bundle '%s': Activation failed: %s", name, e.getMessage()); - if (!initialActivation.isDone()) { - initialActivation.completeExceptionally(e); - } - return; - } + OptionalLong contentLength = response.headers().firstValueAsLong("Content-Length"); + byte[] body; + try { + body = readBodyWithLimit(response.body(), contentLength); + } catch (Exception e) { + manager.getLogger().error("Bundle '%s': Download error: %s", name, e.getMessage()); if (!initialActivation.isDone()) { - initialActivation.complete(null); + initialActivation.completeExceptionally(e); } - } else { - String errorMsg = "Download failed with status " + response.statusCode(); - manager.getLogger().error("Bundle '%s': %s", name, errorMsg); + return; + } + response.headers().firstValue("ETag").ifPresent(newEtag -> this.etag = newEtag); + try { + activateBundle(body); + } catch (Exception e) { + manager.getLogger().error("Bundle '%s': Activation failed: %s", name, e.getMessage()); if (!initialActivation.isDone()) { - initialActivation.completeExceptionally(new RuntimeException(errorMsg)); + initialActivation.completeExceptionally(e); } + return; + } + if (!initialActivation.isDone()) { + initialActivation.complete(null); + } + } else { + String errorMsg = "Download failed with status " + response.statusCode(); + manager.getLogger().error("Bundle '%s': %s", name, errorMsg); + if (!initialActivation.isDone()) { + initialActivation.completeExceptionally(new RuntimeException(errorMsg)); } - } catch (IOException e) { - manager - .getLogger() - .debug("Bundle '%s': Error closing response body: %s", name, e.getMessage()); } } @@ -453,9 +447,11 @@ private static boolean isAcceptableContentType(String contentType) { private byte[] readBodyWithLimit(InputStream body, OptionalLong contentLength) throws IOException { long limit = Math.min(maxSizeBytes, Integer.MAX_VALUE); - rejectIfContentLengthExceedsLimit(contentLength, limit); - return readUpTo(body, limit); + try (InputStream in = body) { + rejectIfContentLengthExceedsLimit(contentLength, limit); + return readUpTo(in, limit); } + } /** * Rejects a response whose advertised Content-Length already exceeds the limit, so an oversized