Skip to content

Commit 2be12cb

Browse files
committed
Add forward proto header configuration for cluster monitoring
1 parent e1b4ee7 commit 2be12cb

File tree

6 files changed

+55
-18
lines changed

6 files changed

+55
-18
lines changed

docs/installation.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,20 @@ that are marked as active.
362362
See [TrinoStatus](routing-rules.md#trinostatus) for more details on
363363
what each Trino status means.
364364

365+
Username and password for the health check can be configured by adding
366+
`backendState` to your configuration. The username and password must be valid
367+
across all backends.
368+
SSL and addXForwardProtoHeader can be configured based on whether the
369+
connection between the Trino Gateway and the backend is secure.
370+
371+
```yaml
372+
backendState:
373+
username: "user"
374+
password: "password"
375+
ssl: <false/true>
376+
addXForwardedProtoHeader: <false/true>
377+
```
378+
365379
The type of health check is configured by setting
366380

367381
```yaml
@@ -442,15 +456,7 @@ monitor:
442456
This uses a JDBC connection to query `system.runtime` tables for cluster
443457
information. It is required for the query count based routing strategy. This is
444458
recommended over `UI_API` since it does not restrict the Web UI authentication
445-
method of backend clusters. Configure a username and password by adding
446-
`backendState` to your configuration. The username and password must be valid
447-
across all backends.
448-
449-
```yaml
450-
backendState:
451-
username: "user"
452-
password: "password"
453-
```
459+
method of backend clusters.
454460

455461
Trino Gateway uses `explicitPrepare=false` by default. This property was introduced
456462
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+
addXForwardedProtoHeader: <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 addXForwardedProtoHeader;
5153

5254
public ClusterStatsHttpMonitor(BackendStateConfiguration backendStateConfiguration)
5355
{
5456
username = backendStateConfiguration.getUsername();
5557
password = backendStateConfiguration.getPassword();
58+
addXForwardedProtoHeader = backendStateConfiguration.getAddXForwardedProtoHeader();
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+
final Request.Builder requestBuilder = new Request.Builder()
141144
.url(HttpUrl.parse(targetUrl))
142-
.get()
143-
.build();
145+
.get();
146+
if (addXForwardedProtoHeader) {
147+
requestBuilder.addHeader(X_FORWARDED_PROTO, "HTTPS");
148+
}
149+
final 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 addXForwardedProtoHeader;
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.addXForwardedProtoHeader = backendStateConfiguration.getAddXForwardedProtoHeader();
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 (addXForwardedProtoHeader) {
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 addForwardedProtoHeader;
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+
addForwardedProtoHeader = backendStateConfiguration.getAddXForwardedProtoHeader();
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 (addForwardedProtoHeader) {
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 addXForwardedProtoHeader = false;
2122

2223
public BackendStateConfiguration() {}
2324

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

0 commit comments

Comments
 (0)