Skip to content

Commit

Permalink
Fix remote service redirects so that they will work in docker-compose…
Browse files Browse the repository at this point in the history
…/consul or in kubernetes (#2408)
  • Loading branch information
billoley committed Jun 7, 2024
1 parent c5a0f3a commit a13d7d0
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 9 deletions.
8 changes: 6 additions & 2 deletions properties/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ auditing.remoteauditservice.port=8443
# The scheme to use when connecting to the remote dictionary service
dictionary.remoteservice.scheme=https
# The host to connect to (or do a SRV lookup on) for the remote dictionary service
dictionary.remoteservice.host=dictionary
dictionary.remoteservice.host=localhost
# The port to connect to (unless a SRV lookup was performed) for the remote dictionary service
dictionary.remoteservice.port=8443
dictionary.remoteservice.port=8843
# Use the configured scheme/host/port for redirect calls (instead of those from the request)
dictionary.remoteservice.useConfiguredURIForRedirect=true

############################
#
Expand Down Expand Up @@ -147,6 +149,8 @@ querymetric.remoteservice.host=localhost
querymetric.remoteservice.port=9043
# Is the remote service enabled
querymetric.remoteservice.enabled=false
# Use the configured scheme/host/port for redirect calls (instead of those from the request)
querymetric.remoteservice.useConfiguredURIForRedirect=true

############################
#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package datawave.webservice.datadictionary;

import java.net.URI;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.apache.deltaspike.core.api.config.ConfigProperty;
import org.apache.http.client.utils.URIBuilder;
import org.xbill.DNS.TextParseException;

import com.codahale.metrics.Counter;
import com.codahale.metrics.annotation.Metric;
Expand Down Expand Up @@ -50,6 +53,10 @@ public class RemoteDataDictionary extends RemoteHttpService {
@ConfigProperty(name = "dw.remoteDictionary.port", defaultValue = "8843")
private int dictServicePort;

@Inject
@ConfigProperty(name = "dw.remoteDictionary.useConfiguredURIForRedirect", defaultValue = "false")
private boolean useConfiguredURIForRedirect;

@Inject
@ConfigProperty(name = "dw.remoteDictionary.data.uri", defaultValue = "/dictionary/data/v1/")
private String dictServiceURI;
Expand Down Expand Up @@ -111,6 +118,10 @@ public DataDictionaryBase<?,? extends MetadataFieldBase<?,?>> getDataDictionary(
// @formatter:on
}

public URIBuilder buildRedirectURI(String suffix, URI baseURI) throws TextParseException {
return buildRedirectURI(suffix, baseURI, useConfiguredURIForRedirect);
}

@Override
protected String serviceHost() {
return dictServiceHost;
Expand Down Expand Up @@ -175,5 +186,4 @@ protected Counter retryCounter() {
protected Counter failureCounter() {
return failureCounter;
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package datawave.webservice.edgedictionary;

import java.net.URI;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.apache.deltaspike.core.api.config.ConfigProperty;
import org.apache.http.client.utils.URIBuilder;
import org.xbill.DNS.TextParseException;

import com.codahale.metrics.Counter;
import com.codahale.metrics.annotation.Metric;
Expand Down Expand Up @@ -51,6 +54,10 @@ public class RemoteEdgeDictionary extends RemoteHttpService {
@ConfigProperty(name = "dw.remoteDictionary.port", defaultValue = "8843")
private int dictServicePort;

@Inject
@ConfigProperty(name = "dw.remoteDictionary.useConfiguredURIForRedirect", defaultValue = "false")
private boolean useConfiguredURIForRedirect;

@Inject
@ConfigProperty(name = "dw.remoteDictionary.edge.uri", defaultValue = "/dictionary/edge/v1/")
private String dictServiceURI;
Expand Down Expand Up @@ -109,6 +116,10 @@ public EdgeDictionaryBase<?,? extends MetadataBase<?>> getEdgeDictionary(Query s
// @formatter:on
}

public URIBuilder buildRedirectURI(String suffix, URI baseURI) throws TextParseException {
return buildRedirectURI(suffix, baseURI, useConfiguredURIForRedirect);
}

@Override
protected String serviceHost() {
return dictServiceHost;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package datawave.metrics.remote;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
Expand All @@ -15,7 +16,9 @@

import org.apache.deltaspike.core.api.config.ConfigProperty;
import org.apache.http.HttpEntity;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.xbill.DNS.TextParseException;

import com.codahale.metrics.Counter;
import com.codahale.metrics.annotation.Metric;
Expand Down Expand Up @@ -78,6 +81,10 @@ public class RemoteQueryMetricService extends RemoteHttpService {
@ConfigProperty(name = "dw.remoteQueryMetricService.port", defaultValue = "8443")
private int servicePort;

@Inject
@ConfigProperty(name = "dw.remoteQueryMetricService.useConfiguredURIForRedirect", defaultValue = "false")
private boolean useConfiguredURIForRedirect;

@Inject
@ConfigProperty(name = "dw.remoteQueryMetricService.uri", defaultValue = "/querymetric/v1/")
private String serviceURI;
Expand Down Expand Up @@ -224,6 +231,10 @@ public QueryMetricsSummaryResponse summaryUser(Date begin, Date end) {
// @formatter:on
}

public URIBuilder buildRedirectURI(String suffix, URI baseURI) throws TextParseException {
return buildRedirectURI(suffix, baseURI, useConfiguredURIForRedirect);
}

protected String getBearer() {
return "Bearer " + jwtTokenHandler.createTokenFromUsers(callerPrincipal.getName(), callerPrincipal.getProxiedUsers());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.security.Key;
Expand Down Expand Up @@ -258,6 +259,17 @@ public URIBuilder buildURI(String suffix) throws TextParseException {
return buildURI().setPath(serviceURI() + suffix);
}

public URIBuilder buildRedirectURI(String suffix, URI baseURI, boolean useConfiguredBaseURI) throws TextParseException {
URIBuilder builder;
if (useConfiguredBaseURI) {
builder = buildURI();
} else {
builder = new URIBuilder(baseURI);
}
builder.setPath(serviceURI() + suffix);
return builder;
}

protected <T> T executeGetMethod(Consumer<URIBuilder> uriCustomizer, Consumer<HttpGet> requestCustomizer, IOFunction<T> resultConverter,
Supplier<String> errorSupplier) throws URISyntaxException, IOException {
return executeGetMethod("", uriCustomizer, requestCustomizer, resultConverter, errorSupplier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ module add --name=com.mysql.driver --dependencies=javax.api,javax.transaction.ap
/system-property=dw.remoteDictionary.scheme:add(value=${dictionary.remoteservice.scheme})
/system-property=dw.remoteDictionary.host:add(value=${dictionary.remoteservice.host})
/system-property=dw.remoteDictionary.port:add(value=${dictionary.remoteservice.port})
/system-property=dw.remoteDictionary.useConfiguredURIForRedirect:add(value=${dictionary.remoteservice.useConfiguredURIForRedirect})

# Accumulo microservice config
/system-property=dw.remoteAccumuloService.useSrvDnsLookup:add(value=${accumulo.remoteservice.srv.lookup.enabled})
Expand All @@ -147,6 +148,7 @@ module add --name=com.mysql.driver --dependencies=javax.api,javax.transaction.ap
/system-property=dw.remoteQueryMetricService.scheme:add(value=${querymetric.remoteservice.scheme})
/system-property=dw.remoteQueryMetricService.host:add(value=${querymetric.remoteservice.host})
/system-property=dw.remoteQueryMetricService.port:add(value=${querymetric.remoteservice.port})
/system-property=dw.remoteQueryMetricService.useConfiguredURIForRedirect:add(value=${querymetric.remoteservice.useConfiguredURIForRedirect})

# Disable SASL client authentication in zookeeper
/system-property=zookeeper.sasl.client:add(value=false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Response getDataDictionaryWithSuffix(@PathParam("suffix") String suffix,
}

private Response sendRedirect(String suffix, UriInfo uriInfo) throws TextParseException, URISyntaxException {
URIBuilder builder = remoteDataDictionary.buildURI(suffix);
URIBuilder builder = remoteDataDictionary.buildRedirectURI(suffix, uriInfo.getBaseUri());
uriInfo.getQueryParameters().forEach((pname, valueList) -> valueList.forEach(pvalue -> builder.addParameter(pname, pvalue)));
return Response.temporaryRedirect(builder.build()).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.apache.deltaspike.core.api.config.ConfigProperty;
import org.apache.http.client.utils.URIBuilder;
import org.xbill.DNS.TextParseException;

Expand Down Expand Up @@ -42,7 +43,7 @@ public class EdgeDictionaryBean {
@GET
@Path("/")
public Response getEdgeDictionary(@Context UriInfo uriInfo) throws TextParseException, URISyntaxException {
URIBuilder builder = remoteEdgeDictionary.buildURI("");
URIBuilder builder = remoteEdgeDictionary.buildRedirectURI("", uriInfo.getBaseUri());
uriInfo.getQueryParameters().forEach((pname, valueList) -> valueList.forEach(pvalue -> builder.addParameter(pname, pvalue)));
return Response.temporaryRedirect(builder.build()).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,8 @@ private boolean isHtmlResponse(HttpServletRequest request) {
}

private Response sendRedirect(String suffix, UriInfo uriInfo) throws TextParseException, URISyntaxException {
URIBuilder builder = remoteQueryMetricService.buildURI(suffix);
if (uriInfo != null) {
uriInfo.getQueryParameters().forEach((pname, valueList) -> valueList.forEach(pvalue -> builder.addParameter(pname, pvalue)));
}
URIBuilder builder = remoteQueryMetricService.buildRedirectURI(suffix, uriInfo.getBaseUri());
uriInfo.getQueryParameters().forEach((pname, valueList) -> valueList.forEach(pvalue -> builder.addParameter(pname, pvalue)));
return Response.temporaryRedirect(builder.build()).build();
}
}

0 comments on commit a13d7d0

Please sign in to comment.