def delete_plan(plan_name): plan = plans.find_one({'name': plan_name}) if not plan: return jsonify(success=False, reason='no-such-plan') # Remove the plan plans.remove({'name': plan_name}) return jsonify(success=True)
def get_plan(plan_name): plan = plans.find_one({"name": plan_name}) if not plan: return jsonify(success=False, reason='no-such-plan') # Fill in the details of the plugin for step in plan['workflow']: plugin = plugins.get(step['plugin_name']) return jsonify(success=True, plan=sanitize_plan(plan))
def update_plan(plan_name): new_plan = request.json # Find the existing plan old_plan = plans.find_one({'name': plan_name}) if old_plan is None: return jsonify(success=False, reason='unknown-plan') if not _check_plan_workflow(new_plan['workflow']): return jsonify(success=False, reason='invalid-plan') # Update the plan changes = {} if 'description' in new_plan: changes['description'] = new_plan['description'] if 'workflow' in new_plan: changes['workflow'] = new_plan['workflow'] plans.update({'name': plan_name}, {'$set': changes}) # Return the plan plan = plans.find_one({"name": plan_name}) return jsonify(success=True, plan=sanitize_plan(plan))
def _check_plan_by_email(email, plan_name): plan = plans.find_one({'name': plan_name}) if not plan: return False sitez = sites.find({'plans': plan_name}) if sitez.count(): matches = 0 for site in sitez: groupz = groups.find({'users': email, 'sites': site['url']}) if groupz.count(): matches += 1 return matches
def create_plan(): plan = request.json # Verify incoming plan if plans.find_one({'name': plan['name']}) is not None: return jsonify(success=False, reason='plan-already-exists') if not _check_plan_workflow(plan['workflow']): return jsonify(success=False, reason='invalid-plan-exists') # Create the plan new_plan = { 'name': plan['name'], 'description': plan['description'], 'workflow': plan['workflow'], 'created': datetime.datetime.utcnow() } plans.insert(new_plan) # Return the new plan plan = plans.find_one({"name": plan['name']}) if not plan: return jsonify(success=False) return jsonify(success=True, plan=sanitize_plan(plan))
def post_scan_create(): # try to decode the configuration configuration = request.json # See if the plan exists plan = plans.find_one({"name": configuration['plan']}) if not plan: return jsonify(success=False) # Merge the configuration # Create a scan object now = datetime.datetime.utcnow() scan = { "id": str(uuid.uuid4()), "state": "CREATED", "created": now, "queued": None, "started": None, "finished": None, "plan": { "name": plan['name'], "revision": 0 }, "configuration": configuration['configuration'], "sessions": [], "meta": { "user": configuration['user'], "tags": [] } } for step in plan['workflow']: session_configuration = step['configuration'] session_configuration.update(configuration['configuration']) session = { "id": str(uuid.uuid4()), "state": "CREATED", "plugin": plugins[step['plugin_name']]['descriptor'], "configuration": session_configuration, # TODO Do recursive merging here, not just at the top level "description": step["description"], "artifacts": {}, "issues": [], "created": now, "queued": None, "started": None, "finished": None, "progress": None } scan['sessions'].append(session) scans.insert(scan) return jsonify(success=True, scan=sanitize_scan(scan))
def update_plan(plan_name): if not get_plan_by_plan_name(plan_name): return jsonify(success=True) new_plan = request.json if not _check_plan_workflow(new_plan['workflow']): return jsonify(success=False, reason='invalid-plan') # Update the plan changes = {} if 'description' in new_plan: changes['description'] = new_plan['description'] if 'workflow' in new_plan: changes['workflow'] = new_plan['workflow'] plans.update({'name': plan_name}, {'$set': changes}) # Return the plan plan = plans.find_one({"name": plan_name}) return jsonify(success=True, plan=sanitize_plan(plan))
def post_scan_create(): # try to decode the configuration configuration = request.json # See if the plan exists plan = plans.find_one({"name": configuration["plan"]}) if not plan: return jsonify(success=False) # Merge the configuration # Create a scan object now = datetime.datetime.utcnow() scan = { "id": str(uuid.uuid4()), "state": "CREATED", "created": now, "queued": None, "started": None, "finished": None, "plan": {"name": plan["name"], "revision": 0}, "configuration": configuration["configuration"], "sessions": [], "meta": {"user": configuration["user"], "tags": []}, } for step in plan["workflow"]: session_configuration = step["configuration"] session_configuration.update(configuration["configuration"]) session = { "id": str(uuid.uuid4()), "state": "CREATED", "plugin": plugins[step["plugin_name"]]["descriptor"], "configuration": session_configuration, # TODO Do recursive merging here, not just at the top level "description": step["description"], "artifacts": {}, "issues": [], "created": now, "queued": None, "started": None, "finished": None, "progress": None, } scan["sessions"].append(session) scans.insert(scan) return jsonify(success=True, scan=sanitize_scan(scan))
def get_plan_by_plan_name(plan_name): return plans.find_one({'name': plan_name})
def _check_plan_exists(plan_name): return plans.find_one({'name': plan_name}) is not None