def update_hids_agent_status(agent_id, sensor_id, agent_status): """ Update status of HIDS agent Raises: APICannotResolveSensorID APIInvalidHIDSAgentID APICannotUpdateHIDSAgent """ if sensor_id is None: api_log.error( "[update_hids_agent_status]: Sensor ID could not be empty") raise APICannotResolveSensorID(sensor_id) if agent_id is None: api_log.error( "[update_hids_agent_status]: Agent ID could not be empty") raise APIInvalidHIDSAgentID(agent_id) try: sensor_id_bin = get_bytes_from_uuid(sensor_id) status_integer = Hids_Agents.get_status_integer_from_string( agent_status) db.session.begin() db.session.query(Hids_Agents).filter( and_(Hids_Agents.agent_id == agent_id, Hids_Agents.sensor_id == sensor_id_bin)).update( {"agent_status": status_integer}) db.session.commit() except Exception as msg: db.session.rollback() api_log.error("[update_hids_agent_status]: %s" % str(msg)) raise APICannotUpdateHIDSAgent(agent_id, sensor_id)
def delete_hids_agent(agent_id, sensor_id): """ Delete a HIDS agent Args: agent_id(str): HIDS agent ID sensor_id(str): Sensor ID Raises: APICannotResolveSensorID APIInvalidHIDSAgentID APICannotDeleteHIDSAgent """ if sensor_id is None: api_log.error("[delete_hids_agent]: Sensor ID could not be empty") raise APICannotResolveSensorID(sensor_id) if agent_id is None: api_log.error("[delete_hids_agent]: Agent ID could not be empty") raise APIInvalidHIDSAgentID(agent_id) try: sensor_id_bin = get_bytes_from_uuid(sensor_id) db.session.begin() db.session.query(Hids_Agents).filter( and_(Hids_Agents.agent_id == agent_id, Hids_Agents.sensor_id == sensor_id_bin)).delete() db.session.commit() except Exception as msg: db.session.rollback() api_log.error("[delete_hids_agent] %s" % str(msg)) raise APICannotDeleteHIDSAgent(agent_id, sensor_id)
def update_asset_id(sensor_id, agent_id, asset_id): """ Update Asset ID related to agent Raises: APICannotResolveSensorID APIInvalidHIDSAgentID APICannotUpdateHIDSAgent APICannotResolveAssetID """ if sensor_id is None: api_log.error("[update_asset_id]: Sensor ID could not be empty") raise APICannotResolveSensorID(sensor_id) if agent_id is None: api_log.error("[update_asset_id]: Agent ID could not be empty") raise APIInvalidHIDSAgentID(agent_id) if asset_id is None: api_log.error("[update_asset_id]: Asset ID could not be empty") raise APICannotResolveAssetID(asset_id) try: sensor_id_bin = get_bytes_from_uuid(sensor_id) asset_id_bin = get_bytes_from_uuid(asset_id) db.session.query(Hids_Agents).filter( and_(Hids_Agents.agent_id == agent_id, Hids_Agents.sensor_id == sensor_id_bin)).update( {"host_id": asset_id_bin}) except Exception as msg: api_log.error("[update_asset_id]: %s" % str(msg)) raise APICannotUpdateHIDSAgent(agent_id, sensor_id)
def add_hids_agent(agent_id, sensor_id, agent_name, agent_ip, agent_status, host_id=None): """ Add a new HIDS agent Raises: APICannotResolveSensorID APIInvalidHIDSAgentID APICannotAddHIDSAgent """ if sensor_id is None: api_log.error("[add_hids_agent]: Sensor ID could not be empty") raise APICannotResolveSensorID(sensor_id) if agent_id is None: api_log.error("[add_hids_agent]: Agent ID could not be empty") raise APIInvalidHIDSAgentID(agent_id) try: db.session.begin() sensor_id_bin = get_bytes_from_uuid(sensor_id) if host_id: hex_id_bin = get_bytes_from_uuid(host_id) else: hex_id_bin = None status_integer = Hids_Agents.get_status_integer_from_string( agent_status) hids_agent = Hids_Agents() hids_agent.agent_id = agent_id hids_agent.sensor_id = sensor_id_bin hids_agent.agent_name = agent_name hids_agent.agent_ip = agent_ip hids_agent.agent_status = status_integer hids_agent.host_id = hex_id_bin db.session.merge(hids_agent) db.session.commit() except Exception as msg: db.session.rollback() api_log.error("[add_hids_agent]: %s" % str(msg)) raise APICannotAddHIDSAgent(agent_id, sensor_id)
def get_hids_agent_by_sensor(sensor_id, agent_id): """ Get HIDS agent by sensor Args: sensor_id(str): Sensor ID agent_id(str): Agent ID Returns: Dictionary with the HIDS agent of the sensor in the database Raises: APICannotResolveSensorID APIInvalidHIDSAgentID APICannotGetHIDSAgents """ if sensor_id is None: api_log.error( "[get_hids_agent_by_sensor]: Sensor ID could not be empty") raise APICannotResolveSensorID(sensor_id) if agent_id is None: api_log.error( "[get_hids_agent_by_sensor]: Agent ID could not be empty") raise APIInvalidHIDSAgentID(agent_id) hids_agent = {} try: sensor_id_hex = get_hex_string_from_uuid(sensor_id) query = "SELECT HEX(ha.sensor_id) AS sensor_id, ha.agent_id, ha.agent_name, ha.agent_ip, " \ "ha.agent_status, HEX(ha.host_id) AS host_id " \ "FROM hids_agents ha WHERE ha.sensor_id = UNHEX('{0}') AND ha.agent_id = '{1}' " \ "LIMIT 1".format(sensor_id_hex, agent_id) ha_list = db.session.connection( mapper=Hids_Agents).execute(query).fetchall() if ha_list: ha_list = ha_list[0] ha_id = ha_list.agent_id ha_name = ha_list.agent_name ha_ip = ha_list.agent_ip ha_status = ha_list.agent_status ha_host_id = ha_list.host_id if ha_list.host_id is not None else '' hids_agent = { 'id': ha_id, 'name': ha_name, 'ip_cidr': ha_ip, 'status': { 'id': ha_status, 'descr': Hids_Agents.get_status_string_from_integer(ha_status) }, 'host_id': ha_host_id } except Exception as msg: api_log.error("[get_hids_agent_by_sensor]: %s" % str(msg)) raise APICannotGetHIDSAgents(sensor_id) return hids_agent