-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathImageScanHistoryRepository.go
More file actions
170 lines (147 loc) · 6.68 KB
/
ImageScanHistoryRepository.go
File metadata and controls
170 lines (147 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (c) 2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package repository
import (
"github.com/devtron-labs/common-lib/constants"
"github.com/go-pg/pg"
"go.uber.org/zap"
"time"
)
type ImageScanExecutionHistory struct {
tableName struct{} `sql:"image_scan_execution_history" pg:",discard_unknown_columns"`
Id int `sql:"id,pk"`
Image string `sql:"image,notnull"`
ImageHash string `sql:"image_hash,notnull"` // TODO Migrate to request metadata
ExecutionTime time.Time `sql:"execution_time"`
ExecutedBy int `sql:"executed_by,notnull"`
SourceMetadataJson string `sql:"source_metadata_json"` // to have relevant info to process a scan for a given source type and subtype
ExecutionHistoryDirectoryPath string `sql:"execution_history_directory_path"` // Deprecated
SourceType constants.SourceType `sql:"source_type"`
SourceSubType constants.SourceSubType `sql:"source_sub_type"`
ParentId int `sql:"parent_id"`
IsLatest bool `sql:"is_latest"`
}
func (r *ImageScanExecutionHistory) IsSourceAndSubSourceTypeSame(sourceType constants.SourceType, sourceSubType constants.SourceSubType) bool {
return r.SourceType == sourceType && r.SourceSubType == sourceSubType
}
func (r *ImageScanExecutionHistory) UpdateIsLatest(isLatest bool) *ImageScanExecutionHistory {
r.IsLatest = isLatest
return r
}
func (r *ImageScanExecutionHistory) UpdateParentId(parentId int) {
r.ParentId = parentId
}
//Refer image_scan_deploy_info table for source_type relation
// ci workflow will have scans for ci-code and ci artifact
// cd workflow will have scans for deployment manifest, manifest images
// helm chart will have scans for manifest images and manifest
type ImageScanHistoryRepository interface {
GetConnection() (dbConnection *pg.DB)
Save(tx *pg.Tx, model *ImageScanExecutionHistory) error
FindAll() ([]*ImageScanExecutionHistory, error)
FindOne(id int) (*ImageScanExecutionHistory, error)
FindByIds(ids []int) ([]*ImageScanExecutionHistory, error)
FindByImageDigest(image string) (*ImageScanExecutionHistory, error)
FindByImageDigests(digest []string) ([]*ImageScanExecutionHistory, error)
Update(model *ImageScanExecutionHistory) error
FindByImage(image string) (*ImageScanExecutionHistory, error)
FindByImageWithNoSource(image string) (*ImageScanExecutionHistory, error)
FindByImageWithSource(image string) (*ImageScanExecutionHistory, error)
FindLatestByParentScanHistory(parentScanHistoryId int) (*ImageScanExecutionHistory, error)
}
type ImageScanHistoryRepositoryImpl struct {
dbConnection *pg.DB
logger *zap.SugaredLogger
}
func NewImageScanHistoryRepositoryImpl(dbConnection *pg.DB, logger *zap.SugaredLogger) *ImageScanHistoryRepositoryImpl {
return &ImageScanHistoryRepositoryImpl{
dbConnection: dbConnection,
logger: logger,
}
}
func (impl ImageScanHistoryRepositoryImpl) GetConnection() (dbConnection *pg.DB) {
return impl.dbConnection
}
func (impl ImageScanHistoryRepositoryImpl) Save(tx *pg.Tx, model *ImageScanExecutionHistory) error {
err := tx.Insert(model)
return err
}
func (impl ImageScanHistoryRepositoryImpl) FindAll() ([]*ImageScanExecutionHistory, error) {
var models []*ImageScanExecutionHistory
err := impl.dbConnection.Model(&models).Select()
return models, err
}
func (impl ImageScanHistoryRepositoryImpl) FindOne(id int) (*ImageScanExecutionHistory, error) {
var model ImageScanExecutionHistory
err := impl.dbConnection.Model(&model).
Where("id = ?", id).Select()
return &model, err
}
func (impl ImageScanHistoryRepositoryImpl) FindByIds(ids []int) ([]*ImageScanExecutionHistory, error) {
models := make([]*ImageScanExecutionHistory, 0)
if len(ids) == 0 {
return models, nil
}
err := impl.dbConnection.Model(&models).
Where("id in (?)", pg.In(ids)).Select()
return models, err
}
func (impl ImageScanHistoryRepositoryImpl) FindByImageDigest(image string) (*ImageScanExecutionHistory, error) {
var model ImageScanExecutionHistory
err := impl.dbConnection.Model(&model).
Where("image_hash = ?", image).Order("execution_time desc").Limit(1).Select()
return &model, err
}
func (impl ImageScanHistoryRepositoryImpl) FindByImageDigests(digest []string) ([]*ImageScanExecutionHistory, error) {
var models []*ImageScanExecutionHistory
err := impl.dbConnection.Model(&models).
Where("image_hash in (?)", pg.In(digest)).Order("execution_time desc").Select()
return models, err
}
func (impl ImageScanHistoryRepositoryImpl) Update(team *ImageScanExecutionHistory) error {
err := impl.dbConnection.Update(team)
return err
}
func (impl ImageScanHistoryRepositoryImpl) FindByImage(image string) (*ImageScanExecutionHistory, error) {
var model ImageScanExecutionHistory
err := impl.dbConnection.Model(&model).
Where("image = ?", image).Order("execution_time desc").Limit(1).Select()
return &model, err
}
func (impl ImageScanHistoryRepositoryImpl) FindByImageWithNoSource(image string) (*ImageScanExecutionHistory, error) {
var model ImageScanExecutionHistory
q := impl.dbConnection.Model(&model).
Where("image = ?", image).Where("source_type is null or source_type = 0").Where("source_type is null or source_type = 0")
err := q.Order("execution_time desc").
Limit(1).Select()
return &model, err
}
func (impl ImageScanHistoryRepositoryImpl) FindByImageWithSource(image string) (*ImageScanExecutionHistory, error) {
var model ImageScanExecutionHistory
q := impl.dbConnection.Model(&model).
Where("image = ?", image).Where("source_type != 0 and source_type is not null")
err := q.Order("execution_time desc").
Limit(1).Select()
return &model, err
}
func (impl ImageScanHistoryRepositoryImpl) FindLatestByParentScanHistory(parentScanHistoryId int) (*ImageScanExecutionHistory, error) {
var model ImageScanExecutionHistory
err := impl.dbConnection.Model(&model).
Where("parent_id = ?", parentScanHistoryId).
Where("is_latest = ?", true).
Select()
return &model, err
}