|
| 1 | +package daemon_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + integration_tests "github.com/l3montree-dev/devguard/integrationtestutil" |
| 9 | + "github.com/l3montree-dev/devguard/internal/core" |
| 10 | + "github.com/l3montree-dev/devguard/internal/core/daemon" |
| 11 | + "github.com/l3montree-dev/devguard/internal/database/models" |
| 12 | + "github.com/l3montree-dev/devguard/internal/database/repositories" |
| 13 | + "github.com/l3montree-dev/devguard/internal/utils" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +func TestAutoReopenAcceptedVulnerabilities(t *testing.T) { |
| 18 | + db, terminate := integration_tests.InitDatabaseContainer("../../../initdb.sql") |
| 19 | + defer terminate() |
| 20 | + |
| 21 | + err := db.AutoMigrate( |
| 22 | + &models.Org{}, |
| 23 | + &models.Project{}, |
| 24 | + &models.AssetVersion{}, |
| 25 | + &models.Asset{}, |
| 26 | + &models.ComponentDependency{}, |
| 27 | + &models.Component{}, |
| 28 | + &models.CVE{}, |
| 29 | + &models.AffectedComponent{}, |
| 30 | + &models.DependencyVuln{}, |
| 31 | + &models.Exploit{}, |
| 32 | + &models.VulnEvent{}, |
| 33 | + &models.FirstPartyVuln{}, |
| 34 | + ) |
| 35 | + assert.NoError(t, err) |
| 36 | + |
| 37 | + // Create test data |
| 38 | + _, project, asset, assetVersion := integration_tests.CreateOrgProjectAndAssetAssetVersion(db) |
| 39 | + |
| 40 | + // Set up repositories |
| 41 | + assetRepo := repositories.NewAssetRepository(db) |
| 42 | + dependencyVulnRepo := repositories.NewDependencyVulnRepository(db) |
| 43 | + |
| 44 | + t.Run("should not reopen vulnerabilities if auto-reopen is not configured", func(t *testing.T) { |
| 45 | + // Ensure asset has no auto-reopen configuration |
| 46 | + asset.VulnAutoReopenAfterDays = nil |
| 47 | + err := assetRepo.Update(db, &asset) |
| 48 | + assert.NoError(t, err) |
| 49 | + |
| 50 | + // Create a vulnerability that was accepted 2 hours ago |
| 51 | + vulnerability := createTestVulnerability(t, db, asset, assetVersion, 2*time.Hour) |
| 52 | + acceptVulnerability(t, db, &vulnerability, 2*time.Hour) |
| 53 | + |
| 54 | + // Run auto-reopen |
| 55 | + err = daemon.AutoReopenAcceptedVulnerabilities(db) |
| 56 | + assert.NoError(t, err) |
| 57 | + |
| 58 | + // Verify vulnerability is still accepted |
| 59 | + updatedVuln, err := dependencyVulnRepo.Read(vulnerability.ID) |
| 60 | + assert.NoError(t, err) |
| 61 | + assert.Equal(t, models.VulnStateAccepted, updatedVuln.State) |
| 62 | + }) |
| 63 | + |
| 64 | + t.Run("should not reopen vulnerabilities that are within the time threshold", func(t *testing.T) { |
| 65 | + // Configure asset for auto-reopen after 1 day |
| 66 | + autoReopenAfterDays := 1 |
| 67 | + asset.VulnAutoReopenAfterDays = &autoReopenAfterDays |
| 68 | + err := assetRepo.Update(db, &asset) |
| 69 | + assert.NoError(t, err) |
| 70 | + |
| 71 | + // Create a vulnerability that was accepted 1 hour ago (within threshold) |
| 72 | + vulnerability := createTestVulnerability(t, db, asset, assetVersion, 1*time.Hour) |
| 73 | + acceptVulnerability(t, db, &vulnerability, 1*time.Hour) |
| 74 | + |
| 75 | + // Run auto-reopen |
| 76 | + err = daemon.AutoReopenAcceptedVulnerabilities(db) |
| 77 | + assert.NoError(t, err) |
| 78 | + |
| 79 | + // Verify vulnerability is still accepted |
| 80 | + updatedVuln, err := dependencyVulnRepo.Read(vulnerability.ID) |
| 81 | + assert.NoError(t, err) |
| 82 | + assert.Equal(t, models.VulnStateAccepted, updatedVuln.State) |
| 83 | + }) |
| 84 | + |
| 85 | + t.Run("should reopen vulnerabilities that exceed the time threshold", func(t *testing.T) { |
| 86 | + // Configure asset for auto-reopen after 1 day |
| 87 | + autoReopenAfterDays := 1 |
| 88 | + asset.VulnAutoReopenAfterDays = &autoReopenAfterDays |
| 89 | + err := assetRepo.Update(db, &asset) |
| 90 | + assert.NoError(t, err) |
| 91 | + |
| 92 | + // Create a vulnerability that was accepted 2 days ago (exceeds threshold) |
| 93 | + vulnerability := createTestVulnerability(t, db, asset, assetVersion, 48*time.Hour) |
| 94 | + acceptVulnerability(t, db, &vulnerability, 48*time.Hour) |
| 95 | + |
| 96 | + // Run auto-reopen |
| 97 | + err = daemon.AutoReopenAcceptedVulnerabilities(db) |
| 98 | + assert.NoError(t, err) |
| 99 | + |
| 100 | + // Verify vulnerability has been reopened |
| 101 | + updatedVuln, err := dependencyVulnRepo.Read(vulnerability.ID) |
| 102 | + assert.NoError(t, err) |
| 103 | + assert.Equal(t, models.VulnStateOpen, updatedVuln.State) |
| 104 | + |
| 105 | + // Verify a reopen event was created |
| 106 | + events := updatedVuln.Events |
| 107 | + assert.NotEmpty(t, events) |
| 108 | + |
| 109 | + // Find the reopen event |
| 110 | + var reopenEvent *models.VulnEvent |
| 111 | + for _, event := range events { |
| 112 | + if event.Type == models.EventTypeReopened { |
| 113 | + reopenEvent = &event |
| 114 | + break |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + assert.NotNil(t, reopenEvent, "Expected to find a reopen event") |
| 119 | + assert.Equal(t, "system", reopenEvent.UserID) |
| 120 | + assert.NotNil(t, reopenEvent.Justification, "Expected justification to not be nil") |
| 121 | + assert.Contains(t, *reopenEvent.Justification, "Automatically reopened") |
| 122 | + }) |
| 123 | + |
| 124 | + t.Run("should handle multiple assets with different configurations", func(t *testing.T) { |
| 125 | + // Create another asset with different auto-reopen configuration |
| 126 | + asset2 := models.Asset{ |
| 127 | + Name: "test-asset-2", |
| 128 | + Slug: "test-asset-2", |
| 129 | + ProjectID: project.ID, |
| 130 | + Type: models.AssetTypeApplication, |
| 131 | + Description: "Test asset 2", |
| 132 | + } |
| 133 | + autoReopenAfter2Days := 2 |
| 134 | + asset2.VulnAutoReopenAfterDays = &autoReopenAfter2Days |
| 135 | + err := assetRepo.Create(db, &asset2) |
| 136 | + assert.NoError(t, err) |
| 137 | + |
| 138 | + assetVersion2 := models.AssetVersion{ |
| 139 | + AssetID: asset2.ID, |
| 140 | + Name: "main", |
| 141 | + DefaultBranch: true, |
| 142 | + } |
| 143 | + assetVersionRepo := repositories.NewAssetVersionRepository(db) |
| 144 | + err = assetVersionRepo.Create(db, &assetVersion2) |
| 145 | + assert.NoError(t, err) |
| 146 | + |
| 147 | + // Set different auto-reopen thresholds |
| 148 | + autoReopenAfter1Days := 1 |
| 149 | + asset.VulnAutoReopenAfterDays = &autoReopenAfter1Days |
| 150 | + err = assetRepo.Update(db, &asset) |
| 151 | + assert.NoError(t, err) |
| 152 | + |
| 153 | + // Create vulnerabilities for both assets |
| 154 | + vuln1 := createTestVulnerability(t, db, asset, assetVersion, 1*time.Hour) |
| 155 | + acceptVulnerability(t, db, &vuln1, 36*time.Hour) // Accepted 1.5 days ago |
| 156 | + |
| 157 | + vuln2 := createTestVulnerability(t, db, asset2, assetVersion2, 2*time.Hour) |
| 158 | + acceptVulnerability(t, db, &vuln2, 72*time.Hour) // Accepted 3 days ago |
| 159 | + |
| 160 | + // Run auto-reopen |
| 161 | + err = daemon.AutoReopenAcceptedVulnerabilities(db) |
| 162 | + assert.NoError(t, err) |
| 163 | + |
| 164 | + // Verify both vulnerabilities are reopened |
| 165 | + updatedVuln1, err := dependencyVulnRepo.Read(vuln1.ID) |
| 166 | + assert.NoError(t, err) |
| 167 | + assert.Equal(t, models.VulnStateOpen, updatedVuln1.State) |
| 168 | + |
| 169 | + updatedVuln2, err := dependencyVulnRepo.Read(vuln2.ID) |
| 170 | + assert.NoError(t, err) |
| 171 | + assert.Equal(t, models.VulnStateOpen, updatedVuln2.State) |
| 172 | + }) |
| 173 | +} |
| 174 | + |
| 175 | +// createTestVulnerability creates a test dependency vulnerability |
| 176 | +func createTestVulnerability(t *testing.T, db core.DB, asset models.Asset, assetVersion models.AssetVersion, timeAgo time.Duration) models.DependencyVuln { |
| 177 | + // Create a unique CVE ID for each test case |
| 178 | + cveID := fmt.Sprintf("CVE-2025-TEST-%d", time.Now().UnixNano()) |
| 179 | + |
| 180 | + // Create a test CVE |
| 181 | + cve := models.CVE{ |
| 182 | + CVE: cveID, |
| 183 | + DatePublished: time.Now().Add(-24 * time.Hour), |
| 184 | + DateLastModified: time.Now().Add(-12 * time.Hour), |
| 185 | + Description: "Test vulnerability for auto-reopen testing", |
| 186 | + CVSS: 7.5, |
| 187 | + Severity: models.SeverityHigh, |
| 188 | + ExploitabilityScore: 3.9, |
| 189 | + ImpactScore: 3.6, |
| 190 | + } |
| 191 | + err := db.Create(&cve).Error |
| 192 | + assert.NoError(t, err) |
| 193 | + |
| 194 | + // Create the vulnerability - ID will be auto-generated by BeforeSave hook |
| 195 | + vulnerability := models.DependencyVuln{ |
| 196 | + Vulnerability: models.Vulnerability{ |
| 197 | + AssetVersionName: assetVersion.Name, |
| 198 | + AssetID: asset.ID, |
| 199 | + State: models.VulnStateOpen, |
| 200 | + ScannerIDs: "test-scanner", |
| 201 | + LastDetected: time.Now().Add(-timeAgo), |
| 202 | + }, |
| 203 | + CVEID: utils.Ptr(cveID), |
| 204 | + ComponentPurl: utils.Ptr("pkg:npm/test-package@1.0.0"), |
| 205 | + ComponentDepth: utils.Ptr(0), |
| 206 | + } |
| 207 | + err = db.Create(&vulnerability).Error |
| 208 | + assert.NoError(t, err) |
| 209 | + |
| 210 | + return vulnerability |
| 211 | +} |
| 212 | + |
| 213 | +// acceptVulnerability creates an accepted event for a vulnerability |
| 214 | +func acceptVulnerability(t *testing.T, db core.DB, vulnerability *models.DependencyVuln, timeAgo time.Duration) { |
| 215 | + // Create an accepted event using the model constructor |
| 216 | + acceptEvent := models.NewAcceptedEvent(vulnerability.CalculateHash(), models.VulnTypeDependencyVuln, "test-user", "Accepted for testing") |
| 217 | + |
| 218 | + // Manually set the creation time for testing |
| 219 | + acceptEvent.CreatedAt = time.Now().Add(-timeAgo) |
| 220 | + acceptEvent.UpdatedAt = time.Now().Add(-timeAgo) |
| 221 | + |
| 222 | + err := db.Create(&acceptEvent).Error |
| 223 | + assert.NoError(t, err) |
| 224 | + |
| 225 | + // Update vulnerability state |
| 226 | + vulnerability.State = models.VulnStateAccepted |
| 227 | + vulnerability.LastDetected = time.Now().Add(-timeAgo) |
| 228 | + err = db.Save(vulnerability).Error |
| 229 | + assert.NoError(t, err) |
| 230 | +} |
0 commit comments