async def make_application(self) -> web.Application: """Get the aiohttp application instance.""" middlewares = [] admin_api_key = self.context.settings.get("admin.admin_api_key") admin_insecure_mode = self.context.settings.get( "admin.admin_insecure_mode") # admin-token and admin-token are mutually exclusive and required. # This should be enforced during parameter parsing but to be sure, # we check here. assert admin_insecure_mode or admin_api_key assert not (admin_insecure_mode and admin_api_key) # If admin_api_key is None, then admin_insecure_mode must be set so # we can safely enable the admin server with no security if admin_api_key: @web.middleware async def check_token(request, handler): header_admin_api_key = request.headers.get("x-api-key") if not header_admin_api_key: raise web.HTTPUnauthorized() if admin_api_key == header_admin_api_key: return await handler(request) else: raise web.HTTPUnauthorized() middlewares.append(check_token) stats: Collector = await self.context.inject(Collector, required=False) if stats: @web.middleware async def collect_stats(request, handler): handler = stats.wrap_coro( handler, [handler.__qualname__, "any-admin-request"]) return await handler(request) middlewares.append(collect_stats) app = web.Application(middlewares=middlewares) app["request_context"] = self.context app["outbound_message_router"] = self.responder.send # outbound router that returns the encoded message without sending it app["outbound_message_router_get_encoded_messages"] = self.responder.create_outbound app.add_routes([ web.get("/", self.redirect_handler), web.get("/modules", self.modules_handler), web.get("/status", self.status_handler), web.post("/status/reset", self.status_reset_handler), web.get("/ws", self.websocket_handler), ]) await register_module_routes(app) for protocol_module_path in self.context.settings.get( "external_protocols", []): try: routes_module = ClassLoader.load_module( f"{protocol_module_path}.routes") await routes_module.register(app) except Exception as e: raise ConfigError( f"Failed to load external protocol module '{protocol_module_path}'." ) from e cors = aiohttp_cors.setup( app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", allow_methods="*", ) }, ) for route in app.router.routes(): cors.add(route) setup_aiohttp_apispec(app=app, title="Aries Cloud Agent", version="v1", swagger_path="/api/doc") app.on_startup.append(self.on_startup) return app
except Exception as err: logs.error(f"Cannot read secrets\nError: {err}") pass logs.info(f"Launch API through port {port}") endpoint = GetName(data=dat, secrets=secrets) app = web.Application() app.router.add_get("/rgb", endpoint.on_rgb) app.router.add_get("/hex", endpoint.on_hex) # make CORS cors_default_opts = aiohttp_cors.ResourceOptions( allow_credentials=False, expose_headers="*", allow_headers=("Content-Type", "APIKEY"), allow_methods=["GET"]) cors = aiohttp_cors.setup(app, defaults={ 'http://localhost:3000': cors_default_opts, 'http://localhost:10000': cors_default_opts }) for route in list(app.router.routes()): cors.add(route) web.run_app(app, port=port, reuse_port=True)
curr_rules = cloudwatch_events_client.list_targets_by_rule( Rule='15-min') target['Id'] = str(len(curr_rules['Targets']) + 1) target['Arn'] = str(response['FunctionArn']) cloudwatch_events_client.put_targets(Rule='15-min', Targets=[target]) return web.Response( text=str("Successfully created function and attached rule")) except Exception as e: print(e) return web.Response(text=str(e)) app.router.add_get('/funcs', get_functions) app.router.add_get('/create', create_lambda_function) # Configure default CORS settings. cors = aiohttp_cors.setup(app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", ) }) for route in list(app.router.routes()): cors.add(route) if __name__ == '__main__': web.run_app(app)
async def init_app(): """ Initialize the main application instance and return it""" async def close_redis(app): """Close redis connections""" log.server_logger.info('Closing redis connections') app['rdata'].close() async def open_redis(app): """Open redis connections""" log.server_logger.info("Opening redis connections") app['rdata'] = await aioredis.create_redis_pool( (redis_host, redis_port), db=int(os.getenv('REDIS_DB', '2')), encoding='utf-8', minsize=2, maxsize=15) # Global vars app['clients'] = {} # Keep track of connected clients app['last_msg'] = {} # Last time a client has sent a message app['active_messages'] = set( ) # Avoid duplicate messages from being processes simultaneously app['cur_prefs'] = {} # Client currency preferences app['subscriptions'] = { } # Store subscription UUIDs, this is used for targeting callback accounts app['active_work'] = set( ) # Keep track of active work requests to prevent duplicates # Setup logger if debug_mode: logging.basicConfig(level=logging.DEBUG) else: root = logging.getLogger('aiohttp.server') logging.basicConfig(level=logging.INFO) if options.log_to_stdout: handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter( "%(asctime)s;%(levelname)s;%(message)s", "%Y-%m-%d %H:%M:%S %z") handler.setFormatter(formatter) root.addHandler(handler) else: handler = WatchedFileHandler(log_file) formatter = logging.Formatter( "%(asctime)s;%(levelname)s;%(message)s", "%Y-%m-%d %H:%M:%S %z") handler.setFormatter(formatter) root.addHandler(handler) root.addHandler( TimedRotatingFileHandler(log_file, when="d", interval=1, backupCount=100)) app = web.Application() cors = aiohttp_cors.setup(app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", ) }) app.add_routes([web.get('/', websocket_handler)]) # All WS requests app.add_routes([web.post('/callback', callback)]) # HTTP Callback from node # HTTP API users_resource = cors.add(app.router.add_resource("/api")) cors.add(users_resource.add_route("POST", http_api)) alerts_resource = cors.add(app.router.add_resource("/alerts/{lang}")) cors.add(alerts_resource.add_route("GET", alerts_api)) #app.add_routes([web.post('/callback', callback)]) app.on_startup.append(open_redis) app.on_shutdown.append(close_redis) return app
def run(self): """ Starts the server. """ server_logger = logging.getLogger('aiohttp.server') # In debug mode we don't use the standard request log but a more complete in response.py if log.getEffectiveLevel() == logging.DEBUG: server_logger.setLevel(logging.CRITICAL) logger = logging.getLogger("asyncio") logger.setLevel(logging.ERROR) if sys.platform.startswith("win"): loop = asyncio.get_event_loop() # Add a periodic callback to give a chance to process signals on Windows # because asyncio.add_signal_handler() is not supported yet on that platform # otherwise the loop runs outside of signal module's ability to trap signals. def wakeup(): loop.call_later(0.5, wakeup) loop.call_later(0.5, wakeup) server_config = Config.instance().get_section_config("Server") self._ssl_context = None if server_config.getboolean("ssl"): if sys.platform.startswith("win"): log.critical("SSL mode is not supported on Windows") raise SystemExit self._ssl_context = self._create_ssl_context(server_config) self._loop = asyncio.get_event_loop() if log.getEffectiveLevel() == logging.DEBUG: # On debug version we enable info that # coroutine is not called in a way await/await self._loop.set_debug(True) for key, val in os.environ.items(): log.debug("ENV %s=%s", key, val) self._app = aiohttp.web.Application() # Keep a list of active websocket connections self._app['websockets'] = weakref.WeakSet() # Background task started with the server self._app.on_startup.append(self._on_startup) resource_options = aiohttp_cors.ResourceOptions(expose_headers="*", allow_headers="*", max_age=0) # Allow CORS for this domains cors = aiohttp_cors.setup( self._app, defaults={ # Default web server for web gui dev "http://127.0.0.1:8080": resource_options, "http://localhost:8080": resource_options, "http://127.0.0.1:4200": resource_options, "http://localhost:4200": resource_options, "http://gns3.github.io": resource_options, "https://gns3.github.io": resource_options }) PortManager.instance().console_host = self._host for method, route, handler in Route.get_routes(): log.debug("Adding route: {} {}".format(method, route)) cors.add(self._app.router.add_route(method, route, handler)) for module in MODULES: log.debug("Loading module {}".format(module.__name__)) m = module.instance() m.port_manager = PortManager.instance() log.info("Starting server on {}:{}".format(self._host, self._port)) self._handler = self._app.make_handler() if self._run_application(self._handler, self._ssl_context) is False: self._loop.stop() sys.exit(1) self._signal_handling() self._exit_handling() if server_config.getboolean("shell"): asyncio.ensure_future(self.start_shell()) try: self._loop.run_forever() except TypeError as e: # This is to ignore an asyncio.windows_events exception # on Windows when the process gets the SIGBREAK signal # TypeError: async() takes 1 positional argument but 3 were given log.warning("TypeError exception in the loop {}".format(e)) finally: if self._loop.is_running(): try: self._loop.run_until_complete(self.shutdown_server()) except asyncio.CancelledError: pass
def init(loopy): asyncio.set_event_loop(loopy) app = web.Application(loop=loopy) async def graphql(request): back = await request.json() result = await gql.graphql(executable_schema, back.get('query', ''), variable_values=back.get('variables', ''), operation_name=back.get( 'operationName', ''), return_promise=True, allow_subscriptions=True) data = dict() if result.errors: data['errors'] = [str(err) for err in result.errors] if result.data: data['data'] = result.data if result.invalid: data['invalid'] = result.invalid return web.Response(text=json.dumps(data), headers={'Content-Type': 'application/json'}) # For /graphql app.router.add_post('/graphql', graphql, name='graphql') app.router.add_get('/graphql', graphql, name='graphql') # Configure default CORS settings. cors = aiohttp_cors.setup(app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", ) }) for route in list(app.router.routes()): cors.add(route) # For graphIQL j_env = Environment(enable_async=True) gql_view = GraphIQL.GraphIQL(schema=executable_schema, jinja_env=j_env, graphiql=True) app.router.add_route('*', handler=gql_view, path="/graphiql", name='graphiql') loopy.run_until_complete( asyncio.gather( asyncio.ensure_future( loopy.create_server(app.make_handler(), SERVICE_ADDRESS, SERVICE_PORT)), asyncio.ensure_future( amqp_pubsub.AmqpPubSub( configuration.AmqpConnectionConfig( RABBITMQ_ADDR, RABBITMQ_PORT, SERVICE_ID)).subscribe("fileAdded", lambda x: handle_event(x))))) try: logging.info("Started server on {}:{}".format(SERVICE_ADDRESS, SERVICE_PORT)) loopy.run_forever() except Exception as e: loopy.close() logger.error(e) sys.exit(-1) return None
async def setup(self, **kwargs): self.app = web.Application(middlewares=[self.error_middleware]) if self.cors_domains: self.cors = aiohttp_cors.setup( self.app, defaults={ domain: aiohttp_cors.ResourceOptions( allow_headers=("Content-Type",) ) for domain in self.cors_domains }, ) # http://docs.aiohttp.org/en/stable/faq.html#where-do-i-put-my-database-connection-so-handlers-can-access-it self.app["exit_stack"] = AsyncExitStack() await self.app["exit_stack"].__aenter__() self.app.on_shutdown.append(self.on_shutdown) self.app["multicomm_contexts"] = {"self": self} self.app["multicomm_routes"] = {} self.app["sources"] = {} self.app["source_contexts"] = {} self.app["source_records_iterkeys"] = {} self.app["models"] = {} self.app["model_contexts"] = {} self.app.update(kwargs) # Allow no routes other than pre-registered if in atomic mode self.routes = ( [] if self.mc_atomic else [ # HTTP Service specific APIs ("POST", "/service/upload/{filepath:.+}", self.service_upload), ("GET", "/service/files", self.service_files), # DFFML APIs ("GET", "/list/sources", self.list_sources), ( "POST", "/configure/source/{source}/{label}", self.configure_source, ), ( "GET", "/context/source/{label}/{ctx_label}", self.context_source, ), ("GET", "/list/models", self.list_models), ( "POST", "/configure/model/{model}/{label}", self.configure_model, ), ( "GET", "/context/model/{label}/{ctx_label}", self.context_model, ), # MutliComm APIs (Data Flow) ( "POST", "/multicomm/{label}/register", self.multicomm_register, ), # Source APIs ("GET", "/source/{label}/record/{key}", self.source_record), ("POST", "/source/{label}/update/{key}", self.source_update), ( "GET", "/source/{label}/records/{chunk_size}", self.source_records, ), ( "GET", "/source/{label}/records/{iterkey}/{chunk_size}", self.source_records_iter, ), # TODO route to delete iterkey before iteration has completed # Model APIs ("POST", "/model/{label}/train", self.model_train), ("POST", "/model/{label}/accuracy", self.model_accuracy), # TODO Provide an iterkey method for model prediction ( "POST", "/model/{label}/predict/{chunk_size}", self.model_predict, ), ] ) # Serve api.js if self.js: self.routes.append(("GET", "/api.js", self.api_js)) # Add all the routes and make them cors if needed for route in self.routes: route = self.app.router.add_route(*route) # Add cors to all routes if self.cors_domains: self.cors.add(route) # Serve static content if self.static: self.app.router.add_static("/", self.static) self.runner = web.AppRunner(self.app) await self.runner.setup()
async def setup(self, **kwargs): self.app = web.Application(middlewares=[self.error_middleware]) if self.cors_domains: self.cors = aiohttp_cors.setup( self.app, defaults={ domain: aiohttp_cors.ResourceOptions( allow_headers=("Content-Type",) ) for domain in self.cors_domains }, ) # http://docs.aiohttp.org/en/stable/faq.html#where-do-i-put-my-database-connection-so-handlers-can-access-it self.app["exit_stack"] = AsyncExitStack() await self.app["exit_stack"].__aenter__() self.app.on_shutdown.append(self.on_shutdown) self.app["multicomm_contexts"] = {"self": self} self.app["multicomm_routes"] = {} self.app["source_records_iterkeys"] = {} # Instantiate sources if they aren't instantiated yet for i, source in enumerate(self.sources): if inspect.isclass(source): self.sources[i] = source.withconfig(self.extra_config) await self.app["exit_stack"].enter_async_context(self.sources) self.app["sources"] = { source.ENTRY_POINT_LABEL: source for source in self.sources } mctx = await self.app["exit_stack"].enter_async_context(self.sources()) self.app["source_contexts"] = { source_ctx.parent.ENTRY_POINT_LABEL: source_ctx for source_ctx in mctx } # Instantiate models if they aren't instantiated yet for i, model in enumerate(self.models): if inspect.isclass(model): self.models[i] = model.withconfig(self.extra_config) await self.app["exit_stack"].enter_async_context(self.models) self.app["models"] = { model.ENTRY_POINT_LABEL: model for model in self.models } mctx = await self.app["exit_stack"].enter_async_context(self.models()) self.app["model_contexts"] = { model_ctx.parent.ENTRY_POINT_LABEL: model_ctx for model_ctx in mctx } # Instantiate scorers if they aren't instantiated yet for i, scorer in enumerate(self.scorers): if inspect.isclass(scorer): self.scorers[i] = scorer.withconfig(self.extra_config) await self.app["exit_stack"].enter_async_context(self.scorers) self.app["scorers"] = { scorer.ENTRY_POINT_LABEL: scorer for scorer in self.scorers } actx = await self.app["exit_stack"].enter_async_context(self.scorers()) self.app["scorer_contexts"] = { scorer_ctx.parent.ENTRY_POINT_LABEL: scorer_ctx for scorer_ctx in actx } self.app.update(kwargs) # Allow no routes other than pre-registered if in atomic mode self.routes = ( [] if self.mc_atomic else [ # HTTP Service specific APIs ("POST", "/service/upload/{filepath:.+}", self.service_upload), ("GET", "/service/files", self.service_files), # DFFML APIs ("GET", "/list/sources", self.list_sources), ( "POST", "/configure/source/{source}/{label}", self.configure_source, ), ( "GET", "/context/source/{label}/{ctx_label}", self.context_source, ), ("GET", "/list/models", self.list_models), ( "POST", "/configure/model/{model}/{label}", self.configure_model, ), ( "GET", "/context/model/{label}/{ctx_label}", self.context_model, ), ( "POST", "/configure/scorer/{scorer}/{label}", self.configure_scorer, ), ( "GET", "/context/scorer/{label}/{ctx_label}", self.context_scorer, ), # MutliComm APIs (Data Flow) ( "POST", "/multicomm/{label}/register", self.multicomm_register, ), # Source APIs ("GET", "/source/{label}/record/{key}", self.source_record), ("POST", "/source/{label}/update/{key}", self.source_update), ( "GET", "/source/{label}/records/{chunk_size}", self.source_records, ), ( "GET", "/source/{label}/records/{iterkey}/{chunk_size}", self.source_records_iter, ), ( "POST", "/scorer/{label}/{mlabel}/score", self.scorer_accuracy, ), # TODO route to delete iterkey before iteration has completed # Model APIs ("POST", "/model/{label}/train", self.model_train), # TODO Provide an iterkey method for model prediction ( "POST", "/model/{label}/predict/{chunk_size}", self.model_predict, ), ] ) # Redirects for method, src, dst in ( self.redirect[i : i + 3] for i in range(0, len(self.redirect), 3) ): self.routes.append((method.upper(), src, self.mkredirect(dst))) # Serve api.js if self.js: self.routes.append(("GET", "/api.js", self.api_js)) # Add all the routes and make them cors if needed for route in self.routes: route = self.app.router.add_route(*route) # Add cors to all routes if self.cors_domains: self.cors.add(route) # Serve static content if self.static: self.app.router.add_static("/", self.static) self.runner = web.AppRunner(self.app) await self.runner.setup()
async def make_application(self) -> web.Application: """Get the aiohttp application instance.""" middlewares = [ ready_middleware, debug_middleware, validation_middleware ] # admin-token and admin-token are mutually exclusive and required. # This should be enforced during parameter parsing but to be sure, # we check here. assert self.admin_insecure_mode ^ bool(self.admin_api_key) def is_unprotected_path(path: str): return (path in [ "/api/doc", "/api/docs/swagger.json", "/favicon.ico", "/ws", # ws handler checks authentication ] or path.startswith("/static/swagger/")) # If admin_api_key is None, then admin_insecure_mode must be set so # we can safely enable the admin server with no security if self.admin_api_key: @web.middleware async def check_token(request: web.Request, handler): header_admin_api_key = request.headers.get("x-api-key") valid_key = self.admin_api_key == header_admin_api_key if valid_key or is_unprotected_path(request.path): return await handler(request) else: raise web.HTTPUnauthorized() middlewares.append(check_token) collector = self.context.inject(Collector, required=False) @web.middleware async def setup_context(request: web.Request, handler): # TODO may dynamically adjust the profile used here according to # headers or other parameters context = AdminRequestContext(self.root_profile, context=self.context) context.injector.bind_instance(BaseResponder, self.responder) request["context"] = context if collector: handler = collector.wrap_coro(handler, [handler.__qualname__]) if self.task_queue: task = await self.task_queue.put(handler(request)) return await task return await handler(request) middlewares.append(setup_context) app = web.Application(middlewares=middlewares) app["outbound_message_router"] = self.responder.send app.add_routes([ web.get("/", self.redirect_handler, allow_head=False), web.get("/plugins", self.plugins_handler, allow_head=False), web.get("/status", self.status_handler, allow_head=False), web.post("/status/reset", self.status_reset_handler), web.get("/status/live", self.liveliness_handler, allow_head=False), web.get("/status/ready", self.readiness_handler, allow_head=False), web.get("/shutdown", self.shutdown_handler, allow_head=False), web.get("/ws", self.websocket_handler, allow_head=False), ]) plugin_registry = self.context.inject(PluginRegistry, required=False) if plugin_registry: await plugin_registry.register_admin_routes(app) cors = aiohttp_cors.setup( app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", allow_methods="*", ) }, ) for route in app.router.routes(): cors.add(route) # get agent label agent_label = self.context.settings.get("default_label") version_string = f"v{__version__}" setup_aiohttp_apispec(app=app, title=agent_label, version=version_string, swagger_path="/api/doc") app.on_startup.append(self.on_startup) # ensure we always have status values app._state["ready"] = False app._state["alive"] = False return app
def init_app() -> Tuple[web.Application, AsyncIOMotorClient]: """ Initializes the web application. Returns ------- Tuple[web.Application, AsyncIOMotorClient] aiohttp.web.Application An initialized AIOHTTP web application. motor.motor_asyncio.AsyncIOMotorClient An async connection to the MongoDB Atlas cluster. """ # Initialize fully async database client USERNAME = os.environ.get("MONGO_USERNAME") PASSWORD = os.environ.get("MONGO_PASSWORD") uri = f"mongodb+srv://{USERNAME}:{PASSWORD}@pathshare-cluster0-j1oyz.mongodb.net/main" client = AsyncIOMotorClient(uri).main # Initialize application and database app = web.Application() cors = aiohttp_cors.setup(app) db = MongoConnection(client) # Initialize endpoint classes gets = GetEndpoints(db) posts = PostEndpoints(db) patches = PatchEndpoints(db) deletes = DeleteEndpoints(db) # Add routes to application with CORS enabled # GET routes app.router.add_get("/", gets.home) # Default home app.router.add_get("/get/ride", gets.get_ride) app.router.add_get("/get/user", gets.get_user) app.router.add_get("/get/ride/all", gets.get_all_rides) app.router.add_get("/get/validation", gets.get_validation) # POST routes app.router.add_post("/post/user/new", posts.post_new_user) app.router.add_post("/post/ride/new", posts.post_new_ride) # PATCH routes app.router.add_patch("/patch/ride/{ride_id}/add/rider/{rider_id}", patches.add_rider) app.router.add_patch("/patch/ride/{ride_id}/delete/rider/{rider_id}", patches.remove_rider) # DELETE routes app.router.add_delete("/delete/user", deletes.delete_user) app.router.add_delete("/delete/ride", deletes.delete_ride) # Enable unrestricted CORS on all routes cors = aiohttp_cors.setup(app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", ) }) for route in list(app.router.routes()): cors.add(route) return app, db
def _create_cors(app: web.Application): wildcard = '*' options = aiohttp_cors.ResourceOptions(expose_headers=wildcard, allow_headers=wildcard) defaults = {wildcard: options} return aiohttp_cors.setup(app, defaults=defaults)
print("responded: %s" % str(results)) resp = {"data": results} except Exception as e: print("An error occured:") print(e) resp = {"error": str(e)} return web.json_response(resp) if __name__ == "__main__": args = parser.parse_args() nest_asyncio.apply() #ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) #ssl_context.load_cert_chain('cert.crt', 'cert.key') app = web.Application() cors = aiohttp_cors.setup(app) resource = cors.add(app.add_resource("/peregrine")) route = cors.add( resource.add_route("GET", handle), { "http://gengix.com": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers=("X-Custom-Server-Header", ), max_age=360, ) }) #web.run_app(app, port=args.port, ssl_context=ssl_context) web.run_app(app, port=args.port)
async def application(): logger = get_logger(__name__) app = web.Application(logger=logger) logger.debug(object_to_dict(config)) mysql_config = config.api_server.mysql await init_db( host=mysql_config.host, port=mysql_config.port, user=mysql_config.user, password=mysql_config.password, db=mysql_config.database, ) user_repository = UserRepositoryImpl() token_repository = TokenRepositoryImpl() external = { "third_party": { "facebook": FacebookToken(), "kakao": KakaoToken(), "apple": AppleToken(), "google": GoogleToken(), }, } secret = { "jwt_secret": config.api_server.jwt_secret, "internal_api_keys": config.api_server.internal_api_keys, } user_creation_callback_handler = UserCreationCallbackHandler( config.api_server.event_callback_urls ) user_update_callback_handler = UserUpdateCallbackHandler( config.api_server.event_callback_urls ) resource_list: Dict[str, BaseResource] = { "/users": UsersHttpResource( user_creation_callback_handler=user_creation_callback_handler, user_update_callback_handler=user_update_callback_handler, user_repository=user_repository, secret=secret, external=external, ), "/token": TokenHttpResource( user_repository=user_repository, token_repository=token_repository, secret=secret, external=external, ), "/internal": InternalHttpResource( user_repository=user_repository, secret=secret, ), } for path, resource in resource_list.items(): subapp = web.Application(logger=logger) resource.route(subapp.router) plugin_app(app, path, subapp) cors = aiohttp_cors.setup(app) allow_url = "*" for route in list(app.router.routes()): cors.add( route, { allow_url: aiohttp_cors.ResourceOptions( allow_credentials=True, allow_headers="*", allow_methods=[route.method], ) }, ) return app
def get_app(loop=None): # Make sure the Telemetry user ID is configured uid = os.getenv('TELEMETRY_USER_ID') try: int(uid) except (ValueError, TypeError): print("ERROR: Please make sure you've defined a valid TELEMETRY_USER_ID " "environment variable.", file=sys.stderr) exit(1) app = web.Application(loop=loop) # Setup middlewares setup_middlewares(app) # Allow Web Application calls. cors = aiohttp_cors.setup(app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", ) }) # Home cors.add(app.router.add_get('/', home.redirect)) # Redirects to /v1/ cors.add(app.router.add_get('/v1', home.redirect)) # Redirects to /v1/ cors.add(app.router.add_get('/v1/', home.index)) # Utilities cors.add(app.router.add_get('/contribute.json', utilities.contribute_redirect)) cors.add(app.router.add_get('/v1/contribute.json', utilities.contribute_json)) cors.add(app.router.add_get('/v1/__api__', utilities.oas_spec)) cors.add(app.router.add_get('/v1/__version__', utilities.version)) # Heartbeat cors.add(app.router.add_get('/v1/__heartbeat__', utilities.heartbeat)) cors.add(app.router.add_get('/v1/__lbheartbeat__', utilities.lbheartbeat)) # Statuses cors.add(app.router.add_get('/v1/{product}', release.view_get_releases)) cors.add(app.router.add_get('/v1/{product}/ongoing-versions', product.get_ongoing_versions)) cors.add(app.router.add_get('/v1/{product}/{version}', release.view_get_checks)) cors.add(app.router.add_get('/v1/{product}/{version}/archive', release.archive, name="archive")) cors.add(app.router.add_get('/v1/{product}/{version}/archive/partner-repacks', release.partner_repacks, name="partner-repacks")) cors.add(app.router.add_get('/v1/{product}/{version}/crash-stats/uptake', release.crash_stats_uptake, name="crash-stats-uptake")) cors.add(app.router.add_get('/v1/{product}/{version}/bedrock/release-notes', release.bedrock_release_notes, name="release-notes")) cors.add(app.router.add_get('/v1/{product}/{version}/bedrock/security-advisories', release.bedrock_security_advisories, name="security-advisories")) cors.add(app.router.add_get('/v1/{product}/{version}/bedrock/download-links', release.bedrock_download_links, name="download-links")) cors.add(app.router.add_get('/v1/{product}/{version}/product-details', release.product_details, name="product-details")) cors.add(app.router.add_get('/v1/{product}/{version}/product-details' '/devedition-beta-versions-matches', release.devedition_beta_check, name="devedition-beta-matches")) cors.add(app.router.add_get('/v1/{product}/{version}/balrog-rules', release.balrog_rules, name="balrog-rules")) cors.add(app.router.add_get('/v1/{product}/{version}/bouncer', release.bouncer_download_links, name="bouncer")) cors.add(app.router.add_get('/v1/{product}/{version}/buildhub', release.buildhub_check, name="buildhub")) cors.add(app.router.add_get('/v1/{product}/{version}/telemetry/main-summary-uptake', release.telemetry_uptake, name="telemetry-main-summary-uptake")) # Swagger UI and documentation setup_swagger(app, swagger_url="/v1/api/doc", swagger_from_file=os.path.join(HERE, "api.yaml")) return app
async def run(self): async def _check_parent(): """Check if raylet is dead.""" curr_proc = psutil.Process() while True: parent = curr_proc.parent() if parent is None or parent.pid == 1: logger.error("raylet is dead, agent will die because " "it fate-shares with raylet.") sys.exit(0) await asyncio.sleep( dashboard_consts. DASHBOARD_AGENT_CHECK_PARENT_INTERVAL_SECONDS) check_parent_task = create_task(_check_parent()) # Create an aioredis client for all modules. try: self.aioredis_client = await dashboard_utils.get_aioredis_client( self.redis_address, self.redis_password, dashboard_consts.CONNECT_REDIS_INTERNAL_SECONDS, dashboard_consts.RETRY_REDIS_CONNECTION_TIMES) except (socket.gaierror, ConnectionRefusedError): logger.error( "Dashboard agent exiting: " "Failed to connect to redis at %s", self.redis_address) sys.exit(-1) # Create a http session for all modules. self.http_session = aiohttp.ClientSession( loop=asyncio.get_event_loop()) # Start a grpc asyncio server. await self.server.start() modules = self._load_modules() # Http server should be initialized after all modules loaded. app = aiohttp.web.Application() app.add_routes(routes=routes.bound_routes()) # Enable CORS on all routes. cors = aiohttp_cors.setup( app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_methods="*", allow_headers=("Content-Type", "X-Header"), ) }) for route in list(app.router.routes()): cors.add(route) runner = aiohttp.web.AppRunner(app) await runner.setup() site = aiohttp.web.TCPSite(runner, self.ip, 0) await site.start() http_host, http_port = site._server.sockets[0].getsockname() logger.info("Dashboard agent http address: %s:%s", http_host, http_port) # Dump registered http routes. dump_routes = [ r for r in app.router.routes() if r.method != hdrs.METH_HEAD ] for r in dump_routes: logger.info(r) logger.info("Registered %s routes.", len(dump_routes)) # Write the dashboard agent port to redis. await self.aioredis_client.set( f"{dashboard_consts.DASHBOARD_AGENT_PORT_PREFIX}{self.node_id}", json.dumps([http_port, self.grpc_port])) # Register agent to agent manager. raylet_stub = agent_manager_pb2_grpc.AgentManagerServiceStub( self.aiogrpc_raylet_channel) await raylet_stub.RegisterAgent( agent_manager_pb2.RegisterAgentRequest( agent_pid=os.getpid(), agent_port=self.grpc_port, agent_ip_address=self.ip)) await asyncio.gather(check_parent_task, *(m.run(self.server) for m in modules)) await self.server.wait_for_termination() # Wait for finish signal. await runner.cleanup()
import configparser from datetime import date, datetime, timedelta from nulsexplorer import TRANSACTION_TYPES #bottle.install(bottle.JSONPlugin(json_dumps=json_util.dumps)) app = web.Application() auth = None # Configure default CORS settings. cors = aiohttp_cors.setup(app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_methods=["GET", "POST"], allow_credentials=True, expose_headers="*", allow_headers="*", ) }) tpl_path = pkg_resources.resource_filename('nulsexplorer.web', 'templates') JINJA_LOADER = jinja2.ChoiceLoader([jinja2.FileSystemLoader(tpl_path),]) aiohttp_jinja2.setup(app, loader=JINJA_LOADER) env = aiohttp_jinja2.get_env(app) env.globals.update({ 'app': app, 'date': date, 'datetime': datetime, 'time': time, 'timedelta': timedelta,
def adaptor(): port = Configuration.parameters['adaptor_port'] jid = Configuration.parameters['adaptor'] basejid = Configuration.parameters['userjid'] protocol_version = Configuration.parameters['protocol'] simulation_dir = Configuration.parameters['simulation_dir'] logging.debug("simdir:" + simulation_dir) hostname = Configuration.parameters['adaptor_address'] password = Configuration.parameters['xmpp_password'] if protocol_version == "2.0": logging.info("Starting Adaptor") adaptor = sche.Adaptor(basejid + "/" + jid, password) # scheduler.web.add_get("/gettime", scheduler.get_time, "message.html") # scheduler.web.add_post("/postanswer", scheduler.post_answer, "message2.html") sc2 = adaptor.start() cors = aiohttp_cors.setup(adaptor.web.app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", allow_methods="*", ) }) route = { 'method': 'GET', 'path': '/getmessage', 'handler': adaptor.exposeGetRestAPI, 'name': 'test' } route2 = { 'method': 'POST', 'path': '/postanswer', 'handler': adaptor.exposePostRestAPI, 'name': 'test2' } route3 = { 'method': 'GET', 'path': '/gettime', 'handler': adaptor.get_time, 'name': 'test3' } cors.add( adaptor.web.app.router.add_route(method=route3['method'], path=route3['path'], handler=route3['handler'], name=route3['name'])) cors.add( adaptor.web.app.router.add_route(method=route['method'], path=route['path'], handler=route['handler'], name=route['name'])) cors.add( adaptor.web.app.router.add_route(method=route2['method'], path=route2['path'], handler=route2['handler'], name=route2['name'])) temp = adaptor.web.start(hostname=hostname, port=port) temp2 = adaptor.web.is_started() sc2.result() elif protocol_version == "1.0": adaptor = sche1.Adaptor(basejid + "/" + jid, password) # scheduler.web.add_get("/gettime", scheduler.get_time, "message.html") #scheduler.web.add_post("/postanswer", scheduler.post_answer, "message2.html") sc2 = adaptor.start() cors = aiohttp_cors.setup(adaptor.web.app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", allow_methods="*", ) }) route = { 'method': 'POST', 'path': '/postanswer', 'handler': adaptor.exposePostRestAPI, 'name': 'test2' } cors.add( adaptor.web.app.router.add_route(method=route['method'], path=route['path'], handler=route['handler'], name=route['name'])) temp = adaptor.web.start(hostname=hostname, port=port) temp.result() temp2 = adaptor.web.is_started() sc2.result()
def register_graphql_handlers( app: "aiohttp.web.Application", engine_sdl: str = None, engine_schema_name: str = "default", executor_context: Optional[Dict[str, Any]] = None, executor_http_endpoint: str = "/graphql", executor_http_methods: List[str] = None, engine: Engine = None, subscription_ws_endpoint: Optional[str] = None, subscription_keep_alive_interval: Optional[int] = None, graphiql_enabled: bool = False, graphiql_options: Optional[Dict[str, Any]] = None, engine_modules: Optional[ List[Union[str, Dict[str, Union[str, Dict[str, str]]]]] ] = None, context_factory: Optional[AsyncContextManager] = None, response_formatter: Optional[ Callable[ [aiohttp.web.Request, Dict[str, Any], Dict[str, Any]], aiohttp.web.Response, ] ] = None, ) -> "aiohttp.web.Application": """Copy of tartiflette_aiohttp.register_graphql_handlers with CORS support enabled Register a Tartiflette Engine to an app Pass a SDL or an already initialized Engine, not both, not neither. Keyword Arguments: app {aiohttp.web.Application} -- The application to register to. engine_sdl {str} -- The SDL defining your API (default: {None}) engine_schema_name {str} -- The name of your sdl (default: {"default"}) executor_context {Optional[Dict[str, Any]]} -- Context dict that will be passed to the resolvers (default: {None}) executor_http_endpoint {str} -- Path part of the URL the graphql endpoint will listen on (default: {"/graphql"}) executor_http_methods {list[str]} -- List of HTTP methods allowed on the endpoint (only GET and POST are supported) (default: {None}) engine {Engine} -- An uncooked engine, or a create_engine coroutines (default: {None}) subscription_ws_endpoint {Optional[str]} -- Path part of the URL the WebSocket GraphQL subscription endpoint will listen on (default: {None}) subscription_keep_alive_interval {Optional[int]} -- Number of seconds before each Keep Alive messages (default: {None}) graphiql_enabled {bool} -- Determines whether or not we should handle a GraphiQL endpoint (default: {False}) graphiql_options {dict} -- Customization options for the GraphiQL instance (default: {None}) engine_modules: {Optional[List[Union[str, Dict[str, Union[str, Dict[str, str]]]]]]} -- Module to import (default:{None}) context_factory: {Optional[AsyncContextManager]} -- asynccontextmanager in charge of generating the context for each request (default: {None}) response_formatter: {Optional[Callable[[aiohttp.web.Request, Dict[str, Any], Dict[str, Any]], aiohttp.web.Response]]} -- In charger of the transformation of the resulting data into an aiohttp.web.Response (default: {None}) Raises: Exception -- On bad sdl/engine parameter combinaison. Exception -- On unsupported HTTP Method. Return: The app object. """ if not executor_context: executor_context = {} executor_context["app"] = app if not executor_http_methods: executor_http_methods = ["GET", "POST"] if context_factory is None: context_factory = default_context_factory context_factory = partial(context_factory, executor_context) # type: ignore if not engine: engine = Engine() if iscoroutine(engine): app.on_startup.append(_await_on_startup) else: app.on_startup.append( partial( _cook_on_startup, engine_sdl, engine_schema_name, engine_modules, ) ) app["ttftt_engine"] = engine app["response_formatter"] = response_formatter or prepare_response cors = aiohttp_cors.setup(app) for method in executor_http_methods: try: route = app.router.add_route( method, executor_http_endpoint, partial( getattr(Handlers, "handle_%s" % method.lower()), context_factory=context_factory, ), ) cors.add(route, {"*": aiohttp_cors.ResourceOptions(allow_headers="*")}) except AttributeError: raise Exception("Unsupported < %s > http method" % method) _set_subscription_ws_handler( app, subscription_ws_endpoint, subscription_keep_alive_interval, context_factory, ) _set_graphiql_handler( app, graphiql_enabled, graphiql_options, executor_http_endpoint, executor_http_methods, subscription_ws_endpoint, ) return app
def add_routes(app, with_ui=False): app.router.add_view(r"/usersiteroles/{user_id}/{site_id}/{role_id}", views.UsersiterolesUserIdSiteIdRoleId) app.router.add_view(r"/usersiteroles", views.Usersiteroles) app.router.add_view(r"/usersitedata/{user_id}/{site_id}", views.UsersitedataUserIdSiteId) app.router.add_view(r"/usersitedata", views.Usersitedata) app.router.add_view(r"/users/{user_id}", views.UsersUserId) app.router.add_view(r"/users", views.Users) app.router.add_view(r"/userdomainroles/{user_id}/{domain_id}/{role_id}", views.UserdomainrolesUserIdDomainIdRoleId) app.router.add_view(r"/userdomainroles", views.Userdomainroles) app.router.add_view(r"/sites/{site_id}", views.SitesSiteId) app.router.add_view(r"/sites", views.Sites) app.router.add_view(r"/siteroles/{site_id}/{role_id}", views.SiterolesSiteIdRoleId) app.router.add_view(r"/siteroles", views.Siteroles) app.router.add_view(r"/sitedataschemas/{site_id}", views.SitedataschemasSiteId) app.router.add_view(r"/sitedataschemas", views.Sitedataschemas) app.router.add_view(r"/roles/{role_id}", views.RolesRoleId) app.router.add_view(r"/roles", views.Roles) app.router.add_view( r"/roleresourcepermissions/{role_id}/{resource_id}/{permission_id}", views.RoleresourcepermissionsRoleIdResourceIdPermissionId) app.router.add_view(r"/roleresourcepermissions", views.Roleresourcepermissions) app.router.add_view(r"/resources/{resource_id}", views.ResourcesResourceId) app.router.add_view(r"/resources", views.Resources) app.router.add_view(r"/request_user_deletion", views.RequestUserDeletion) app.router.add_view(r"/refresh/sites", views.RefreshSites) app.router.add_view(r"/refresh/roles", views.RefreshRoles) app.router.add_view(r"/refresh/resources", views.RefreshResources) app.router.add_view(r"/refresh/permissions", views.RefreshPermissions) app.router.add_view(r"/refresh/keys", views.RefreshKeys) app.router.add_view(r"/refresh/domains", views.RefreshDomains) app.router.add_view(r"/refresh/credentials", views.RefreshCredentials) app.router.add_view(r"/refresh/clients", views.RefreshClients) app.router.add_view(r"/refresh/all", views.RefreshAll) app.router.add_view(r"/permissions/{permission_id}", views.PermissionsPermissionId) app.router.add_view(r"/permissions", views.Permissions) app.router.add_view(r"/organisations/{organisation_id}", views.OrganisationsOrganisationId) app.router.add_view(r"/organisations", views.Organisations) app.router.add_view(r"/ops/usersitedata", views.OpsUsersitedata) app.router.add_view(r"/ops/users_with_roles_for_site/{site_id}", views.OpsUsersWithRolesForSiteSiteId) app.router.add_view(r"/ops/users_with_roles_for_domain/{domain_id}", views.OpsUsersWithRolesForDomainDomainId) app.router.add_view( r"/ops/user_site_role_labels_aggregated/{user_id}/{site_id}", views.OpsUserSiteRoleLabelsAggregatedUserIdSiteId) app.router.add_view(r"/ops/user_site_permissions/{user_id}/{site_id}", views.OpsUserSitePermissionsUserIdSiteId) app.router.add_view(r"/ops/user_management_portal_permissions/{user_id}", views.OpsUserManagementPortalPermissionsUserId) app.router.add_view(r"/ops/user_has_permissions/{user_id}", views.OpsUserHasPermissionsUserId) app.router.add_view(r"/ops/user_domain_permissions/{user_id}/{domain_id}", views.OpsUserDomainPermissionsUserIdDomainId) app.router.add_view(r"/ops/site_role_labels_aggregated/{site_id}", views.OpsSiteRoleLabelsAggregatedSiteId) app.router.add_view(r"/ops/site_and_domain_roles/{site_id}", views.OpsSiteAndDomainRolesSiteId) app.router.add_view(r"/ops/get_sites_under_domain/{domain_id}", views.OpsGetSitesUnderDomainDomainId) app.router.add_view( r"/ops/get_site_from_client_token_id/{client_token_id}", views.OpsGetSiteFromClientTokenIdClientTokenId) app.router.add_view(r"/ops/domain_roles/{domain_id}", views.OpsDomainRolesDomainId) app.router.add_view(r"/ops/confirm_user_data_deletion/{user_id}", views.OpsConfirmUserDataDeletionUserId) app.router.add_view(r"/ops/all_user_roles/{user_id}", views.OpsAllUserRolesUserId) app.router.add_view( r"/invitationsiteroles/{invitation_id}/{site_id}/{role_id}", views.InvitationsiterolesInvitationIdSiteIdRoleId) app.router.add_view(r"/invitationsiteroles", views.Invitationsiteroles) app.router.add_view(r"/invitations/{invitation_id}/send", views.InvitationsInvitationIdSend) app.router.add_view(r"/invitations/{invitation_id}", views.InvitationsInvitationId) app.router.add_view(r"/invitations/purge/expired", views.InvitationsPurgeExpired) app.router.add_view(r"/invitations", views.Invitations) app.router.add_view(r"/invitationredirecturls/{invitationredirecturl_id}", views.InvitationredirecturlsInvitationredirecturlId) app.router.add_view(r"/invitationredirecturls", views.Invitationredirecturls) app.router.add_view( r"/invitationdomainroles/{invitation_id}/{domain_id}/{role_id}", views.InvitationdomainrolesInvitationIdDomainIdRoleId) app.router.add_view(r"/invitationdomainroles", views.Invitationdomainroles) app.router.add_view(r"/healthcheck", views.Healthcheck) app.router.add_view(r"/events", views.Events) app.router.add_view(r"/domains/{domain_id}", views.DomainsDomainId) app.router.add_view(r"/domains", views.Domains) app.router.add_view(r"/domainroles/{domain_id}/{role_id}", views.DomainrolesDomainIdRoleId) app.router.add_view(r"/domainroles", views.Domainroles) app.router.add_view(r"/deletionmethods/{deletionmethod_id}", views.DeletionmethodsDeletionmethodId) app.router.add_view(r"/deletionmethods", views.Deletionmethods) app.router.add_view(r"/deletedusersites/{user_id}/{site_id}", views.DeletedusersitesUserIdSiteId) app.router.add_view(r"/deletedusersites", views.Deletedusersites) app.router.add_view(r"/deletedusers/{user_id}", views.DeletedusersUserId) app.router.add_view(r"/deletedusers", views.Deletedusers) app.router.add_view(r"/credentials/{credentials_id}", views.CredentialsCredentialsId) app.router.add_view(r"/credentials", views.Credentials) app.router.add_view(r"/countries/{country_code}", views.CountriesCountryCode) app.router.add_view(r"/countries", views.Countries) app.router.add_view(r"/clients/{client_id}", views.ClientsClientId) app.router.add_view(r"/clients", views.Clients) app.router.add_view(r"/adminnotes/{admin_note_id}", views.AdminnotesAdminNoteId) app.router.add_view(r"/adminnotes", views.Adminnotes) if with_ui: app.router.add_view(r"/the_specification", views.__SWAGGER_SPEC__) app.router.add_static(r"/ui", path="ui") # Configure default CORS settings. cors = aiohttp_cors.setup(app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", ) }) # Configure CORS on all routes. for route in app.router.routes(): cors.add(route)
async def swagger_handler(request): with open(SWAGGER_PATH, 'r') as f: swagger = yaml.load(f.read()) swagger['paths']['/']['post']['x-taskModel'] = os.environ[ 'MITIE_MODEL_LANG'] return JSONResponse(swagger) app = web.Application() app.router.add_route("POST", "/", extract_handler) app.router.add_route("GET", "/", info_handler) app.router.add_route("GET", "/swagger.json", swagger_handler) # Enable CORS cors = aiohttp_cors.setup(app, defaults={ "*": aiohttp_cors.ResourceOptions( expose_headers="*", allow_headers="*", ) }) for route in list(app.router.routes()): cors.add(route) if __name__ == "__main__": web.run_app(app)
async def setup(self, **kwargs): self.app = web.Application(middlewares=[self.error_middleware]) if self.cors_domains: self.cors = aiohttp_cors.setup( self.app, defaults={ domain: aiohttp_cors.ResourceOptions( allow_headers=("Content-Type", )) for domain in self.cors_domains }, ) # http://docs.aiohttp.org/en/stable/faq.html#where-do-i-put-my-database-connection-so-handlers-can-access-it self.app["exit_stack"] = AsyncExitStack() await self.app["exit_stack"].__aenter__() self.app.on_shutdown.append(self.on_shutdown) self.app["sources"] = {} self.app["source_contexts"] = {} self.app["source_repos_iterkeys"] = {} self.app["models"] = {} self.app["model_contexts"] = {} self.app.update(kwargs) self.routes = [ # HTTP Service specific APIs ("POST", "/service/upload/{filepath:.+}", self.service_upload), # DFFML APIs ("GET", "/list/sources", self.list_sources), ( "POST", "/configure/source/{source}/{label}", self.configure_source, ), ( "GET", "/context/source/{label}/{ctx_label}", self.context_source, ), ("GET", "/list/models", self.list_models), ("POST", "/configure/model/{model}/{label}", self.configure_model), ("POST", "/context/model/{label}/{ctx_label}", self.context_model), # Source APIs ("GET", "/source/{label}/repo/{key}", self.source_repo), ("POST", "/source/{label}/update/{key}", self.source_update), ("GET", "/source/{label}/repos/{chunk_size}", self.source_repos), ( "GET", "/source/{label}/repos/{iterkey}/{chunk_size}", self.source_repos_iter, ), # TODO route to delete iterkey before iteration has completed # Model APIs ("POST", "/model/{label}/train", self.model_train), ("POST", "/model/{label}/accuracy", self.model_accuracy), # TODO Provide an iterkey method for model prediction ( "POST", "/model/{label}/predict/{chunk_size}", self.model_predict, ), ] for route in self.routes: route = self.app.router.add_route(*route) # Add cors to all routes if self.cors_domains: self.cors.add(route) self.runner = web.AppRunner(self.app) await self.runner.setup()
userid=data["user"] stop = data["stop"] if(stop=="false"): await sio.emit('message',str('{"color":"'+ random.choice(["green"])+'"}')) elif(stop=="true"): await sio.emit('message',str('{"color":"white"}')) return web.Response(text="Sent?") # app.router.add_get('/', index) # app.router.add_get('/process', process) cors.add(app.router.add_get("/",index), { "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers=("X-Custom-Server-Header",), allow_headers=("X-Requested-With", "Content-Type"), max_age=3600, ) }) cors.add(app.router.add_get("/process",process), { "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers=("X-Custom-Server-Header",), allow_headers=("X-Requested-With", "Content-Type"), max_age=3600, ) }) cors.add(app.router.add_post("/save-subscription",save), { "*": aiohttp_cors.ResourceOptions( allow_credentials=True,
async def make_application(self) -> web.Application: """Get the aiohttp application instance.""" middlewares = [] stats: Collector = await self.context.inject(Collector, required=False) if stats: @web.middleware async def collect_stats(request, handler): handler = stats.wrap_coro( handler, [handler.__qualname__, "any-admin-request"]) return await handler(request) middlewares.append(collect_stats) app = web.Application(middlewares=middlewares) app["request_context"] = self.context app["outbound_message_router"] = self.responder.send app.add_routes([ web.get("/", self.redirect_handler), web.get("/modules", self.modules_handler), web.get("/status", self.status_handler), web.post("/status/reset", self.status_reset_handler), web.get("/ws", self.websocket_handler), ]) await register_module_routes(app) for protocol_module_path in self.context.settings.get( "external_protocols", []): try: routes_module = ClassLoader.load_module( f"{protocol_module_path}.routes") await routes_module.register(app) except Exception as e: raise ConfigError( f"Failed to load external protocol module '{protocol_module_path}'." ) from e cors = aiohttp_cors.setup( app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", allow_methods="*", ) }, ) for route in app.router.routes(): cors.add(route) setup_aiohttp_apispec( app=app, title="Aries Cloud Agent", version="v1", swagger_path="/api/doc", ) app.on_startup.append(self.on_startup) return app
import aiohttp_cors CORS_CONFIG = aiohttp_cors.ResourceOptions( allow_credentials=True, allow_methods='*', allow_headers='*', )
def start(foreground, root, config_file): """Run the Wazuh API. If another Wazuh API is running, this function fails. This function exits with 0 if successful or 1 if failed because the API was already running. Arguments --------- foreground : bool If the API must be daemonized or not root : bool If true, the daemon is run as root. Normally not recommended for security reasons config_file : str Path to the API config file """ import asyncio import logging import os import ssl import connexion import uvloop from aiohttp_cache import setup_cache from api import alogging, configuration import wazuh.security from api import __path__ as api_path # noinspection PyUnresolvedReferences from api import validator from api.api_exception import APIError from api.constants import CONFIG_FILE_PATH from api.middlewares import set_user_name, security_middleware, response_postprocessing, request_logging, \ set_secure_headers from api.signals import modify_response_headers from api.uri_parser import APIUriParser from api.util import to_relative_path from wazuh.core import pyDaemonModule def set_logging(log_path='logs/api.log', foreground_mode=False, debug_mode='info'): for logger_name in ('connexion.aiohttp_app', 'connexion.apis.aiohttp_api', 'wazuh-api'): api_logger = alogging.APILogger( log_path=log_path, foreground_mode=foreground_mode, logger_name=logger_name, debug_level='info' if logger_name != 'wazuh-api' and debug_mode != 'debug2' else debug_mode) api_logger.setup_logger() configuration.api_conf.update( configuration.read_yaml_config(config_file=config_file)) api_conf = configuration.api_conf security_conf = configuration.security_conf log_path = api_conf['logs']['path'] # Set up logger set_logging(log_path=log_path, debug_mode=api_conf['logs']['level'], foreground_mode=foreground) logger = logging.getLogger('wazuh-api') # Set correct permissions on api.log file if os.path.exists(os.path.join(common.wazuh_path, log_path)): os.chown(os.path.join(common.wazuh_path, log_path), common.wazuh_uid(), common.wazuh_gid()) os.chmod(os.path.join(common.wazuh_path, log_path), 0o660) # Configure https ssl_context = None if api_conf['https']['enabled']: try: # Generate SSL if it does not exist and HTTPS is enabled if not os.path.exists( api_conf['https']['key']) or not os.path.exists( api_conf['https']['cert']): logger.info( 'HTTPS is enabled but cannot find the private key and/or certificate. ' 'Attempting to generate them') private_key = configuration.generate_private_key( api_conf['https']['key']) logger.info( f"Generated private key file in WAZUH_PATH/{to_relative_path(api_conf['https']['key'])}" ) configuration.generate_self_signed_certificate( private_key, api_conf['https']['cert']) logger.info( f"Generated certificate file in WAZUH_PATH/{to_relative_path(api_conf['https']['cert'])}" ) allowed_ssl_protocols = { 'tls': ssl.PROTOCOL_TLS, 'tlsv1': ssl.PROTOCOL_TLSv1, 'tlsv1.1': ssl.PROTOCOL_TLSv1_1, 'tlsv1.2': ssl.PROTOCOL_TLSv1_2 } ssl_protocol = allowed_ssl_protocols[api_conf['https'] ['ssl_protocol'].lower()] ssl_context = ssl.SSLContext(protocol=ssl_protocol) if api_conf['https']['use_ca']: ssl_context.verify_mode = ssl.CERT_REQUIRED ssl_context.load_verify_locations(api_conf['https']['ca']) ssl_context.load_cert_chain(certfile=api_conf['https']['cert'], keyfile=api_conf['https']['key']) # Loads SSL ciphers if any have been specified if api_conf['https']['ssl_ciphers']: ssl_ciphers = api_conf['https']['ssl_ciphers'].upper() try: ssl_context.set_ciphers(ssl_ciphers) except ssl.SSLError: logger.error( str( APIError( 2003, details='SSL ciphers can not be selected'))) sys.exit(1) except ssl.SSLError: logger.error( str( APIError( 2003, details= 'Private key does not match with the certificate'))) sys.exit(1) except IOError as e: if e.errno == 22: logger.error( str(APIError(2003, details='PEM phrase is not correct'))) elif e.errno == 13: logger.error( str( APIError( 2003, details= 'Ensure the certificates have the correct permissions' ))) else: print( 'Wazuh API SSL ERROR. Please, ensure if path to certificates is correct in the configuration ' f'file WAZUH_PATH/{to_relative_path(CONFIG_FILE_PATH)}') sys.exit(1) # Drop privileges to wazuh if not root: if api_conf['drop_privileges']: os.setgid(common.wazuh_gid()) os.setuid(common.wazuh_uid()) else: print(f"Starting API as root") # Foreground/Daemon utils.check_pid('wazuh-apid') if not foreground: pyDaemonModule.pyDaemon() pid = os.getpid() pyDaemonModule.create_pid('wazuh-apid', pid) or register( pyDaemonModule.delete_pid, 'wazuh-apid', pid) if foreground: print(f"Starting API in foreground (pid: {pid})") # Load the SPEC file into memory to use as a reference for future calls wazuh.security.load_spec() # Set up API asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) app = connexion.AioHttpApp(__name__, host=api_conf['host'], port=api_conf['port'], specification_dir=os.path.join( api_path[0], 'spec'), options={ "swagger_ui": False, 'uri_parser_class': APIUriParser }, only_one_api=True) app.add_api('spec.yaml', arguments={ 'title': 'Wazuh API', 'protocol': 'https' if api_conf['https']['enabled'] else 'http', 'host': api_conf['host'], 'port': api_conf['port'] }, strict_validation=True, validate_responses=False, pass_context_arg_name='request', options={ "middlewares": [ response_postprocessing, set_user_name, security_middleware, request_logging, set_secure_headers ] }) # Maximum body size that the API can accept (bytes) app.app._client_max_size = configuration.api_conf['max_upload_size'] # Enable CORS if api_conf['cors']['enabled']: import aiohttp_cors cors = aiohttp_cors.setup( app.app, defaults={ api_conf['cors']['source_route']: aiohttp_cors.ResourceOptions( expose_headers=api_conf['cors']['expose_headers'], allow_headers=api_conf['cors']['allow_headers'], allow_credentials=api_conf['cors']['allow_credentials']) }) # Configure CORS on all endpoints. for route in list(app.app.router.routes()): cors.add(route) # Enable cache plugin if api_conf['cache']['enabled']: setup_cache(app.app) # Add application signals app.app.on_response_prepare.append(modify_response_headers) # API configuration logging logger.debug(f'Loaded API configuration: {api_conf}') logger.debug(f'Loaded security API configuration: {security_conf}') # Start API app.run(port=api_conf['port'], host=api_conf['host'], ssl_context=ssl_context, access_log_class=alogging.AccessLogger, use_default_access_log=True)
async def application(): logger = get_logger(__name__) app = web.Application(logger=logger) mysql_config = config.api_server.mysql await init_db( host=mysql_config.host, port=mysql_config.port, user=mysql_config.user, password=mysql_config.password, db=mysql_config.database, ) mongo_config = config.api_server.mongo mongo_client = motor.motor_asyncio.AsyncIOMotorClient( f'mongodb://{mongo_config.user}:{mongo_config.password}' f'@{mongo_config.host}:{mongo_config.port}' )[mongo_config.database] task_queue_config = config.api_server.task_queue task_queue_pool = await aiomysql.create_pool( host=task_queue_config.host, port=task_queue_config.port, user=task_queue_config.user, password=task_queue_config.password, db=task_queue_config.database, autocommit=False, ) notification_task_queue_repository = TaskRepository( pool=task_queue_pool, topic_name='NOTIFICATION_TOPIC', ) await notification_task_queue_repository.initialize() notification_task_queue = TasksDispatcher( repository=notification_task_queue_repository, ) resource_list = { '/devices': DevicesHttpResource( device_dispatcher=DeviceDispatcher( database=mongo_client ) ), '/notifications': NotificationsHttpResource( device_dispatcher=DeviceDispatcher( database=mongo_client ), notification_task_queue=notification_task_queue, ), '/internal': InternalHttpResource( internal_api_keys=config.api_server.internal_api_keys, ), } for path, resource in resource_list.items(): resource: AbstractResource = resource # NOTE(pjongy): For type hinting resource.route() plugin_app(app, path, resource.app) cors = aiohttp_cors.setup(app) allow_url = '*' for route in list(app.router.routes()): cors.add( route, { allow_url: aiohttp_cors.ResourceOptions( allow_credentials=True, allow_headers='*', allow_methods=[route.method] ) } ) return app
if options.database_audio_folder is None and options.database_video_folder is None: print( "--database-audio-folder and/or --database-video-folder should be given." ) exit() if options.chainAPI is None: print("--chainAPI should be given.") exit() manager.cron_recordings_list(options.chainAPI) manager.pq_disconnect() elif options.database_new_recordings: manager.process_new_recordings(options.database_new_recordings) manager.pq_disconnect() else: app = web.Application() cors = aiohttp_cors.setup(app, defaults={ "tidmarsh.media.mit.edu": aiohttp_cors.ResourceOptions(), }) manager.start(options.port) manager.pq_disconnect() App.ok(0, "exit.") os.kill(os.getpid(), signal.SIGKILL)
async def make_application(self) -> web.Application: """Get the aiohttp application instance.""" middlewares = [ ready_middleware, debug_middleware, validation_middleware ] # admin-token and admin-token are mutually exclusive and required. # This should be enforced during parameter parsing but to be sure, # we check here. assert self.admin_insecure_mode ^ bool(self.admin_api_key) def is_unprotected_path(path: str): return (path in [ "/api/doc", "/api/docs/swagger.json", "/favicon.ico", "/ws", # ws handler checks authentication ] or path.startswith("/static/swagger/")) # If admin_api_key is None, then admin_insecure_mode must be set so # we can safely enable the admin server with no security if self.admin_api_key: @web.middleware async def check_token(request: web.Request, handler): header_admin_api_key = request.headers.get("x-api-key") valid_key = self.admin_api_key == header_admin_api_key if valid_key or is_unprotected_path(request.path): return await handler(request) else: raise web.HTTPUnauthorized() middlewares.append(check_token) collector = self.context.inject(Collector, required=False) if self.multitenant_manager: @web.middleware async def check_multitenant_authorization(request: web.Request, handler): authorization_header = request.headers.get("Authorization") path = request.path is_multitenancy_path = path.startswith("/multitenancy") is_server_path = path in self.server_paths or path == "/features" # subwallets are not allowed to access multitenancy routes if authorization_header and is_multitenancy_path: raise web.HTTPUnauthorized() # base wallet is not allowed to perform ssi related actions. # Only multitenancy and general server actions if (not authorization_header and not is_multitenancy_path and not is_server_path and not is_unprotected_path(path)): raise web.HTTPUnauthorized() return await handler(request) middlewares.append(check_multitenant_authorization) @web.middleware async def setup_context(request: web.Request, handler): authorization_header = request.headers.get("Authorization") profile = self.root_profile # Multitenancy context setup if self.multitenant_manager and authorization_header: try: bearer, _, token = authorization_header.partition(" ") if bearer != "Bearer": raise web.HTTPUnauthorized( reason="Invalid Authorization header structure") profile = await self.multitenant_manager.get_profile_for_token( self.context, token) except MultitenantManagerError as err: raise web.HTTPUnauthorized(reason=err.roll_up) except (jwt.InvalidTokenError, StorageNotFoundError): raise web.HTTPUnauthorized() # Create a responder with the request specific context responder = AdminResponder( profile, self.outbound_message_router, self.send_webhook, ) profile.context.injector.bind_instance(BaseResponder, responder) # TODO may dynamically adjust the profile used here according to # headers or other parameters admin_context = AdminRequestContext(profile) request["context"] = admin_context request["outbound_message_router"] = responder.send if collector: handler = collector.wrap_coro(handler, [handler.__qualname__]) if self.task_queue: task = await self.task_queue.put(handler(request)) return await task return await handler(request) middlewares.append(setup_context) app = web.Application(middlewares=middlewares) server_routes = [ web.get("/", self.redirect_handler, allow_head=False), web.get("/plugins", self.plugins_handler, allow_head=False), web.get("/status", self.status_handler, allow_head=False), web.post("/status/reset", self.status_reset_handler), web.get("/status/live", self.liveliness_handler, allow_head=False), web.get("/status/ready", self.readiness_handler, allow_head=False), web.get("/shutdown", self.shutdown_handler, allow_head=False), web.get("/ws", self.websocket_handler, allow_head=False), ] # Store server_paths for multitenant authorization handling self.server_paths = [route.path for route in server_routes] app.add_routes(server_routes) plugin_registry = self.context.inject(PluginRegistry, required=False) if plugin_registry: await plugin_registry.register_admin_routes(app) cors = aiohttp_cors.setup( app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", allow_methods="*", ) }, ) for route in app.router.routes(): cors.add(route) # get agent label agent_label = self.context.settings.get("default_label") version_string = f"v{__version__}" setup_aiohttp_apispec(app=app, title=agent_label, version=version_string, swagger_path="/api/doc") app.on_startup.append(self.on_startup) # ensure we always have status values app._state["ready"] = False app._state["alive"] = False return app
async def make_application(self) -> web.Application: """Get the aiohttp application instance.""" middlewares = [] admin_api_key = self.context.settings.get("admin.admin_api_key") admin_insecure_mode = self.context.settings.get( "admin.admin_insecure_mode") # admin-token and admin-token are mutually exclusive and required. # This should be enforced during parameter parsing but to be sure, # we check here. assert admin_insecure_mode or admin_api_key assert not (admin_insecure_mode and admin_api_key) # If admin_api_key is None, then admin_insecure_mode must be set so # we can safely enable the admin server with no security if admin_api_key: @web.middleware async def check_token(request, handler): header_admin_api_key = request.headers.get("x-api-key") if not header_admin_api_key: raise web.HTTPUnauthorized() if admin_api_key == header_admin_api_key: return await handler(request) else: raise web.HTTPUnauthorized() middlewares.append(check_token) if self.task_queue: @web.middleware async def apply_limiter(request, handler): task = await self.task_queue.put(handler(request)) return await task middlewares.append(apply_limiter) stats: Collector = await self.context.inject(Collector, required=False) if stats: @web.middleware async def collect_stats(request, handler): handler = stats.wrap_coro( handler, [handler.__qualname__, "any-admin-request"]) return await handler(request) middlewares.append(collect_stats) app = web.Application(middlewares=middlewares) app["request_context"] = self.context app["outbound_message_router"] = self.responder.send app.add_routes([ web.get("/", self.redirect_handler), web.get("/plugins", self.plugins_handler), web.get("/status", self.status_handler), web.post("/status/reset", self.status_reset_handler), web.get("/ws", self.websocket_handler), ]) plugin_registry: PluginRegistry = await self.context.inject( PluginRegistry, required=False) if plugin_registry: await plugin_registry.register_admin_routes(app) cors = aiohttp_cors.setup( app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", allow_methods="*", ) }, ) for route in app.router.routes(): cors.add(route) # get agent label agent_label = self.context.settings.get("default_label"), version_string = f"v{__version__}" setup_aiohttp_apispec(app=app, title=agent_label, version=version_string, swagger_path="/api/doc") app.on_startup.append(self.on_startup) return app
def get_app(loop=None): app = web.Application(loop=loop) # Setup middlewares setup_middlewares(app) # Allow Web Application calls. cors = aiohttp_cors.setup(app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", ) }) # Home cors.add(app.router.add_get('/', home.redirect)) # Redirects to /v1/ cors.add(app.router.add_get('/v1', home.redirect)) # Redirects to /v1/ cors.add(app.router.add_get('/v1/', home.index)) # Utilities cors.add( app.router.add_get('/contribute.json', utilities.contribute_redirect)) cors.add( app.router.add_get('/v1/contribute.json', utilities.contribute_json)) cors.add(app.router.add_get('/v1/__api__', utilities.oas_spec)) cors.add(app.router.add_get('/v1/__version__', utilities.version)) # Heartbeat cors.add(app.router.add_get('/v1/__heartbeat__', utilities.heartbeat)) cors.add(app.router.add_get('/v1/__lbheartbeat__', utilities.lbheartbeat)) # Statuses cors.add(app.router.add_get('/v1/{product}', release.view_get_releases)) cors.add( app.router.add_get('/v1/{product}/ongoing-versions', product.get_ongoing_versions)) cors.add( app.router.add_get('/v1/{product}/{version}', release.view_get_checks)) cors.add( app.router.add_get('/v1/{product}/{version}/archive', release.archive, name="archive")) cors.add( app.router.add_get('/v1/{product}/{version}/archive/partner-repacks', release.partner_repacks, name="partner-repacks")) cors.add( app.router.add_get('/v1/{product}/{version}/bedrock/release-notes', release.bedrock_release_notes, name="release-notes")) cors.add( app.router.add_get( '/v1/{product}/{version}/bedrock/security-advisories', release.bedrock_security_advisories, name="security-advisories")) cors.add( app.router.add_get('/v1/{product}/{version}/bedrock/download-links', release.bedrock_download_links, name="download-links")) cors.add( app.router.add_get('/v1/{product}/{version}/product-details', release.product_details, name="product-details")) cors.add( app.router.add_get( '/v1/{product}/{version}/product-details' '/devedition-beta-versions-matches', release.devedition_beta_check, name="devedition-beta-matches")) cors.add( app.router.add_get('/v1/{product}/{version}/balrog-rules', release.balrog_rules, name="balrog-rules")) cors.add( app.router.add_get('/v1/{product}/{version}/bouncer', release.bouncer_download_links, name="bouncer")) cors.add( app.router.add_get('/v1/{product}/{version}/buildhub', release.buildhub_check, name="buildhub")) cors.add( app.router.add_get( '/v1/{product}/{version}/telemetry/main-summary-uptake', release.telemetry_uptake, name="telemetry-main-summary-uptake")) # Swagger UI and documentation setup_swagger(app, swagger_url="/v1/api/doc", swagger_from_file=os.path.join(HERE, "api.yaml")) return app