Example #1
0
 def testCreateConnectUrl(self, target, expected_base, expected_query):
     tunnel_target = utils.IapTunnelTargetInfo(*target)
     created_url = utils.CreateWebSocketConnectUrl(tunnel_target)
     created_url_parts = created_url.split('?', 1)
     self.assertEqual(created_url_parts[0], expected_base)
     self.assertEqual(sorted(created_url_parts[1].split('&')),
                      expected_query)
Example #2
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: ' + 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 _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 testStartNewWebSocket(self, websocket_helper_cls_mock,
                            make_user_agent_mock):
    new_socket = iap_tunnel_websocket.IapTunnelWebSocket(
        self.tunnel_target, GetAccessToken, self.DataCallback,
        self.CloseCallback, ignore_certs=True)
    websocket_helper_mock = mock.MagicMock()
    websocket_helper_cls_mock.return_value = websocket_helper_mock
    make_user_agent_mock.return_value = 'my-user-agent/1234'

    new_socket._StartNewWebSocket()

    url = utils.CreateWebSocketConnectUrl(self.tunnel_target)
    headers = ['User-Agent: my-user-agent/1234',
               'Authorization: Bearer one-access-token']
    websocket_helper_cls_mock.assert_called_once()
    websocket_helper_cls_mock.assert_called_with(
        url, headers, True, None, new_socket._OnData, new_socket._OnClose)
    self.assertIs(new_socket._websocket_helper, websocket_helper_mock)
    websocket_helper_mock.StartReceivingThread.assert_called_once()