serve icon indicating copy to clipboard operation
serve copied to clipboard

SSL-enabled TorchServe handles http: protocol incorrectly

Open fbbradheintz opened this issue 6 years ago • 14 comments

If you're running a TorchServe installation with the following config:

inference_address=https://0.0.0.0:8443
management_address=https://0.0.0.0:8444
keystore=keystore.p12
keystore_pass=asdfasdf
keystore_type=PKCS12

And you attempt an http call - not https - such as:

curl -X POST "http://localhost:8444/models?url=r18ts.mar&initial_workers=1"

There is no response, and an exception shows in the log:

2020-04-15 17:04:46,302 [ERROR] epollEventLoopGroup-3-2 org.pytorch.serve.http.HttpRequestHandler -
io.netty.handler.codec.DecoderException: io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: 504f5354202f6d6f64656c733f75726c3d72313874732e6d617226696e697469616c5f776f726b6572733d3120485454502f312e310d0a486f73743a206c6f63616c686f73743a383434340d0a557365722d4167656e743a206375726c2f372e36302e300d0a4163636570743a202a2f2a0d0a0d0a
        at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:459)
        at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965)
        at io.netty.channel.epoll.AbstractEpollStreamChannel$EpollStreamUnsafe.epollInReady(AbstractEpollStreamChannel.java:808)
        at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:404)
        at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:304)
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: 504f5354202f6d6f64656c733f75726c3d72313874732e6d617226696e697469616c5f776f726b6572733d3120485454502f312e310d0a486f73743a206c6f63616c686f73743a383434340d0a557365722d4167656e743a206375726c2f372e36302e300d0a4163636570743a202a2f2a0d0a0d0a
        at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1156)
        at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1221)
        at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:489)
        at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:428)
        ... 14 more

Suggested Remediation:

  • Handle this case without throwing an exception, as it is something that is likely to happen during normal operation.
  • Log an informative error message, perhaps in access_log.log.
  • Give a useful error message in an HTTP response to the user, e.g., "This TorchServe instance only accepts HTTPS requests.', with a response code of 403.

fbbradheintz avatar Apr 15 '20 17:04 fbbradheintz

