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 testDomainAdd(self): data = { 'domain_reference': DOM_REF, 'domain_source': DOM_SRC, 'domain_method': DOM_METH, 'domain': DOMAIN, } errors = [] (result, errors, retVal) = handlers.add_new_domain(data, self, errors)
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 setUp(self): prep_db() self.factory = RequestFactory() self.user = CRITsUser.objects(username=TUSER_NAME).first() self.user.save() # Add a test domain data = { 'domain_reference': DOM_REF, 'domain_source': DOM_SRC, 'domain_method': DOM_METH, 'domain': DOMAIN, } errors = [] (result, errors, retVal) = handlers.add_new_domain(data, self, errors)
def obj_create(self, bundle, **kwargs): """ Handles creating Domains through the API. :param bundle: Bundle containing the information to create the Domain. :type bundle: Tastypie Bundle object. :returns: HttpResponse. """ request = bundle.request # Domain and source information domain = bundle.data.get('domain', None) name = bundle.data.get('source', None) reference = bundle.data.get('reference', None) method = bundle.data.get('method', None) # Campaign information campaign = bundle.data.get('campaign', None) confidence = bundle.data.get('confidence', None) # Also add IP information add_ip = bundle.data.get('add_ip', None) ip = bundle.data.get('ip', None) ip_type = bundle.data.get('ip_type', None) same_source = bundle.data.get('same_source', None) ip_source = bundle.data.get('ip_source', None) ip_method = bundle.data.get('ip_method', None) ip_reference = bundle.data.get('ip_reference', None) # Also add indicators add_indicators = bundle.data.get('add_indicators', None) bucket_list = bundle.data.get('bucket_list', None) ticket = bundle.data.get('ticket', None) data = {'domain_reference': reference, 'domain_source': name, 'domain_method': method, 'confidence': confidence, 'campaign': campaign, 'domain': domain, 'same_source': same_source, 'ip_source': ip_source, 'ip_method': ip_method, 'ip_reference': ip_reference, 'add_ip': add_ip, 'ip': ip, 'ip_type': ip_type, 'add_indicators': add_indicators, 'bucket_list': bucket_list, 'ticket': ticket} content = {'return_code': 1, 'type': 'Domain'} if not domain: content['message'] = 'Need a Domain Name.' self.crits_response(content) # The empty list is necessary. The function requires a list of # non-fatal errors so it can be added to if any other errors # occur. Since we have none, we pass the empty list. (result, errors, retVal) = add_new_domain(data, request, []) if not 'message' in retVal: retVal['message'] = "" elif not isinstance(retVal['message'], basestring): retVal['message'] = str(retVal['message']) if errors: for e in errors: retVal['message'] += " %s " % str(e) obj = retVal.get('object', None) content['message'] = retVal.get('message', '') if obj: content['id'] = str(obj.id) url = reverse('api_dispatch_detail', kwargs={'resource_name': 'domains', 'api_name': 'v1', 'pk': str(obj.id)}) content['url'] = url if result: content['return_code'] = 0 self.crits_response(content)
def obj_create(self, bundle, **kwargs): """ Handles creating Domains through the API. :param bundle: Bundle containing the information to create the Domain. :type bundle: Tastypie Bundle object. :returns: Bundle object. :raises BadRequest: If a domain name is not provided or creation fails. """ analyst = bundle.request.user.username request = bundle.request # Domain and source information domain = bundle.data.get('domain', None) name = bundle.data.get('source', None) reference = bundle.data.get('reference', None) method = bundle.data.get('method', None) # Campaign information campaign = bundle.data.get('campaign', None) confidence = bundle.data.get('confidence', None) # Also add IP information add_ip = bundle.data.get('add_ip', None) ip = bundle.data.get('ip', None) same_source = bundle.data.get('same_source', None) ip_source = bundle.data.get('ip_source', None) ip_method = bundle.data.get('ip_method', None) ip_reference = bundle.data.get('ip_reference', None) # Also add indicators add_indicators = bundle.data.get('add_indicators', None) bucket_list = bundle.data.get('bucket_list', None) ticket = bundle.data.get('ticket', None) data = {'domain_reference': reference, 'domain_source': name, 'domain_method': method, 'confidence': confidence, 'campaign': campaign, 'domain': domain, 'same_source': same_source, 'ip_source': ip_source, 'ip_method': ip_method, 'ip_reference': ip_reference, 'add_ip': add_ip, 'ip': ip, 'add_indicators': add_indicators, 'bucket_list': bucket_list, 'ticket': ticket} if analyst: if not domain: raise BadRequest('Need a Domain Name.') # The empty list is necessary. The function requires a list of # non-fatal errors so it can be added to if any other errors # occur. Since we have none, we pass the empty list. (result, errors, retVal) = add_new_domain(data, request, []) if errors: if not 'message' in retVal: retVal['message'] = "" elif not isinstance(retVal['message'], basestring): retVal['message'] = str(retVal['message']) for e in errors: retVal['message'] += " %s " % str(e) raise BadRequest(retVal['message']) else: return bundle else: raise BadRequest('You must be an authenticated user!')
def obj_create(self, bundle, **kwargs): """ Handles creating Domains through the API. :param bundle: Bundle containing the information to create the Domain. :type bundle: Tastypie Bundle object. :returns: HttpResponse. """ request = bundle.request # Domain and source information domain = bundle.data.get('domain', None) name = bundle.data.get('source', None) reference = bundle.data.get('reference', None) method = bundle.data.get('method', None) # Campaign information campaign = bundle.data.get('campaign', None) confidence = bundle.data.get('confidence', None) # Also add IP information add_ip = bundle.data.get('add_ip', None) ip = bundle.data.get('ip', None) same_source = bundle.data.get('same_source', None) ip_source = bundle.data.get('ip_source', None) ip_method = bundle.data.get('ip_method', None) ip_reference = bundle.data.get('ip_reference', None) # Also add indicators add_indicators = bundle.data.get('add_indicators', None) bucket_list = bundle.data.get('bucket_list', None) ticket = bundle.data.get('ticket', None) data = {'domain_reference': reference, 'domain_source': name, 'domain_method': method, 'confidence': confidence, 'campaign': campaign, 'domain': domain, 'same_source': same_source, 'ip_source': ip_source, 'ip_method': ip_method, 'ip_reference': ip_reference, 'add_ip': add_ip, 'ip': ip, 'add_indicators': add_indicators, 'bucket_list': bucket_list, 'ticket': ticket} content = {'return_code': 1, 'type': 'Domain'} if not domain: content['message'] = 'Need a Domain Name.' self.crits_response(content) # The empty list is necessary. The function requires a list of # non-fatal errors so it can be added to if any other errors # occur. Since we have none, we pass the empty list. (result, errors, retVal) = add_new_domain(data, request, []) if not 'message' in retVal: retVal['message'] = "" elif not isinstance(retVal['message'], basestring): retVal['message'] = str(retVal['message']) if errors: for e in errors: retVal['message'] += " %s " % str(e) obj = retVal.get('object', None) content['message'] = retVal.get('message', '') if obj: content['id'] = str(obj.id) url = reverse('api_dispatch_detail', kwargs={'resource_name': 'domains', 'api_name': 'v1', 'pk': str(obj.id)}) content['url'] = url if result: content['return_code'] = 0 self.crits_response(content)
def obj_create(self, bundle, **kwargs): """ Handles creating Domains through the API. :param bundle: Bundle containing the information to create the Domain. :type bundle: Tastypie Bundle object. :returns: Bundle object. :raises BadRequest: If a domain name is not provided or creation fails. """ analyst = bundle.request.user.username request = bundle.request # Domain and source information domain = bundle.data.get('domain', None) name = bundle.data.get('source', None) reference = bundle.data.get('reference', None) method = bundle.data.get('method', None) # Campaign information campaign = bundle.data.get('campaign', None) confidence = bundle.data.get('confidence', None) # Also add IP information add_ip = bundle.data.get('add_ip', None) ip = bundle.data.get('ip', None) same_source = bundle.data.get('same_source', None) ip_source = bundle.data.get('ip_source', None) ip_method = bundle.data.get('ip_method', None) ip_reference = bundle.data.get('ip_reference', None) # Also add indicators add_indicators = bundle.data.get('add_indicators', None) bucket_list = bundle.data.get('bucket_list', None) ticket = bundle.data.get('ticket', None) data = { 'domain_reference': reference, 'domain_source': name, 'domain_method': method, 'confidence': confidence, 'campaign': campaign, 'domain': domain, 'same_source': same_source, 'ip_source': ip_source, 'ip_method': ip_method, 'ip_reference': ip_reference, 'add_ip': add_ip, 'ip': ip, 'add_indicators': add_indicators, 'bucket_list': bucket_list, 'ticket': ticket } if analyst: if not domain: raise BadRequest('Need a Domain Name.') # The empty list is necessary. The function requires a list of # non-fatal errors so it can be added to if any other errors # occur. Since we have none, we pass the empty list. (result, errors, retVal) = add_new_domain(data, request, []) if errors: if not 'message' in retVal: retVal['message'] = "" elif not isinstance(retVal['message'], basestring): retVal['message'] = str(retVal['message']) for e in errors: retVal['message'] += " %s " % str(e) raise BadRequest(retVal['message']) else: return bundle else: raise BadRequest('You must be an authenticated user!')