Filter to change scheme and apply it to discovery integration.
I have a cloud-gateway enabled service discovery with consul using https, a http service and a https service both registered to consul for test. Gateway worked fine when it(https) redirect to https service, but failed when redirecting to http. Here is the exception:
io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record
I also changed my gateway to http, and this time it turned to fail while proxying http to https.
I googled this exception, it seems it's caused by accessing a http resource using https. I found some solutions in other issues, like adding a https2http convert filter or specifying the exact route redirecting https url to http url. The former needs other micro services to be either all https or all http, while the latter does not using auto-discovery, they cannot solve my question very well.
As I have registered my services to consul annoucing scheme as http/https, and I can find my service with [secure=true] and [secure=false] tags in consul-ui, so I guess cloud-gateway can tell which service is using https and which is http. I wonder is there a way to config cloud-gateway to auto redict to https and http service using consul discovery?
my gateway configuration (https):
spring:
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enable: true
httpclient:
ssl:
use-insecure-trust-manager: true
consul:
host: localhost
port: 8500
discovery:
register: false
server:
port: 10090
ssl:
enabled: true
key-store: classpath:keystore.p12
key-store-password: tomcat
key-store-type: PKCS12
key-alias: tomcat
service1 configuration (http):
spring:
application:
name: service1
cloud:
consul:
host: localhost
port: 8500
discovery:
health-check-interval: 15s
instanceId: service1
health-check-tls-skip-verify: true
scheme: http
server:
port: 10091
service2 configuration (https):
spring:
application:
name: service2
cloud:
consul:
host: localhost
port: 8500
discovery:
health-check-interval: 15s
instance-id: service2
health-check-tls-skip-verify: true
scheme: https
server:
port: 10092
ssl:
key-store: classpath:keystore.p12
key-store-password: tomcat
key-store-type: PKCS12
key-alias: tomcat
The gateway doesn't change the incoming request by default. So it's still trying to send an https request downstream. Also, the default filters added by the discovery integration only rewrite the path. There's currently no easy way to rewrite the scheme without a custom filter.
Might be related to #1842
@spencergibb
I don't recommend this solution. I've identified the root cause:
DefaultServiceInstance:
public void setUri(URI uri) { this.uri = uri; this.host = this.uri.getHost(); this.port = this.uri.getPort(); String scheme = this.uri.getScheme(); if ("https".equals(scheme)) { this.secure = true; } }
Since ConsulServiceInstance uses metadata to determine the secure status, it should not inherit from DefaultServiceInstance. Instead, it should directly implements ServiceInstance to prevent the secure property from being contaminated.
ConsulServiceInstance: `package org.springframework.cloud.consul.discovery;
import com.ecwid.consul.v1.health.model.HealthService; import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; import org.springframework.core.style.ToStringCreator;
import java.net.URI; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map;
import static org.springframework.cloud.consul.discovery.ConsulServerUtils.findHost;
public class ConsulServiceInstance implements ServiceInstance {
private String instanceId;
private String serviceId;
private String host;
private int port;
private boolean secure;
private Map<String, String> metadata = new LinkedHashMap<>();
@Override
public String getServiceId() {
return serviceId;
}
@Override
public String getInstanceId() {
return instanceId;
}
@Override
public String getHost() {
return host;
}
@Override
public int getPort() {
return port;
}
@Override
public boolean isSecure() {
return secure;
}
@Override
public URI getUri() {
return DefaultServiceInstance.getUri(this);
}
@Override
public Map<String, String> getMetadata() {
return metadata;
}
@Override
public String getScheme() {
return getUri().getScheme();
}
private HealthService healthService;
public ConsulServiceInstance(HealthService healthService, String serviceId) {
this(healthService.getService().getId(), serviceId, findHost(healthService),
getPort(healthService.getService()), getSecure(healthService), getMetadata(healthService),
healthService.getService().getTags());
this.healthService = healthService;
}
public ConsulServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure,
Map<String, String> metadata, List<String> tags) {
this(instanceId, serviceId, host, port, secure, metadata);
}
public ConsulServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure) {
this(instanceId, serviceId, host, port, secure, new LinkedHashMap<>());
}
public ConsulServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure,
Map<String, String> metadata) {
this.instanceId = instanceId;
this.serviceId = serviceId;
this.host = host;
this.port = port;
this.secure = secure;
this.metadata = metadata;
}
public ConsulServiceInstance() {
}
private static Map<String, String> getMetadata(HealthService healthService) {
Map<String, String> metadata = healthService.getService().getMeta();
if (metadata == null) {
metadata = new LinkedHashMap<>();
}
return metadata;
}
private static int getPort(HealthService.Service service) {
Integer port = service.getPort();
// Services without registered port receive '0' from consul 1.9 and null from
// consul 1.10
if (port == null) {
return 0;
}
return port;
}
private static boolean getSecure(HealthService healthService) {
boolean secure = false;
Map<String, String> metadata = getMetadata(healthService);
// getMetadata() above returns an empty Map if meta is null
if (metadata.containsKey("secure")) {
secure = Boolean.parseBoolean(metadata.get("secure"));
}
return secure;
}
public HealthService getHealthService() {
return this.healthService;
}
public void setHealthService(HealthService healthService) {
this.healthService = healthService;
}
public List<String> getTags() {
if (healthService != null) {
return healthService.getService().getTags();
}
return Collections.emptyList();
}
public void setInstanceId(String instanceId) {
this.instanceId = instanceId;
}
public void setHost(String host) {
this.host = host;
}
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}
public void setPort(int port) {
this.port = port;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
public void setSecure(boolean secure) {
this.secure = secure;
}
@Override
public String toString() {
return new ToStringCreator(this).append("instanceId", getInstanceId())
.append("serviceId", getServiceId())
.append("host", getHost())
.append("port", getPort())
.append("secure", isSecure())
.append("metadata", getMetadata())
.append("uri", getUri())
.append("healthService", healthService)
.toString();
}
} `