Also fails for Inference API calls (e.g., curl https://localhost:8443/ping).

fbbradheintz avatar Apr 15 '20 17:04 fbbradheintz

Also fails for localhost-only config:

inference_address=https://127.0.0.1:8443
management_address=https://127.0.0.1:8444
keystore=keystore.p12
keystore_pass=aoeuaoeu
keystore_type=PKCS12

fbbradheintz avatar Apr 15 '20 17:04 fbbradheintz

Also fails for "Option 2" (the openssl option), with config file:

inference_address=https://127.0.0.1:8443
management_address=https://127.0.0.1:8444
private_key_file=tstest.key
certificate_file=tstest.pem

Same result with the 0.0.0.0 configuration.

fbbradheintz avatar Apr 15 '20 18:04 fbbradheintz

Sorry I was meaning to change tags on #203 Reverting the tags to original here.

mycpuorg avatar Apr 20 '20 20:04 mycpuorg

This is still an issue.

A better solution would have the server not throwing an exception, but spotting the issue and sending a 301 redirect to the client.

fbbradheintz avatar Apr 26 '20 22:04 fbbradheintz

I learned we should use --insecure option with curl to make ssl work. How this translates to some other machine trying to access APIs with IP address of machine hosting torchserve server?

I tried using the same --insecure option on other machine but it got "Connection refused" error. (My firewall is disabled)

cc: @mycpuorg

Kshitij09 avatar Jul 21 '20 13:07 Kshitij09

@dhaniram-kshirsagar what's the progress on this issue?

Kshitij09 avatar Jul 23 '20 07:07 Kshitij09

@Kshitij09 As of no progress on this one due to release activities. Will try to reply by tomorrow.

codinnvrends avatar Jul 23 '20 14:07 codinnvrends

I've written a client app and trying make REST calls to torchserve, but due to this issue I'm not able to do so. I've made sure that the problem is not with my firewall settings since I was able to make calls to other servers on the same machine. So let me know the progress and workaround for the same.

Kshitij09 avatar Jul 23 '20 15:07 Kshitij09

@maaquib @dhaniram-kshirsagar could you confirm that this is a bug? or am I missing something and there is a workaround for this issue?

All I want is to be able to make REST calls from a remote machine

Kshitij09 avatar Jul 28 '20 15:07 Kshitij09

@Kshitij09: I was successfully able to start TorchServe with SSL enabled on one EC2 node using following steps:

  • generate a keystore with Java's keytool
keytool -genkey -keyalg RSA -alias ts -keystore keystore.p12 -storepass changeit -storetype PKCS12 -validity 3600 -keysize 2048 -dname "CN=www.MY_TS.com, OU=Cloud Service, O=model server, L=Palo Alto, ST=California, C=US"
  • created a config.properties with SSL related configurations :
inference_address=https://0.0.0.0:8443
management_address=https://0.0.0.0:8444
keystore=keystore.p12
keystore_pass=changeit
keystore_type=PKCS12
  • Started TorchServe with the following command:
torchserve --start --model-store model_store --ts-config config.properties 

I was also able to successfully execute the following REST API calls from another EC2 instance :

ubuntu@ip-172-31-30-88:~$ curl --insecure "https://172.31.54.27:8444/models"
{
  "models": []
}
ubuntu@ip-172-31-30-88:~$ curl --insecure -X POST  "https://172.31.54.27:8444/models?url=https://torchserve.s3.amazonaws.com/mar_files/densenet161.mar&initial_workers=1"
{
  "status": "Model \"densenet161\" Version: 1.0 registered with 1 initial workers"
}
ubuntu@ip-172-31-30-88:~$ curl --insecure "https://172.31.54.27:8443/predictions/densenet161" -T kitten.jpg 
[
  {
    "tiger_cat": 0.46933549642562866
  },
  {
    "tabby": 0.4633878469467163
  },
  {
    "Egyptian_cat": 0.06456148624420166
  },
  {
    "lynx": 0.0012828214094042778
  },
  {
    "plastic_bag": 0.00023323034110944718
  }
]

Could you please share your config file used for starting TorchServe. Also, check if you are able to connect to the inference and management port through telnet command from another machine.

harshbafna avatar Jul 29 '20 06:07 harshbafna

I have followed almost similar steps but I was not able to connect inference and management API ports through telnet.

However, I was able to connect some other ports(nginx server) on the same machine using telnet.

Are there any subtleties involved?

Kshitij09 avatar Jul 29 '20 08:07 Kshitij09

I have followed almost similar steps but I was not able to connect inference and management API ports through telnet.

However, I was able to connect some other ports(nginx server) on the same machine using telnet.

Are there any subtleties involved?

I just had to make the ports accessible in the inbound rules of the security group. But that is AWS environment-specific. Mostly looks like the firewall issue (or security group issue in case you are also on AWS environment).

harshbafna avatar Jul 29 '20 08:07 harshbafna

@harshbafna I've created keystore file using keytool. My config.properties has following details:

inference_address=https://127.0.0.1:8083
management_address=https://127.0.0.1:8084
keystore=keystore.p12
keystore_pass=changeit
keystore_type=PKCS12
cors_allowed_origin=*
cors_allowed_methods=GET, POST, PUT, OPTIONS

I used this command to start the server

torchserve --start --model-store model_store --models densenet161=densenet161.mar --no-config-snapshots --ts-config config.properties

This is my firewall setting:

image

doing curl from same machine is working

curl "https://127.0.0.1:8084/models" --insecure

Output:

{
  "models": [
    {
      "modelName": "densenet161",
      "modelUrl": "densenet161.mar"
    }
  ]
}

But telnet from the same machine isn't working

telnet 127.0.0.1

Output:

Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

Kshitij09 avatar Aug 05 '20 11:08 Kshitij09