-
Notifications
You must be signed in to change notification settings - Fork 30
refactor: prelculate advisory and vulnerability average scores and severities #1480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dejanb
wants to merge
1
commit into
main
Choose a base branch
from
precalculate-averages
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
// Add columns to advisory | ||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(Advisory::Table) | ||
.add_column(ColumnDef::new(Advisory::AverageScore).double()) | ||
.add_column( | ||
ColumnDef::new(Advisory::AverageSeverity) | ||
.custom(Alias::new("cvss3_severity")), | ||
) | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
// Add columns to vulnerability | ||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(Vulnerability::Table) | ||
.add_column(ColumnDef::new(Vulnerability::AverageScore).double()) | ||
.add_column( | ||
ColumnDef::new(Vulnerability::AverageSeverity) | ||
.custom(Alias::new("cvss3_severity")), | ||
) | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
manager | ||
.get_connection() | ||
.execute_unprepared(include_str!( | ||
"m000080_alter_aggregate_scores_fns/recalculate_cvss_aggregates.sql" | ||
)) | ||
.await | ||
.map(|_| ())?; | ||
|
||
manager | ||
.get_connection() | ||
.execute_unprepared(include_str!( | ||
"m000080_alter_aggregate_scores_fns/update_cvss_aggregates_on_change.sql" | ||
)) | ||
.await | ||
.map(|_| ())?; | ||
Ok(()) | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
// Drop trigger | ||
manager | ||
.get_connection() | ||
.execute_unprepared("DROP TRIGGER IF EXISTS cvss3_insert_update_trigger ON cvss3") | ||
.await?; | ||
|
||
// Drop functions | ||
manager | ||
.get_connection() | ||
.execute_unprepared("DROP FUNCTION IF EXISTS update_cvss_aggregates_on_change") | ||
.await?; | ||
|
||
manager | ||
.get_connection() | ||
.execute_unprepared("DROP FUNCTION IF EXISTS recalculate_cvss_aggregates") | ||
.await?; | ||
|
||
// Drop columns from vulnerability | ||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(Vulnerability::Table) | ||
.drop_column(Vulnerability::AverageScore) | ||
.drop_column(Vulnerability::AverageSeverity) | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
// Drop columns from advisory | ||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(Advisory::Table) | ||
.drop_column(Advisory::AverageScore) | ||
.drop_column(Advisory::AverageSeverity) | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Iden)] | ||
enum Advisory { | ||
Table, | ||
AverageScore, | ||
AverageSeverity, | ||
} | ||
|
||
#[derive(Iden)] | ||
enum Vulnerability { | ||
Table, | ||
AverageScore, | ||
AverageSeverity, | ||
} |
28 changes: 28 additions & 0 deletions
28
migration/src/m000080_alter_aggregate_scores_fns/recalculate_cvss_aggregates.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
CREATE OR REPLACE FUNCTION recalculate_cvss_aggregates() | ||
RETURNS void AS $$ | ||
BEGIN | ||
-- Update advisories | ||
UPDATE advisory SET | ||
average_score = sub.avg_score, | ||
average_severity = cvss3_severity(sub.avg_score) | ||
FROM ( | ||
SELECT advisory_id, AVG(score) AS avg_score | ||
FROM cvss3 | ||
GROUP BY advisory_id | ||
) AS sub | ||
WHERE advisory.id = sub.advisory_id; | ||
|
||
-- Update vulnerabilities | ||
UPDATE vulnerability SET | ||
average_score = sub.avg_score, | ||
average_severity = cvss3_severity(sub.avg_score) | ||
FROM ( | ||
SELECT vulnerability_id, AVG(score) AS avg_score | ||
FROM cvss3 | ||
GROUP BY vulnerability_id | ||
) AS sub | ||
WHERE vulnerability.id = sub.vulnerability_id; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
SELECT recalculate_cvss_aggregates(); |
38 changes: 38 additions & 0 deletions
38
migration/src/m000080_alter_aggregate_scores_fns/update_cvss_aggregates_on_change.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
CREATE OR REPLACE FUNCTION update_cvss_aggregates_on_change() | ||
RETURNS trigger AS $$ | ||
BEGIN | ||
-- Update advisory aggregate | ||
IF NEW.advisory_id IS NOT NULL THEN | ||
UPDATE advisory SET | ||
average_score = sub.avg_score, | ||
average_severity = cvss3_severity(sub.avg_score) | ||
FROM ( | ||
SELECT AVG(score) AS avg_score | ||
FROM cvss3 | ||
WHERE advisory_id = NEW.advisory_id | ||
) AS sub | ||
WHERE advisory.id = NEW.advisory_id; | ||
END IF; | ||
|
||
-- Update vulnerability aggregate | ||
IF NEW.vulnerability_id IS NOT NULL THEN | ||
UPDATE vulnerability SET | ||
average_score = sub.avg_score, | ||
average_severity = cvss3_severity(sub.avg_score) | ||
FROM ( | ||
SELECT AVG(score) AS avg_score | ||
FROM cvss3 | ||
WHERE vulnerability_id = NEW.vulnerability_id | ||
) AS sub | ||
WHERE vulnerability.id = NEW.vulnerability_id; | ||
END IF; | ||
|
||
RETURN NULL; | ||
END; | ||
$$ LANGUAGE plpgsql | ||
PARALLEL SAFE; | ||
|
||
CREATE TRIGGER cvss3_insert_update_trigger | ||
AFTER INSERT OR UPDATE OR DELETE ON cvss3 | ||
FOR EACH ROW | ||
EXECUTE FUNCTION update_cvss_aggregates_on_change(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually a bug: #1374
Can we just change these to Options in
AdvisoryVulnerabilityHead
and kill 2 birds with 1 stone? 😄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. It bothered me as well, but didn't want to change API on my own. I can include it as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure when this is getting merged, so went ahead and pushed #1591