Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Host proxy object #423

Merged
merged 2 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.hotels.styx.routing.RoutingObjectRecord;
import com.hotels.styx.routing.db.StyxObjectStore;
import com.hotels.styx.routing.handlers.ConditionRouter;
import com.hotels.styx.routing.handlers.HostProxy;
import com.hotels.styx.routing.handlers.HttpInterceptorPipeline;
import com.hotels.styx.routing.handlers.PathPrefixRouter;
import com.hotels.styx.routing.handlers.ProxyToBackend;
Expand Down Expand Up @@ -58,6 +59,7 @@ public class RoutingObjectFactory {
private static final String INTERCEPTOR_PIPELINE = "InterceptorPipeline";
private static final String PROXY_TO_BACKEND = "ProxyToBackend";
private static final String PATH_PREFIX_ROUTER = "PathPrefixRouter";
private static final String HOST_PROXY = "HostProxy";


static {
Expand All @@ -67,6 +69,7 @@ public class RoutingObjectFactory {
.put(INTERCEPTOR_PIPELINE, new HttpInterceptorPipeline.Factory())
.put(PROXY_TO_BACKEND, new ProxyToBackend.Factory())
.put(PATH_PREFIX_ROUTER, new PathPrefixRouter.Factory())
.put(HOST_PROXY, new HostProxy.Factory())
.build();

BUILTIN_HANDLER_SCHEMAS = ImmutableMap.<String, Schema.FieldType>builder()
Expand All @@ -75,6 +78,7 @@ public class RoutingObjectFactory {
.put(INTERCEPTOR_PIPELINE, HttpInterceptorPipeline.SCHEMA)
.put(PROXY_TO_BACKEND, ProxyToBackend.SCHEMA)
.put(PATH_PREFIX_ROUTER, PathPrefixRouter.SCHEMA)
.put(HOST_PROXY, HostProxy.SCHEMA)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
/*
Copyright (C) 2013-2019 Expedia Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.routing.handlers;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.net.HostAndPort;
import com.hotels.styx.api.Eventual;
import com.hotels.styx.api.HttpInterceptor;
import com.hotels.styx.api.LiveHttpRequest;
import com.hotels.styx.api.LiveHttpResponse;
import com.hotels.styx.api.extension.Origin;
import com.hotels.styx.api.extension.service.ConnectionPoolSettings;
import com.hotels.styx.api.extension.service.TlsSettings;
import com.hotels.styx.client.Connection;
import com.hotels.styx.client.OriginStatsFactory;
import com.hotels.styx.client.StyxHostHttpClient;
import com.hotels.styx.client.applications.metrics.OriginMetrics;
import com.hotels.styx.client.connectionpool.ConnectionPool;
import com.hotels.styx.client.connectionpool.ExpiringConnectionFactory;
import com.hotels.styx.client.connectionpool.SimpleConnectionPoolFactory;
import com.hotels.styx.client.netty.connectionpool.NettyConnectionFactory;
import com.hotels.styx.config.schema.Schema;
import com.hotels.styx.infrastructure.configuration.yaml.JsonNodeConfig;
import com.hotels.styx.routing.RoutingObject;
import com.hotels.styx.routing.config.HttpHandlerFactory;
import com.hotels.styx.routing.config.RoutingObjectDefinition;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import static com.hotels.styx.api.extension.Origin.newOriginBuilder;
import static com.hotels.styx.api.extension.service.ConnectionPoolSettings.defaultConnectionPoolSettings;
import static com.hotels.styx.client.HttpRequestOperationFactory.Builder.httpRequestOperationFactoryBuilder;
import static com.hotels.styx.config.schema.SchemaDsl.atLeastOne;
import static com.hotels.styx.config.schema.SchemaDsl.bool;
import static com.hotels.styx.config.schema.SchemaDsl.field;
import static com.hotels.styx.config.schema.SchemaDsl.integer;
import static com.hotels.styx.config.schema.SchemaDsl.list;
import static com.hotels.styx.config.schema.SchemaDsl.object;
import static com.hotels.styx.config.schema.SchemaDsl.optional;
import static com.hotels.styx.config.schema.SchemaDsl.string;
import static com.hotels.styx.routing.config.RoutingSupport.missingAttributeError;
import static java.lang.String.format;
import static java.lang.String.join;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.CompletableFuture.completedFuture;

/**
* A routing object that proxies all incoming traffic to a remote host.
*/
public class HostProxy implements RoutingObject {
public static final Schema.FieldType SCHEMA = object(
field("host", string()),
optional("tlsSettings", object(
optional("trustAllCerts", bool()),
optional("sslProvider", string()),
optional("trustStorePath", string()),
optional("trustStorePassword", string()),
optional("protocols", list(string())),
optional("cipherSuites", list(string())),
optional("additionalCerts", list(object(
field("alias", string()),
field("certificatePath", string())
))),
atLeastOne("trustAllCerts",
"trustStorePath",
"trustStorePassword",
"protocols",
"cipherSuites",
"additionalCerts")
)),
optional("connectionPool", object(
optional("maxConnections", integer()),
optional("maxPendingConnections", integer()),
optional("connectTimeoutMillis", integer()),
optional("socketTimeoutMillis", integer()),
optional("pendingConnectionTimeoutMillis", integer()),
optional("connectionExpirationSeconds", integer()),
atLeastOne("maxConnections",
"maxPendingConnections",
"connectTimeoutMillis",
"socketTimeoutMillis",
"pendingConnectionTimeoutMillis",
"connectionExpirationSeconds")
)),
optional("responseTimeoutMillis", integer()),
optional("metricPrefix", string())
);

private final String message;
kvosper marked this conversation as resolved.
Show resolved Hide resolved
private final StyxHostHttpClient client;
private volatile boolean active = true;

@VisibleForTesting
final HostAndPort hostAndPort;

public HostProxy(HostAndPort hostAndPort, StyxHostHttpClient client) {
this.hostAndPort = requireNonNull(hostAndPort);
this.message = format("HostProxy %s:%d is stopped but received traffic.",
hostAndPort.getHostText(),
hostAndPort.getPort());
this.client = requireNonNull(client);
}

@Override
public Eventual<LiveHttpResponse> handle(LiveHttpRequest request, HttpInterceptor.Context context) {
if (active) {
return new Eventual<>(client.sendRequest(request));
} else {
return Eventual.error(new IllegalStateException(message));
}
}

@Override
public CompletableFuture<Void> stop() {
active = false;
client.close();
return completedFuture(null);
}

/**
* A factory for creating HostProxy routingObject objects.
*/
public static class Factory implements HttpHandlerFactory {
private static final int DEFAULT_REQUEST_TIMEOUT = 60000;
private static final int DEFAULT_TLS_PORT = 443;
private static final int DEFAULT_HTTP_PORT = 80;

private static Connection.Factory connectionFactory(
dvlato marked this conversation as resolved.
Show resolved Hide resolved
TlsSettings tlsSettings,
int responseTimeoutMillis,
OriginStatsFactory originStatsFactory,
long connectionExpiration) {

NettyConnectionFactory factory = new NettyConnectionFactory.Builder()
.httpRequestOperationFactory(
httpRequestOperationFactoryBuilder()
.flowControlEnabled(true)
.originStatsFactory(originStatsFactory)
.responseTimeoutMillis(responseTimeoutMillis)
.build()
)
.tlsSettings(Optional.ofNullable(tlsSettings).orElse(null))
.build();

if (connectionExpiration > 0) {
return new ExpiringConnectionFactory(connectionExpiration, factory);
} else {
return factory;
}
}

@Override
public RoutingObject build(List<String> parents, Context context, RoutingObjectDefinition configBlock) {
JsonNodeConfig config = new JsonNodeConfig(configBlock.config());

ConnectionPoolSettings poolSettings = config.get("connectionPool", ConnectionPoolSettings.class)
.orElse(defaultConnectionPoolSettings());

TlsSettings tlsSettings = config.get("tlsSettings", TlsSettings.class)
.orElse(null);

int responseTimeoutMillis = config.get("responseTimeoutMillis", Integer.class)
.orElse(DEFAULT_REQUEST_TIMEOUT);

String metricPrefix = config.get("metricPrefix", String.class)
.orElse("routing.objects.hostProxy");

HostAndPort hostAndPort = config.get("host")
.map(HostAndPort::fromString)
.map(it -> addDefaultPort(it, tlsSettings))
.orElseThrow(() -> missingAttributeError(configBlock, join(".", parents), "host"));

return createHostProxyHandler(context, hostAndPort, poolSettings, tlsSettings, responseTimeoutMillis, metricPrefix);
}

private static HostAndPort addDefaultPort(HostAndPort hostAndPort, TlsSettings tlsSettings) {
if (hostAndPort.hasPort()) {
return hostAndPort;
}

int defaultPort = Optional.ofNullable(tlsSettings)
.map(it -> DEFAULT_TLS_PORT)
.orElse(DEFAULT_HTTP_PORT);

return HostAndPort.fromParts(hostAndPort.getHostText(), defaultPort);
}

@NotNull
private static HostProxy createHostProxyHandler(
Context context,
HostAndPort hostAndPort,
ConnectionPoolSettings poolSettings,
TlsSettings tlsSettings,
int responseTimeoutMillis,
String metricPrefix) {

Origin origin = newOriginBuilder(hostAndPort.getHostText(), hostAndPort.getPort())
.applicationId(metricPrefix)
.id(format("%s:%s", hostAndPort.getHostText(), hostAndPort.getPort()))
.build();

OriginMetrics originMetrics = OriginMetrics.create(
origin,
context.environment().metricRegistry());

ConnectionPool.Factory connectionPoolFactory = new SimpleConnectionPoolFactory.Builder()
.connectionFactory(
connectionFactory(
tlsSettings,
responseTimeoutMillis,
theOrigin -> originMetrics,
poolSettings.connectionExpirationSeconds()))
.connectionPoolSettings(poolSettings)
.metricRegistry(context.environment().metricRegistry())
.build();

return new HostProxy(hostAndPort, StyxHostHttpClient.create(connectionPoolFactory.create(origin)));
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import java.util.concurrent.CompletableFuture
fun routingObjectDef(text: String) = YamlConfig(text).`as`((RoutingObjectDefinition::class.java))

data class RoutingContext(
val environment: Environment = mockk(),
val environment: Environment = Environment.Builder().build(),
val routeDb: StyxObjectStore<RoutingObjectRecord> = StyxObjectStore(),
val factory: RoutingObjectFactory = routingObjectFactory(),
val plugins: Iterable<NamedPlugin> = listOf(),
Expand Down
Loading