Skip to content

Commit

Permalink
fix: Fix Discovery Eureka response if the service is not registred to…
Browse files Browse the repository at this point in the history
… allow reconnect by Enabler (#3795)

---------

Signed-off-by: Pavel Jareš <[email protected]>
Signed-off-by: Pablo Hernán Carle <[email protected]>
Co-authored-by: Pablo Carle <[email protected]>
Co-authored-by: Pablo Hernán Carle <[email protected]>
  • Loading branch information
3 people authored Sep 26, 2024
1 parent 13f89ff commit 9f58010
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public class SslContext {
private static AtomicBoolean isInitialized = new AtomicBoolean(false);
private static AtomicReference<SslContextConfigurer> configurer = new AtomicReference<>();

public synchronized static void reset() {
clientCertValid = null;
clientCertApiml = null;
clientCertUser = null;
clientCertUnknownUser = null;
apimlRootCert = null;
selfSignedUntrusted = null;
tlsWithoutCert = null;
configurer.set(null);
isInitialized.set(false);
}

public synchronized static void prepareSslAuthentication(SslContextConfigurer providedConfigurer) throws Exception {

if (configurer.get() != null && !configurer.get().equals(providedConfigurer)) {
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ configure(subprojects.findAll {it.name != 'platform'}) {
jvmArgs '--add-opens=java.base/java.io=ALL-UNNAMED'
jvmArgs '--add-opens=java.base/java.util=ALL-UNNAMED'
jvmArgs '--add-opens=java.base/java.util.concurrent=ALL-UNNAMED'
jvmArgs '--add-opens=java.base/java.lang=ALL-UNNAMED'
jvmArgs '--add-opens=java.base/java.lang.invoke=ALL-UNNAMED'
jvmArgs '--add-opens=java.base/java.lang.reflect=ALL-UNNAMED'
jvmArgs '--add-opens=java.base/javax.net.ssl=ALL-UNNAMED'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/

package org.zowe.apiml.discovery.config;

import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Primary
@Controller
@RequestMapping(value = "${server.error.path:${error.path:/error}}")
public class DiscoveryErrorController extends BasicErrorController {

public DiscoveryErrorController(ErrorAttributes errorAttributes, ServerProperties serverProperties, List<ErrorViewResolver> errorViewResolvers) {
super(errorAttributes, serverProperties.getError(), errorViewResolvers);
}

@Override
@RequestMapping // NOSONAR - No security risk, the controller cleans body for specified endpoint patterns
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
HttpStatus status = getStatus(request);
String originalUrl = String.valueOf(request.getAttribute("jakarta.servlet.error.request_uri"));
if ((status == HttpStatus.NOT_FOUND) && StringUtils.startsWith(originalUrl, "/eureka/apps/")) {
return new ResponseEntity<>(status);
}
return super.error(request);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/

package org.zowe.apiml.discovery.eureka;

import io.restassured.http.ContentType;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ActiveProfiles;
import org.zowe.apiml.discovery.functional.DiscoveryFunctionalTest;
import org.zowe.apiml.util.config.SslContext;
import org.zowe.apiml.util.config.SslContextConfigurer;

import static io.restassured.RestAssured.given;
import static org.hamcrest.core.IsEqual.equalTo;

@ActiveProfiles("https")
@TestInstance(Lifecycle.PER_CLASS)
public class EurekaEndpointTest extends DiscoveryFunctionalTest {

@Value("${server.ssl.keyPassword}")
char[] password;
@Value("${server.ssl.keyStore}")
String client_cert_keystore;
@Value("${server.ssl.keyStore}")
String keystore;

@BeforeEach
void setup() throws Exception {
SslContextConfigurer configurer = new SslContextConfigurer(password, client_cert_keystore, keystore);
SslContext.prepareSslAuthentication(configurer);
}

@BeforeAll
void init() {
SslContext.reset();
}

@AfterAll
void tearDown() {
SslContext.reset();
}

@Override
protected String getProtocol() {
return "https";
}

@Test
void givenInvalidService_whenRenewInstance_thenReturnEmptyBody() {
given()
.config(SslContext.clientCertApiml)
.contentType(ContentType.JSON)
.log().all()
.when()
.put(getDiscoveryUriWithPath("/eureka/apps/unknown-service-id/unknown-instance-id"))
.then()
.statusCode(HttpStatus.NOT_FOUND.value())
.body(equalTo(""));
}

}

0 comments on commit 9f58010

Please sign in to comment.