Skip to content

Commit d7d8030

Browse files
committed
Implemented redis persistence
1 parent f43e1cd commit d7d8030

File tree

3 files changed

+44
-38
lines changed

3 files changed

+44
-38
lines changed

Tasks.todo

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Container Image Scanner API:
44
✔ Define data structure for API request / response @done(20-01-29 15:45)
55
✔ Added CORS @done(20-01-29 11:58)
66
✔ Implement async scanning with in-memory data structure @done(20-01-29 15:45)
7-
Implement async scanning with persistence (Database/Redis)
7+
Implement async scanning with persistence (Redis) @done(20-02-01 12:03)
88
✔ Integrate dockle for CIS benchmark on docker images @done(20-01-31 15:33)
9-
☐ Serve Swagger docs (HTML) from /
9+
☐ Serve Swagger docs (HTML) from /
10+
☐ Test cases
11+
☐ Setup CI in Github

persistence.go

+29-7
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
log "github.com/sirupsen/logrus"
1111
)
1212

13-
type RedisPersistence struct {
13+
type Persistence struct {
1414
redis *redis.Client
1515
}
1616

1717
var DEFAULT_EXPIRATION time.Duration = 15 * time.Minute
1818

19-
func (client *RedisPersistence) Init() {
19+
func (client *Persistence) Init() {
2020
log.Debugf("Initializing Redis client")
2121

2222
client.redis = redis.NewClient(&redis.Options{
@@ -32,7 +32,7 @@ func (client *RedisPersistence) Init() {
3232
log.Debugf("Redis client initialized: %s", pong)
3333
}
3434

35-
func (client *RedisPersistence) SetScanStatus(scanID, status string) error {
35+
func (client *Persistence) SetScanStatus(scanID, status string) error {
3636
key := fmt.Sprintf("scans:%s:status", scanID)
3737
_, err := client.redis.Set(key, status, 0).Result()
3838

@@ -43,7 +43,7 @@ func (client *RedisPersistence) SetScanStatus(scanID, status string) error {
4343
return err
4444
}
4545

46-
func (client *RedisPersistence) SetScanReport(scanID string, report ScanReport) error {
46+
func (client *Persistence) SetScanReport(scanID string, report ScanReport) error {
4747
key := fmt.Sprintf("scans:%s:report", scanID)
4848
data, err := json.Marshal(report)
4949

@@ -61,10 +61,32 @@ func (client *RedisPersistence) SetScanReport(scanID string, report ScanReport)
6161
return err
6262
}
6363

64-
func (client *RedisPersistence) GetScanStatus(scanID string) string {
65-
return SCAN_STATUS_ERROR
64+
func (client *Persistence) GetScanStatus(scanID string) string {
65+
key := fmt.Sprintf("scans:%s:status", scanID)
66+
value, err := client.redis.Get(key).Result()
67+
68+
if err != nil {
69+
log.Debugf("Failed to get status from Redis: %#v", err)
70+
return SCAN_STATUS_ERROR
71+
}
72+
73+
return value
6674
}
6775

68-
func (client *RedisPersistence) GetScanReport(scanID string) (report ScanReport, err error) {
76+
func (client *Persistence) GetScanReport(scanID string) (report ScanReport, err error) {
77+
key := fmt.Sprintf("scans:%s:report", scanID)
78+
79+
value, err := client.redis.Get(key).Result()
80+
81+
if err != nil {
82+
log.Debugf("Failed to get report from Redis: %#v", err)
83+
return report, err
84+
}
85+
86+
err = json.Unmarshal([]byte(value), &report)
87+
if err != nil {
88+
log.Debugf("Failed to unmarshal report JSON from redis: %#v", err)
89+
}
90+
6991
return report, err
7092
}

service.go

+11-29
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/base64"
66
"encoding/json"
7-
"errors"
87

98
"github.com/google/uuid"
109

@@ -31,10 +30,8 @@ type ScanReport struct {
3130

3231
// Hash index is not addressable in Go so we use flat key-value pairing
3332
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
3835
}
3936

4037
type ScanJob struct {
@@ -87,11 +84,8 @@ func dockerPullImage(imageRef, user, password string) error {
8784
func (ctx *ScanningService) Init() {
8885
log.Debugf("Initalizing Scanning Service")
8986

90-
ctx.persistentStore.Init()
91-
87+
ctx.store.Init()
9288
ctx.scannerChannel = make(chan ScanJob, 5)
93-
ctx.statusMap = make(map[string]string, 0)
94-
ctx.reportMap = make(map[string]ScanReport, 0)
9589

9690
go func() {
9791
for job := range ctx.scannerChannel {
@@ -105,16 +99,16 @@ func (ctx *ScanningService) Init() {
10599
func (ctx *ScanningService) processJob(job ScanJob) {
106100
log.Debugf("Processing scan job with id: %s", job.JobID)
107101

108-
ctx.statusMap[job.JobID] = SCAN_STATUS_IN_PROGRESS
102+
ctx.store.SetScanStatus(job.JobID, SCAN_STATUS_IN_PROGRESS)
109103
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)
111105
} 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)
114108
}
115109

116110
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))
118112
}
119113

120114
// This method is synchronous
@@ -155,28 +149,16 @@ func (ctx *ScanningService) AsyncScanImage(req ScanRequest) string {
155149

156150
log.Debugf("Async submit scan for image: %s id: %s", req.ImageRef, scanID)
157151

158-
ctx.statusMap[job.JobID] = SCAN_STATUS_NEW
152+
ctx.store.SetScanStatus(job.JobID, SCAN_STATUS_NEW)
159153
ctx.scannerChannel <- job
160154

161155
return scanID
162156
}
163157

164158
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)
172160
}
173161

174162
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)
182164
}

0 commit comments

Comments
 (0)