Skip to content

Commit f00b4f0

Browse files
cherry-pick
1 parent df83c78 commit f00b4f0

8 files changed

+110260
-6
lines changed

internal/sql/repository/security/ImageScanHistoryRepository.go

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,79 @@
1818
package security
1919

2020
import (
21+
serverBean "github.com/devtron-labs/devtron/pkg/server/bean"
2122
"github.com/go-pg/pg"
2223
"go.uber.org/zap"
2324
"time"
2425
)
2526

2627
type ImageScanExecutionHistory struct {
27-
tableName struct{} `sql:"image_scan_execution_history" pg:",discard_unknown_columns"`
28-
Id int `sql:"id,pk"`
29-
Image string `sql:"image,notnull"`
30-
ImageHash string `sql:"image_hash,notnull"`
31-
ExecutionTime time.Time `sql:"execution_time"`
32-
ExecutedBy int `sql:"executed_by,notnull"`
28+
tableName struct{} `sql:"image_scan_execution_history" pg:",discard_unknown_columns"`
29+
Id int `sql:"id,pk"`
30+
Image string `sql:"image,notnull"`
31+
ImageHash string `sql:"image_hash,notnull"` // TODO Migrate to request metadata
32+
ExecutionTime time.Time `sql:"execution_time"`
33+
ExecutedBy int `sql:"executed_by,notnull"`
34+
SourceMetadataJson string `sql:"source_metadata_json"` // to have relevant info to process a scan for a given source type and subtype
35+
ExecutionHistoryDirectoryPath string `sql:"execution_history_directory_path"` // Deprecated
36+
SourceType SourceType `sql:"source_type"`
37+
SourceSubType SourceSubType `sql:"source_sub_type"`
38+
ResourceScanExecutionResult *ResourceScanExecutionResult
39+
ScanToolExecutionHistoryMapping *ScanToolExecutionHistoryMapping
3340
}
3441

42+
func (ed *ExecutionData) IsBuiltImage() bool {
43+
return ed.SourceType == SourceTypeImage && ed.SourceSubType == SourceSubTypeCi
44+
}
45+
46+
func (ed *ExecutionData) IsManifestImage() bool {
47+
return ed.SourceType == SourceTypeImage && ed.SourceSubType == SourceSubTypeManifest
48+
}
49+
50+
func (ed *ExecutionData) IsManifest() bool {
51+
return ed.SourceType == SourceTypeCode && ed.SourceSubType == SourceSubTypeManifest
52+
}
53+
54+
func (ed *ExecutionData) IsCode() bool {
55+
return ed.SourceType == SourceTypeCode && ed.SourceSubType == SourceSubTypeCi
56+
}
57+
58+
func (ed *ExecutionData) ContainsType(typeToCheck ResourceScanType) bool {
59+
for _, scanType := range ed.Types {
60+
if scanType == int(typeToCheck) {
61+
return true
62+
}
63+
}
64+
return false
65+
}
66+
67+
type ExecutionData struct {
68+
Image string
69+
ScanDataJson string
70+
StartedOn time.Time
71+
ScanToolName string
72+
SourceType SourceType
73+
SourceSubType SourceSubType
74+
Types []int `sql:"types" pg:",array"`
75+
Status serverBean.ScanExecutionProcessState
76+
}
77+
78+
// multiple history rows for one source event
79+
type SourceType int
80+
81+
const (
82+
SourceTypeImage SourceType = 1
83+
SourceTypeCode SourceType = 2
84+
SourceTypeSbom SourceType = 3 // can be used in future for direct sbom scanning
85+
)
86+
87+
type SourceSubType int
88+
89+
const (
90+
SourceSubTypeCi SourceSubType = 1 // relevant for ci code(2,1) or ci built image(1,1)
91+
SourceSubTypeManifest SourceSubType = 2 // relevant for devtron app deployment manifest/helm app manifest(2,2) or images retrieved from manifest(1,2))
92+
)
93+
3594
type ImageScanHistoryRepository interface {
3695
Save(model *ImageScanExecutionHistory) error
3796
FindAll() ([]*ImageScanExecutionHistory, error)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package security
2+
3+
import (
4+
"github.com/go-pg/pg"
5+
"go.uber.org/zap"
6+
)
7+
8+
type ResourceScanExecutionResult struct {
9+
tableName struct{} `sql:"resource_scan_execution_result" pg:",discard_unknown_columns"`
10+
Id int `sql:"id,pk"`
11+
ImageScanExecutionHistoryId int `sql:"image_scan_execution_history_id"`
12+
ScanDataJson string `sql:"scan_data_json"`
13+
Format ResourceScanFormat `sql:"format"`
14+
Types []ResourceScanType `sql:"types"`
15+
ScanToolId int `sql:"scan_tool_id"`
16+
}
17+
18+
type ResourceScanFormat int
19+
20+
const (
21+
CycloneDxSbom ResourceScanFormat = 1 // SBOM
22+
TrivyJson = 2
23+
Json = 3
24+
)
25+
26+
type ResourceScanType int
27+
28+
const (
29+
Vulnerabilities ResourceScanType = 1
30+
License = 2
31+
Config = 3
32+
Secrets = 4
33+
)
34+
35+
type ResourceScanResultRepository interface {
36+
SaveInBatch(tx *pg.Tx, models []*ResourceScanExecutionResult) error
37+
}
38+
39+
type ResourceScanResultRepositoryImpl struct {
40+
dbConnection *pg.DB
41+
logger *zap.SugaredLogger
42+
}
43+
44+
func NewResourceScanResultRepositoryImpl(dbConnection *pg.DB, logger *zap.SugaredLogger) *ResourceScanResultRepositoryImpl {
45+
return &ResourceScanResultRepositoryImpl{
46+
dbConnection: dbConnection,
47+
logger: logger,
48+
}
49+
}
50+
51+
func (impl ResourceScanResultRepositoryImpl) SaveInBatch(tx *pg.Tx, models []*ResourceScanExecutionResult) error {
52+
return tx.Insert(&models)
53+
}

internal/sql/repository/security/ScanToolExecutionHistoryMapping.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type ScanToolExecutionHistoryMapping struct {
1717
ExecutionFinishTime time.Time `sql:"execution_finish_time,notnull"`
1818
State serverBean.ScanExecutionProcessState `sql:"state"`
1919
TryCount int `sql:"try_count"`
20+
ErrorMessage string `sql:"error_message"`
2021
sql.AuditLog
2122
}
2223

0 commit comments

Comments
 (0)