fix(metadata): keep created_at on CVE bypass edit; repair MySQL suite

This commit is contained in:
2026-11-23 03:18:42 +00:00
parent bde9eebf3d
commit 5420480ef8
3 changed files with 78 additions and 11 deletions
+30 -5
View File
@@ -977,11 +977,7 @@ func (s *GORMStoreV2) SaveCVEBypass(ctx context.Context, bypass *metadata.CVEByp
if bypass.ID != "" {
id, err := strconv.ParseInt(bypass.ID, 10, 64)
if err == nil {
model.ID = id
if err := s.db.WithContext(ctx).Save(model).Error; err != nil {
return errors.Wrap(err, errors.ErrCodeStorageFailure, "failed to update CVE bypass")
}
return nil
return s.updateCVEBypass(ctx, id, model)
}
}
@@ -997,6 +993,35 @@ func (s *GORMStoreV2) SaveCVEBypass(ctx context.Context, bypass *metadata.CVEByp
return nil
}
// updateCVEBypass rewrites the editable columns only, so created_at and usage counters survive an edit.
func (s *GORMStoreV2) updateCVEBypass(ctx context.Context, id int64, model *CVEBypassModel) error {
db := s.db.WithContext(ctx)
res := db.Model(&CVEBypassModel{}).Where("id = ?", id).Updates(map[string]interface{}{
"type": model.Type,
"target": model.Target,
"reason": model.Reason,
"created_by": model.CreatedBy,
"expires_at": model.ExpiresAt,
"notify_on_expiry": model.NotifyOnExpiry,
"active": model.Active,
})
if res.Error != nil {
return errors.Wrap(res.Error, errors.ErrCodeStorageFailure, "failed to update CVE bypass")
}
if res.RowsAffected > 0 {
return nil
}
// MySQL reports 0 rows for an unchanged row, so tell that apart from a missing one.
var count int64
if err := db.Model(&CVEBypassModel{}).Where("id = ?", id).Count(&count).Error; err != nil {
return errors.Wrap(err, errors.ErrCodeStorageFailure, "failed to update CVE bypass")
}
if count == 0 {
return errors.NotFound("CVE bypass not found")
}
return nil
}
// GetActiveCVEBypasses retrieves all active (non-expired) CVE bypasses
func (s *GORMStoreV2) GetActiveCVEBypasses(ctx context.Context) ([]*metadata.CVEBypass, error) {
var models []CVEBypassModel
@@ -91,9 +91,20 @@ func (s *MySQLV2IntegrationTestSuite) TearDownTest() {
"registries",
}
for _, table := range tables {
s.store.db.Exec("TRUNCATE TABLE " + table)
}
// FOREIGN_KEY_CHECKS is per connection, so truncate on one pinned connection.
err := s.store.db.Connection(func(tx *gorm.DB) error {
if err := tx.Exec("SET FOREIGN_KEY_CHECKS = 0").Error; err != nil {
return err
}
defer tx.Exec("SET FOREIGN_KEY_CHECKS = 1")
for _, table := range tables {
if err := tx.Exec("TRUNCATE TABLE " + table).Error; err != nil {
return err
}
}
return nil
})
s.Require().NoError(err)
// Re-seed default registries after truncate
defaultRegistries := []RegistryModel{
@@ -124,10 +135,11 @@ func TestMySQLV2IntegrationTestSuite(t *testing.T) {
// Test_MySQLV2_SpecificFeatures tests MySQL-specific features
func (s *MySQLV2IntegrationTestSuite) Test_MySQLV2_SpecificFeatures() {
// Test that we're actually using MySQL
var version string
err := s.store.db.Raw("SELECT VERSION()").Scan(&version).Error
// VERSION() is a bare number ("8.0.46"); the server flavour is in version_comment.
var comment string
err := s.store.db.Raw("SELECT @@version_comment").Scan(&comment).Error
s.NoError(err)
s.Contains(version, "MySQL")
s.Contains(comment, "MySQL")
}
// Test_MySQLV2_NoPartitioning tests that partition manager is nil for MySQL
@@ -713,6 +713,36 @@ func (s *GORMStoreV2TestSuite) Test_V2_SaveCVEBypass_Update() {
s.NoError(err)
}
// Test_V2_SaveCVEBypass_UpdateKeepsCreatedAt guards against an edit zeroing created_at.
func (s *GORMStoreV2TestSuite) Test_V2_SaveCVEBypass_UpdateKeepsCreatedAt() {
bypass := &metadata.CVEBypass{
Type: metadata.BypassTypeCVE, Target: "CVE-2024-0003", Reason: "before",
CreatedBy: "[email protected]", ExpiresAt: time.Now().Add(time.Hour), Active: true,
}
s.Require().NoError(s.store.SaveCVEBypass(s.ctx, bypass))
var before CVEBypassModel
s.Require().NoError(s.store.db.First(&before, bypass.ID).Error)
bypass.Reason = "after"
s.Require().NoError(s.store.SaveCVEBypass(s.ctx, bypass))
var after CVEBypassModel
s.Require().NoError(s.store.db.First(&after, bypass.ID).Error)
s.Equal("after", after.Reason)
s.False(after.CreatedAt.IsZero())
s.WithinDuration(before.CreatedAt, after.CreatedAt, time.Second)
}
// Test_V2_SaveCVEBypass_UpdateMissing tests that editing an unknown bypass fails.
func (s *GORMStoreV2TestSuite) Test_V2_SaveCVEBypass_UpdateMissing() {
err := s.store.SaveCVEBypass(s.ctx, &metadata.CVEBypass{
ID: "999999", Type: metadata.BypassTypeCVE, Target: "CVE-2024-0004", Reason: "x",
CreatedBy: "[email protected]", ExpiresAt: time.Now().Add(time.Hour), Active: true,
})
s.Error(err)
}
// Test_V2_GetActiveCVEBypasses tests retrieving active CVE bypasses
func (s *GORMStoreV2TestSuite) Test_V2_GetActiveCVEBypasses() {
// Create active bypass with unique target