Skip to content

Commit

Permalink
make linter happy
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
  • Loading branch information
matthyx committed Mar 22, 2023
1 parent 32f06e6 commit 6aaf075
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion adapters/mockplatform.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (m MockPlatform) GetCVEExceptions(_ context.Context) (domain.CVEExceptions,
// SendStatus logs the given status and details
func (m MockPlatform) SendStatus(ctx context.Context, step int) error {
// retrieve workload from context
workload, ok := ctx.Value(domain.WorkloadKey).(domain.ScanCommand)
workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand)
if !ok {
return errors.New("no workload found in context")
}
Expand Down
2 changes: 1 addition & 1 deletion adapters/mockplatform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestMockPlatform_SendStatus(t *testing.T) {
ctx := context.TODO()
err := m.SendStatus(ctx, domain.Done)
assert.Assert(t, err != nil)
ctx = context.WithValue(ctx, domain.WorkloadKey, domain.ScanCommand{})
ctx = context.WithValue(ctx, domain.WorkloadKey{}, domain.ScanCommand{})
err = m.SendStatus(ctx, domain.Done)
assert.Assert(t, err == nil)
}
Expand Down
13 changes: 8 additions & 5 deletions adapters/v1/armo.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (a *ArmoAdapter) GetCVEExceptions(ctx context.Context) (domain.CVEException
defer span.End()

// retrieve workload from context
workload, ok := ctx.Value(domain.WorkloadKey).(domain.ScanCommand)
workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand)
if !ok {
return nil, errors.New("no workload found in context")
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func (a *ArmoAdapter) SendStatus(ctx context.Context, step int) error {
ctx, span := otel.Tracer("").Start(ctx, "ArmoAdapter.SendStatus")
defer span.End()
// retrieve workload from context
workload, ok := ctx.Value(domain.WorkloadKey).(domain.ScanCommand)
workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand)
if !ok {
return errors.New("no workload found in context")
}
Expand Down Expand Up @@ -127,23 +127,26 @@ func (a *ArmoAdapter) SubmitCVE(ctx context.Context, cve domain.CVEManifest, cve
ctx, span := otel.Tracer("").Start(ctx, "ArmoAdapter.SubmitCVE")
defer span.End()
// retrieve timestamp from context
timestamp, ok := ctx.Value(domain.TimestampKey).(int64)
timestamp, ok := ctx.Value(domain.TimestampKey{}).(int64)
if !ok {
return errors.New("no timestamp found in context")
}
// retrieve scanID from context
scanID, ok := ctx.Value(domain.ScanIDKey).(string)
scanID, ok := ctx.Value(domain.ScanIDKey{}).(string)
if !ok {
return errors.New("no scanID found in context")
}
// retrieve workload from context
workload, ok := ctx.Value(domain.WorkloadKey).(domain.ScanCommand)
workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand)
if !ok {
return errors.New("no workload found in context")
}

// get exceptions
exceptions, err := a.GetCVEExceptions(ctx)
if err != nil {
return err
}
// convert to vulnerabilities
vulnerabilities, err := domainToArmo(ctx, *cve.Content, exceptions)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions adapters/v1/armo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestArmoAdapter_GetCVEExceptions(t *testing.T) {
}
ctx := context.TODO()
if tt.workload {
ctx = context.WithValue(ctx, domain.WorkloadKey, domain.ScanCommand{})
ctx = context.WithValue(ctx, domain.WorkloadKey{}, domain.ScanCommand{})
}
got, err := a.GetCVEExceptions(ctx)
if (err != nil) != tt.wantErr {
Expand Down Expand Up @@ -115,9 +115,9 @@ func TestArmoAdapter_SubmitCVE(t *testing.T) {
httpPostFunc: tt.fields.httpPostFunc,
}
ctx := context.TODO()
ctx = context.WithValue(ctx, domain.TimestampKey, time.Now().Unix())
ctx = context.WithValue(ctx, domain.ScanIDKey, uuid.New().String())
ctx = context.WithValue(ctx, domain.WorkloadKey, domain.ScanCommand{})
ctx = context.WithValue(ctx, domain.TimestampKey{}, time.Now().Unix())
ctx = context.WithValue(ctx, domain.ScanIDKey{}, uuid.New().String())
ctx = context.WithValue(ctx, domain.WorkloadKey{}, domain.ScanCommand{})
b, err := os.ReadFile("testdata/alpine-cve.json")
tools.EnsureSetup(t, err == nil)
var grypeCVE models.Document
Expand Down
6 changes: 3 additions & 3 deletions adapters/v1/domain_to_armo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ func domainToArmo(ctx context.Context, grypeDocument v1beta1.GrypeDocument, vuln
var vulnerabilityResults []containerscan.CommonContainerVulnerabilityResult

// retrieve timestamp from context
timestamp, ok := ctx.Value(domain.TimestampKey).(int64)
timestamp, ok := ctx.Value(domain.TimestampKey{}).(int64)
if !ok {
return vulnerabilityResults, errors.New("no timestamp found in context")
}
// retrieve scanID from context
scanID, ok := ctx.Value(domain.ScanIDKey).(string)
scanID, ok := ctx.Value(domain.ScanIDKey{}).(string)
if !ok {
return vulnerabilityResults, errors.New("no scanID found in context")
}
// retrieve workload from context
workload, ok := ctx.Value(domain.WorkloadKey).(domain.ScanCommand)
workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand)
if !ok {
return vulnerabilityResults, errors.New("no workload found in context")
}
Expand Down
4 changes: 2 additions & 2 deletions adapters/v1/grype.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewGrypeAdapter() *GrypeAdapter {

// DBVersion returns the vulnerabilities DB checksum which is used to tag CVE manifests
func (g *GrypeAdapter) DBVersion(ctx context.Context) string {
ctx, span := otel.Tracer("").Start(ctx, "GrypeAdapter.DBVersion")
_, span := otel.Tracer("").Start(ctx, "GrypeAdapter.DBVersion")
defer span.End()

g.mu.RLock()
Expand Down Expand Up @@ -174,7 +174,7 @@ func getMatchers() []matcher.Matcher {

// Version returns Grype's version which is used to tag CVE manifests
func (g *GrypeAdapter) Version(ctx context.Context) string {
ctx, span := otel.Tracer("").Start(ctx, "GrypeAdapter.Ready")
_, span := otel.Tracer("").Start(ctx, "GrypeAdapter.Ready")
defer span.End()
return tools.PackageVersion("github.com/anchore/grype")
}
6 changes: 3 additions & 3 deletions adapters/v1/grype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func Test_grypeAdapter_ScanSBOM(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.TODO()
ctx = context.WithValue(ctx, domain.TimestampKey, time.Now().Unix())
ctx = context.WithValue(ctx, domain.ScanIDKey, uuid.New().String())
ctx = context.WithValue(ctx, domain.WorkloadKey, domain.ScanCommand{})
ctx = context.WithValue(ctx, domain.TimestampKey{}, time.Now().Unix())
ctx = context.WithValue(ctx, domain.ScanIDKey{}, uuid.New().String())
ctx = context.WithValue(ctx, domain.WorkloadKey{}, domain.ScanCommand{})
g := NewGrypeAdapter()
g.Ready(ctx) // need to call ready to load the DB
got, err := g.ScanSBOM(ctx, tt.sbom)
Expand Down
2 changes: 1 addition & 1 deletion adapters/v1/syft.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *SyftAdapter) CreateSBOM(ctx context.Context, imageID string, options do

// Version returns Syft's version which is used to tag SBOMs
func (s *SyftAdapter) Version(ctx context.Context) string {
ctx, span := otel.Tracer("").Start(ctx, "SyftAdapter.Version")
_, span := otel.Tracer("").Start(ctx, "SyftAdapter.Version")
defer span.End()
return tools.PackageVersion("github.com/anchore/syft")
}
2 changes: 1 addition & 1 deletion controllers/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestHTTPController_Alive(t *testing.T) {
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Assert(t, http.StatusOK == w.Code, w.Code)
assert.Assert(t, "{\"status\":200,\"title\":\"OK\"}" == w.Body.String(), w.Body.String())
assert.Assert(t, w.Body.String() == "{\"status\":200,\"title\":\"OK\"}", w.Body.String())
}

func TestHTTPController_GenerateSBOM(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions core/domain/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package domain

import wssc "github.com/armosec/armoapi-go/apis"

const ScanIDKey = "scanID"
const TimestampKey = "timestamp"
const WorkloadKey = "scanCommand"
type ScanIDKey struct{}
type TimestampKey struct{}
type WorkloadKey struct{}

// ScanCommand is a proxy type for wssc.WebsocketScanCommand used to decouple business logic from implementation
// it might evolve into its own struct at a later time
Expand Down
5 changes: 1 addition & 4 deletions core/services/mockscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ func (m MockScanService) GenerateSBOM(context.Context) error {
}

func (m MockScanService) Ready(context.Context) bool {
if m.happy {
return true
}
return false
return m.happy
}

func (m MockScanService) ScanCVE(context.Context) error {
Expand Down
10 changes: 5 additions & 5 deletions core/services/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *ScanService) GenerateSBOM(ctx context.Context) error {
ctx, span := otel.Tracer("").Start(ctx, "ScanService.GenerateSBOM")
defer span.End()
// retrieve workload from context
workload, ok := ctx.Value(domain.WorkloadKey).(domain.ScanCommand)
workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand)
if !ok {
return errors.New("no workload found in context")
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func (s *ScanService) ScanCVE(ctx context.Context) error {
ctx, span := otel.Tracer("").Start(ctx, "ScanService.ScanCVE")
defer span.End()
// retrieve workload from context
workload, ok := ctx.Value(domain.WorkloadKey).(domain.ScanCommand)
workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand)
if !ok {
return errors.New("no workload found in context")
}
Expand Down Expand Up @@ -195,15 +195,15 @@ func (s *ScanService) ScanCVE(ctx context.Context) error {

func enrichContext(ctx context.Context, workload domain.ScanCommand) context.Context {
// record start time
ctx = context.WithValue(ctx, domain.TimestampKey, time.Now().Unix())
ctx = context.WithValue(ctx, domain.TimestampKey{}, time.Now().Unix())
// generate unique scanID and add to context
scanID, err := uuid.NewRandom()
if err != nil {
logger.L().Ctx(ctx).Error("error generating scanID", helpers.Error(err))
}
ctx = context.WithValue(ctx, domain.ScanIDKey, scanID.String())
ctx = context.WithValue(ctx, domain.ScanIDKey{}, scanID.String())
// add workload to context
ctx = context.WithValue(ctx, domain.WorkloadKey, workload)
ctx = context.WithValue(ctx, domain.WorkloadKey{}, workload)
return ctx
}

Expand Down
6 changes: 3 additions & 3 deletions repositories/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestAPIServerStore_GetCVE(t *testing.T) {
tools.EnsureSetup(t, err == nil)
err = a.StoreCVE(tt.args.ctx, tt.cve, false)
tools.EnsureSetup(t, err == nil)
gotCve, err := a.GetCVE(tt.args.ctx, tt.args.imageID, tt.args.SBOMCreatorVersion, tt.args.CVEScannerVersion, tt.args.CVEDBVersion)
gotCve, _ := a.GetCVE(tt.args.ctx, tt.args.imageID, tt.args.SBOMCreatorVersion, tt.args.CVEScannerVersion, tt.args.CVEDBVersion)
if (gotCve.Content == nil) != tt.wantEmptyCVE {
t.Errorf("GetCVE() gotCve.Content = %v, wantEmptyCVE %v", gotCve.Content, tt.wantEmptyCVE)
return
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestAPIServerStore_GetSBOM(t *testing.T) {
tools.EnsureSetup(t, err == nil)
err = a.StoreSBOM(tt.args.ctx, tt.sbom)
tools.EnsureSetup(t, err == nil)
gotSbom, err := a.GetSBOM(tt.args.ctx, tt.args.imageID, tt.args.SBOMCreatorVersion)
gotSbom, _ := a.GetSBOM(tt.args.ctx, tt.args.imageID, tt.args.SBOMCreatorVersion)
if (gotSbom.Content == nil) != tt.wantEmptySBOM {
t.Errorf("GetSBOM() gotSbom.Content = %v, wantEmptySBOM %v", gotSbom.Content, tt.wantEmptySBOM)
return
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestAPIServerStore_GetSBOMp(t *testing.T) {
tools.EnsureSetup(t, err == nil)
err = a.storeSBOMp(tt.args.ctx, tt.sbom)
tools.EnsureSetup(t, err == nil)
gotSbom, err := a.GetSBOMp(tt.args.ctx, tt.args.instanceID, tt.args.SBOMCreatorVersion)
gotSbom, _ := a.GetSBOMp(tt.args.ctx, tt.args.instanceID, tt.args.SBOMCreatorVersion)
if (gotSbom.Content == nil) != tt.wantEmptySBOM {
t.Errorf("GetSBOM() gotSbom.Content = %v, wantEmptySBOM %v", gotSbom.Content, tt.wantEmptySBOM)
return
Expand Down
10 changes: 5 additions & 5 deletions repositories/broken.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,31 @@ func NewBrokenStorage() *BrokenStore {
}

func (b BrokenStore) GetSBOM(ctx context.Context, _ string, _ string) (sbom domain.SBOM, err error) {
ctx, span := otel.Tracer("").Start(ctx, "BrokenStore.GetSBOM")
_, span := otel.Tracer("").Start(ctx, "BrokenStore.GetSBOM")
defer span.End()
return domain.SBOM{}, errors.New("expected error")
}

func (b BrokenStore) GetSBOMp(ctx context.Context, _ string, _ string) (sbom domain.SBOM, err error) {
ctx, span := otel.Tracer("").Start(ctx, "BrokenStore.GetSBOMp")
_, span := otel.Tracer("").Start(ctx, "BrokenStore.GetSBOMp")
defer span.End()
return domain.SBOM{}, errors.New("expected error")
}

func (b BrokenStore) StoreSBOM(ctx context.Context, _ domain.SBOM) error {
ctx, span := otel.Tracer("").Start(ctx, "BrokenStore.StoreSBOM")
_, span := otel.Tracer("").Start(ctx, "BrokenStore.StoreSBOM")
defer span.End()
return errors.New("expected error")
}

func (b BrokenStore) GetCVE(ctx context.Context, _ string, _ string, _ string, _ string) (cve domain.CVEManifest, err error) {
ctx, span := otel.Tracer("").Start(ctx, "BrokenStore.GetCVE")
_, span := otel.Tracer("").Start(ctx, "BrokenStore.GetCVE")
defer span.End()
return domain.CVEManifest{}, errors.New("expected error")
}

func (b BrokenStore) StoreCVE(ctx context.Context, cve domain.CVEManifest, withRelevancy bool) error {
ctx, span := otel.Tracer("").Start(ctx, "BrokenStore.StoreCVE")
_, span := otel.Tracer("").Start(ctx, "BrokenStore.StoreCVE")
defer span.End()
return errors.New("expected error")
}
10 changes: 5 additions & 5 deletions repositories/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewMemoryStorage() *MemoryStore {

// GetCVE returns a CVE manifest from an in-memory map
func (m *MemoryStore) GetCVE(ctx context.Context, imageID, SBOMCreatorVersion, CVEScannerVersion, CVEDBVersion string) (cve domain.CVEManifest, err error) {
ctx, span := otel.Tracer("").Start(ctx, "MemoryStore.GetCVE")
_, span := otel.Tracer("").Start(ctx, "MemoryStore.GetCVE")
defer span.End()

id := cveID{
Expand All @@ -57,7 +57,7 @@ func (m *MemoryStore) GetCVE(ctx context.Context, imageID, SBOMCreatorVersion, C

// StoreCVE stores a CVE manifest to an in-memory map
func (m *MemoryStore) StoreCVE(ctx context.Context, cve domain.CVEManifest, withRelevancy bool) error {
ctx, span := otel.Tracer("").Start(ctx, "MemoryStore.StoreCVE")
_, span := otel.Tracer("").Start(ctx, "MemoryStore.StoreCVE")
defer span.End()

id := cveID{
Expand All @@ -72,7 +72,7 @@ func (m *MemoryStore) StoreCVE(ctx context.Context, cve domain.CVEManifest, with

// GetSBOM returns a SBOM from an in-memory map
func (m *MemoryStore) GetSBOM(ctx context.Context, imageID, SBOMCreatorVersion string) (sbom domain.SBOM, err error) {
ctx, span := otel.Tracer("").Start(ctx, "MemoryStore.GetSBOM")
_, span := otel.Tracer("").Start(ctx, "MemoryStore.GetSBOM")
defer span.End()

id := sbomID{
Expand All @@ -87,7 +87,7 @@ func (m *MemoryStore) GetSBOM(ctx context.Context, imageID, SBOMCreatorVersion s

// GetSBOMp returns a SBOM' from an in-memory map
func (m *MemoryStore) GetSBOMp(ctx context.Context, instanceID, SBOMCreatorVersion string) (sbom domain.SBOM, err error) {
ctx, span := otel.Tracer("").Start(ctx, "MemoryStore.GetSBOMp")
_, span := otel.Tracer("").Start(ctx, "MemoryStore.GetSBOMp")
defer span.End()

id := sbomID{
Expand All @@ -102,7 +102,7 @@ func (m *MemoryStore) GetSBOMp(ctx context.Context, instanceID, SBOMCreatorVersi

// StoreSBOM stores an SBOM to an in-memory map
func (m *MemoryStore) StoreSBOM(ctx context.Context, sbom domain.SBOM) error {
ctx, span := otel.Tracer("").Start(ctx, "MemoryStore.StoreSBOM")
_, span := otel.Tracer("").Start(ctx, "MemoryStore.StoreSBOM")
defer span.End()

id := sbomID{
Expand Down

0 comments on commit 6aaf075

Please sign in to comment.