def _StartNewWebSocket(self):
        """Start a new WebSocket and thread to listen for incoming data."""
        headers = ['User-Agent: ' + http.MakeUserAgentString()]
        if self._get_access_token_callback:
            headers += [
                'Authorization: Bearer ' + self._get_access_token_callback()
            ]

        use_mtls = False
        if self._caa_config is not None:
            use_mtls = self._caa_config.use_client_certificate
        if self._connection_sid:
            url = utils.CreateWebSocketReconnectUrl(self._tunnel_target,
                                                    self._connection_sid,
                                                    self._total_bytes_received,
                                                    use_mtls)
            log.info('Reconnecting with URL [%r]', url)
        else:
            url = utils.CreateWebSocketConnectUrl(self._tunnel_target,
                                                  use_mtls)
            log.info('Connecting with URL [%r]', url)

        self._connect_msg_received = False
        self._websocket_helper = helper.IapTunnelWebSocketHelper(
            url, headers, self._ignore_certs, self._tunnel_target.proxy_info,
            self._OnData, self._OnClose, self._caa_config)
        self._websocket_helper.StartReceivingThread()
  def testStartReceivingThread(self, websocket_app_cls_mock, thread_cls_mock,
                               check_ca_certs_mock):
    websocket_app_mock = mock.MagicMock()
    websocket_app_cls_mock.return_value = websocket_app_mock
    thread_mock = mock.MagicMock()
    thread_cls_mock.return_value = thread_mock
    check_ca_certs_mock.return_value = 'ca certs'
    new_helper = iap_tunnel_websocket_helper.IapTunnelWebSocketHelper(
        TEST_URL, TEST_HEADERS, True, None, self.DataCallback,
        self.CloseCallback)

    check_ca_certs_mock.assert_called_once()
    check_ca_certs_mock.assert_called_with(True)

    websocket_app_cls_mock.assert_called_once()
    websocket_app_cls_mock.assert_called_with(
        TEST_URL, header=TEST_HEADERS, on_error=new_helper._OnError,
        on_close=new_helper._OnClose, on_data=new_helper._OnData,
        subprotocols=[utils.SUBPROTOCOL_NAME])
    self.assertIs(new_helper._websocket, websocket_app_mock)

    new_helper.StartReceivingThread()
    thread_cls_mock.assert_called_once()
    thread_cls_mock.assert_called_with(target=new_helper._ReceiveFromWebSocket)
    self.assertIs(new_helper._receiving_thread, thread_mock)
    self.assertTrue(thread_mock.daemon)
    thread_mock.start.assert_called_once()
Esempio n. 3
0
    def _StartNewWebSocket(self):
        """Start a new WebSocket and thread to listen for incoming data."""
        headers = [
            'User-Agent: ' + http.MakeUserAgentString(),
            'Sec-WebSocket-Protocol: ' + utils.SUBPROTOCOL_NAME
        ]
        if self._get_access_token_callback:
            headers += [
                'Authorization: Bearer ' + self._get_access_token_callback()
            ]

        if self._connection_sid:
            url = utils.CreateWebSocketReconnectUrl(self._tunnel_target,
                                                    self._connection_sid,
                                                    self._total_bytes_received)
            log.info('Reconnecting with URL [%r]', url)
        else:
            url = utils.CreateWebSocketConnectUrl(self._tunnel_target)
            log.info('Connecting with URL [%r]', url)

        self._connect_msg_received = False
        self._websocket_helper = helper.IapTunnelWebSocketHelper(
            url, headers, self._ignore_certs, self._tunnel_target.proxy_info,
            self._OnData, self._OnClose)
        self._websocket_helper.StartReceivingThread()
    def _StartNewWebSocket(self):
        """Start a new WebSocket and thread to listen for incoming data."""
        headers = ['User-Agent: ' + transport.MakeUserAgentString()]
        request_reason = properties.VALUES.core.request_reason.Get()
        if request_reason:
            headers += ['X-Goog-Request-Reason: ' + request_reason]

        if self._get_access_token_callback:
            headers += [
                'Authorization: Bearer ' + self._get_access_token_callback()
            ]

        if self._connection_sid:
            url = utils.CreateWebSocketReconnectUrl(self._tunnel_target,
                                                    self._connection_sid,
                                                    self._total_bytes_received)
            log.info('Reconnecting with URL [%r]', url)
        else:
            url = utils.CreateWebSocketConnectUrl(self._tunnel_target)
            log.info('Connecting with URL [%r]', url)

        self._connect_msg_received = False
        self._websocket_helper = helper.IapTunnelWebSocketHelper(
            url, headers, self._ignore_certs, self._tunnel_target.proxy_info,
            self._OnData, self._OnClose)
        self._websocket_helper.StartReceivingThread()
 def SetUp(self):
   self._received_data = []
   self._close_received = False
   self.helper = iap_tunnel_websocket_helper.IapTunnelWebSocketHelper(
       TEST_URL, TEST_HEADERS, True, None, self.DataCallback,
       self.CloseCallback)
   self.helper._websocket = mock.MagicMock()
   self.helper._websocket.sock.connected = True
  def testInit(self):
    self.assertEqual(self.helper._on_data, self.DataCallback,
                     self.CloseCallback)
    self.assertListEqual(sorted(self.helper._sslopt.keys()),
                         ['ca_certs', 'cert_reqs', 'check_hostname'])
    self.AssertFileExists(self.helper._sslopt['ca_certs'])
    self.assertEqual(self.helper._sslopt['cert_reqs'], ssl.CERT_NONE)
    self.assertFalse(self.helper._sslopt['check_hostname'])
    self.assertFalse(self.helper._is_closed)

    second_helper = iap_tunnel_websocket_helper.IapTunnelWebSocketHelper(
        TEST_URL, TEST_HEADERS, False, None, self.DataCallback,
        self.CloseCallback)
    self.assertListEqual(sorted(second_helper._sslopt.keys()),
                         ['ca_certs', 'cert_reqs'])
    self.AssertFileExists(second_helper._sslopt['ca_certs'])
    self.assertEqual(second_helper._sslopt['cert_reqs'], ssl.CERT_REQUIRED)