def update_service(ws, service, key, value): if key == 'owned': value = value == 'True' service.owned = value logger.info("Changing property %s to %s in service '%s' with id %s" % (key, value, service.name, service.id)) else: to_add = True if key.startswith('-'): key = key.strip('-') to_add = False field = get_field(service, key) if field is not None: if isinstance(field, str) or isinstance(field, unicode): setattr(service, key, value) logger.info( "Changing property %s to %s in service '%s' with id %s" % (key, value, service.name, service.id)) else: set_array(field, value, add=to_add) action = 'Adding %s to %s list in service %s with id %s' % (value, key, service.name, service.id) if not to_add: action = 'Removing %s from %s list in service %s with id %s' % ( value, key, service.name, service.id) logger.info(action) try: models.update_service(ws, service, "") except Exception as error: logger.error(error) return False logger.info("Done") return True
def save_objs(workspace_name): """ This function uses a set to avoid hitting too much couchdb. Wifi packets usually are repeated, for example for beacons. :param workspace_name: :return: """ order = ['Host', 'Interface', 'Service', 'Vulnerability'] saved_ids = set() tmp = created_objs iterable = tmp.items() for type in order: for key, objs in iterable: if key == type: try: if key == 'Host': print('Total {0}: {1}'.format(key, len(objs))) for obj in objs: if obj.id in saved_ids: models.update_host(workspace_name, obj) else: models.create_host(workspace_name, obj) saved_ids.add(obj.id) if key == 'Service': print('Total {0}: {1}'.format(key, len(objs))) for obj in objs: if obj.id in saved_ids: models.update_service(workspace_name, obj) else: models.create_service(workspace_name, obj) saved_ids.add(obj.id) if key == 'Vulnerability': print('Total {0}: {1}'.format(key, len(objs))) for obj in objs: if obj.id in saved_ids: models.update_vuln(workspace_name, obj) else: models.create_vuln(workspace_name, obj) if key == 'Interface': print('Total {0}: {1}'.format(key, len(objs))) for obj in objs: if obj.id in saved_ids: models.update_interface(workspace_name, obj) else: models.create_interface(workspace_name, obj) saved_ids.add(obj.id) except ConflictInDatabase as e: print('Document already exists skipping.') print(e) continue except CantCommunicateWithServerError as e: print('error') print(e) except ResourceDoesNotExist as e: print('Missing DB {0}'.format(workspace_name)) print(e) continue except Exception as e: print(e)