You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ch...@apache.org on 2018/10/15 11:50:59 UTC

[flink] branch release-1.5 updated: [FLINK-10075][rest] Redirect non-ssl requests to https url if ssl is enabled

This is an automated email from the ASF dual-hosted git repository.

chesnay pushed a commit to branch release-1.5
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/release-1.5 by this push:
     new 1a680bc  [FLINK-10075][rest] Redirect non-ssl requests to https url if ssl is enabled
1a680bc is described below

commit 1a680bcc1040f3c32a39c2ac70d90deee9fdd543
Author: azagrebin <az...@users.noreply.github.com>
AuthorDate: Wed Oct 10 13:18:08 2018 +0200

    [FLINK-10075][rest] Redirect non-ssl requests to https url if ssl is enabled
---
 .../flink/runtime/net/RedirectingSslHandler.java   | 115 +++++++++++++++++++++
 .../flink/runtime/rest/RestServerEndpoint.java     |   5 +-
 .../rest/handler/util/HandlerRedirectUtils.java    |   7 +-
 .../runtime/rest/RestServerEndpointITCase.java     |  19 ++++
 4 files changed, 142 insertions(+), 4 deletions(-)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/net/RedirectingSslHandler.java b/flink-runtime/src/main/java/org/apache/flink/runtime/net/RedirectingSslHandler.java
new file mode 100644
index 0000000..b9fab97
--- /dev/null
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/net/RedirectingSslHandler.java
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.flink.runtime.net;
+
+import org.apache.flink.runtime.rest.handler.util.HandlerRedirectUtils;
+
+import org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf;
+import org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener;
+import org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext;
+import org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandlerAdapter;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.ByteToMessageDecoder;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec;
+import org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler;
+import org.apache.flink.shaded.netty4.io.netty.util.ReferenceCountUtil;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+
+/** SSL handler which automatically redirects Non-SSL requests to SSL address. */
+public class RedirectingSslHandler extends ByteToMessageDecoder {
+	private static final Logger log = LoggerFactory.getLogger(RedirectingSslHandler.class);
+
+	private static final String SSL_HANDLER_NAME = "ssl";
+	private static final String HTTP_CODEC_HANDLER_NAME = "http-codec";
+	private static final String NON_SSL_HANDLER_NAME = "redirecting-non-ssl";
+
+	/** the length of the ssl record header (in bytes). */
+	private static final int SSL_RECORD_HEADER_LENGTH = 5;
+
+	@Nonnull private final String confRedirectBaseUrl;
+	@Nonnull private final CompletableFuture<String> redirectBaseUrl;
+	@Nonnull private final SSLEngineFactory sslEngineFactory;
+
+	public RedirectingSslHandler(
+		@Nonnull String confRedirectHost,
+		@Nonnull CompletableFuture<String> redirectBaseUrl,
+		@Nonnull SSLEngineFactory sslEngineFactory) {
+		this.confRedirectBaseUrl = "https://" + confRedirectHost + ":";
+		this.redirectBaseUrl = redirectBaseUrl;
+		this.sslEngineFactory = sslEngineFactory;
+	}
+
+	@Override
+	protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) {
+		if (in.readableBytes() >= SSL_RECORD_HEADER_LENGTH && SslHandler.isEncrypted(in)) {
+			handleSsl(context);
+		} else {
+			context.pipeline().addAfter(SSL_HANDLER_NAME, NON_SSL_HANDLER_NAME, new NonSslHandler());
+			context.pipeline().replace(this, HTTP_CODEC_HANDLER_NAME, new HttpServerCodec());
+		}
+	}
+
+	private void handleSsl(ChannelHandlerContext context) {
+		SslHandler sslHandler = new SslHandler(sslEngineFactory.createSSLEngine());
+		try {
+			context.pipeline().replace(this, SSL_HANDLER_NAME, sslHandler);
+		} catch (Throwable t) {
+			ReferenceCountUtil.safeRelease(sslHandler.engine());
+			throw t;
+		}
+	}
+
+	private class NonSslHandler extends ChannelInboundHandlerAdapter {
+		@Override
+		public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
+			HttpRequest request = msg instanceof HttpRequest ? (HttpRequest) msg : null;
+			String path = request == null ? "" : request.getUri();
+			String redirectAddress = getRedirectAddress(ctx);
+			log.trace("Received non-SSL request, redirecting to {}{}", redirectAddress, path);
+			HttpResponse response = HandlerRedirectUtils.getRedirectResponse(
+				redirectAddress, path, HttpResponseStatus.MOVED_PERMANENTLY);
+			if (request != null) {
+				if (!HttpHeaders.isKeepAlive(request)) {
+					ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
+				} else {
+					response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
+					ctx.writeAndFlush(response);
+				}
+			} else {
+				ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
+			}
+		}
+
+		private String getRedirectAddress(ChannelHandlerContext ctx) throws Exception {
+			return redirectBaseUrl.isDone() ? redirectBaseUrl.get() :
+				confRedirectBaseUrl + ((InetSocketAddress) (ctx.channel()).localAddress()).getPort();
+		}
+	}
+}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rest/RestServerEndpoint.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rest/RestServerEndpoint.java
index 3e85271..f215d3b 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/rest/RestServerEndpoint.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rest/RestServerEndpoint.java
@@ -22,6 +22,7 @@ import org.apache.flink.annotation.VisibleForTesting;
 import org.apache.flink.api.common.time.Time;
 import org.apache.flink.api.java.tuple.Tuple2;
 import org.apache.flink.runtime.concurrent.FutureUtils;
+import org.apache.flink.runtime.net.RedirectingSslHandler;
 import org.apache.flink.runtime.net.SSLEngineFactory;
 import org.apache.flink.runtime.rest.handler.PipelineErrorHandler;
 import org.apache.flink.runtime.rest.handler.RestHandlerSpecification;
@@ -41,7 +42,6 @@ import org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioServerSocke
 import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec;
 import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.router.Handler;
 import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.router.Router;
-import org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler;
 import org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler;
 import org.apache.flink.shaded.netty4.io.netty.util.concurrent.DefaultThreadFactory;
 
@@ -158,7 +158,8 @@ public abstract class RestServerEndpoint implements AutoCloseableAsync {
 
 					// SSL should be the first handler in the pipeline
 					if (sslEngineFactory != null) {
-						ch.pipeline().addLast("ssl", new SslHandler(sslEngineFactory.createSSLEngine()));
+						ch.pipeline().addLast("ssl",
+							new RedirectingSslHandler(restAddress, restAddressFuture, sslEngineFactory));
 					}
 
 					ch.pipeline()
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rest/handler/util/HandlerRedirectUtils.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rest/handler/util/HandlerRedirectUtils.java
index 900e1d2..eed1c71 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/rest/handler/util/HandlerRedirectUtils.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rest/handler/util/HandlerRedirectUtils.java
@@ -68,13 +68,16 @@ public class HandlerRedirectUtils {
 	}
 
 	public static HttpResponse getRedirectResponse(String redirectAddress, String path) {
+		return getRedirectResponse(redirectAddress, path, HttpResponseStatus.TEMPORARY_REDIRECT);
+	}
+
+	public static HttpResponse getRedirectResponse(String redirectAddress, String path, HttpResponseStatus code) {
 		checkNotNull(redirectAddress, "Redirect address");
 		checkNotNull(path, "Path");
 
 		String newLocation = String.format("%s%s", redirectAddress, path);
 
-		HttpResponse redirectResponse = new DefaultFullHttpResponse(
-				HttpVersion.HTTP_1_1, HttpResponseStatus.TEMPORARY_REDIRECT);
+		HttpResponse redirectResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, code);
 		redirectResponse.headers().set(HttpHeaders.Names.LOCATION, newLocation);
 		redirectResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0);
 
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/rest/RestServerEndpointITCase.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rest/RestServerEndpointITCase.java
index 59db163..e4a246e 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/rest/RestServerEndpointITCase.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/rest/RestServerEndpointITCase.java
@@ -56,8 +56,12 @@ import org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandler;
 import org.apache.flink.shaded.netty4.io.netty.handler.codec.TooLongFrameException;
 import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus;
 
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
 import org.apache.commons.io.IOUtils;
 import org.junit.After;
+import org.junit.Assume;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -88,6 +92,7 @@ import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
 
 import static java.util.Objects.requireNonNull;
+import static org.hamcrest.CoreMatchers.hasItems;
 import static org.hamcrest.Matchers.containsString;
 import static org.hamcrest.Matchers.instanceOf;
 import static org.junit.Assert.assertEquals;
@@ -413,6 +418,20 @@ public class RestServerEndpointITCase extends TestLogger {
 		assertEquals("foobar", fileContents.trim());
 	}
 
+	@Test
+	public void testNonSslRedirectForEnabledSsl() throws Exception {
+		Assume.assumeTrue(config.getBoolean(SecurityOptions.SSL_ENABLED));
+		OkHttpClient client = new OkHttpClient.Builder().followRedirects(false).build();
+		String httpsUrl = serverEndpoint.getRestBaseUrl() + "/path";
+		String httpUrl = httpsUrl.replace("https://", "http://");
+		Request request = new Request.Builder().url(httpUrl).build();
+		try (final Response response = client.newCall(request).execute()) {
+			assertEquals(HttpResponseStatus.MOVED_PERMANENTLY.code(), response.code());
+			assertThat(response.headers().names(), hasItems("Location"));
+			assertEquals(httpsUrl, response.header("Location"));
+		}
+	}
+
 	private HttpURLConnection openHttpConnectionForUpload(final String boundary) throws IOException {
 		final HttpURLConnection connection =
 			(HttpURLConnection) new URL(serverEndpoint.getRestBaseUrl() + "/upload").openConnection();