Skip to content
This repository was archived by the owner on Aug 7, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
7b8ce94
Added ExtendedSSLHandler to gracefully handle non ssl request
May 27, 2020
636125e
Merge branch 'staging_0_1_1' into issue_202
codinnvrends May 27, 2020
12ebe49
formatted the java files
May 27, 2020
10c0bed
Merge branch 'issue_202' of https://github.com/pytorch/serve into iss…
May 27, 2020
7d4ad62
Merge branch 'staging_0_1_1' into issue_202
codinnvrends May 28, 2020
4996b90
Fixed pmd error and added mar file cleanup
May 28, 2020
018ab48
Merge branch 'staging_0_1_1' into issue_202
codinnvrends May 30, 2020
9a1a448
Merge branch 'staging_0_1_1' into issue_202
codinnvrends Jun 3, 2020
75c4ad7
reverted to the change which was intended and mar file removal happen…
Jun 4, 2020
3038a6f
removed blank line
Jun 4, 2020
7d83769
Merge branch 'staging_0_1_1' into issue_202
harshbafna Jun 10, 2020
e4a8ea3
Merge branch 'master' into issue_202
codinnvrends Jun 29, 2020
d7edbc4
Merge branch 'master' into issue_202
codinnvrends Jul 7, 2020
7b79e6b
Merge branch 'master' into issue_202
codinnvrends Jul 9, 2020
bbd8a66
Merge branch 'master' into issue_202
codinnvrends Jul 15, 2020
c7ea390
Merge branch 'master' into issue_202
codinnvrends Jul 20, 2020
bb31db6
Merge branch 'master' into issue_202
maaquib Jul 21, 2020
e1c97db
Merge branch 'master' into issue_202
harshbafna Jul 29, 2020
e006e89
Merge branch 'master' into issue_202
harshbafna Aug 8, 2020
c652fb4
Merge branch 'master' into issue_202
harshbafna Dec 9, 2020
4ab1d77
fixed compilation issue
harshbafna Dec 9, 2020
ed30295
check if ssl enabled based on connector type
lxning Mar 12, 2021
7646789
check if ssl enabled based on connector type
lxning Mar 12, 2021
2d015d6
Merge branch 'master' into issue_202
lxning Mar 12, 2021
f0256a4
fmt
lxning Mar 12, 2021
c42586b
fix conflict
lxning Mar 12, 2021
1898fd4
fix import order
lxning Mar 12, 2021
b4be92d
fmt
lxning Mar 12, 2021
419d137
fmt ConfigManager.java
lxning Mar 12, 2021
d1dc90f
Merge branch 'master' into issue_202
maaquib Mar 18, 2021
4917b80
Merge branch 'master' into issue_202
maaquib Mar 19, 2021
5efea7f
Merge branch 'master' into issue_202
lxning Apr 15, 2021
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 @@ -7,6 +7,7 @@
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.ssl.SslContext;
import org.pytorch.serve.http.ApiDescriptionRequestHandler;
import org.pytorch.serve.http.ExtendedSSLHandler;
import org.pytorch.serve.http.HttpRequestHandler;
import org.pytorch.serve.http.HttpRequestHandlerChain;
import org.pytorch.serve.http.InferenceRequestHandler;
Expand Down Expand Up @@ -48,7 +49,7 @@ public void initChannel(Channel ch) {

int maxRequestSize = ConfigManager.getInstance().getMaxRequestSize();
if (sslCtx != null) {
pipeline.addLast("ssl", sslCtx.newHandler(ch.alloc()));
pipeline.addLast("ssl", new ExtendedSSLHandler(sslCtx, connectorType));
}
pipeline.addLast("http", new HttpServerCodec());
pipeline.addLast("aggregator", new HttpObjectAggregator(maxRequestSize));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.pytorch.serve.http;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.ssl.OptionalSslHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslHandler;
import java.util.List;
import org.pytorch.serve.util.ConfigManager;
import org.pytorch.serve.util.ConnectorType;
import org.pytorch.serve.util.NettyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExtendedSSLHandler extends OptionalSslHandler {
private static final Logger logger = LoggerFactory.getLogger(ExtendedSSLHandler.class);
/** the length of the ssl record header (in bytes) */
private static final int SSL_RECORD_HEADER_LENGTH = 5;

private ConnectorType connectorType;

public ExtendedSSLHandler(SslContext sslContext, ConnectorType connectorType) {
super(sslContext);
this.connectorType = connectorType;
}

@Override
protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out)
throws Exception {
if (in.readableBytes() < SSL_RECORD_HEADER_LENGTH) {
return;
}
ConfigManager configMgr = ConfigManager.getInstance();
if (SslHandler.isEncrypted(in) || !configMgr.isSSLEnabled(connectorType)) {
super.decode(context, in, out);
} else {
logger.error("Recieved HTTP request!");
NettyUtils.sendJsonResponse(
context,
new StatusResponse(
"This TorchServe instance only accepts HTTPS requests",
HttpResponseStatus.FORBIDDEN.code()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,15 @@ public final class ConfigManager {

public static final String PYTHON_EXECUTABLE = "python";

public static final Pattern ADDRESS_PATTERN =
Pattern.compile(
"((https|http)://([^:^/]+)(:([0-9]+))?)|(unix:(/.*))",
Pattern.CASE_INSENSITIVE);
private static Pattern pattern = Pattern.compile("\\$\\$([^$]+[^$])\\$\\$");

private Pattern blacklistPattern;
private Properties prop;
private static Pattern pattern = Pattern.compile("\\$\\$([^$]+[^$])\\$\\$");

private boolean snapshotDisabled;

private static ConfigManager instance;
Expand Down Expand Up @@ -718,6 +724,30 @@ public boolean isSnapshotDisabled() {
return snapshotDisabled;
}

public boolean isSSLEnabled(ConnectorType connectorType) {
String address = prop.getProperty(TS_INFERENCE_ADDRESS, "http://127.0.0.1:8080");
switch (connectorType) {
case MANAGEMENT_CONNECTOR:
address = prop.getProperty(TS_MANAGEMENT_ADDRESS, "http://127.0.0.1:8081");
break;
case METRICS_CONNECTOR:
address = prop.getProperty(TS_METRICS_ADDRESS, "http://127.0.0.1:8082");
break;
default:
break;
}
// String inferenceAddress = prop.getProperty(TS_INFERENCE_ADDRESS,
// "http://127.0.0.1:8080");
Matcher matcher = ConfigManager.ADDRESS_PATTERN.matcher(address);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid binding address: " + address);
}

String protocol = matcher.group(2);

return "https".equalsIgnoreCase(protocol);
}

public int getIniitialWorkerPort() {
return Integer.parseInt(prop.getProperty(TS_INITIAL_WORKER_PORT, "9000"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,10 @@
import java.net.SocketAddress;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;

public class Connector {

private static final Pattern ADDRESS_PATTERN =
Pattern.compile(
"((https|http)://([^:^/]+)(:([0-9]+))?)|(unix:(/.*))",
Pattern.CASE_INSENSITIVE);

private static boolean useNativeIo = ConfigManager.getInstance().useNativeIo();

private boolean uds;
Expand Down Expand Up @@ -75,7 +69,7 @@ private Connector(
}

public static Connector parse(String binding, ConnectorType connectorType) {
Matcher matcher = ADDRESS_PATTERN.matcher(binding);
Matcher matcher = ConfigManager.ADDRESS_PATTERN.matcher(binding);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid binding address: " + binding);
}
Expand Down