Skip to content

Commit 97aff65

Browse files
authored
Merge pull request #1252 from Lokowandtg/fixLogCacheRootAccess
Fix: wrong key used to access log-cache root endpoint. Fix #1251
2 parents e57b0ff + 33a1d0c commit 97aff65

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

cloudfoundry-client-reactor/src/main/java/org/cloudfoundry/reactor/logcache/v1/_ReactorLogCacheClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Map<String, String> getRequestTags() {
7070

7171
@Value.Default
7272
Mono<String> getRoot() {
73-
final Mono<String> cached = getConnectionContext().getRootProvider().getRoot("log-cache", getConnectionContext())
73+
final Mono<String> cached = getConnectionContext().getRootProvider().getRoot("log_cache", getConnectionContext())
7474
.onErrorResume(IllegalArgumentException.class, e -> deriveLogCacheUrl());
7575

7676
return getConnectionContext().getCacheDuration()
@@ -85,7 +85,7 @@ Mono<String> getRoot() {
8585

8686
private Mono<String> deriveLogCacheUrl() {
8787
return getConnectionContext().getRootProvider().getRoot(getConnectionContext())
88-
.map(root -> root.replace("api", "log-cache"))
88+
.map(root -> root.replaceFirst("://api", "://log-cache"))
8989
.map(URI::create)
9090
.delayUntil(uri -> getConnectionContext().trust(uri.getHost(), uri.getPort()))
9191
.map(URI::toString);

cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/logcache/v1/ReactorLogCacheClientTest.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2021 the original author or authors.
2+
* Copyright 2013-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,13 @@
1818

1919
import static io.netty.handler.codec.http.HttpMethod.GET;
2020
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.mockito.ArgumentMatchers.any;
23+
import static org.mockito.ArgumentMatchers.eq;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.when;
2126

27+
import java.net.URI;
2228
import java.time.Duration;
2329
import java.util.Collections;
2430
import org.cloudfoundry.logcache.v1.Envelope;
@@ -34,18 +40,81 @@
3440
import org.cloudfoundry.logcache.v1.Metric;
3541
import org.cloudfoundry.logcache.v1.ReadRequest;
3642
import org.cloudfoundry.logcache.v1.ReadResponse;
43+
import org.cloudfoundry.reactor.ConnectionContext;
44+
import org.cloudfoundry.reactor.DefaultConnectionContext;
3745
import org.cloudfoundry.reactor.InteractionContext;
46+
import org.cloudfoundry.reactor.RootProvider;
3847
import org.cloudfoundry.reactor.TestRequest;
3948
import org.cloudfoundry.reactor.TestResponse;
4049
import org.junit.jupiter.api.Test;
50+
import reactor.core.publisher.Mono;
4151
import reactor.test.StepVerifier;
4252

4353
class ReactorLogCacheClientTest extends AbstractLogCacheApiTest {
54+
private static final String API_ROOT =
55+
"http://api.my.rapid.server.com"; // the ".rAPId." part also contains the string "api".
56+
private static final String LOGCACHE =
57+
"http://log-cache.my.rapid.server.com"; // only the "api" at the start of
58+
// the url should be replaced.
4459

4560
private final ReactorLogCacheEndpoints logCacheEndpoints =
4661
new ReactorLogCacheEndpoints(
4762
CONNECTION_CONTEXT, this.root, TOKEN_PROVIDER, Collections.emptyMap());
4863

64+
@Test
65+
void getRootFromFallback() {
66+
URI webServerUri = URI.create(this.root.block(Duration.ofSeconds(5)));
67+
Mono<String> apiRoot = Mono.just(API_ROOT);
68+
RootProvider rootProvider = mock(RootProvider.class);
69+
when(rootProvider.getRoot(eq("log_cache"), any()))
70+
.thenReturn(Mono.error(new IllegalArgumentException())); // trigger fallback
71+
when(rootProvider.getRoot(any())).thenReturn(apiRoot);
72+
ConnectionContext connectionContext =
73+
DefaultConnectionContext.builder()
74+
.rootProvider(rootProvider)
75+
.apiHost(webServerUri.getHost())
76+
.port(webServerUri.getPort())
77+
.secure(false)
78+
.build();
79+
ReactorLogCacheClient examinee =
80+
ReactorLogCacheClient.builder()
81+
.connectionContext(connectionContext)
82+
.tokenProvider(TOKEN_PROVIDER)
83+
.build();
84+
Mono<String> logCacheRoot = examinee.getRoot();
85+
String rootString = logCacheRoot.block(Duration.ofSeconds(15));
86+
assertThat(rootString).isEqualTo(LOGCACHE);
87+
}
88+
89+
@Test
90+
void getRootFromEndpoint() {
91+
mockRequest(
92+
InteractionContext.builder()
93+
.request(TestRequest.builder().method(GET).path("/").build())
94+
.response(
95+
TestResponse.builder()
96+
.status(OK)
97+
.payload("fixtures/GET_response.json")
98+
.build())
99+
.build());
100+
URI webServerUri = URI.create(this.root.block(Duration.ofSeconds(5)));
101+
ConnectionContext connectionContext =
102+
DefaultConnectionContext.builder()
103+
.apiHost(webServerUri.getHost())
104+
.port(webServerUri.getPort())
105+
.secure(false)
106+
.build();
107+
ReactorLogCacheClient examinee =
108+
ReactorLogCacheClient.builder()
109+
.connectionContext(connectionContext)
110+
.tokenProvider(TOKEN_PROVIDER)
111+
.build();
112+
Mono<String> logCacheRoot = examinee.getRoot();
113+
String rootString = logCacheRoot.block(Duration.ofSeconds(5));
114+
assertThat(rootString)
115+
.isEqualTo("http://cache-for-logging.run.pivotal.io:" + webServerUri.getPort());
116+
}
117+
49118
@Test
50119
void info() {
51120
mockRequest(

cloudfoundry-client-reactor/src/test/resources/fixtures/GET_response.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
"uaa": {
2626
"href": "https://uaa.run.pivotal.io"
2727
},
28+
"log_cache": {
29+
"href": "https://cache-for-logging.run.pivotal.io"
30+
},
2831
"logging": {
2932
"href": "wss://doppler.run.pivotal.io:443"
3033
}

0 commit comments

Comments
 (0)