serve icon indicating copy to clipboard operation
serve copied to clipboard

SSL instructions fail

Open fbbradheintz opened this issue 6 years ago • 13 comments

Following "Option 1" of the "Enable SSL" section of the docs does not bring up working endpoints.

The config.properties file:

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

The failing Management API command:

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

Here's the result on the client side:

curl: (60) SSL certificate problem: self signed certificate
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

On the server, the following exception turns up in the log:

2020-04-15 17:05:50,396 [ERROR] epollEventLoopGroup-3-3 org.pytorch.serve.http.HttpRequestHandler -
io.netty.handler.codec.DecoderException: javax.net.ssl.SSLHandshakeException: Received fatal alert: unknown_ca
        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: javax.net.ssl.SSLHandshakeException: Received fatal alert: unknown_ca
        at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:131)
        at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:117)
        at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:307)
        at java.base/sun.security.ssl.Alert$AlertConsumer.consume(Alert.java:291)
        at java.base/sun.security.ssl.TransportContext.dispatch(TransportContext.java:180)
        at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:164)
        at java.base/sun.security.ssl.SSLEngineImpl.decode(SSLEngineImpl.java:668)
        at java.base/sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:623)
        at java.base/sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:441)
        at java.base/sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:420)
        at java.base/javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:634)
        at io.netty.handler.ssl.SslHandler$SslEngineType$3.unwrap(SslHandler.java:294)
        at io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1275)
        at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1177)
        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:

  • Catch the exception and provide informative logging on the server side.
  • Return HTTP response 403 with an informative client side message.
  • Provide working instructions for a self-signed cert installation.

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

Not sure if we have verified keystore. However, using certificate should work with the following change. You create a certificate locally yourself for testing this, but be sure to disable certificate validation on the client side, like --insecure on curl

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

@vdantu thoughts?

mycpuorg avatar Apr 17 '20 14:04 mycpuorg

@mycpuorg If we have not verified SSL functionality to work as expected, I would expect this section to be removed from the documentation for v0.1 release.

virgo24i avatar Apr 18 '20 16:04 virgo24i

I would agree with @lokeshgupta1975 . It may be worth while exposing this in the document, after adding a few tests.

vdantu avatar Apr 18 '20 17:04 vdantu

Here are the steps that worked for me:

  • Create Certificate with:
openssl req -x509 -nodes -days 2 -newkey rsa:2048 -keyout mykey.key -out mycert.pem
  • verify that the key and cert are created
ubuntu@ip-172-31-44-49:~/ts_ssl$ ls -ltr
total 132
-rw-rw-r-- 1 ubuntu ubuntu   1704 Apr 20 19:03 mykey.key
-rw-rw-r-- 1 ubuntu ubuntu   1212 Apr 20 19:03 mycert.pem
...
  • Create a config properties file like this:
ubuntu@ip-172-31-44-49:~/ts_ssl$ cat config.properties 
model_store=model_store
load_models=densenet161.mar
inference_address=https://0.0.0.0:8443
management_address=https://0.0.0.0:8444
private_key_file=mykey.key
certificate_file=mycert.pem
  • Start the server like this:
torchserve --start --ts-config /home/ubuntu/ts_ssl/config.properties
  • Query the Health of Server that's running on Secure Port (8443)
curl --insecure https://0.0.0.0:8443/ping
  • Why this option? From curl man page:
      -k, --insecure
              (TLS) By default, every SSL connection curl makes is verified to be secure. This option  allows  curl  to
              proceed and operate even for server connections otherwise considered insecure.
  • We should receive Health Status of the server if it is running on secure ports:
ubuntu@ip-172-31-44-49:~/ts_ssl$ curl --insecure https://0.0.0.0:8443/ping
2020-04-20 19:53:36,665 [INFO ] pool-1-thread-35 ACCESS_LOG - /127.0.0.1:38554 "GET /ping HTTP/1.1" 200 2
2020-04-20 19:53:36,665 [INFO ] pool-1-thread-35 TS_METRICS - Requests2XX.Count:1|#Level:Host|#hostname:ip-172-31-44-49,timestamp:null
{
  "status": "Healthy"
}
  • Make secure inference request actually happen Downloaded my favorite pic
ubuntu@ip-172-31-44-49:~/ts_ssl$ curl --insecure https://0.0.0.0:8443/predictions/densenet161 -T kitten.jpg 
2020-04-20 19:56:27,250 [INFO ] W-9009-densenet161_1.0 org.pytorch.serve.wlm.WorkerThread - Backend response time: 711
2020-04-20 19:56:27,251 [INFO ] W-9009-densenet161_1.0 ACCESS_LOG - /127.0.0.1:38556 "PUT /predictions/densenet161 HTTP/1.1" 200 713
2020-04-20 19:56:27,251 [INFO ] W-9009-densenet161_1.0 TS_METRICS - Requests2XX.Count:1|#Level:Host|#hostname:ip-172-31-44-49,timestamp:null
2020-04-20 19:56:27,252 [DEBUG] W-9009-densenet161_1.0 org.pytorch.serve.wlm.Job - Waiting time: 0, Backend time: 714
[
  {
    "tiger_cat": 0.46933451294898987
  },
  {
    "tabby": 0.4633886516094208
  },
  {
    "Egyptian_cat": 0.06456172466278076
  },
  {
    "lynx": 0.0012828236212953925
  },
  {
    "plastic_bag": 0.00023323073401115835
  }

mycpuorg avatar Apr 20 '20 19:04 mycpuorg

FWIW, @fbbradheintz @adityabindal @lokeshgupta1975 and I verified the steps described under "Enable SSL" section in https://github.com/pytorch/serve/blob/master/docs/configuration.md

We were able to successfully ping the server and make inferences identical to the steps shared above.

Your steps above are missing the following curl option:

--insecure

mycpuorg avatar Apr 20 '20 20:04 mycpuorg

Looks good! Please close this issue after merging.

fbbradheintz avatar Apr 21 '20 02:04 fbbradheintz

Self-assigned for testing

fbbradheintz avatar Apr 24 '20 22:04 fbbradheintz

Verified - closing.

fbbradheintz avatar Apr 26 '20 22:04 fbbradheintz

curl -k https://0.0.0.0:8443/ping

wugoukanle avatar May 09 '20 09:05 wugoukanle

@fbbradheintz Hi, We are currently developing a website that includes a backend processing that should be done in the pytorch serve on AWS server. The problem is we need a SSL certificate for the application. So we have tried 2 methods that have been shown in the "torchserve advanced configurations". But in there, they only have provided a way to create a "self-signed" SSL certificate. With that, we are getting "self signed certificate error" on browser. We have tried "lets encrypt SSL certification" in order to obtained CA-signed SSL certificate but it is required to provide a domain name instead of IP address. In order to obtain a domain name, we have tried freenom.com but we were unable to proceed with that also.

Please let us know the proper way to obtain a CA-signed SSL certificate to make secure communication between backend on AWS (torchserve) and front end deployed on Heroku.

Thank you in advance

udithhaputhanthri avatar Aug 28 '20 16:08 udithhaputhanthri

Please let us know the proper way to obtain a CA-signed SSL certificate to make secure communication between backend on AWS (torchserve) and front end deployed on Heroku.

As far as I know, you will need a domain name to get CA signed SSL cert. There are different articles on the net on how to get CA signed cert. NOTE this is a general process for any webserver and nothing to do with torchserve as such.

codinnvrends avatar Aug 31 '20 06:08 codinnvrends