Skip to content

Commit fa33312

Browse files
raj-manvarebyhr
authored andcommitted
Add forward proto header configuration for cluster monitoring
1 parent 8a7b968 commit fa33312

File tree

6 files changed

+58
-18
lines changed

6 files changed

+58
-18
lines changed

docs/installation.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,23 @@ that are marked as active.
381381
See [TrinoStatus](routing-rules.md#trinostatus) for more details on
382382
what each Trino status means.
383383

384+
Username and password for the health check can be configured by adding
385+
`backendState` to your configuration. The username and password must be valid
386+
across all backends.
387+
388+
SSL and xForwardProtoHeader can be configured based on whether the
389+
connection between the Trino Gateway and the backend is secure.
390+
By default, both are set to false.
391+
Find more information in [the related Trino documentation](https://trino.io/docs/current/security/tls.html#use-a-load-balancer-to-terminate-tls-https).
392+
393+
```yaml
394+
backendState:
395+
username: "user"
396+
password: "password"
397+
ssl: <false/true>
398+
xForwardedProtoHeader: <false/true>
399+
```
400+
384401
The type of health check is configured by setting
385402

386403
```yaml
@@ -461,15 +478,7 @@ monitor:
461478
This uses a JDBC connection to query `system.runtime` tables for cluster
462479
information. It is required for the query count based routing strategy. This is
463480
recommended over `UI_API` since it does not restrict the Web UI authentication
464-
method of backend clusters. Configure a username and password by adding
465-
`backendState` to your configuration. The username and password must be valid
466-
across all backends.
467-
468-
```yaml
469-
backendState:
470-
username: "user"
471-
password: "password"
472-
```
481+
method of backend clusters.
473482

474483
Trino Gateway uses `explicitPrepare=false` by default. This property was introduced
475484
in Trino 431, and uses a single query for prepared statements, instead of a

docs/routers.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ backendState:
6767
username: <usernme>
6868
password: <password>
6969
ssl: <false/true>
70+
xForwardedProtoHeader: <false/true>
7071

7172
clusterStatsConfiguration:
7273
monitorType: UI_API

gateway-ha/src/main/java/io/trino/gateway/ha/clustermonitor/ClusterStatsHttpMonitor.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Map;
3434

3535
import static com.google.common.base.Strings.isNullOrEmpty;
36+
import static com.google.common.net.HttpHeaders.X_FORWARDED_PROTO;
3637
import static io.airlift.http.client.HttpStatus.fromStatusCode;
3738
import static io.trino.gateway.ha.handler.HttpUtils.UI_API_QUEUED_LIST_PATH;
3839
import static io.trino.gateway.ha.handler.HttpUtils.UI_API_STATS_PATH;
@@ -48,11 +49,13 @@ public class ClusterStatsHttpMonitor
4849

4950
private final String username;
5051
private final String password;
52+
private final boolean xForwardedProtoHeader;
5153

5254
public ClusterStatsHttpMonitor(BackendStateConfiguration backendStateConfiguration)
5355
{
5456
username = backendStateConfiguration.getUsername();
5557
password = backendStateConfiguration.getPassword();
58+
xForwardedProtoHeader = backendStateConfiguration.getXForwardedProtoHeader();
5659
}
5760

5861
@Override
@@ -137,10 +140,13 @@ private String queryCluster(ProxyBackendConfiguration backend, String path)
137140
}
138141

139142
String targetUrl = backend.getProxyTo() + path;
140-
Request request = new Request.Builder()
143+
Request.Builder requestBuilder = new Request.Builder()
141144
.url(HttpUrl.parse(targetUrl))
142-
.get()
143-
.build();
145+
.get();
146+
if (xForwardedProtoHeader) {
147+
requestBuilder.addHeader(X_FORWARDED_PROTO, "https");
148+
}
149+
Request request = requestBuilder.build();
144150

145151
Call call = client.newCall(request);
146152

gateway-ha/src/main/java/io/trino/gateway/ha/clustermonitor/ClusterStatsJmxMonitor.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Optional;
3030
import java.util.stream.Collectors;
3131

32+
import static com.google.common.net.HttpHeaders.X_FORWARDED_PROTO;
3233
import static io.airlift.http.client.HttpUriBuilder.uriBuilderFrom;
3334
import static io.airlift.http.client.JsonResponseHandler.createJsonResponseHandler;
3435
import static io.airlift.http.client.Request.Builder.prepareGet;
@@ -45,12 +46,14 @@ public class ClusterStatsJmxMonitor
4546
private final HttpClient client;
4647
private final String username;
4748
private final String password;
49+
private final boolean xForwardedProtoHeader;
4850

4951
public ClusterStatsJmxMonitor(HttpClient client, BackendStateConfiguration backendStateConfiguration)
5052
{
5153
this.client = requireNonNull(client, "client is null");
5254
this.username = backendStateConfiguration.getUsername();
5355
this.password = backendStateConfiguration.getPassword();
56+
this.xForwardedProtoHeader = backendStateConfiguration.getXForwardedProtoHeader();
5457
}
5558

5659
private static void updateClusterStatsFromDiscoveryNodeManagerResponse(JmxResponse response, ClusterStats.Builder clusterStats)
@@ -125,13 +128,16 @@ private Optional<JmxResponse> queryJmx(ProxyBackendConfiguration backend, String
125128
requireNonNull(mbeanName, "mbeanName is null");
126129

127130
String jmxUrl = backend.getProxyTo();
128-
Request preparedRequest = prepareGet()
131+
Request.Builder requestBuilder = prepareGet()
129132
.setUri(uriBuilderFrom(URI.create(jmxUrl))
130133
.appendPath(JMX_PATH)
131134
.appendPath(mbeanName)
132135
.build())
133-
.addHeader("X-Trino-User", username)
134-
.build();
136+
.addHeader("X-Trino-User", username);
137+
if (xForwardedProtoHeader) {
138+
requestBuilder.addHeader(X_FORWARDED_PROTO, "https");
139+
}
140+
Request preparedRequest = requestBuilder.build();
135141

136142
boolean isHttps = preparedRequest.getUri().getScheme().equalsIgnoreCase("https");
137143

gateway-ha/src/main/java/io/trino/gateway/ha/clustermonitor/ClusterStatsMetricsMonitor.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import static com.google.common.base.Strings.isNullOrEmpty;
3737
import static com.google.common.collect.ImmutableMap.toImmutableMap;
38+
import static com.google.common.net.HttpHeaders.X_FORWARDED_PROTO;
3839
import static io.airlift.http.client.HttpUriBuilder.uriBuilderFrom;
3940
import static io.airlift.http.client.Request.Builder.prepareGet;
4041
import static io.airlift.http.client.ResponseHandlerUtils.propagate;
@@ -58,6 +59,7 @@ public class ClusterStatsMetricsMonitor
5859
private final ImmutableSet<String> metricNames;
5960
private final Map<String, Float> metricMinimumValues;
6061
private final Map<String, Float> metricMaximumValues;
62+
private final boolean xForwardedProtoHeader;
6163

6264
public ClusterStatsMetricsMonitor(HttpClient httpClient, BackendStateConfiguration backendStateConfiguration, MonitorConfiguration monitorConfiguration)
6365
{
@@ -81,6 +83,7 @@ public ClusterStatsMetricsMonitor(HttpClient httpClient, BackendStateConfigurati
8183
.addAll(metricMaximumValues.keySet())
8284
.build();
8385
metricsResponseHandler = new MetricsResponseHandler(metricNames);
86+
xForwardedProtoHeader = backendStateConfiguration.getXForwardedProtoHeader();
8487
}
8588

8689
private static ClusterStats getUnhealthyStats(ProxyBackendConfiguration backend)
@@ -134,11 +137,15 @@ private Map<String, String> getMetrics(String baseUrl, int retriesRemaining)
134137
uri.addParameter("name[]", metric);
135138
}
136139

137-
Request request = prepareGet()
140+
Request.Builder requestBuilder = prepareGet()
138141
.setUri(uri.build())
139142
.addHeader(identityHeader.name, identityHeader.value)
140-
.addHeader("Content-Type", "application/openmetrics-text; version=1.0.0; charset=utf-8")
141-
.build();
143+
.addHeader("Content-Type", "application/openmetrics-text; version=1.0.0; charset=utf-8");
144+
if (xForwardedProtoHeader) {
145+
requestBuilder.addHeader(X_FORWARDED_PROTO, "https");
146+
}
147+
Request request = requestBuilder.build();
148+
142149
try {
143150
return httpClient.execute(request, metricsResponseHandler);
144151
}

gateway-ha/src/main/java/io/trino/gateway/ha/config/BackendStateConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class BackendStateConfiguration
1818
private String username;
1919
private String password = "";
2020
private Boolean ssl = false;
21+
private boolean xForwardedProtoHeader;
2122

2223
public BackendStateConfiguration() {}
2324

@@ -50,4 +51,14 @@ public void setSsl(Boolean ssl)
5051
{
5152
this.ssl = ssl;
5253
}
54+
55+
public boolean getXForwardedProtoHeader()
56+
{
57+
return xForwardedProtoHeader;
58+
}
59+
60+
public void setXForwardedProtoHeader(boolean xForwardedProtoHeader)
61+
{
62+
this.xForwardedProtoHeader = xForwardedProtoHeader;
63+
}
5364
}

0 commit comments

Comments
 (0)