feat: Database update and policy engine #37
Closed
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Replaced by : PR 46
Preparing Macaron for policy engine which uses the Souffle datalog interpreter.
Architecture
The goal is to use souffle to evaluate the policy, while loading the facts directly from the sqlite database.
For this to work there is the following requirements, from the souffle docs:
To input it there needs to be a relation declared that matches the view, and a corresponding input statement.
So broadly the way this pr works is:
DatabaseManager.create_tables()creates the database, tables, and views if they don’t exist.CheckResult["result-tables"]Analyzerstores these to the database after analysis is completed, along with the information macaron stores such as the analyzed repositories and dependency treeThe policy engine is invoked from a separate script which is passed the database file and a policy file
At this stage what this does is
_a corresponding souffle declaration and import is generatedChanges Summary
Import SQLAlchemy to manage database connection
Refactor
DatabaseManagerto use SQLAlchemy (api change)AnalyzeContextnow returns orm-mapped tables to be inserted into the database, rather than constructing SQL queriesCheckResulthas a new field"result_tables: list[Table]"Analyzernow populates tables to store the analysis, dependency, and slsa-level results, and check_resultsAnalyzeContextnow stores a orm-mapped table to represent the repository being analyzed, which is stored to the database by theAnalyzerobject before analysis startsAnalyzerstores all tables which checks insert intoCheckResult["result_tables"]to the database after analysisbase_check.pydefines a table to store check resultsbase_check.pydefines an SQLAlchemy declarative mixinCheckFactsTablewhich defines check_result id, and repository id, foreign key fields which when result tables inherit from it, the analyzer will automatically populate.provenance_l3_checkis stricter as per pull/29.add:
policy_engine/__main__.pyis the entry point for the policy engineadd:
policy_engine/souffle_code_generator.pycontains the logic for generating the souffle datalog for data importadd:
policy_engine/souffle.pycontains the wrapper for invoking souffle in a temporary directorypolicy_engine/policy.pyhas some changes due to a manually reverted refactor; it will likely have to be refactored again to integrate the policy engineTo do