Skip to content

Commit

Permalink
Disable routing to Discovery and ZAAS from Gateway (#3688)
Browse files Browse the repository at this point in the history
Signed-off-by: Elena Kubantseva <[email protected]>
  • Loading branch information
arxioly authored Aug 16, 2024
1 parent 6ce0254 commit 1139243
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.util.PatternMatchUtils;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.zowe.apiml.auth.Authentication;
import org.zowe.apiml.auth.AuthenticationScheme;
Expand All @@ -44,6 +45,9 @@ public class RouteLocator implements RouteDefinitionLocator {

private static final EurekaMetadataParser metadataParser = new EurekaMetadataParser();

@Value("${apiml.routing.ignoredServices:}")
private String[] ignoredServices;

@Value("${apiml.service.forwardClientCertEnabled:false}")
private boolean forwardingClientCertEnabled;

Expand Down Expand Up @@ -80,6 +84,7 @@ public RouteLocator(

Flux<List<ServiceInstance>> getServiceInstances() {
return discoveryClient.getServices()
.filter(this::filterIgnored)
.flatMap(service -> discoveryClient.getInstances(service)
.collectList());
}
Expand Down Expand Up @@ -186,4 +191,7 @@ public Flux<RouteDefinition> getRouteDefinitions() {
.flatMapIterable(list -> list);
}

private boolean filterIgnored(String serviceId) {
return !PatternMatchUtils.simpleMatch(ignoredServices, serviceId);
}
}
8 changes: 1 addition & 7 deletions gateway-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ apiml:
timeToLive: 10000
routing:
instanceIdHeader: false
ignoredServices: discovery,zaas # to disable routing to the Discovery and ZAAS service
service:
apimlId: apiml1
corsEnabled: true
Expand Down Expand Up @@ -113,13 +114,6 @@ server:
trustStore: keystore/localhost/localhost.truststore.p12
trustStorePassword: password
trustStoreType: PKCS12

cloud:
gateway:
discovery:
locator:
enabled: false
lowerCaseServiceId: true
application:
name: gateway
main:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ void givenDiscoveryClient_whenGetServiceInstances_thenReturnAllServiceInstances(
);
}

@Test
void givenIgnoredServices_whenGetServiceInstances_thenFilteredOut() {
ReflectionTestUtils.setField(routeLocator, "ignoredServices", new String[]{"discovery"} );
when(discoveryClient.getServices()).thenReturn(Flux.fromArray(new String[] {"service", "discovery"}));
ServiceInstance serviceInstance = mock(ServiceInstance.class);
ServiceInstance discoveryInstance = mock(ServiceInstance.class);
doReturn(Flux.just(serviceInstance)).when(discoveryClient).getInstances("service");
doReturn(Flux.just(discoveryInstance)).when(discoveryClient).getInstances("discovery");

doCallRealMethod().when(routeLocator).getServiceInstances();
assertArrayEquals(new Object[] {Collections.singletonList(serviceInstance)}, routeLocator.getServiceInstances().toStream().toArray());
}

@Test
void givenNoAuthentication_whenSetAuth_thenDoNothing() {
assertDoesNotThrow(() -> routeLocator.setAuth(MOCK_SERVICE,null, null));
Expand Down

0 comments on commit 1139243

Please sign in to comment.