def _get(col: str, col_from: str, _from: str, col_to: str, _to: str) -> dict: """ Get an existing Edge :param col: the Edge collection :param col_from: the _from collection :param _from: the _from _key :param col_to: the _to collection :param _to: the _to key :return: the existing Edge """ _from_id = Edge._get_id(col_from, _from) _to_id = Edge._get_id(col_to, _to) session = get_db() o = session.exec_aql(f""" FOR o IN {col} FILTER o._from == @from AND o._to == @to RETURN o """, bind_vars={"from": _from_id, "to": _to_id}) if len(o) == 0: raise ObjectNotFound( f"object in {col} with _from {_from_id}" f" and _to {_to_id} not found" ) return o[0]
def insert(self): """ Insert a Node into the collection :return: """ # fixme not filtering user input... session = get_db() session.exec_aql(f""" INSERT {self.json()} INTO {self._collection} """)
def delete(self): """ Delete a Node from the collection :return: """ session = get_db() session.exec_aql(f""" REMOVE @key IN {self._collection} """, bind_vars={"key": self._key})
def get_all_entries(): c = get_db().cursor() c.execute(""" select identifier, CAST(sent as TEXT) as sent, info_parameter_temp_mean, info_parameter_temp_std, info_parameter_humidity_mean, info_parameter_humidity_std, info_parameter_pressure_mean, info_parameter_pressure_std, info_parameter_light_mean, info_parameter_light_std from entry order by sent; """) return c.fetchall()
def delete(self): """ Delete an Edge from the collection :return: """ session = get_db() session.exec_aql(f""" FOR o IN {self._collection} FILTER o._from == @from AND o._to == @to REMOVE o IN {self._collection} """, bind_vars={"from": self._from, "to": self._to})
def _update(self, values: dict): """ Update an Edge from the collection :param values: the value to update :return: """ session = get_db() session.exec_aql(f""" FOR o IN {self._collection} FILTER o._from == @from AND o._to == @to UPDATE o WITH {values} IN {self._collection} """, bind_vars={"from": self._from, "to": self._to})
def _update(self): """ Update a node in the collection :return: """ session = get_db() session.exec_aql(f""" FOR c IN {self._collection} FILTER c._key == @key UPDATE c WITH {self.json()} IN {self._collection} """, bind_vars={"key": self._key})
def _list(col: str) -> list: """ List all the Node in a collection :param col: the Node collection :return: the list of existing Node """ session = get_db() o = session.exec_aql(f""" FOR o IN {col} RETURN o """) return o
def _list(col: str) -> list: """ List all the Edge :param col: the Edge collection :return: the Edge parsed list """ session = get_db() o = session.exec_aql(f""" FOR o IN {col} RETURN o """) return o
def _exists(col: str, value: str) -> bool: """ Check if a Node exists :param col: the Node collection :param value: the Node _key value :return: True if the Node exists, False else """ session = get_db() o = session.exec_aql(f""" RETURN DOCUMENT("{col}", @key) """, bind_vars={"key": value}) return o[0] is not None
def list(username, input_filter, input_filter_by, owned_filter: bool, followed_filter: bool, sort_by, limit: int): """ List the DomainName, filter, sort, and limit the results (all in the AQL query) :param followed_filter: filter the followed DN only :param owned_filter: filter the owned DN only :param username: the User name :param input_filter: the text to use for filter :param input_filter_by: domainName, dnTags, ipTags :param sort_by: domainName, ipAddress, lastIpChange :param limit: the maximum count of results to return :return: the processed list of results of DomainName """ query = DOMAIN_NAME_LIST_QUERY bind_vars = dict() bind_vars['username'] = username if input_filter != '': # unhandled filter type if input_filter_by not in DOMAIN_NAME_FILTERS.keys(): raise DomainNameFilterNotFound filter_query = DOMAIN_NAME_FILTERS[input_filter_by] query += filter_query bind_vars['filter'] = input_filter # unhandled sort type if sort_by not in DOMAIN_NAME_SORT.keys(): raise DomainNameSortNotFound sort_query = DOMAIN_NAME_SORT[sort_by] query += sort_query if owned_filter and followed_filter: query += DOMAIN_NAME_LIST_FILTER_FOLLOWED_OWNED elif followed_filter: query += DOMAIN_NAME_LIST_FILTER_FOLLOWED elif owned_filter: query += DOMAIN_NAME_LIST_FILTER_OWNED query += DOMAIN_NAME_LIST_RETURN bind_vars['limit'] = limit session = get_db() dn_list = session.exec_aql(query, bind_vars=bind_vars) return dn_list
def _list_to(col: str, col_from: str, _from: str): """ List all the Edge connected to the object _from :param col: the Edge collection :param col_from: the _from collection :param _from: the _from _Key :return: the list of connected Edge """ _from_id = Edge._get_id(col_from, _from) session = get_db() o = session.exec_aql(f""" FOR o IN {col} FILTER o._from == @from RETURN o """, bind_vars={"from": _from_id}) return o
def _list_from(col: str, col_to: str, _to: str): """ List all the Edge connected to the object _to :param col: the Edge collection :param col_to: the _to collection :param _to: the _to _key :return: the list of connected Edge """ _to_id = Edge._get_id(col_to, _to) session = get_db() o = session.exec_aql(f""" FOR o IN {col} FILTER o._to == @to RETURN o """, bind_vars={"to": _to_id}) return o
def _get(col: str, key) -> dict: """ Get an existing Node :param col: the Node collection :param key: the Node _key :return: the existing Node """ session = get_db() o = session.exec_aql(f""" RETURN DOCUMENT("{col}", @key) """, bind_vars=({ "key": key })) if o[0] is None: raise ObjectNotFound(f"object in {col} with key {key} not found") return o[0]
def _exists_from_key(col: str, key_name: str, key_value: str) -> bool: """ Check if a Node exists, using a different field than _key :param col: the Node collection :param key_name: the field name to use for filter :param key_value: the field value to use for filter :return: True if the Node exists, False else """ session = get_db() o = session.exec_aql(f""" FOR u IN {col} FILTER u.{key_name} == @key_value RETURN u """, bind_vars=({ "key_value": key_value })) return len(o) != 0
def _exists(col: str, col_from: str, _from: str, col_to: str, _to: str) -> bool: """ Check if an Edge exists :param col: the Edge collection :param col_from: the _from collection :param _from: the _from _key :param col_to: the _to collection :param _to: the _to key :return: True if the Edge exits, False else """ _from_id = Edge._get_id(col_from, _from) _to_id = Edge._get_id(col_to, _to) session = get_db() o = session.exec_aql(f""" FOR o IN {col} FILTER o._from == @from AND o._to == @to RETURN o """, bind_vars={"from": _from_id, "to": _to_id}) return len(o) != 0
def list_recent_changes(username: str, days: int, input_filter, input_filter_by, sort_by, limit: int): """ List the DomainName from which the IP recently changed :param username: the User name :param days: the maximum days delay of IP change :param input_filter: the text to use for filter :param input_filter_by: domainName, dnTags, ipTags :param sort_by: domainName, lastIpAddress, currentIpAddress :param limit: the maximum count of results to return :return: the processed list of results of DomainName """ bind_vars = dict() query = ALERT_LIST_QUERY bind_vars['username'] = username if input_filter != '': if input_filter_by not in ALERT_FILTERS.keys(): raise DomainNameFilterNotFound filter_query = ALERT_FILTERS[input_filter_by] query += filter_query bind_vars['filter'] = input_filter if sort_by not in ALERT_SORT.keys(): raise DomainNameSortNotFound sort_query = ALERT_SORT[sort_by] query += sort_query query += ALERT_LIST_RETURN bind_vars['limit'] = limit bind_vars['days'] = days session = get_db() alert_list = session.exec_aql(query, bind_vars=bind_vars) return alert_list
def save_entry(cap, params): conn = get_db() c = conn.cursor() c.execute(""" INSERT INTO entry (identifier, sender, sent, status, msgType, scope, info_category, info_event, info_responseType, info_urgency, info_severity, info_certainty, info_senderName, info_parameter_temp_mean, info_parameter_temp_std, info_parameter_humidity_mean, info_parameter_humidity_std, info_parameter_pressure_mean, info_parameter_pressure_std, info_parameter_light_mean, info_parameter_light_std) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); """, ( str(cap['cap_id']), str(cap['cap_sender']), datetime.fromisoformat(str(cap['cap_sent'])), str(cap['cap_status']), str(cap['cap_message_type']), str(cap['cap_scope']), str(cap['cap_info'][0]['cap_category']), str(cap['cap_info'][0]['cap_event']), str(cap['cap_info'][0]['cap_response_type']), str(cap['cap_info'][0]['cap_urgency']), str(cap['cap_info'][0]['cap_severity']), str(cap['cap_info'][0]['cap_certainty']), str(cap['cap_info'][0]['cap_sender_name']), float(params['TEMP_MEAN']), float(params['TEMP_STD']), float(params['HUMIDITY_MEAN']), float(params['HUMIDITY_STD']), float(params['PRESSURE_MEAN']), float(params['PRESSURE_STD']), float(params['LIGHT_MEAN']), float(params['LIGHT_STD']), )) conn.commit()
def _get_from_key(col: str, key_name: str, key_value) -> dict: """ Get an existing Node with a key field different than _key :param col: the Node collection :param key_name: the field name to use for filter :param key_value: the field value to use for filter :return: the existing Node """ session = get_db() o = session.exec_aql(f""" FOR u IN {col} FILTER {key_name} == @key_value RETURN u """, bind_vars=({ "key_value": key_value })) if len(o) == 0: raise ObjectNotFound( f"object in {col} with key:value {key_name}:{key_value} not found" ) return o[0]
# Quit early if the config file does not exist if not os.path.exists("etc/defaults.yml"): print("etc/defaults.yml does not exist!") sys.exit() data = None with open("etc/defaults.yml", "r") as yamlfile: data = yaml.load(yamlfile, Loader=yaml.FullLoader) # Quit early if the config file is empty if data is None: print("etc/defaults.yml is empty!") sys.exit() # Get a database connection db: Session = next(get_db()) # Add the objects to the database but only if their respective tables do not have any items already. if "alert_disposition" in data and not crud.read_all(db_table=AlertDisposition, db=db): for rank, value in enumerate(data["alert_disposition"]): db.add(AlertDisposition(rank=rank, value=value)) print(f"Adding alert disposition: {rank}:{value}") if not crud.read_all(db_table=AlertQueue, db=db): if "alert_queue" in data: # Make sure there is always a "default" queue if "default" not in data["alert_queue"]: data["alert_queue"].append("default") for value in data["alert_queue"]: