def add_domain(request): """ Add a domain. Should be an AJAX POST. :param request: Django request. :type request: :class:`django.http.HttpRequest` :returns: :class:`django.http.HttpResponse` """ if request.is_ajax() and request.method == "POST": add_form = AddDomainForm(request.user, request.POST) result = False retVal = {} errors = [] if add_form.is_valid(): #form is valid, but we may still have post-validation errors errors = add_form._errors.setdefault("domain", ErrorList()) data = add_form.cleaned_data (result, errors, retVal) = add_new_domain(data, request, errors) if not result: retVal['form'] = add_form.as_table() if errors: if not 'message' in retVal: retVal['message'] = "" elif not isinstance(retVal['message'], str): retVal['message'] = str(retVal['message']) for e in errors: retVal['message'] += '<div>' + str(e) + '</div>' retVal['success'] = result return HttpResponse(json.dumps(retVal, default=json_handler), mimetype="application/json") else: return render_to_response("error.html", {"error": 'Expected POST'}, RequestContext(request))
def add_domain(request): """ Add a domain. Should be an AJAX POST. :param request: Django request. :type request: :class:`django.http.HttpRequest` :returns: :class:`django.http.HttpResponse` """ if request.is_ajax() and request.method == "POST": add_form = AddDomainForm(request.user, request.POST) result = False retVal = {} errors = [] if add_form.is_valid(): # form is valid, but we may still have post-validation errors errors = add_form._errors.setdefault("domain", ErrorList()) data = add_form.cleaned_data (result, errors, retVal) = add_new_domain(data, request, errors) if not result: retVal["form"] = add_form.as_table() if errors: if not "message" in retVal: retVal["message"] = "" elif not isinstance(retVal["message"], str): retVal["message"] = str(retVal["message"]) for e in errors: retVal["message"] += "<div>" + str(e) + "</div>" retVal["success"] = result return HttpResponse(json.dumps(retVal, default=json_handler), mimetype="application/json") else: return render_to_response("error.html", {"error": "Expected POST"}, RequestContext(request))
def add_domain(request): """ Add a domain. Should be an AJAX POST. :param request: Django request. :type request: :class:`django.http.HttpRequest` :returns: :class:`django.http.HttpResponse` """ if request.is_ajax() and request.method == "POST": add_form = AddDomainForm(request.user, request.POST) result = False retVal = {} errors = [] user = request.user if add_form.is_valid(): errors = [] data = add_form.cleaned_data if user.has_access_to(DomainACL.WRITE): (result, errors, retVal) = add_new_domain(data, request, errors) else: result = {'success':False, 'message':'User does not have permission to add Domain.'} return HttpResponse(json.dumps(result, default=json_handler), content_type="application/json") if errors: if not 'message' in retVal: retVal['message'] = "" elif not isinstance(retVal['message'], str): retVal['message'] = str(retVal['message']) for e in errors: if 'Domain' in e or 'TLD' in e: dom_form_error = add_form._errors.setdefault("domain", ErrorList()) dom_form_error.append('Invalid Domain') elif 'IP' in e: ip_form_error = add_form._errors.setdefault("ip", ErrorList()) ip_form_error.append('Invalid IP') retVal['message'] += '<div>' + str(e) + '</div>' if not result: retVal['form'] = add_form.as_table() retVal['success'] = result return HttpResponse(json.dumps(retVal, default=json_handler), content_type="application/json") else: return render_to_response("error.html", {"error" : 'Expected POST' }, RequestContext(request))
def add_domain(request): """ Add a domain. Should be an AJAX POST. :param request: Django request. :type request: :class:`django.http.HttpRequest` :returns: :class:`django.http.HttpResponse` """ if request.is_ajax() and request.method == "POST": add_form = AddDomainForm(request.user, request.POST) result = False retVal = {} errors = [] user = request.user if add_form.is_valid(): errors = [] data = add_form.cleaned_data if user.has_access_to(DomainACL.WRITE): (result, errors, retVal) = add_new_domain(data, request, errors) else: result = { 'success': False, 'message': 'User does not have permission to add Domain.' } return HttpResponse(json.dumps(result, default=json_handler), content_type="application/json") if errors: if not 'message' in retVal: retVal['message'] = "" elif not isinstance(retVal['message'], str): retVal['message'] = str(retVal['message']) for e in errors: if 'Domain' in e or 'TLD' in e: dom_form_error = add_form._errors.setdefault( "domain", ErrorList()) dom_form_error.append('Invalid Domain') elif 'IP' in e: ip_form_error = add_form._errors.setdefault( "ip", ErrorList()) ip_form_error.append('Invalid IP') retVal['message'] += '<div>' + str(e) + '</div>' if not result: retVal['form'] = add_form.as_table() retVal['success'] = result return HttpResponse(json.dumps(retVal, default=json_handler), content_type="application/json") else: return render_to_response("error.html", {"error": 'Expected POST'}, RequestContext(request))
def bulk_add_domain(request): """ Bulk add domains via a bulk upload form. Args: request: The Django context which contains information about the session and key/value pairs for the bulk add domains request Returns: If the request is not a POST and not a Ajax call then: Returns a rendered HTML form for a bulk add of domains If the request is a POST and a Ajax call then: Returns a response that contains information about the status of the bulk uploaded domains. This may include information such as domains that failed or successfully added. This may also contain helpful status messages about each operation. """ formdict = form_to_dict(AddDomainForm(request.user)) user = request.user if request.method == "POST" and request.is_ajax(): if user.has_access_to(DomainACL.WRITE): response = process_bulk_add_domain(request, formdict) else: response = { "success": False, "message": "User does not have permission to add domains." } return HttpResponse(json.dumps(response, default=json_handler), content_type="application/json") else: if user.has_access_to(DomainACL.WRITE): objectformdict = form_to_dict(AddObjectForm(request.user)) return render_to_response( 'bulk_add_default.html', { 'formdict': formdict, 'objectformdict': objectformdict, 'title': "Bulk Add Domains", 'table_name': 'domain', 'local_validate_columns': [form_consts.Domain.DOMAIN_NAME], 'custom_js': "domain_handsontable.js", 'is_bulk_add_objects': True }, RequestContext(request)) else: response = { "success": False, "message": "User does not have permission to add domains." } return HttpResponse(json.dumps(response, default=json_handler), content_type="application/json")
def bulk_add_domain(request): """ Bulk add domains via a bulk upload form. Args: request: The Django context which contains information about the session and key/value pairs for the bulk add domains request Returns: If the request is not a POST and not a Ajax call then: Returns a rendered HTML form for a bulk add of domains If the request is a POST and a Ajax call then: Returns a response that contains information about the status of the bulk uploaded domains. This may include information such as domains that failed or successfully added. This may also contain helpful status messages about each operation. """ all_obj_type_choices = [(c[0], c[0], {'datatype':c[1].keys()[0], 'datatype_value':c[1].values()[0]} ) for c in get_object_types(False)] formdict = form_to_dict(AddDomainForm(request.user)) if request.method == "POST" and request.is_ajax(): response = process_bulk_add_domain(request, formdict); return HttpResponse(json.dumps(response, default=json_handler), mimetype='application/json') else: objectformdict = form_to_dict(AddObjectForm(request.user, all_obj_type_choices)) return render_to_response('bulk_add_default.html', {'formdict': formdict, 'objectformdict': objectformdict, 'title': "Bulk Add Domains", 'table_name': 'domain', 'local_validate_columns': [form_consts.Domain.DOMAIN_NAME], 'custom_js': "domain_handsontable.js", 'is_bulk_add_objects': True}, RequestContext(request));
def parse_row_to_bound_domain_form(request, rowData, cache): """ Parse a row in bulk upload into form data that can be used to add a Domain. :param request: Django request. :type request: :class:`django.http.HttpRequest` :param rowData: The objects to add for the Domain. :type rowData: dict :param cache: Cached data, typically for performance enhancements during bulk uperations. :type cache: dict :returns: :class:`crits.domains.forms.AddDomainForm` """ bound_domain_form = None # TODO Add common method to convert data to string domain_name = rowData.get(form_consts.Domain.DOMAIN_NAME, "").strip() campaign = rowData.get(form_consts.Domain.CAMPAIGN, "") confidence = rowData.get(form_consts.Domain.CAMPAIGN_CONFIDENCE, "") domain_source = rowData.get(form_consts.Domain.DOMAIN_SOURCE, "") domain_method = rowData.get(form_consts.Domain.DOMAIN_METHOD, "") domain_reference = rowData.get(form_consts.Domain.DOMAIN_REFERENCE, "") #is_add_ip = convert_string_to_bool(rowData.get(form_consts.Domain.ADD_IP_ADDRESS, "")) is_add_ip = False ip = rowData.get(form_consts.Domain.IP_ADDRESS, "") created = rowData.get(form_consts.Domain.IP_DATE, "") #is_same_source = convert_string_to_bool(rowData.get(form_consts.Domain.SAME_SOURCE, "False")) is_same_source = False ip_source = rowData.get(form_consts.Domain.IP_SOURCE, "") ip_method = rowData.get(form_consts.Domain.IP_METHOD, "") ip_reference = rowData.get(form_consts.Domain.IP_REFERENCE, "") is_add_indicator = convert_string_to_bool( rowData.get(form_consts.Domain.ADD_INDICATOR, "False")) bucket_list = rowData.get(form_consts.Common.BUCKET_LIST, "") ticket = rowData.get(form_consts.Common.TICKET, "") if (ip or created or ip_source or ip_method or ip_reference): is_add_ip = True if is_add_ip == True: data = { 'domain': domain_name, 'campaign': campaign, 'confidence': confidence, 'domain_source': domain_source, 'domain_method': domain_method, 'domain_reference': domain_reference, 'add_ip': is_add_ip, 'ip': ip, 'created': created, 'same_source': is_same_source, 'ip_source': ip_source, 'ip_method': ip_method, 'ip_reference': ip_reference, 'add_indicators': is_add_indicator, 'bucket_list': bucket_list, 'ticket': ticket } bound_domain_form = cache.get("domain_ip_form") if bound_domain_form == None: bound_domain_form = AddDomainForm(request.user, data) cache['domain_ip_form'] = bound_domain_form else: bound_domain_form.data = data else: data = { 'domain': domain_name, 'campaign': campaign, 'confidence': confidence, 'domain_source': domain_source, 'domain_method': domain_method, 'domain_reference': domain_reference, 'add_ip': is_add_ip, 'bucket_list': bucket_list, 'ticket': ticket } bound_domain_form = cache.get("domain_form") if bound_domain_form == None: bound_domain_form = AddDomainForm(request.user, data) cache['domain_form'] = bound_domain_form else: bound_domain_form.data = data if bound_domain_form != None: bound_domain_form.full_clean() return bound_domain_form
def parse_row_to_bound_domain_form(request, rowData, cache): """ Parse a row in bulk upload into form data that can be used to add a Domain. :param request: Django request. :type request: :class:`django.http.HttpRequest` :param rowData: The objects to add for the Domain. :type rowData: dict :param cache: Cached data, typically for performance enhancements during bulk uperations. :type cache: dict :returns: :class:`crits.domains.forms.AddDomainForm` """ bound_domain_form = None # TODO Add common method to convert data to string domain_name = rowData.get(form_consts.Domain.DOMAIN_NAME, "").strip(); campaign = rowData.get(form_consts.Domain.CAMPAIGN, "") confidence = rowData.get(form_consts.Domain.CAMPAIGN_CONFIDENCE, "") domain_source = rowData.get(form_consts.Domain.DOMAIN_SOURCE, "") domain_method = rowData.get(form_consts.Domain.DOMAIN_METHOD, "") domain_reference = rowData.get(form_consts.Domain.DOMAIN_REFERENCE, "") #is_add_ip = convert_string_to_bool(rowData.get(form_consts.Domain.ADD_IP_ADDRESS, "")) is_add_ip = False ip = rowData.get(form_consts.Domain.IP_ADDRESS, "") ip_type = rowData.get(form_consts.Domain.IP_TYPE, "") created = rowData.get(form_consts.Domain.IP_DATE, "") #is_same_source = convert_string_to_bool(rowData.get(form_consts.Domain.SAME_SOURCE, "False")) is_same_source = False ip_source = rowData.get(form_consts.Domain.IP_SOURCE, "") ip_method = rowData.get(form_consts.Domain.IP_METHOD, "") ip_reference = rowData.get(form_consts.Domain.IP_REFERENCE, "") is_add_indicators = convert_string_to_bool(rowData.get(form_consts.Domain.ADD_INDICATORS, "False")) bucket_list = rowData.get(form_consts.Common.BUCKET_LIST, "") ticket = rowData.get(form_consts.Common.TICKET, "") if(ip or created or ip_source or ip_method or ip_reference): is_add_ip = True if is_add_ip == True: data = {'domain': domain_name, 'campaign': campaign, 'confidence': confidence, 'domain_source': domain_source, 'domain_method': domain_method, 'domain_reference': domain_reference, 'add_ip': is_add_ip, 'ip': ip, 'ip_type': ip_type, 'created': created, 'same_source': is_same_source, 'ip_source': ip_source, 'ip_method': ip_method, 'ip_reference': ip_reference, 'add_indicators': is_add_indicators, 'bucket_list': bucket_list, 'ticket': ticket} bound_domain_form = cache.get("domain_ip_form") if bound_domain_form == None: bound_domain_form = AddDomainForm(request.user, data) cache['domain_ip_form'] = bound_domain_form else: bound_domain_form.data = data else: data = {'domain': domain_name, 'campaign': campaign, 'confidence': confidence, 'domain_source': domain_source, 'domain_method': domain_method, 'domain_reference': domain_reference, 'add_ip': is_add_ip, 'bucket_list': bucket_list, 'ticket': ticket} bound_domain_form = cache.get("domain_form") if bound_domain_form == None: bound_domain_form = AddDomainForm(request.user, data) cache['domain_form'] = bound_domain_form else: bound_domain_form.data = data if bound_domain_form != None: bound_domain_form.full_clean() return bound_domain_form