flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] zentol commented on a change in pull request #6796: [FLINK-10075] Redirect non-ssl requests to https url if ssl is enabled
Date Tue, 09 Oct 2018 09:12:58 GMT
zentol commented on a change in pull request #6796: [FLINK-10075] Redirect non-ssl requests
to https url if ssl is enabled
URL: https://github.com/apache/flink/pull/6796#discussion_r223617039
 
 

 ##########
 File path: 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.shaded.netty4.io.netty.buffer.ByteBuf;
+import org.apache.flink.shaded.netty4.io.netty.buffer.Unpooled;
+import org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler;
+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.DefaultFullHttpResponse;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequestDecoder;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseEncoder;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpVersion;
+import org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler;
+import org.apache.flink.shaded.netty4.io.netty.util.ReferenceCountUtil;
+
+import org.jboss.netty.handler.codec.http.HttpHeaders;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+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 {
+	protected final Logger log = LoggerFactory.getLogger(getClass());
+
+	/** the length of the ssl record header (in bytes). */
+	private static final int SSL_RECORD_HEADER_LENGTH = 5;
+
+	private final String confRedirectBaseUrl;
+	private final CompletableFuture<String> redirectBaseUrl;
+	private final SSLEngineFactory sslEngineFactory;
+
+	public RedirectingSslHandler(
+		String confRedirectHost,
+		CompletableFuture<String> redirectBaseUrl,
+		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) {
+			return;
+		}
+		if (SslHandler.isEncrypted(in)) {
+			handleSsl(context);
+		} else {
+			context.pipeline().replace(this, "http-decoder", new HttpRequestDecoder());
+			context.pipeline().addAfter("http-decoder", "redirecting-non-ssl", newNonSslHandler());
+		}
+	}
+
+	private void handleSsl(ChannelHandlerContext context) {
+		SslHandler sslHandler = null;
+		try {
+			sslHandler = new SslHandler(sslEngineFactory.createSSLEngine());
+			context.pipeline().replace(this, "ssl", sslHandler);
+			sslHandler = null;
+		} finally {
+			// Since the SslHandler was not inserted into the pipeline the ownership of the SSLEngine
was not
+			// transferred to the SslHandler.
+			if (sslHandler != null) {
+				ReferenceCountUtil.safeRelease(sslHandler.engine());
+			}
+		}
+	}
+
+	private ChannelHandler newNonSslHandler() {
+		return new ChannelInboundHandlerAdapter() {
 
 Review comment:
   I would make this a proper class, right now we're creating an entirely new class every
time a non-ssl request comes in.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message