From 95fa51a4035fed7322be3f80909adf08d49ce361 Mon Sep 17 00:00:00 2001 From: db0 Date: Wed, 21 Jun 2023 00:33:37 +0200 Subject: [PATCH] Allow to return as list or csv --- overseer/apis/models/v1.py | 11 ++++------- overseer/apis/v1/base.py | 39 +++++++++----------------------------- 2 files changed, 13 insertions(+), 37 deletions(-) diff --git a/overseer/apis/models/v1.py b/overseer/apis/models/v1.py index 1058cdb..6c9e643 100644 --- a/overseer/apis/models/v1.py +++ b/overseer/apis/models/v1.py @@ -14,11 +14,8 @@ class Models: 'signup': fields.Boolean(default=False,description="True when subscriptions are open, else False"), 'user_post_ratio': fields.Float(description="Users to Post Ratio"), }) - self.input_model_SusInstances_post = api.model('SuspiciousInstancesListInput', { - 'user_to_post_ratio': fields.Integer(default=20,description="The threshold over which to consider instances suspicious."), - 'whitelist': fields.List(fields.String(description="List of domains to avoid returning in the supicion list.")), - 'blacklist': fields.List(fields.String(description="List of domains to append to the supicion list.")), - }) - self.response_model_model_SusInstances_post = api.model('SuspiciousInstancesDomainList', { - 'domains': fields.List(fields.String(description="The domains in shit suspicious list.")), + self.response_model_model_SusInstances_get = api.model('SuspiciousInstancesDomainList', { + 'instances': fields.List(fields.Nested(self.response_model_suspicious_instances)), + 'domains': fields.List(fields.String(description="The suspicious domains as a list.")), + 'csv': fields.String(description="The suspicious domains as a csv."), }) diff --git a/overseer/apis/v1/base.py b/overseer/apis/v1/base.py index e0c8794..59ec62b 100644 --- a/overseer/apis/v1/base.py +++ b/overseer/apis/v1/base.py @@ -19,43 +19,22 @@ def get_request_path(): class SusInstances(Resource): get_parser = reqparse.RequestParser() get_parser.add_argument("Client-Agent", default="unknown:0:unknown", type=str, required=False, help="The client name and version.", location="headers") + get_parser.add_argument("domains", required=False, default=False, type=bool, help="Set to true to return just the domains as a list. Mutually exclusive with csv", location="args") get_parser.add_argument("user_to_post_ratio", required=False, default=20, type=int, help="The amount of local users / amount of local posts to consider suspicious", location="args") + get_parser.add_argument("csv", required=False, default=False, type=bool, help="Set to true to return just the domains as a csv. Mutually exclusive with domains", location="args") @api.expect(get_parser) @logger.catch(reraise=True) @cache.cached(timeout=10, query_string=True) - @api.marshal_with(models.response_model_suspicious_instances, code=200, description='Suspicious Instances', as_list=True, skip_none=True) + @api.marshal_with(models.response_model_model_SusInstances_get, code=200, description='Suspicious Instances', skip_none=True) def get(self): '''A List with the details of all suspicious instances ''' self.args = self.get_parser.parse_args() - return retrieve_suspicious_instances(self.args.user_to_post_ratio),200 - - post_parser = reqparse.RequestParser() - post_parser.add_argument("Client-Agent", default="unknown:0:unknown", type=str, required=False, help="The client name and version.", location="headers") - post_parser.add_argument("user_to_post_ratio", required=False, default=20, type=int, help="The amount of local users / amount of local posts to consider suspicious", location="json") - post_parser.add_argument("whitelist", type=list, required=False, help="Workers to whitelist even if we think they're suspicious", location="json") - post_parser.add_argument("blacklist", type=list, required=False, help="Extra workers to blacklist.", location="json") - - @api.expect(post_parser, models.input_model_SusInstances_post, validate=True) - @logger.catch(reraise=True) - # @cache.cached(timeout=10, query_string=True) - @api.marshal_with(models.response_model_model_SusInstances_post, code=200, description='Suspicious Instances List') - @api.response(400, 'Validation Error', models.response_model_error) - def post(self): - '''A List with just the domains of all suspicious instances - This can then be easily converted into a defederation list - ''' - self.args = self.post_parser.parse_args() - logger.debug(self.args) sus_instances = retrieve_suspicious_instances(self.args.user_to_post_ratio) - final_list = [] - if self.args.blacklist: - final_list = self.args.blacklist - whitelist = [] - if self.args.whitelist: - whitelist = self.args.whitelist - for instance in sus_instances: - if instance["domain"] not in whitelist: - final_list.append(instance["domain"]) - return {"domains": final_list},200 + logger.debug(self.args) + if self.args.csv: + return {"csv": ",".join([instance["domain"] for instance in sus_instances])},200 + if self.args.domains: + return {"domains": [instance["domain"] for instance in sus_instances]},200 + return {"instances": sus_instances},200