Browse Source

Polishing contribution

See gh-49744

Signed-off-by: Brian Clozel <brian.clozel@broadcom.com>
pull/49744/head
Brian Clozel 1 week ago
parent
commit
dba19c73c7
  1. 33
      module/spring-boot-cloudfoundry/src/main/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/reactive/SecurityService.java
  2. 4
      module/spring-boot-cloudfoundry/src/test/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/reactive/SecurityServiceTests.java

33
module/spring-boot-cloudfoundry/src/main/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/reactive/SecurityService.java

@ -22,7 +22,6 @@ import java.util.Map; @@ -22,7 +22,6 @@ import java.util.Map;
import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import org.jspecify.annotations.Nullable;
import reactor.core.publisher.Mono;
import reactor.netty.http.Http11SslContextSpec;
import reactor.netty.http.client.HttpClient;
@ -55,7 +54,7 @@ class SecurityService { @@ -55,7 +54,7 @@ class SecurityService {
private final String cloudControllerUrl;
private volatile @Nullable String uaaUrl;
private final Mono<String> uaaUrl;
SecurityService(WebClient.Builder webClientBuilder, String cloudControllerUrl, boolean skipSslValidation) {
Assert.notNull(webClientBuilder, "'webClientBuilder' must not be null");
@ -65,6 +64,18 @@ class SecurityService { @@ -65,6 +64,18 @@ class SecurityService {
}
this.webClient = webClientBuilder.build();
this.cloudControllerUrl = cloudControllerUrl;
this.uaaUrl = this.webClient.get()
.uri(this.cloudControllerUrl + "/info")
.retrieve()
.bodyToMono(Map.class)
.map((response) -> {
String tokenEndpoint = (String) response.get("token_endpoint");
Assert.state(tokenEndpoint != null, "No 'token_endpoint' found in response");
return tokenEndpoint;
})
.cacheInvalidateIf((token) -> false)
.onErrorMap((ex) -> new CloudFoundryAuthorizationException(Reason.SERVICE_UNAVAILABLE,
"Unable to fetch token keys from UAA."));
}
protected ReactorClientHttpConnector buildTrustAllSslConnector() {
@ -152,23 +163,7 @@ class SecurityService { @@ -152,23 +163,7 @@ class SecurityService {
* @return the UAA url Mono
*/
Mono<String> getUaaUrl() {
String uaaUrl = this.uaaUrl;
if (uaaUrl != null) {
return Mono.just(uaaUrl);
}
return this.webClient.get()
.uri(this.cloudControllerUrl + "/info")
.retrieve()
.bodyToMono(Map.class)
.map((response) -> {
String tokenEndpoint = (String) response.get("token_endpoint");
Assert.state(tokenEndpoint != null, "No 'token_endpoint' found in response");
this.uaaUrl = tokenEndpoint;
return tokenEndpoint;
})
.cache()
.onErrorMap((ex) -> new CloudFoundryAuthorizationException(Reason.SERVICE_UNAVAILABLE,
"Unable to fetch token keys from UAA."));
return this.uaaUrl;
}
}

4
module/spring-boot-cloudfoundry/src/test/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/reactive/SecurityServiceTests.java

@ -213,6 +213,10 @@ class SecurityServiceTests { @@ -213,6 +213,10 @@ class SecurityServiceTests {
.consumeNextWith((uaaUrl) -> assertThat(uaaUrl).isEqualTo(UAA_URL))
.expectComplete()
.verify();
prepareResponse((response) -> {
response.setBody("{\"token_endpoint\":\"" + UAA_URL + "\"}");
response.setHeader("Content-Type", "application/json");
});
StepVerifier.create(this.securityService.getUaaUrl())
.consumeNextWith((uaaUrl) -> assertThat(uaaUrl).isEqualTo(UAA_URL))
.expectComplete()

Loading…
Cancel
Save