Exemple #1
0
def update_device(device):
    deviceID = device['deviceID']
    serializer = DeviceSerializer(data=device)
    serializer.is_valid(raise_exception=True)
    device, res = serializer.save()  # same like register
    serializer = DeviceSerializer(device)
    update_task(deviceID, serializer.data)
    return res
Exemple #2
0
 def post(self, request):
     serializer = UploadFileSerializer(request.POST, request.FILES)
     serializer.is_valid(raise_exception=True)
     filename, full_filename = serializer.save()
     update_task(pk="upload",
                 filename=full_filename,
                 sourceip=get_ip(request))
     return Response({"resourceName": filename, "status": 0})
Exemple #3
0
 def data(self, serializer, use, deviceID):
     logSources = serializer.validated_data.get('logSources', None)
     device, _ = serializer.save()
     serializer = DeviceSerializer(device)
     logSources = logSources or serializer.data_device['logSources']
     mysql_beat(use, deviceID, logSources if use else {})
     update_task(deviceID, serializer.data)
     device_iptables_mapping("enable", deviceID,
                             [i['ip'] for i in logSources])
     cache.set(deviceID, serializer.data['is_active'])
     data = {} if use else serializer.data_device
     return data
Exemple #4
0
 def post(self, request, **kwargs):
     data_pre = request.data
     data_pre.update(kwargs)
     data_pre['taskIDs'] = [
         i['policyID']
         for i in request.device['itmConfigs']['mrs']['chosenDLPPolicies']
     ]
     serializer = MRSTasksSerializer(data=data_pre)
     serializer.is_valid(raise_exception=True)
     data = serializer.data
     chosenGroups = data.pop('chosenGroups')
     for groupID in chosenGroups:
         update_task(pk="status", groupID=groupID, **data)
     return Response()
Exemple #5
0
def register(request, deviceID):
    use = request.method == 'POST'
    serializer = DeviceSerializer(data=request.data if use else {
        "is_active": False,
        "deviceID": deviceID
    })
    serializer.is_valid(raise_exception=True)
    logSources = serializer.validated_data.get('logSources', None)
    device, _ = serializer.save(
    )  # actually, run itm.serializers.DeviceSerializer.create()
    serializer = DeviceSerializer(device)
    logSources = logSources or serializer.data_device['logSources']
    # mysql_beat(use, deviceID, logSources if use else {})  # if testing, disable it
    update_task(deviceID, serializer.data)
    device_iptables_mapping("enable", deviceID, [i['ip'] for i in logSources])
    cache.set(deviceID, serializer.data['is_active'])
    data = {} if use else serializer.data_device
    return Response(data)
Exemple #6
0
def upload_file(request):
    serializer = UploadFileSerializer(request.POST, request.FILES)
    serializer.is_valid(raise_exception=True)
    filename, full_filename = serializer.save()
    status = update_task(pk="upload",
                         filename=full_filename,
                         sourceip=get_ip(request))
    data = {"resourceName": filename, "status": int(not status)}
    return Response(data)
Exemple #7
0
def _ers_custom_models(deviceID,
                       ers_models_custom_orm=None,
                       old_custom_models=None,
                       modelID_list=None,
                       new_custom_models=None):
    _old_custom_models = copy.deepcopy(old_custom_models)

    up_modelID_list = []
    if new_custom_models:  # post custom model, run this
        for model in new_custom_models['ersModels']:
            model.setdefault('display',
                             True)  # new custom model add k-v,display:True
            up_modelID_list.append(
                model["modelID"])  # add new modelID into the list

        for new_model in new_custom_models[
                'ersModels']:  # traverse the new custom models
            if new_model['modelID'] in [
                    old_model['modelID']
                    for old_model in old_custom_models['ersModels']
            ]:  # if new modelID in old_custom_models_modelID_list
                if _get_models(new_model['modelID'],
                               old_custom_models['ersModels']
                               )['display']:  # if included,display=True
                    new_custom_models['ersModels'].remove(
                        _get_models(new_model['modelID'],
                                    new_custom_models['ersModels'])
                    )  # because there are models in old-custom-models, rm the new custom models, all of the new
                else:
                    old_custom_models['ersModels'].remove(
                        _get_models(new_model['modelID'],
                                    old_custom_models['ersModels'])
                    )  # new custom models in old custom, but display=false, among the old custom rm the exist one(new)

        old_custom_models['ersModels'] += new_custom_models[
            'ersModels']  # after fix the exist problems, append the new models which is absolute new

    if modelID_list:  # delete, run
        for modelID in modelID_list:
            if modelID in [
                    old_model['modelID']
                    for old_model in old_custom_models['ersModels']
            ]:
                old_custom_models['ersModels'] = _change_display(
                    modelID, old_custom_models['ersModels'])
            else:
                raise APIDataNotFound(_('Model {} not found.'.format(modelID)),
                                      1)

    if old_custom_models != _old_custom_models:  # after updated, there're some change
        old_custom_models['version'] += 1  # version+1
        en_updated = ers_models_custom_orm.update(
            deviceID, old_custom_models)  # update custom models, return true
        if en_updated:
            if modelID_list:  # delete
                update_task(deviceID=deviceID,
                            pk="custom_models",
                            action="remove",
                            modelID_list=modelID_list)
            if up_modelID_list:  # post, add custom models
                update_task(deviceID=deviceID,
                            pk="custom_models",
                            action="upload",
                            modelID_list=up_modelID_list)  # update task
    return {}