def delete_by_name(title: str, category: str, config: Dict[str, str]): """ Edit the content of an artifact by name, that is title/category Arguments: title: - the title assigned to the artifact to delete category: - the category assigned to the artifact to delete config: - a configuration dictionary containing at least the following keys: PATH_KB_DB - the database path of KB PATH_KB_DATA - the data directory of KB PATH_KB_HIST - the history menu path of KB EDITOR - the editor program to call """ conn = db.create_connection(config["PATH_KB_DB"]) artifacts = db.get_artifacts_by_filter(conn, title=title, category=category, is_strict=True) if len(artifacts) == 1: artifact = artifacts.pop() db.delete_artifact_by_id(conn, artifact.id) print("Artifact {}/{} removed!".format(artifact.category, artifact.title)) elif len(artifacts) > 1: print( "There is more than one artifact with that title, please specify a category") else: print( "There is no artifact with that name, please specify a correct artifact name")
def test_delete_artifact_by_id(): db_path = Path("tests","data","newdb.db") db.create_kb_database(db_path) conn = db.create_connection(db_path) db.insert_artifact(conn, path="pentest/smb", title="pentest_smb", category="procedure", tags=['pt','smb'], status="OK", author="gnc") db.insert_artifact(conn, path="protocol/ftp", title="ftp", category="cheatsheet", tags=[], status="Draft", author="elektroniz") db.delete_artifact_by_id(conn,1) sql = "SELECT * FROM artifacts;" cur = conn.cursor() cur.execute(sql) rows = cur.fetchall() assert len(rows) == 1 assert set(rows) == {(2, 'ftp', 'cheatsheet', 'protocol/ftp', '', 'Draft', 'elektroniz')} db.delete_artifact_by_id(conn,2) sql = "SELECT * FROM artifacts;" cur = conn.cursor() cur.execute(sql) rows = cur.fetchall() assert len(rows) == 0 db_path.unlink()
def delete_by_id(id: int, config: Dict[str, str]): """ Edit the content of an artifact by id. Arguments: id: - the ID (the one you see with kb list) associated to the artifact to delete config: - a configuration dictionary containing at least the following keys: PATH_KB_DB - the database path of KB PATH_KB_DATA - the data directory of KB PATH_KB_HIST - the history menu path of KB EDITOR - the editor program to call """ conn = db.create_connection(config["PATH_KB_DB"]) artifact_id = history.get_artifact_id(config["PATH_KB_HIST"], id) artifact = db.get_artifact_by_id(conn, artifact_id) if not artifact: return db.delete_artifact_by_id(conn, artifact_id) category_path = Path(config["PATH_KB_DATA"], artifact.category) try: Path(category_path, artifact.title).unlink() except FileNotFoundError: pass if fs.count_files(category_path) == 0: fs.remove_directory(category_path) print("Artifact {category}/{title} removed!".format( category=artifact.category, title=artifact.title))
def delete(args: Dict[str, str], config: Dict[str, str]): """ Delete a list of artifacts from the kb knowledge base. Arguments: args: - a dictionary containing the following fields: id -> a list of IDs (the ones you see with kb list) associated to the artifacts we want to delete title -> the title assigned to the artifact(s) category -> the category assigned to the artifact(s) config: - a configuration dictionary containing at least the following keys: PATH_KB_DB - the database path of KB PATH_KB_DATA - the data directory of KB PATH_KB_HIST - the history menu path of KB """ initializer.init(config) conn = db.create_connection(config["PATH_KB_DB"]) if args["id"]: for i in args["id"]: artifact_id = history.get_artifact_id(config["PATH_KB_HIST"], i) artifact = db.get_artifact_by_id(conn, artifact_id) if not artifact: continue db.delete_artifact_by_id(conn, artifact_id) category_path = Path(config["PATH_KB_DATA"], artifact.category) Path(category_path, artifact.title).unlink() if fs.count_files(category_path) == 0: fs.remove_directory(category_path) print("Artifact {category}/{title} removed!".format( category=artifact.category, title=artifact.title)) sys.exit(0) # else if a title is specified elif args["title"]: artifacts = db.get_artifacts_by_filter(conn, title=args["title"], category=args["category"], is_strict=True) if len(artifacts) == 1: artifact = artifacts.pop() db.delete_artifact_by_id(conn, artifact.id) print("Artifact {}/{} removed!".format(artifact.category, artifact.title)) else: print( "There is more than one artifact with that title, please specify a category")
def test_delete_artifact_by_id(): db_path = Path("tests", "data", "test_id.db") schema_version = 1 db.create_kb_database(str(db_path), schema_version) conn = db.create_connection(str(db_path)) with conn: db.insert_artifact( conn, Artifact(id=None, path="pentest/smb", title="pentest_smb", category="procedure", tags='pt;smb', status="OK", author="gnc")) db.insert_artifact( conn, Artifact(id=None, path="protocol/ftp", title="ftp", category="cheatsheet", status="Draft", author="elektroniz")) db.delete_artifact_by_id(conn, 1) sql = "SELECT * FROM artifacts;" cur = conn.cursor() cur.execute(sql) rows = cur.fetchall() assert len(rows) == 1 assert set(rows) == {(2, 'ftp', 'cheatsheet', 'protocol/ftp', None, 'Draft', 'elektroniz', None)} db.delete_artifact_by_id(conn, 2) sql = "SELECT * FROM artifacts;" cur = conn.cursor() cur.execute(sql) rows = cur.fetchall() assert len(rows) == 0