4
4
"context"
5
5
"encoding/base64"
6
6
"encoding/json"
7
- "errors"
8
7
9
8
"github.com/google/uuid"
10
9
@@ -31,10 +30,8 @@ type ScanReport struct {
31
30
32
31
// Hash index is not addressable in Go so we use flat key-value pairing
33
32
type ScanningService struct {
34
- statusMap map [string ]string
35
- reportMap map [string ]ScanReport
36
- persistentStore RedisPersistence
37
- scannerChannel chan ScanJob
33
+ store Persistence
34
+ scannerChannel chan ScanJob
38
35
}
39
36
40
37
type ScanJob struct {
@@ -87,11 +84,8 @@ func dockerPullImage(imageRef, user, password string) error {
87
84
func (ctx * ScanningService ) Init () {
88
85
log .Debugf ("Initalizing Scanning Service" )
89
86
90
- ctx .persistentStore .Init ()
91
-
87
+ ctx .store .Init ()
92
88
ctx .scannerChannel = make (chan ScanJob , 5 )
93
- ctx .statusMap = make (map [string ]string , 0 )
94
- ctx .reportMap = make (map [string ]ScanReport , 0 )
95
89
96
90
go func () {
97
91
for job := range ctx .scannerChannel {
@@ -105,16 +99,16 @@ func (ctx *ScanningService) Init() {
105
99
func (ctx * ScanningService ) processJob (job ScanJob ) {
106
100
log .Debugf ("Processing scan job with id: %s" , job .JobID )
107
101
108
- ctx .statusMap [ job .JobID ] = SCAN_STATUS_IN_PROGRESS
102
+ ctx .store . SetScanStatus ( job .JobID , SCAN_STATUS_IN_PROGRESS )
109
103
if report , err := ctx .ScanImage (job .ScanRequest ); err != nil {
110
- ctx .statusMap [ job .JobID ] = SCAN_STATUS_ERROR
104
+ ctx .store . SetScanStatus ( job .JobID , SCAN_STATUS_ERROR )
111
105
} else {
112
- ctx .reportMap [ job .JobID ] = report
113
- ctx .statusMap [ job .JobID ] = SCAN_STATUS_COMPLETED
106
+ ctx .store . SetScanReport ( job .JobID , report )
107
+ ctx .store . SetScanStatus ( job .JobID , SCAN_STATUS_COMPLETED )
114
108
}
115
109
116
110
log .Debugf ("Finished processing job with id: %s status: %s" ,
117
- job .JobID , ctx .statusMap [ job .JobID ] )
111
+ job .JobID , ctx .store . GetScanStatus ( job .JobID ) )
118
112
}
119
113
120
114
// This method is synchronous
@@ -155,28 +149,16 @@ func (ctx *ScanningService) AsyncScanImage(req ScanRequest) string {
155
149
156
150
log .Debugf ("Async submit scan for image: %s id: %s" , req .ImageRef , scanID )
157
151
158
- ctx .statusMap [ job .JobID ] = SCAN_STATUS_NEW
152
+ ctx .store . SetScanStatus ( job .JobID , SCAN_STATUS_NEW )
159
153
ctx .scannerChannel <- job
160
154
161
155
return scanID
162
156
}
163
157
164
158
func (ctx * ScanningService ) GetScanStatus (scanID string ) string {
165
- status , found := ctx .statusMap [scanID ]
166
-
167
- if found {
168
- return status
169
- }
170
-
171
- return SCAN_STATUS_ERROR
159
+ return ctx .store .GetScanStatus (scanID )
172
160
}
173
161
174
162
func (ctx * ScanningService ) GetScanReport (scanID string ) (ScanReport , error ) {
175
- report , found := ctx .reportMap [scanID ]
176
-
177
- if ! found {
178
- return ScanReport {}, errors .New ("Invalid scanID" )
179
- }
180
-
181
- return report , nil
163
+ return ctx .store .GetScanReport (scanID )
182
164
}
0 commit comments