Example #1
0
    def initialize(self,
                   apiurl,
                   auth_backend,
                   deployer,
                   tokens,
                   ws_source_template,
                   ws_target_template,
                   io_loop=None):
        """Initialize the WebSocket server.

        Create a new WebSocket client and connect it to the Juju API.
        Set up the authentication system.
        Handle the queued messages.
        """
        if io_loop is None:
            io_loop = IOLoop.current()
        self._io_loop = io_loop
        self._summary = request_summary(self.request) + ' '
        logging.info(self._summary + 'client connected')
        self.connected = True
        self.juju_connected = False
        self._juju_message_queue = queue = deque()
        # Set up the authentication infrastructure.
        self.tokens = tokens
        write_message = wrap_write_message(self)
        self.user = User()
        self.auth = AuthMiddleware(self.user, auth_backend, tokens,
                                   write_message)
        # Set up the bundle deployment and change set infrastructure.
        self.deployment = DeployMiddleware(self.user, deployer, write_message)
        self.changeset = ChangeSetMiddleware(self.user, write_message)
        apiurl = get_juju_api_url(self.request.path, ws_source_template,
                                  ws_target_template, apiurl)
        # Juju requires the Origin header to be included in the WebSocket
        # client handshake request. Propagate the client origin if present;
        # use the Juju API server as origin otherwise.
        headers = get_headers(self.request, apiurl)
        # Connect the WebSocket client to the Juju API server.
        self._juju_connected_future = websocket_connect(io_loop,
                                                        apiurl,
                                                        self.on_juju_message,
                                                        headers=headers)
        try:
            self.juju_connection = yield self._juju_connected_future
        except Exception as err:
            logging.error(self._summary + 'unable to connect to the Juju API')
            logging.exception(err)
            self.connected = False
            return
        # At this point the Juju API is successfully connected.
        self.juju_connected = True
        logging.info(self._summary + 'Juju API connected: {}'.format(apiurl))
        # Send all the messages that have been enqueued before the connection
        # to the Juju API server was established.
        while self.connected and self.juju_connected and len(queue):
            message = queue.popleft()
            encoded = message.encode('utf-8')
            logging.debug(self._summary + 'queue -> juju: {}'.format(encoded))
            self.juju_connection.write_message(message)
Example #2
0
    def initialize(self, apiurl, auth_backend, deployer, tokens, io_loop=None):
        """Initialize the WebSocket server.

        Create a new WebSocket client and connect it to the Juju API.
        Set up the authentication system.
        Handle the queued messages.
        """
        if io_loop is None:
            io_loop = IOLoop.current()
        self._io_loop = io_loop
        self._summary = request_summary(self.request) + ' '
        logging.info(self._summary + 'client connected')
        self.connected = True
        self.juju_connected = False
        self._juju_message_queue = queue = deque()
        # Set up the authentication infrastructure.
        self.tokens = tokens
        write_message = wrap_write_message(self)
        self.user = User()
        self.auth = AuthMiddleware(
            self.user, auth_backend, tokens, write_message)
        # Set up the bundle deployment and change set infrastructure.
        self.deployment = DeployMiddleware(self.user, deployer, write_message)
        self.changeset = ChangeSetMiddleware(self.user, write_message)
        # XXX The handler is no longer path agnostic, and this can be fixed by
        # capturing the relevant path fragment on the regexp, and then
        # overriding the handler's open method to store the path in the
        # instance.
        path = self.request.path[len('/ws'):]
        if path:
            # If they provided a path in their request then we need to use
            # that api endpoint.
            apiurl = '{}{}'.format(apiurl, path)
        # Juju requires the Origin header to be included in the WebSocket
        # client handshake request. Propagate the client origin if present;
        # use the Juju API server as origin otherwise.
        headers = get_headers(self.request, apiurl)
        # Connect the WebSocket client to the Juju API server.
        self._juju_connected_future = websocket_connect(
            io_loop, apiurl, self.on_juju_message, headers=headers)
        try:
            self.juju_connection = yield self._juju_connected_future
        except Exception as err:
            logging.error(self._summary + 'unable to connect to the Juju API')
            logging.exception(err)
            self.connected = False
            return
        # At this point the Juju API is successfully connected.
        self.juju_connected = True
        logging.info(self._summary + 'Juju API connected')
        # Send all the messages that have been enqueued before the connection
        # to the Juju API server was established.
        while self.connected and self.juju_connected and len(queue):
            message = queue.popleft()
            encoded = message.encode('utf-8')
            logging.debug(self._summary + 'queue -> juju: {}'.format(encoded))
            self.juju_connection.write_message(message)
