def initialize_scan(urllist: UrlList, manual_or_scheduled: str = "scheduled"):
    # We need to store the scan type in the InternetNLV2Scan at creation, because the type in the list might change:
    translated_scan_types = {'web': 'web', 'mail': 'mail_dashboard'}
    new_scan = InternetNLV2Scan()
    new_scan.type = translated_scan_types[urllist.scan_type]
    new_scan.save()
    internet_nl_v2_websecmap.update_state(
        new_scan, "requested and empty",
        "requested a scan to be performed on internet.nl api")

    accountinternetnlscan = AccountInternetNLScan()
    accountinternetnlscan.account = urllist.account
    accountinternetnlscan.urllist = urllist
    accountinternetnlscan.started_on = datetime.now(pytz.utc)
    accountinternetnlscan.scan = new_scan
    accountinternetnlscan.state = ""
    accountinternetnlscan.save()

    # and start the process.
    update_state("requested", accountinternetnlscan)

    # Sprinkling an activity stream action.
    action.send(urllist.account,
                verb=f'started {manual_or_scheduled} scan',
                target=accountinternetnlscan,
                public=False)

    return accountinternetnlscan
def update_state(state: str, scan: AccountInternetNLScan) -> None:
    """Update the current scan state. Also write it to the scan log. From this log we should also be able to see
    retries... when celery retries on exceptions etc..."""

    # if the state is still the same, just update the last_check, don't append the log.
    # Don't get it from the scan object, that info might be obsolete.
    last_state_for_scan = AccountInternetNLScanLog.objects.all().filter(
        scan=scan).order_by("-at_when").first()

    if last_state_for_scan:
        if last_state_for_scan.state == state:
            return

    # do not update a cancelled scan (#159), even if a certain task has finished after a cancel was issued (letting the
    # task overwriting the cancelled state, continuing the scan)
    if last_state_for_scan == "cancelled":
        return

    # First state, or a new state.
    # New log message:
    scan.state = state
    scan.state_changed_on = timezone.now()
    scan.save()

    scanlog = AccountInternetNLScanLog()
    scanlog.scan = scan
    scanlog.at_when = timezone.now()
    scanlog.state = state
    scanlog.save()
Exemple #3
0
def create_scan(internal_scan_type: str,
                urllist: UrlList,
                manual_or_scheduled: str = "scheduled") -> int:
    new_scan = InternetNLV2Scan()
    new_scan.type = internal_scan_type
    new_scan.save()
    internet_nl_v2_websecmap.update_state(
        new_scan.id, "requested and empty",
        "requested a scan to be performed on internet.nl api")

    # We need to store the scan type in the InternetNLV2Scan at creation, because the type in the list might change:
    accountinternetnlscan = AccountInternetNLScan()
    accountinternetnlscan.account = urllist.account
    accountinternetnlscan.urllist = urllist
    accountinternetnlscan.started_on = datetime.now(pytz.utc)
    accountinternetnlscan.scan = new_scan
    accountinternetnlscan.state = ""
    accountinternetnlscan.save()

    # and start the process.
    update_state("requested", accountinternetnlscan.id)

    # Sprinkling an activity stream action.
    action.send(urllist.account,
                verb=f'started {manual_or_scheduled} scan',
                target=accountinternetnlscan,
                public=False)

    return accountinternetnlscan.id