You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by at...@apache.org on 2018/03/19 11:01:58 UTC

[ambari] branch trunk updated: AMBARI-23202 Websocket client should use secure endpoint when connected over https

This is an automated email from the ASF dual-hosted git repository.

atkach pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ambari.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 844b8ca  AMBARI-23202 Websocket client should use secure endpoint when connected over https
844b8ca is described below

commit 844b8ca4fa2e4cabf8d61c1aa9000fecebdfdef1
Author: Andrii Tkach <at...@apache.org>
AuthorDate: Mon Mar 12 16:30:50 2018 +0200

    AMBARI-23202 Websocket client should use secure endpoint when connected over https
---
 ambari-web/app/utils/stomp_client.js       | 32 ++++++++++++++++++++----------
 ambari-web/test/utils/stomp_client_test.js |  6 ++++++
 2 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/ambari-web/app/utils/stomp_client.js b/ambari-web/app/utils/stomp_client.js
index 5c4847f..2003559 100644
--- a/ambari-web/app/utils/stomp_client.js
+++ b/ambari-web/app/utils/stomp_client.js
@@ -27,12 +27,19 @@ module.exports = Em.Object.extend({
   /**
    * @type {string}
    */
-  webSocketUrl: 'ws://{hostname}:8080/api/stomp/v1/websocket',
+  webSocketUrl: '{protocol}://{hostname}:8080/api/stomp/v1/websocket',
 
   /**
    * @type {string}
    */
-  sockJsUrl: 'http://{hostname}:8080/api/stomp/v1',
+  sockJsUrl: '{protocol}://{hostname}:8080/api/stomp/v1',
+
+  /**
+   * sockJs should use only alternative options as transport in case when websocket supported but connection fails
+   * @const
+   * @type {Array}
+   */
+  sockJsTransports: ['eventsource', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling'],
 
   /**
    * @type {boolean}
@@ -76,7 +83,7 @@ module.exports = Em.Object.extend({
       this.onConnectionSuccess();
       dfd.resolve();
     }, () => {
-      this.onConnectionError();
+      this.onConnectionError(useSockJS);
       dfd.reject();
     });
     client.debug = Em.K;
@@ -91,11 +98,16 @@ module.exports = Em.Object.extend({
    */
   getSocket: function(useSockJS) {
     const hostname = window.location.hostname;
+    const isSecure = window.location.protocol === 'https:';
+
     if (!WebSocket || useSockJS) {
       this.set('isWebSocketSupported', false);
-      return new SockJS(this.get('sockJsUrl').replace('{hostname}', hostname));
+      const protocol = isSecure ? 'https' : 'http';
+      const sockJsUrl = this.get('sockJsUrl').replace('{hostname}', hostname).replace('{protocol}', protocol);
+      return new SockJS(sockJsUrl, null, {transports: this.get('sockJsTransports')});
     } else {
-      return new WebSocket(this.get('webSocketUrl').replace('{hostname}', hostname));
+      const protocol = isSecure ? 'wss' : 'ws';
+      return new WebSocket(this.get('webSocketUrl').replace('{hostname}', hostname).replace('{protocol}', protocol));
     }
   },
 
@@ -103,20 +115,20 @@ module.exports = Em.Object.extend({
     this.set('isConnected', true);
   },
 
-  onConnectionError: function() {
+  onConnectionError: function(useSockJS) {
     if (this.get('isConnected')) {
-      this.reconnect();
-    } else {
+      this.reconnect(useSockJS);
+    } else if (!useSockJS) {//if SockJs connection failed too the stop trying to connect
       //if webSocket failed on initial connect then switch to SockJS
       this.connect(true);
     }
   },
 
-  reconnect: function() {
+  reconnect: function(useSockJS) {
     const subscriptions = this.get('subscriptions');
     setTimeout(() => {
       console.debug('Reconnecting to WebSocket...');
-      this.connect().done(() => {
+      this.connect(useSockJS).done(() => {
         for (var i in subscriptions) {
           subscriptions[i].unsubscribe();
           this.subscribe(subscriptions[i].destination, subscriptions[i].handlers['default']);
diff --git a/ambari-web/test/utils/stomp_client_test.js b/ambari-web/test/utils/stomp_client_test.js
index 48b2e25..c2cb3dc 100644
--- a/ambari-web/test/utils/stomp_client_test.js
+++ b/ambari-web/test/utils/stomp_client_test.js
@@ -102,6 +102,12 @@ describe('App.StompClient', function () {
       stomp.onConnectionError();
       expect(stomp.connect.calledOnce).to.be.true;
     });
+
+    it('connect should not be called when isConnected false and useSockJS true', function() {
+      stomp.set('isConnected', false);
+      stomp.onConnectionError(true);
+      expect(stomp.connect.called).to.be.false;
+    });
   });
 
   describe('#reconnect', function() {

-- 
To stop receiving notification emails like this one, please contact
atkach@apache.org.