Example #3
0
    def initialize(
            self, apiurl, auth_backend, deployer, tokens, ws_source_template,
            ws_target_template, io_loop=None):
        """Initialize the WebSocket server.

        Create a new WebSocket client and connect it to the Juju API.
        Set up the authentication system.
        Handle the queued messages.
        """
        if io_loop is None:
            io_loop = IOLoop.current()
        self._io_loop = io_loop
        self._summary = request_summary(self.request) + ' '
        logging.info(self._summary + 'client connected')
        self.connected = True
        self.juju_connected = False
        self._juju_message_queue = queue = deque()
        # Set up the authentication infrastructure.
        self.tokens = tokens
        write_message = wrap_write_message(self)
        self.user = User()
        self.auth = AuthMiddleware(
            self.user, auth_backend, tokens, write_message)
        # Set up the bundle deployment and change set infrastructure.
        self.deployment = DeployMiddleware(self.user, deployer, write_message)
        self.changeset = ChangeSetMiddleware(self.user, write_message)
        apiurl = get_juju_api_url(
            self.request.path, ws_source_template, ws_target_template, apiurl)
        # Juju requires the Origin header to be included in the WebSocket
        # client handshake request. Propagate the client origin if present;
        # use the Juju API server as origin otherwise.
        headers = get_headers(self.request, apiurl)
        # Connect the WebSocket client to the Juju API server.
        self._juju_connected_future = websocket_connect(
            io_loop, apiurl, self.on_juju_message, headers=headers)
        try:
            self.juju_connection = yield self._juju_connected_future
        except Exception as err:
            logging.error(self._summary + 'unable to connect to the Juju API')
            logging.exception(err)
            self.connected = False
            return
        # At this point the Juju API is successfully connected.
        self.juju_connected = True
        logging.info(self._summary + 'Juju API connected: {}'.format(apiurl))
        # Send all the messages that have been enqueued before the connection
        # to the Juju API server was established.
        while self.connected and self.juju_connected and len(queue):
            message = queue.popleft()
            encoded = message.encode('utf-8')
            logging.debug(self._summary + 'queue -> juju: {}'.format(encoded))
            self.juju_connection.write_message(message)
Example #4
0
 def test_default(self):
     # If the Origin header is not found, the default is used.
     request = mock.Mock(headers={})
     headers = utils.get_headers(request, 'wss://server.example.com')
     self.assertEqual({'Origin': 'https://server.example.com'}, headers)
Example #5
0
 def test_propagation(self):
     # The Origin header is propagated if found in the provided request.
     expected = {'Origin': 'https://browser.example.com'}
     request = mock.Mock(headers=expected)
     headers = utils.get_headers(request, 'wss://server.example.com')
     self.assertEqual(expected, headers)
Example #6
0
 def test_default(self):
     # If the Origin header is not found, the default is used.
     request = mock.Mock(headers={})
     headers = utils.get_headers(request, 'wss://server.example.com')
     self.assertEqual({'Origin': 'https://server.example.com'}, headers)
Example #7
0
 def test_propagation(self):
     # The Origin header is propagated if found in the provided request.
     expected = {'Origin': 'https://browser.example.com'}
     request = mock.Mock(headers=expected)
     headers = utils.get_headers(request, 'wss://server.example.com')
     self.assertEqual(expected, headers)