1
votes

I'm trying to set up RabbitMQ Web Stomp to subscribe to RabbitMQ queues directly from client-side JavaScript. I have errors when trying to use SSL with the RabbitMQ Web Stomp javascript client.

The RabbitMQ server is running properly in a Docker container, and is accessed without troubles via the Tomcat web server running Java Spring on another container.

Here is the RabbitMQ configuration file:

[
  {rabbit, [ 
             {ssl_listeners, [5671] }, 
             {ssl_options, [ 
               {cacertfile,             "/etc/rabbitmq/ssl/ca/cacert.pem" },
               {certfile,               "/etc/rabbitmq/ssl/server/rabbitmq.cert.pem" },
               {keyfile,                "/etc/rabbitmq/ssl/server/rabbitmq.key.pem" },
               {verify,                 verify_peer},
               {fail_if_no_peer_cert,   false}]},
             {loopback_users, []}
  ]},
  {rabbitmq_web_stomp, [ 
                         {ssl_config, [
                           {port,       15671},
                           {backlog,    1024},
                           {certfile,   "/etc/rabbitmq/ssl/ca/cacert.pem"},
                           {keyfile,    "/etc/rabbitmq/ssl/server/rabbitmq.cert.pem"},
                           {cacertfile, "/etc/rabbitmq/ssl/server/rabbitmq.key.pem"},
                           {password,   "changeme"}
                         ]}
                      ]}
].

The Dockerfile exposes the following ports:

EXPOSE 5671 61613 61614 15671 15672 15674

And the docker-compose.yml to set up the RabbitMQ container:

  rabbitmq-server:
    build: rabbitmq-ssl
    image: gprevost/rabbitmq-ssl:latest
    ports:
      - "5671:5671"
      - "61613:61613"
      - "61614:61614"
      - "15671:15671"
      - "15672:15672"
      - "15674:15674"

When running the container, the Rabbit Management is available on port 15672, and the URLs http://192.168.99.100:15674/ws, http://192.168.99.100:15674/stomp and http://192.168.99.100:15674/stomp/info are accessible. The same URLs via HTTPS don't work.

I also manage to interact (i.e. connect and send/receive messages) with the RabbitMQ server using SSL directly in Java (via port 5671) from the web server back-end.

However, when I try to connect via Javascript using the HTTPS, the connection gets closed immediately (HTTPS is required not only for security purposes, but also because the page is loaded via HTTPS so all JavaScript requests need to be secured to not be blocked by the browser). Here is the code I use:

    <script src="//cdnjs.cloudflare.com/ajax/libs/sockjs-client/0.3.4/sockjs.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>

<script th:fragment="script" th:inline="javascript" type="text/javascript">

    $(document).ready(function(e) {
        var ws = new SockJS("https://192.168.99.100:15674/stomp");
        var client = Stomp.over(ws);
    });

</script>

The following error appears in the browser's console:

sockjs.min.js:27 GET https://192.168.99.100:15674/stomp/info net::ERR_CONNECTION_CLOSED

Any idea where I'm doing things wrong? I'm not an expert with Web Sockets or STOMP, and I'm not an Erlang developer, which makes it pretty hard to fully understand the error. Any help greatly appreciated !

On the RabbitMQ server side, here are the logs that appear for that request, causing the connection to be closed:

rabbitmq-server_1 | =ERROR REPORT==== 20-Jun-2016::05:59:03 ===

