From 6a00730a5576ede6517019a896b66017b8b1116d Mon Sep 17 00:00:00 2001 From: Georg Lokowandt Date: Mon, 2 Dec 2024 16:56:51 +0100 Subject: [PATCH 1/3] Fix: wrong key used to access log-cache root endpoint --- .../logcache/v1/_ReactorLogCacheClient.java | 3 +- .../v1/ReactorLogCacheClientTest.java | 71 ++++++++++++++++++- .../test/resources/fixtures/GET_response.json | 3 + 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/cloudfoundry-client-reactor/src/main/java/org/cloudfoundry/reactor/logcache/v1/_ReactorLogCacheClient.java b/cloudfoundry-client-reactor/src/main/java/org/cloudfoundry/reactor/logcache/v1/_ReactorLogCacheClient.java index 68ce71f4af0..02f3619a6d3 100644 --- a/cloudfoundry-client-reactor/src/main/java/org/cloudfoundry/reactor/logcache/v1/_ReactorLogCacheClient.java +++ b/cloudfoundry-client-reactor/src/main/java/org/cloudfoundry/reactor/logcache/v1/_ReactorLogCacheClient.java @@ -24,6 +24,7 @@ import org.cloudfoundry.logcache.v1.ReadRequest; import org.cloudfoundry.logcache.v1.ReadResponse; import org.cloudfoundry.reactor.ConnectionContext; +import org.cloudfoundry.reactor.RootProvider; import org.cloudfoundry.reactor.TokenProvider; import org.immutables.value.Value; import reactor.core.publisher.Mono; @@ -70,7 +71,7 @@ Map getRequestTags() { @Value.Default Mono getRoot() { - final Mono cached = getConnectionContext().getRootProvider().getRoot("log-cache", getConnectionContext()) + final Mono cached = getConnectionContext().getRootProvider().getRoot("log_cache", getConnectionContext()) .onErrorResume(IllegalArgumentException.class, e -> deriveLogCacheUrl()); return getConnectionContext().getCacheDuration() diff --git a/cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/logcache/v1/ReactorLogCacheClientTest.java b/cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/logcache/v1/ReactorLogCacheClientTest.java index e0015204d67..b53df35a313 100644 --- a/cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/logcache/v1/ReactorLogCacheClientTest.java +++ b/cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/logcache/v1/ReactorLogCacheClientTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2021 the original author or authors. + * Copyright 2013-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,14 @@ import static io.netty.handler.codec.http.HttpMethod.GET; import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.IOException; +import java.net.URI; import java.time.Duration; import java.util.Collections; import org.cloudfoundry.logcache.v1.Envelope; @@ -34,18 +41,80 @@ import org.cloudfoundry.logcache.v1.Metric; import org.cloudfoundry.logcache.v1.ReadRequest; import org.cloudfoundry.logcache.v1.ReadResponse; +import org.cloudfoundry.reactor.ConnectionContext; +import org.cloudfoundry.reactor.DefaultConnectionContext; import org.cloudfoundry.reactor.InteractionContext; +import org.cloudfoundry.reactor.RootProvider; import org.cloudfoundry.reactor.TestRequest; import org.cloudfoundry.reactor.TestResponse; import org.junit.jupiter.api.Test; +import reactor.core.publisher.Mono; import reactor.test.StepVerifier; class ReactorLogCacheClientTest extends AbstractLogCacheApiTest { + private static final String API_ROOT = "http://api.my.rapid.server.com"; + private static final String LOGCACHE = "http://log-cache.my.rlog-cached.server.com"; private final ReactorLogCacheEndpoints logCacheEndpoints = new ReactorLogCacheEndpoints( CONNECTION_CONTEXT, this.root, TOKEN_PROVIDER, Collections.emptyMap()); + @Test + void getRootFromFallback() throws IOException { + URI webServerUri = URI.create(this.root.block(Duration.ofSeconds(5))); + Mono apiRoot = Mono.just(API_ROOT); + RootProvider rootProvider = mock(RootProvider.class); + when(rootProvider.getRoot(eq("log_cache"), any())) + .thenReturn(Mono.error(new IllegalArgumentException())); // trigger fallback + when(rootProvider.getRoot(any())).thenReturn(apiRoot); + ConnectionContext connectionContext = + DefaultConnectionContext.builder() + .rootProvider(rootProvider) + .apiHost(webServerUri.getHost()) + .port(webServerUri.getPort()) + .secure(false) + .build(); + ReactorLogCacheClient examinee = + ReactorLogCacheClient.builder() + .connectionContext(connectionContext) + .tokenProvider(TOKEN_PROVIDER) + .build(); + Mono logCacheRoot = examinee.getRoot(); + String rootString = logCacheRoot.block(Duration.ofSeconds(15)); + assertThat(rootString).isEqualTo(LOGCACHE); + } + + @Test + void getRootFromEndpoint() { + mockRequest( + InteractionContext.builder() + .request(TestRequest.builder().method(GET).path("/").build()) + .response( + TestResponse.builder() + .status(OK) + .payload("fixtures/GET_response.json") + .build()) + .build()); + URI webServerUri = URI.create(this.root.block(Duration.ofSeconds(5))); + ConnectionContext connectionContext = + DefaultConnectionContext.builder() + .apiHost(webServerUri.getHost()) + .port(webServerUri.getPort()) + .secure(false) + .build(); + ReactorLogCacheClient examinee = + ReactorLogCacheClient.builder() + .connectionContext(connectionContext) + .tokenProvider(TOKEN_PROVIDER) + .build(); + Mono logCacheRoot = examinee.getRoot(); + String rootString = logCacheRoot.block(Duration.ofSeconds(5)); + assertThat(rootString) + .isEqualTo( + "http://cache-for-logging.cf.lod-cfcli3.cfrt-sof.sapcloud.io:" + + webServerUri.getPort()); + } + @Test void info() { mockRequest( diff --git a/cloudfoundry-client-reactor/src/test/resources/fixtures/GET_response.json b/cloudfoundry-client-reactor/src/test/resources/fixtures/GET_response.json index 476b05da095..84935690343 100644 --- a/cloudfoundry-client-reactor/src/test/resources/fixtures/GET_response.json +++ b/cloudfoundry-client-reactor/src/test/resources/fixtures/GET_response.json @@ -25,6 +25,9 @@ "uaa": { "href": "https://uaa.run.pivotal.io" }, + "log_cache": { + "href": "https://cache-for-logging.cf.lod-cfcli3.cfrt-sof.sapcloud.io" + }, "logging": { "href": "wss://doppler.run.pivotal.io:443" } From 10b8c0d52f6eb5fc97241ca4f9ba82c2e62fb6c8 Mon Sep 17 00:00:00 2001 From: Georg Lokowandt Date: Wed, 11 Jun 2025 22:29:01 +0200 Subject: [PATCH 2/3] minor fixes --- .../reactor/logcache/v1/_ReactorLogCacheClient.java | 3 +-- .../reactor/logcache/v1/ReactorLogCacheClientTest.java | 10 +++++----- .../src/test/resources/fixtures/GET_response.json | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cloudfoundry-client-reactor/src/main/java/org/cloudfoundry/reactor/logcache/v1/_ReactorLogCacheClient.java b/cloudfoundry-client-reactor/src/main/java/org/cloudfoundry/reactor/logcache/v1/_ReactorLogCacheClient.java index 02f3619a6d3..d9460476ea4 100644 --- a/cloudfoundry-client-reactor/src/main/java/org/cloudfoundry/reactor/logcache/v1/_ReactorLogCacheClient.java +++ b/cloudfoundry-client-reactor/src/main/java/org/cloudfoundry/reactor/logcache/v1/_ReactorLogCacheClient.java @@ -24,7 +24,6 @@ import org.cloudfoundry.logcache.v1.ReadRequest; import org.cloudfoundry.logcache.v1.ReadResponse; import org.cloudfoundry.reactor.ConnectionContext; -import org.cloudfoundry.reactor.RootProvider; import org.cloudfoundry.reactor.TokenProvider; import org.immutables.value.Value; import reactor.core.publisher.Mono; @@ -86,7 +85,7 @@ Mono getRoot() { private Mono deriveLogCacheUrl() { return getConnectionContext().getRootProvider().getRoot(getConnectionContext()) - .map(root -> root.replace("api", "log-cache")) + .map(root -> root.replaceFirst("://api", "://log-cache")) .map(URI::create) .delayUntil(uri -> getConnectionContext().trust(uri.getHost(), uri.getPort())) .map(URI::toString); diff --git a/cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/logcache/v1/ReactorLogCacheClientTest.java b/cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/logcache/v1/ReactorLogCacheClientTest.java index b53df35a313..3ce150d62c6 100644 --- a/cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/logcache/v1/ReactorLogCacheClientTest.java +++ b/cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/logcache/v1/ReactorLogCacheClientTest.java @@ -24,7 +24,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.io.IOException; import java.net.URI; import java.time.Duration; import java.util.Collections; @@ -52,15 +51,16 @@ import reactor.test.StepVerifier; class ReactorLogCacheClientTest extends AbstractLogCacheApiTest { - private static final String API_ROOT = "http://api.my.rapid.server.com"; - private static final String LOGCACHE = "http://log-cache.my.rlog-cached.server.com"; + private static final String API_ROOT = "http://api.my.rapid.server.com"; // the ".rAPId." part also contains the string "api". + private static final String LOGCACHE = "http://log-cache.my.rapid.server.com"; // only the "api" at the start of + // the url should be replaced. private final ReactorLogCacheEndpoints logCacheEndpoints = new ReactorLogCacheEndpoints( CONNECTION_CONTEXT, this.root, TOKEN_PROVIDER, Collections.emptyMap()); @Test - void getRootFromFallback() throws IOException { + void getRootFromFallback() { URI webServerUri = URI.create(this.root.block(Duration.ofSeconds(5))); Mono apiRoot = Mono.just(API_ROOT); RootProvider rootProvider = mock(RootProvider.class); @@ -111,7 +111,7 @@ void getRootFromEndpoint() { String rootString = logCacheRoot.block(Duration.ofSeconds(5)); assertThat(rootString) .isEqualTo( - "http://cache-for-logging.cf.lod-cfcli3.cfrt-sof.sapcloud.io:" + "http://cache-for-logging.run.pivotal.io:" + webServerUri.getPort()); } diff --git a/cloudfoundry-client-reactor/src/test/resources/fixtures/GET_response.json b/cloudfoundry-client-reactor/src/test/resources/fixtures/GET_response.json index 84935690343..611fa8679a9 100644 --- a/cloudfoundry-client-reactor/src/test/resources/fixtures/GET_response.json +++ b/cloudfoundry-client-reactor/src/test/resources/fixtures/GET_response.json @@ -26,7 +26,7 @@ "href": "https://uaa.run.pivotal.io" }, "log_cache": { - "href": "https://cache-for-logging.cf.lod-cfcli3.cfrt-sof.sapcloud.io" + "href": "https://cache-for-logging.run.pivotal.io" }, "logging": { "href": "wss://doppler.run.pivotal.io:443" From 33a1d0cf45349372d54023e598070d0a802e9f67 Mon Sep 17 00:00:00 2001 From: Georg Lokowandt Date: Wed, 11 Jun 2025 22:36:39 +0200 Subject: [PATCH 3/3] apply spotless --- .../reactor/logcache/v1/ReactorLogCacheClientTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/logcache/v1/ReactorLogCacheClientTest.java b/cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/logcache/v1/ReactorLogCacheClientTest.java index 3ce150d62c6..740461dc469 100644 --- a/cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/logcache/v1/ReactorLogCacheClientTest.java +++ b/cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/logcache/v1/ReactorLogCacheClientTest.java @@ -51,8 +51,10 @@ import reactor.test.StepVerifier; class ReactorLogCacheClientTest extends AbstractLogCacheApiTest { - private static final String API_ROOT = "http://api.my.rapid.server.com"; // the ".rAPId." part also contains the string "api". - private static final String LOGCACHE = "http://log-cache.my.rapid.server.com"; // only the "api" at the start of + private static final String API_ROOT = + "http://api.my.rapid.server.com"; // the ".rAPId." part also contains the string "api". + private static final String LOGCACHE = + "http://log-cache.my.rapid.server.com"; // only the "api" at the start of // the url should be replaced. private final ReactorLogCacheEndpoints logCacheEndpoints = @@ -110,9 +112,7 @@ void getRootFromEndpoint() { Mono logCacheRoot = examinee.getRoot(); String rootString = logCacheRoot.block(Duration.ofSeconds(5)); assertThat(rootString) - .isEqualTo( - "http://cache-for-logging.run.pivotal.io:" - + webServerUri.getPort()); + .isEqualTo("http://cache-for-logging.run.pivotal.io:" + webServerUri.getPort()); } @Test