def get_by_name(resource_name, resource_type): """ Lookup database for a resource with name "resource_name" if None is found, then create the resource. """ search = "canonical_name" resource_type = ResourceType.get_type_from_string(resource_type) if resource_type == ResourceType.HASH: search = "hash" db = Resource.collection() result = db.find_one({search: resource_name}) if result: return Resource(result["_id"]) # TODO: Legacy method for old database resources # TODO: Get rid of this legacy method if not result: docs = ["ip", "url", "username", "hash", "email", "domain"] for doc in docs: result = DB(doc).collection.find_one({search: resource_name}) if result: return Resource(result["_id"]) print(f"Tried get_by_name nothing found {result}") return None
def create_resource(user): try: resource_name = request.json["resource_name"].strip() resource_type = request.json["resource_type"].lower() resource_type = ResourceType.get_type_from_string(resource_type) resource = Resources.get(resource_name, resource_type, get_by_name=True) project = User(user).get_active_project() project.add_resource(resource) response = [] response.append( { "success_message": f"Added new resource: {resource_name}", "new_resource": resource.to_JSON(), "type": resource.get_type_value(), } ) resource.launch_plugins(project.get_id()) # Deal with the case of URL resources where we have the chance to add a Domain or IP if resource.get_type() == ResourceType.URL: ip_or_domain = urllib.parse.urlparse(resource_name).netloc resource_type = ResourceType.validate_ip_or_domain(ip_or_domain) if ip_or_domain: resource = Resources.get(ip_or_domain, resource_type, get_by_name=True) project.add_resource(resource) response.append( { "success_message": f"Added new resource: {ip_or_domain}", "new_resource": resource.to_JSON(), "type": resource.get_type_value(), } ) resource.launch_plugins(project.get_id()) # TODO: Deal with the case of domain -> IP # TODO: Deal with the case of emails -> domains -> IP return jsonify(response) except ResourceTypeException: return jsonify({"error_message": "Trying to add an unkown resource type"}), 400 except Exception as e: tb1 = traceback.TracebackException.from_exception(e) print("".join(tb1.format())) return jsonify({"error_message": "Server error :("}), 400
def get_type(self): return ResourceType.get_type_from_string(self.resource["resource_type"])