rabbitmq-server_1 | Ranch listener http had connection process started with cowboy_protocol:start_link/4 at <0.27746.0> exit with reason: {function_clause,[{cowboy_protocol,parse_method,[<<>>,{state,#Port<0.30021>,ranch_tcp,[cowboy_router,cowboy_handler],false,[{listener,http},{dispatch,[{'_',[],[{[<<"stomp">>,'...'],[],sockjs_cowboy_handler,{service,"/stomp",#Fun,{},"//cdn.jsdelivr.net/sockjs/1.0.3/sockjs.min.js",false,true,5000,25000,131072,#Fun,undefined}},{[<<"ws">>],[],rabbit_ws_handler,[{type,text}]}]}]}],undefined,undefined,5,1,100,4096,64,4096,100,5000,1466402348682},<<22,3,1,0,137,1,0,0,133,3,2,124,30,85,16,113,253,213,38,165,219,181,225,215,84,87,194,106,235,193,207,37,26,166,115,208,87,3,91,89,138,121,207,0,0,16,192,10,192,20,192,9,192,19,0,53,0,47,0,10,86,0,1,0,0,76,255,1,0,1,0,0,23,0,0,0,35,0,0,0,5,0,5,1,0,0,0,0,51,116,0,0,0,18,0,0,0,16,0,20,0,18,8,115,112,100,121,47,51,46,49,8,104,116,116,112,47,49,46,49,117,80,0,0,0,11,0,2,1,0,0,10,0,8,0,6,0,29,0,23,0,24>>],[{file,"src/cowboy_protocol.erl"},{line,168}]}]}

rabbitmq-server_1 |

rabbitmq-server_1 | =ERROR REPORT==== 20-Jun-2016::05:59:03 ===

rabbitmq-server_1 | Error in process <0.27747.0> on node rabbit@4391a6fed075 with exit value:

rabbitmq-server_1 | {function_clause,

rabbitmq-server_1 | [{cowboy_protocol,parse_method,

rabbitmq-server_1 | [<<>>,

rabbitmq-server_1 | {state,#Port<0.30022>,ranch_tcp,

rabbitmq-server_1 | [cowboy_router,cowboy_handler],

rabbitmq-server_1 | false,

rabbitmq-server_1 | [{listener,http},

rabbitmq-server_1 | {dispatch,

rabbitmq-server_1 | [{'_',[],

rabbitmq-server_1 | [{[<<"stomp">>,'...'],

rabbitmq-server_1 | [],sockjs_cowboy_handler,

rabbitmq-server_1 | {service,"/stomp",

rabbitmq-server_1 | Fun,{},

rabbitmq-server_1 | "//cdn.jsdelivr.net/sockjs/1.0.3/sockjs.min.js",

rabbitmq-server_1 | false,true,5000,25000,131072,

rabbitmq-server_1 | #Fun,undefined}},

rabbitmq-server_1 | {[<<"ws">>],[],rabbit_ws_handler,[{type,text}]}]}]}],

rabbitmq-server_1 | undefined,undefined,5,1,100,4096,64,4096,100,5000,1466402348684},

rabbitmq-server_1 | <<22,3,1,0,141,1,0,0,137,3,2,82,175,8,177,50,73,15,108,57,27,92,102,

rabbitmq-server_1 | 113,100,140,128,201,104,90,3,39,96,30,35,45,2,6,1,46,227,175,146,

rabbitmq-server_1 | 0,0,20,192,10,192,20,0,57,192,9,192,19,0,51,0,53,0,47,0,10,86,0,1,

rabbitmq-server_1 | 0,0,76,255,1,0,1,0,0,23,0,0,0,35,0,0,0,5,0,5,1,0,0,0,0,51,116,0,0,

rabbitmq-server_1 | 0,18,0,0,0,16,0,20,0,18,8,115,112,100,121,47,51,46,49,8,104,116,

rabbitmq-server_1 | 116,112,47,49,46,49,117,80,0,0,0,11,0,2,1,0,0,10,0,8,0,6,0,29,0,

rabbitmq-server_1 | 23,0,24>>],

rabbitmq-server_1 | [{file,"src/cowboy_protocol.erl"},{line,168}]}]}

rabbitmq-server_1 |

rabbitmq-server_1 | =ERROR REPORT==== 20-Jun-2016::05:59:03 ===

rabbitmq-server_1 | Ranch listener http had connection process started with cowboy_protocol:start_link/4 at <0.27747.0> exit with reason: {function_clause,[{cowboy_protocol,parse_method,[<<>>,{state,#Port<0.30022>,ranch_tcp,[cowboy_router,cowboy_handler],false,[{listener,http},{dispatch,[{'_',[],[{[<<"stomp">>,'...'],[],sockjs_cowboy_handler,{service,"/stomp",#Fun,{},"//cdn.jsdelivr.net/sockjs/1.0.3/sockjs.min.js",false,true,5000,25000,131072,#Fun,undefined}},{[<<"ws">>],[],rabbit_ws_handler,[{type,text}]}]}]}],undefined,undefined,5,1,100,4096,64,4096,100,5000,1466402348684},<<22,3,1,0,141,1,0,0,137,3,2,82,175,8,177,50,73,15,108,57,27,92,102,113,100,140,128,201,104,90,3,39,96,30,35,45,2,6,1,46,227,175,146,0,0,20,192,10,192,20,0,57,192,9,192,19,0,51,0,53,0,47,0,10,86,0,1,0,0,76,255,1,0,1,0,0,23,0,0,0,35,0,0,0,5,0,5,1,0,0,0,0,51,116,0,0,0,18,0,0,0,16,0,20,0,18,8,115,112,100,121,47,51,46,49,8,104,116,116,112,47,49,46,49,117,80,0,0,0,11,0,2,1,0,0,10,0,8,0,6,0,29,0,23,0,24>>],[{file,"src/cowboy_protocol.erl"},{line,168}]}]}

Thank you in advance if anyone can help me clear this up.

2

2 Answers

0
votes

When you use ssl the port is another is not the same for http.

you can configure using this steps it worked for me

http://www.gettingcirrius.com/2013/01/configuring-ssl-for-rabbitmq.html

0
votes

The port indeed needs to be changed to use SSL (15671 on my configuration), but kept getting "Connection Refused" error.

Only after a few more hours I noticed my own mistake: the wrong certificates were specified in my Web Stomp configuration.

WRONG:
certfile: cacert.pem
keyfile: rabbitmq.cert.key
cacertfile: rabbitmqkey.pem

CORRECT:
cacertfile: cacert.pem
certfile: rabbitmq.cert.key
keyfile: rabbitmqkey.pem

So, it worked after changing the RabbitMQ configuration from:

{rabbitmq_web_stomp, [ 
                         {ssl_config, [
                           {port,       15671},
                           {backlog,    1024},
                           {certfile,   "/etc/rabbitmq/ssl/ca/cacert.pem"},
                           {keyfile,    "/etc/rabbitmq/ssl/server/rabbitmq.cert.pem"},
                           {cacertfile, "/etc/rabbitmq/ssl/server/rabbitmq.key.pem"},
                           {password,   "changeme"}
                         ]}
                      ]}

to:

{rabbitmq_web_stomp, [ 
                         {ssl_config, [
                           {port,       15671},
                           {backlog,    1024},
                           {cacertfile, "/etc/rabbitmq/ssl/ca/cacert.pem"},
                           {certfile,   "/etc/rabbitmq/ssl/server/rabbitmq.cert.pem"},
                           {keyfile,    "/etc/rabbitmq/ssl/server/rabbitmq.key.pem"},
                           {password,   "changeme"}
                         ]}
                      ]}

and connecting using port 15671:

<script th:fragment="script" th:inline="javascript" type="text/javascript">

    $(document).ready(function(e) {
        var ws = new SockJS("https://192.168.99.100:15671/stomp");
        var client = Stomp.over(ws);
    });

</script>