Example #1
0
def get_reports_sites():
    result = []
    group_name = request.args.get('group_name')
    user_email = request.args.get('user')

    if user_email is not None:
        # User specified, so return recent scans for each site/plan that the user can see
        user = User.get_user(user_email)
        if user is None:
            return jsonify(success=False, reason='no-such-user')
        if group_name:
            group = Group.get_group(group_name)
            if group is None:
                return jsonify(success=False, reason='no-such-group')

            site_list = map(lambda x: x.url, group.sites)
        else:
            site_list = user.sites()
        for site_url in sorted(site_list):
            site = Site.get_site_by_url(site_url)
            if site is not None:
                for plan in site.plans:
                    plan_name = plan.name
                    schedule = ScanSchedule.get_schedule(site.site_uuid, plan.plan_uuid)

                    crontab = None
                    scheduleEnabled = False
                    if schedule is not None:
                        crontab = schedule['crontab']
                        scheduleEnabled = schedule['enabled']

                    scans = []
                    for scan in site.scans:
                        if scan.plan is not None:
                            p = json.loads(scan.plan)
                            if p['name'] == plan_name:
                                scans.append(scan)                    

                    scan_for_site = []
                    for scan in scans:
                        config = json.loads(scan.configuration)
                        
                        if config.get('target', None) == site_url:
                            scan_for_site.append(scan)


                    o = list(sorted(scan_for_site, cmp= lambda x, y: cmp(x.created, y, created)))
                    if len(o):
                     l = [o[0]]
                    else:
                     l = []         
                    
                    if len(l) == 1:
                        scan = summarize_scan(l[0])
                        s = {v: scan.get(v) for v in ('id', 'created', 'state', 'issues')}
                        result.append({'target': site_url, 'plan': plan_name, 'scan': scan, 'crontab': crontab, 'scheduleEnabled': scheduleEnabled})
                    else:
                        result.append({'target': site_url, 'plan': plan_name, 'scan': None, 'crontab': crontab, 'scheduleEnabled': scheduleEnabled})
    return jsonify(success=True, report=result)
Example #2
0
def scanschedule():
    site = request.json


    scan_id = site.get('scan_id')
    schedule = site.get('schedule')

    plan = site.get('plan')
    target = site.get('target')

    removeSite = schedule.get('remove')
    enabled = True
    crontab = {}
    message = "Scan Schedule not set"

    if removeSite is not None:
        # Removing scan from scanschedule results in incomplete removal because of celerybeat-mongo running in background
        # Hence  we just set "enabled" to false
        enabled = False
        message = "Removed Schedule for: " + target

    else:
        enabled = True
        message="Scheduled Scan successfully set for site: " + target



    crontab = {
      'minute':str(schedule.get('minute')),
      'hour':str(schedule.get('hour')),
      'day_of_week':str(schedule.get('dayOfWeek')),
      'day_of_month':str(schedule.get('dayOfMonth')),
      'month_of_year':str(schedule.get('monthOfYear'))
    }

    # Validate Crontab schedule values
    crontab_errors = check_cron(crontab)
    if crontab_errors:
        message = "Error in crontab values"
        return jsonify(message=message,success=False,errors=crontab_errors)

    data = {
      'task': "minion.backend.tasks.run_scheduled_scan",
      'args': [target, plan],
      'site': target,
      'queue':'scanschedule',
      'routing_key':'scanschedule',
      'exchange':'', #Exchange is not required. Fails sometimes if exchange is provided. #TODO Figure out why
      'plan': plan,
      'name': target + ":" + plan,
      'enabled': enabled,
      'crontab': crontab
    }

    # Insert/Update existing schedule by target and plan
    schedule = ScanSchedule.get_schedule(site, plan)

    schedule = scanschedules.find_one({"site":target, "plan":plan})
    if not schedule:
      schedule = ScanSchedule()
      schedule.data = json.dumps(data)
    else:
      old_data = json.loads(schedule.data)
      schedule.data = json.dumps(old_data.update(data))
      scanschedules.update({"site":target, "plan":plan},
                       {"$set": {"crontab": crontab, "enabled":enabled}});


    return jsonify(message=message,success=True)