Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
refactor: replace with go-redis (#399)
Browse files Browse the repository at this point in the history
* refactor: replace with go-redis

Signed-off-by: knqyf263 <[email protected]>

* chore(deps): go mod tidy

Signed-off-by: knqyf263 <[email protected]>

* chore: add run target

Signed-off-by: knqyf263 <[email protected]>

* test: fix args

Signed-off-by: knqyf263 <[email protected]>

---------

Signed-off-by: knqyf263 <[email protected]>
  • Loading branch information
knqyf263 committed Nov 2, 2023
1 parent 637d64a commit 37b2cb1
Show file tree
Hide file tree
Showing 19 changed files with 289 additions and 271 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ lint:
.PHONY: setup
setup:
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v1.21.0

.PHONY: run
run: export SCANNER_TRIVY_CACHE_DIR = $(TMPDIR)harbor-scanner-trivy/.cache/trivy
run: export SCANNER_TRIVY_REPORTS_DIR=$(TMPDIR)harbor-scanner-trivy/.cache/reports
run: export SCANNER_LOG_LEVEL=debug
run:
@mkdir -p $(SCANNER_TRIVY_CACHE_DIR) $(SCANNER_TRIVY_REPORTS_DIR)
@go run cmd/scanner-trivy/main.go
17 changes: 10 additions & 7 deletions cmd/scanner-trivy/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"log/slog"
"os"
Expand Down Expand Up @@ -37,13 +38,14 @@ func main() {
Date: date,
}

if err := run(info); err != nil {
ctx := context.Background()
if err := run(ctx, info); err != nil {
slog.Error("Error: %v", err)
os.Exit(1)
}
}

func run(info etc.BuildInfo) error {
func run(ctx context.Context, info etc.BuildInfo) error {
slog.Info("Starting harbor-scanner-trivy", slog.String("version", info.Version),
slog.String("commit", info.Commit), slog.String("built_at", info.Date),
)
Expand All @@ -56,16 +58,16 @@ func run(info etc.BuildInfo) error {
return fmt.Errorf("checking config: %w", err)
}

pool, err := redisx.NewPool(config.RedisPool)
rdb, err := redisx.NewClient(config.RedisPool)
if err != nil {
return fmt.Errorf("constructing connection pool: %w", err)
}

wrapper := trivy.NewWrapper(config.Trivy, ext.DefaultAmbassador)
store := redis.NewStore(config.RedisStore, pool)
store := redis.NewStore(config.RedisStore, rdb)
controller := scan.NewController(store, wrapper, scan.NewTransformer(&scan.SystemClock{}))
enqueuer := queue.NewEnqueuer(config.JobQueue, pool, store)
worker := queue.NewWorker(config.JobQueue, pool, controller)
enqueuer := queue.NewEnqueuer(config.JobQueue, rdb, store)
worker := queue.NewWorker(config.JobQueue, rdb, controller)

apiHandler := v1.NewAPIHandler(info, config, enqueuer, store, wrapper)
apiServer, err := api.NewServer(config.API, apiHandler)
Expand All @@ -82,11 +84,12 @@ func run(info etc.BuildInfo) error {

apiServer.Shutdown()
worker.Stop()
_ = rdb.Close()

close(shutdownComplete)
}()

worker.Start()
worker.Start(ctx)
apiServer.ListenAndServe()

<-shutdownComplete
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ module github.com/aquasecurity/harbor-scanner-trivy
go 1.21

require (
github.com/FZambia/sentinel v1.1.1
github.com/caarlos0/env/v6 v6.10.1
github.com/docker/docker v24.0.7+incompatible
github.com/docker/go-connections v0.4.0
github.com/gocraft/work v0.5.1
github.com/gomodule/redigo v1.8.9
github.com/gorilla/mux v1.8.0
github.com/opencontainers/go-digest v1.0.0
github.com/prometheus/client_golang v1.17.0
github.com/redis/go-redis/v9 v9.2.1
github.com/samber/lo v1.38.1
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go v0.26.0
golang.org/x/net v0.17.0
Expand All @@ -30,6 +29,7 @@ require (
github.com/containerd/log v0.1.0 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
Expand All @@ -52,7 +52,6 @@ require (
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/shirou/gopsutil/v3 v3.23.9 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
Expand Down
18 changes: 10 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/FZambia/sentinel v1.1.1 h1:0ovTimlR7Ldm+wR15GgO+8C2dt7kkn+tm3PQS+Qk3Ek=
github.com/FZambia/sentinel v1.1.1/go.mod h1:ytL1Am/RLlAoAXG6Kj5LNuw/TRRQrv2rt2FT26vP5gI=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/Microsoft/hcsshim v0.11.1 h1:hJ3s7GbWlGK4YVV92sO88BQSyF4ZLVy7/awqOlPxFbA=
github.com/Microsoft/hcsshim v0.11.1/go.mod h1:nFJmaO4Zr5Y7eADdFOpYswDDlNVbvcIJJNJLECr5JQg=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/caarlos0/env/v6 v6.10.1 h1:t1mPSxNpei6M5yAeu1qtRdPAK29Nbcf/n3G7x+b3/II=
github.com/caarlos0/env/v6 v6.10.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
Expand All @@ -36,6 +38,8 @@ github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxG
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8=
github.com/docker/distribution v2.8.2+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/docker v24.0.7+incompatible h1:Wo6l37AuwP3JaMnZa226lzVXGA3F9Ig1seQen0cKYlM=
Expand All @@ -48,8 +52,6 @@ github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDD
github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/gocraft/work v0.5.1 h1:3bRjMiOo6N4zcRgZWV3Y7uX7R22SF+A9bPTk4xRXr34=
github.com/gocraft/work v0.5.1/go.mod h1:pc3n9Pb5FAESPPGfM0nL+7Q1xtgtRnF8rr/azzhQVlM=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/godbus/dbus/v5 v5.0.6/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand All @@ -58,8 +60,6 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/gomodule/redigo v1.8.9 h1:Sl3u+2BI/kk+VEatbj0scLdrFhjPmbxOc1myhDP41ws=
github.com/gomodule/redigo v1.8.9/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down Expand Up @@ -117,11 +117,13 @@ github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdO
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI=
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg=
github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg=
github.com/shirou/gopsutil/v3 v3.23.9 h1:ZI5bWVeu2ep4/DIxB4U9okeYJ7zp/QLTO4auRb/ty/E=
github.com/shirou/gopsutil/v3 v3.23.9/go.mod h1:x/NWSb71eMcjFIO0vhyGW5nZ7oSIgVjrCnADckb85GA=
Expand Down
4 changes: 2 additions & 2 deletions pkg/http/api/v1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (h *requestHandler) AcceptScanRequest(res http.ResponseWriter, req *http.Re
return
}

scanJob, err := h.enqueuer.Enqueue(scanRequest)
scanJob, err := h.enqueuer.Enqueue(req.Context(), scanRequest)
if err != nil {
slog.Error("Error while enqueuing scan job", slog.String("err", err.Error()))
h.WriteJSONError(res, harbor.Error{
Expand Down Expand Up @@ -163,7 +163,7 @@ func (h *requestHandler) GetScanReport(res http.ResponseWriter, req *http.Reques

reqLog := slog.With(slog.String("scan_job_id", scanJobID))

scanJob, err := h.store.Get(scanJobID)
scanJob, err := h.store.Get(req.Context(), scanJobID)
if err != nil {
reqLog.Error("Error while getting scan job")
h.WriteJSONError(res, harbor.Error{
Expand Down
18 changes: 9 additions & 9 deletions pkg/http/api/v1/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestRequestHandler_AcceptScanRequest(t *testing.T) {
name: "Should accept scan request",
enqueuerExpectation: &mock.Expectation{
Method: "Enqueue",
Args: []interface{}{validScanRequest},
Args: []interface{}{mock.Anything, validScanRequest},
ReturnArgs: []interface{}{job.ScanJob{ID: "job:123"}, nil},
},
requestBody: validScanRequestJSON,
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestRequestHandler_AcceptScanRequest(t *testing.T) {
name: "Should respond with error 500 when enqueuing scan request fails",
enqueuerExpectation: &mock.Expectation{
Method: "Enqueue",
Args: []interface{}{validScanRequest},
Args: []interface{}{mock.Anything, validScanRequest},
ReturnArgs: []interface{}{job.ScanJob{}, errors.New("queue is down")},
},
requestBody: validScanRequestJSON,
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: "Should respond with error 500 when retrieving scan job fails",
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{&job.ScanJob{}, errors.New("data store is down")},
},
expectedStatus: http.StatusInternalServerError,
Expand All @@ -218,7 +218,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: "Should respond with error 404 when scan job cannot be found",
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{(*job.ScanJob)(nil), nil},
},
expectedStatus: http.StatusNotFound,
Expand All @@ -233,7 +233,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: fmt.Sprintf("Should respond with found status 302 when scan job is %s", job.Queued),
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{&job.ScanJob{
ID: "job:123",
Status: job.Queued,
Expand All @@ -245,7 +245,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: fmt.Sprintf("Should respond with found status 302 when scan job is %s", job.Pending),
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{&job.ScanJob{
ID: "job:123",
Status: job.Pending,
Expand All @@ -257,7 +257,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: fmt.Sprintf("Should respond with error 500 when scan job is %s", job.Failed),
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{&job.ScanJob{
ID: "job:123",
Status: job.Failed,
Expand All @@ -276,7 +276,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: fmt.Sprintf("Should respond with error 500 when scan job is NOT %s", job.Finished),
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{&job.ScanJob{
ID: "job:123",
Status: 666,
Expand All @@ -295,7 +295,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: "Should respond with vulnerabilities report",
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{&job.ScanJob{
ID: "job:123",
Status: job.Finished,
Expand Down
5 changes: 3 additions & 2 deletions pkg/mock/enqueuer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mock

import (
"context"
"github.com/aquasecurity/harbor-scanner-trivy/pkg/harbor"
"github.com/aquasecurity/harbor-scanner-trivy/pkg/job"
"github.com/stretchr/testify/mock"
Expand All @@ -14,7 +15,7 @@ func NewEnqueuer() *Enqueuer {
return &Enqueuer{}
}

func (em *Enqueuer) Enqueue(request harbor.ScanRequest) (job.ScanJob, error) {
args := em.Called(request)
func (em *Enqueuer) Enqueue(ctx context.Context, request harbor.ScanRequest) (job.ScanJob, error) {
args := em.Called(ctx, request)
return args.Get(0).(job.ScanJob), args.Error(1)
}
3 changes: 3 additions & 0 deletions pkg/mock/expectation.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package mock

import (
"github.com/stretchr/testify/mock"
"testing"

"github.com/aquasecurity/harbor-scanner-trivy/pkg/trivy"
)

const Anything = mock.Anything

// Expectation represents an expectation of a method being called and its return values.
type Expectation struct {
Method string
Expand Down
17 changes: 9 additions & 8 deletions pkg/mock/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mock

import (
"context"
"github.com/aquasecurity/harbor-scanner-trivy/pkg/harbor"
"github.com/aquasecurity/harbor-scanner-trivy/pkg/job"
"github.com/stretchr/testify/mock"
Expand All @@ -14,22 +15,22 @@ func NewStore() *Store {
return &Store{}
}

func (s *Store) Create(scanJob job.ScanJob) error {
args := s.Called(scanJob)
func (s *Store) Create(ctx context.Context, scanJob job.ScanJob) error {
args := s.Called(ctx, scanJob)
return args.Error(0)
}

func (s *Store) Get(scanJobID string) (*job.ScanJob, error) {
args := s.Called(scanJobID)
func (s *Store) Get(ctx context.Context, scanJobID string) (*job.ScanJob, error) {
args := s.Called(ctx, scanJobID)
return args.Get(0).(*job.ScanJob), args.Error(1)
}

func (s *Store) UpdateStatus(scanJobID string, newStatus job.ScanJobStatus, error ...string) error {
args := s.Called(scanJobID, newStatus, error)
func (s *Store) UpdateStatus(ctx context.Context, scanJobID string, newStatus job.ScanJobStatus, error ...string) error {
args := s.Called(ctx, scanJobID, newStatus, error)
return args.Error(0)
}

func (s *Store) UpdateReport(scanJobID string, report harbor.ScanReport) error {
args := s.Called(scanJobID, report)
func (s *Store) UpdateReport(ctx context.Context, scanJobID string, report harbor.ScanReport) error {
args := s.Called(ctx, scanJobID, report)
return args.Error(0)
}
Loading

0 comments on commit 37b2cb1

Please sign in to comment.