def test_auth_backend(self): # The authentication backend instance is correctly passed to the # WebSocket handler. app = self.get_app() spec = self.get_url_spec(app, r'^/ws(?:/.*)?$') auth_backend = self.assert_in_spec(spec, 'auth_backend') expected = auth.get_backend(manage.DEFAULT_API_VERSION) self.assertIsInstance(auth_backend, type(expected))
def server(): """Return the main server application. The server app is responsible for serving the WebSocket connection, the Juju GUI static files and the main index file for dynamic URLs. """ # Set up static paths. guiroot = options.guiroot static_path = os.path.join(guiroot, 'juju-ui') # Set up the bundle deployer. deployer = Deployer(options.apiurl, options.apiversion, options.charmworldurl) # Set up handlers. server_handlers = [] if not options.sandbox: tokens = auth.AuthenticationTokenHandler() websocket_handler_options = { # The Juju API backend url. 'apiurl': options.apiurl, # The backend to use for user authentication. 'auth_backend': auth.get_backend(options.apiversion), # The Juju deployer to use for importing bundles. 'deployer': deployer, # The tokens collection for authentication token requests. 'tokens': tokens, } server_handlers.append( # Handle WebSocket connections. (r'^/ws$', handlers.WebSocketHandler, websocket_handler_options), ) if options.testsroot: params = {'path': options.testsroot, 'default_filename': 'index.html'} server_handlers.append( # Serve the Juju GUI tests. (r'^/test/(.*)', web.StaticFileHandler, params), ) info_handler_options = { 'apiurl': options.apiurl, 'apiversion': options.apiversion, 'deployer': deployer, 'sandbox': options.sandbox, 'start_time': int(time.time()), } server_handlers.extend([ # Handle static files. (r'^/juju-ui/(.*)', web.StaticFileHandler, {'path': static_path}), (r'^/(favicon\.ico)$', web.StaticFileHandler, {'path': guiroot}), # Handle GUI server info. (r'^/gui-server-info', handlers.InfoHandler, info_handler_options), # Any other path is served by index.html. (r'^/(.*)', handlers.IndexHandler, {'path': guiroot}), ]) return web.Application(server_handlers, debug=options.debug)
def test_auth_backend(self): # The authentication backend instance is correctly passed to the # WebSocket handlers. app = self.get_app() expected = auth.get_backend(manage.DEFAULT_API_VERSION) # The auth backend is registered for the controller connection. spec = self.get_url_spec(app, r'^/ws/model-api(?:/.*)?$') auth_backend = self.assert_in_spec(spec, 'auth_backend') self.assertIsInstance(auth_backend, type(expected)) # The auth backend is registered for the model connection. spec = self.get_url_spec(app, r'^/ws/controller-api(?:/.*)?$') auth_backend = self.assert_in_spec(spec, 'auth_backend') self.assertIsInstance(auth_backend, type(expected))
def server(): """Return the main server application. The server app is responsible for serving the WebSocket connection, the Juju GUI static files and the main index file for dynamic URLs. """ # Set up static paths. guiroot = options.guiroot static_path = os.path.join(guiroot, 'juju-ui') # Set up the bundle deployer. deployer = Deployer(options.apiurl, options.apiversion, options.charmworldurl) # Set up handlers. server_handlers = [] if options.sandbox: # Sandbox mode. server_handlers.append((r'^/ws(?:/.*)?$', handlers.SandboxHandler, {})) else: # Real environment. tokens = auth.AuthenticationTokenHandler() websocket_handler_options = { # The Juju API backend url. 'apiurl': options.apiurl, # The backend to use for user authentication. 'auth_backend': auth.get_backend(options.apiversion), # The Juju deployer to use for importing bundles. 'deployer': deployer, # The tokens collection for authentication token requests. 'tokens': tokens, } juju_proxy_handler_options = { 'target_url': utils.ws_to_http(options.apiurl), 'charmworld_url': options.charmworldurl, } server_handlers.extend([ # Handle WebSocket connections. (r'^/ws(?:/.*)?$', handlers.WebSocketHandler, websocket_handler_options), # Handle connections to the juju-core HTTPS server. # The juju-core HTTPS and WebSocket servers share the same URL. (r'^/juju-core/(.*)', handlers.JujuProxyHandler, juju_proxy_handler_options), ]) if options.testsroot: params = {'path': options.testsroot, 'default_filename': 'index.html'} server_handlers.append( # Serve the Juju GUI tests. (r'^/test/(.*)', web.StaticFileHandler, params), ) info_handler_options = { 'apiurl': options.apiurl, 'apiversion': options.apiversion, 'deployer': deployer, 'sandbox': options.sandbox, 'start_time': int(time.time()), } server_handlers.extend([ # Handle static files. (r'^/juju-ui/(.*)', web.StaticFileHandler, { 'path': static_path }), (r'^/(favicon\.ico)$', web.StaticFileHandler, { 'path': guiroot }), # Handle GUI server info. (r'^/gui-server-info', handlers.InfoHandler, info_handler_options), # Any other path is served by index.html. (r'^/(.*)', handlers.IndexHandler, { 'path': guiroot }), ]) return web.Application(server_handlers, debug=options.debug)
class WebSocketHandlerTestMixin(object): """Base set up for all the WebSocketHandler test cases.""" auth_backend = auth.get_backend(manage.DEFAULT_API_VERSION) hello_message = json.dumps({'hello': 'world'}) def get_app(self): # In test cases including this mixin a WebSocket server is created. # The server creates a new client on each request. This client should # forward messages to a WebSocket echo server. In order to test the # communication, some of the tests create another client that connects # to the server, e.g.: # ws-client -> ws-server -> ws-forwarding-client -> ws-echo-server # Messages arriving to the echo server are returned back to the client: # ws-echo-server -> ws-forwarding-client -> ws-server -> ws-client self.apiurl = self.get_wss_url('/echo') self.api_close_future = concurrent.Future() self.deployer = base.Deployer( self.apiurl, manage.DEFAULT_API_VERSION, io_loop=self.io_loop) self.tokens = auth.AuthenticationTokenHandler(io_loop=self.io_loop) echo_options = { 'close_future': self.api_close_future, 'io_loop': self.io_loop, } ws_options = { 'apiurl': self.apiurl, 'auth_backend': self.auth_backend, 'deployer': self.deployer, 'io_loop': self.io_loop, 'tokens': self.tokens, } return web.Application([ (r'/echo', helpers.EchoWebSocketHandler, echo_options), (r'/ws', handlers.WebSocketHandler, ws_options), ]) def make_client(self): """Return a WebSocket client ready to be connected to the server.""" url = self.get_wss_url('/ws') # The client callback is tested elsewhere. callback = lambda message: None return clients.websocket_connect(self.io_loop, url, callback) def make_handler(self, headers=None, mock_protocol=False, path=None): """Create and return a WebSocketHandler instance.""" if headers is None: headers = {} if path is None: path = '' request = mock.Mock(headers=headers, path=path) handler = handlers.WebSocketHandler(self.get_app(), request) if mock_protocol: # Mock the underlying connection protocol. handler.ws_connection = mock.Mock() return handler @gen.coroutine def make_initialized_handler( self, apiurl=None, headers=None, mock_protocol=False, path=None): """Create and return an initialized WebSocketHandler instance.""" if apiurl is None: apiurl = self.apiurl handler = self.make_handler( headers=headers, mock_protocol=mock_protocol, path=path) yield handler.initialize( apiurl, self.auth_backend, self.deployer, self.tokens, self.io_loop) raise gen.Return(handler)
def server(): """Return the main server application. The server app is responsible for serving the WebSocket connection, the Juju GUI static files and the main index file for dynamic URLs. """ # Set up the bundle deployer. deployer = Deployer(options.apiurl, options.apiversion, options.charmworldurl) # Set up handlers. server_handlers = [] if options.sandbox: # Sandbox mode. server_handlers.append((r"^/ws(?:/.*)?$", handlers.SandboxHandler, {})) else: # Real environment. is_legacy_juju = LooseVersion(options.jujuversion) < LooseVersion("2") tokens = auth.AuthenticationTokenHandler() auth_backend = auth.get_backend(options.apiversion) ws_model_target_template = WEBSOCKET_MODEL_TARGET_TEMPLATE if is_legacy_juju: ws_model_target_template = WEBSOCKET_TARGET_TEMPLATE_PRE2 else: # Register the WebSocket handler for the controller connection. websocket_controller_handler_options = { # The Juju API backend url. "apiurl": options.apiurl, # The backend to use for user authentication. "auth_backend": auth_backend, # The Juju deployer to use for importing bundles. "deployer": deployer, # The tokens collection for authentication token requests. "tokens": tokens, # The WebSocket URL template the browser uses for connecting. "ws_source_template": WEBSOCKET_CONTROLLER_SOURCE_TEMPLATE, # The WebSocket URL template used for connecting to Juju. "ws_target_template": WEBSOCKET_CONTROLLER_TARGET_TEMPLATE, } server_handlers.append( (r"^/ws/controller-api(?:/.*)?$", handlers.WebSocketHandler, websocket_controller_handler_options) ) websocket_model_handler_options = { # The Juju API backend url. "apiurl": options.apiurl, # The backend to use for user authentication. "auth_backend": auth_backend, # The Juju deployer to use for importing bundles. "deployer": deployer, # The tokens collection for authentication token requests. "tokens": tokens, # The WebSocket URL template the browser uses for the connection. "ws_source_template": WEBSOCKET_MODEL_SOURCE_TEMPLATE, # The WebSocket URL template used for connecting to Juju. "ws_target_template": ws_model_target_template, } juju_proxy_handler_options = { "target_url": utils.ws_to_http(options.apiurl), "charmworld_url": options.charmworldurl, } server_handlers.extend( [ # Handle WebSocket connections to the Juju model. (r"^/ws/model-api(?:/.*)?$", handlers.WebSocketHandler, websocket_model_handler_options), # Handle connections to the juju-core HTTPS server. # The juju-core HTTPS and WebSocket servers share the same URL. (r"^/juju-core/(.*)", handlers.JujuProxyHandler, juju_proxy_handler_options), ] ) if options.testsroot: params = {"path": options.testsroot, "default_filename": "index.html"} server_handlers.append( # Serve the Juju GUI tests. (r"^/test/(.*)", web.StaticFileHandler, params) ) info_handler_options = { "apiurl": options.apiurl, "apiversion": options.apiversion, "deployer": deployer, "sandbox": options.sandbox, "start_time": int(time.time()), } wsgi_settings = { "jujugui.apiAddress": options.apiurl, "jujugui.combine": not options.jujuguidebug, "jujugui.gisf": options.gisf, "jujugui.GTM_enabled": options.gtm, "jujugui.gzip": options.gzip, "jujugui.insecure": options.insecure, "jujugui.interactive_login": options.interactivelogin, "jujugui.bundleservice_url": options.bundleservice_url, "jujugui.charmstore_url": options.charmstoreurl, "jujugui.jujuCoreVersion": options.jujuversion, "jujugui.raw": options.jujuguidebug, "jujugui.sandbox": options.sandbox, "jujugui.controllerSocketTemplate": (WEBSOCKET_CONTROLLER_SOURCE_TEMPLATE), "jujugui.socketTemplate": WEBSOCKET_MODEL_SOURCE_TEMPLATE, "jujugui.uuid": options.uuid, } if options.password: wsgi_settings["jujugui.password"] = options.password config = Configurator(settings=wsgi_settings) wsgi_app = WSGIContainer(make_application(config)) server_handlers.extend( [ # Handle GUI server info. (r"^/gui-server-info", handlers.InfoHandler, info_handler_options), (r".*", web.FallbackHandler, dict(fallback=wsgi_app)), ] ) return web.Application(server_handlers, debug=options.debug)
def get_auth_backend(self): """Return an authentication backend suitable for the Go API.""" return auth.get_backend('go')
def server(): """Return the main server application. The server app is responsible for serving the WebSocket connection, the Juju GUI static files and the main index file for dynamic URLs. """ # Set up the bundle deployer. deployer = Deployer(options.apiurl, options.apiversion, options.charmworldurl) # Set up handlers. server_handlers = [] if options.sandbox: # Sandbox mode. server_handlers.append((r'^/ws(?:/.*)?$', handlers.SandboxHandler, {})) else: # Real environment. is_legacy_juju = LooseVersion(options.jujuversion) < LooseVersion('2') tokens = auth.AuthenticationTokenHandler() auth_backend = auth.get_backend(options.apiversion) ws_model_target_template = WEBSOCKET_MODEL_TARGET_TEMPLATE if is_legacy_juju: ws_model_target_template = WEBSOCKET_TARGET_TEMPLATE_PRE2 else: # Register the WebSocket handler for the controller connection. websocket_controller_handler_options = { # The Juju API backend url. 'apiurl': options.apiurl, # The backend to use for user authentication. 'auth_backend': auth_backend, # The Juju deployer to use for importing bundles. 'deployer': deployer, # The tokens collection for authentication token requests. 'tokens': tokens, # The WebSocket URL template the browser uses for connecting. 'ws_source_template': WEBSOCKET_CONTROLLER_SOURCE_TEMPLATE, # The WebSocket URL template used for connecting to Juju. 'ws_target_template': WEBSOCKET_CONTROLLER_TARGET_TEMPLATE, } server_handlers.append( (r'^/ws/controller-api(?:/.*)?$', handlers.WebSocketHandler, websocket_controller_handler_options)) websocket_model_handler_options = { # The Juju API backend url. 'apiurl': options.apiurl, # The backend to use for user authentication. 'auth_backend': auth_backend, # The Juju deployer to use for importing bundles. 'deployer': deployer, # The tokens collection for authentication token requests. 'tokens': tokens, # The WebSocket URL template the browser uses for the connection. 'ws_source_template': WEBSOCKET_MODEL_SOURCE_TEMPLATE, # The WebSocket URL template used for connecting to Juju. 'ws_target_template': ws_model_target_template, } juju_proxy_handler_options = { 'target_url': utils.ws_to_http(options.apiurl), 'charmworld_url': options.charmworldurl, } server_handlers.extend([ # Handle WebSocket connections to the Juju model. (r'^/ws/model-api(?:/.*)?$', handlers.WebSocketHandler, websocket_model_handler_options), # Handle connections to the juju-core HTTPS server. # The juju-core HTTPS and WebSocket servers share the same URL. (r'^/juju-core/(.*)', handlers.JujuProxyHandler, juju_proxy_handler_options), ]) if options.testsroot: params = {'path': options.testsroot, 'default_filename': 'index.html'} server_handlers.append( # Serve the Juju GUI tests. (r'^/test/(.*)', web.StaticFileHandler, params), ) info_handler_options = { 'apiurl': options.apiurl, 'apiversion': options.apiversion, 'deployer': deployer, 'sandbox': options.sandbox, 'start_time': int(time.time()), } wsgi_settings = { 'jujugui.apiAddress': options.apiurl, 'jujugui.combine': not options.jujuguidebug, 'jujugui.gisf': options.gisf, 'jujugui.GTM_enabled': options.gtm, 'jujugui.gzip': options.gzip, 'jujugui.insecure': options.insecure, 'jujugui.interactive_login': options.interactivelogin, 'jujugui.bundleservice_url': options.bundleservice_url, 'jujugui.charmstore_url': options.charmstoreurl, 'jujugui.jujuCoreVersion': options.jujuversion, 'jujugui.raw': options.jujuguidebug, 'jujugui.sandbox': options.sandbox, 'jujugui.controllerSocketTemplate': (WEBSOCKET_CONTROLLER_SOURCE_TEMPLATE), 'jujugui.socketTemplate': WEBSOCKET_MODEL_SOURCE_TEMPLATE, 'jujugui.uuid': options.uuid, } if options.password: wsgi_settings['jujugui.password'] = options.password config = Configurator(settings=wsgi_settings) wsgi_app = WSGIContainer(make_application(config)) server_handlers.extend([ # Handle GUI server info. (r'^/gui-server-info', handlers.InfoHandler, info_handler_options), (r".*", web.FallbackHandler, dict(fallback=wsgi_app)) ]) return web.Application(server_handlers, debug=options.debug)
def get_auth_backend(self): """Return an authentication backend suitable for the Python API.""" return auth.get_backend('python')
def server(): """Return the main server application. The server app is responsible for serving the WebSocket connection, the Juju GUI static files and the main index file for dynamic URLs. """ # Set up the bundle deployer. deployer = Deployer(options.apiurl, options.apiversion, options.charmworldurl) # Set up handlers. server_handlers = [] if options.sandbox: # Sandbox mode. server_handlers.append( (r'^/ws(?:/.*)?$', handlers.SandboxHandler, {})) else: # Real environment. ws_target_template = WEBSOCKET_TARGET_TEMPLATE if LooseVersion(options.jujuversion) < LooseVersion('2'): ws_target_template = WEBSOCKET_TARGET_TEMPLATE_PRE2 tokens = auth.AuthenticationTokenHandler() websocket_handler_options = { # The Juju API backend url. 'apiurl': options.apiurl, # The backend to use for user authentication. 'auth_backend': auth.get_backend(options.apiversion), # The Juju deployer to use for importing bundles. 'deployer': deployer, # The tokens collection for authentication token requests. 'tokens': tokens, # The WebSocket URL template the browser uses for the connection. 'ws_source_template': WEBSOCKET_SOURCE_TEMPLATE, # The WebSocket URL template used for connecting to Juju. 'ws_target_template': ws_target_template, } juju_proxy_handler_options = { 'target_url': utils.ws_to_http(options.apiurl), 'charmworld_url': options.charmworldurl, } server_handlers.extend([ # Handle WebSocket connections. (r'^/ws(?:/.*)?$', handlers.WebSocketHandler, websocket_handler_options), # Handle connections to the juju-core HTTPS server. # The juju-core HTTPS and WebSocket servers share the same URL. (r'^/juju-core/(.*)', handlers.JujuProxyHandler, juju_proxy_handler_options), ]) if options.testsroot: params = {'path': options.testsroot, 'default_filename': 'index.html'} server_handlers.append( # Serve the Juju GUI tests. (r'^/test/(.*)', web.StaticFileHandler, params), ) info_handler_options = { 'apiurl': options.apiurl, 'apiversion': options.apiversion, 'deployer': deployer, 'sandbox': options.sandbox, 'start_time': int(time.time()), } wsgi_settings = { 'jujugui.apiAddress': options.apiurl, 'jujugui.combine': not options.jujuguidebug, 'jujugui.gisf': options.gisf, 'jujugui.GTM_enabled': options.gtm, 'jujugui.gzip': options.gzip, 'jujugui.insecure': options.insecure, 'jujugui.interactive_login': options.interactivelogin, 'jujugui.jem_url': options.jemurl, 'jujugui.charmstore_url': options.charmstoreurl, 'jujugui.jujuCoreVersion': options.jujuversion, 'jujugui.raw': options.jujuguidebug, 'jujugui.sandbox': options.sandbox, 'jujugui.socketTemplate': WEBSOCKET_SOURCE_TEMPLATE, 'jujugui.uuid': options.uuid, } if options.password: wsgi_settings['jujugui.password'] = options.password config = Configurator(settings=wsgi_settings) wsgi_app = WSGIContainer(make_application(config)) server_handlers.extend([ # Handle GUI server info. (r'^/gui-server-info', handlers.InfoHandler, info_handler_options), (r".*", web.FallbackHandler, dict(fallback=wsgi_app)) ]) return web.Application(server_handlers, debug=options.debug)