Skip to content

Commit

Permalink
chore: fix linting for adapter package
Browse files Browse the repository at this point in the history
Signed-off-by: Bradley Jones <[email protected]>
  • Loading branch information
bradleyjones committed Sep 5, 2023
1 parent f39b0a6 commit fe4ff60
Show file tree
Hide file tree
Showing 17 changed files with 341 additions and 361 deletions.
4 changes: 2 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ linters:
- errcheck
- errorlint
- exportloopref
- funlen
- gocognit
# - funlen
# - gocognit
- goconst
- gocritic
- gocyclo
Expand Down
14 changes: 10 additions & 4 deletions cmd/anchore-adapter/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"net/http"

"github.com/anchore/harbor-scanner-adapter/pkg/adapter/anchore"
Expand Down Expand Up @@ -47,15 +48,20 @@ func main() {
// Setup TLS
log.WithField("address", adapterConfig.ListenAddr).Info("listening for HTTPS connections")

err = http.ListenAndServeTLS(adapterConfig.ListenAddr, adapterConfig.TLSCertFile, adapterConfig.TLSKeyFile, router)
if err != nil && err != http.ErrServerClosed {
err = http.ListenAndServeTLS(
adapterConfig.ListenAddr,
adapterConfig.TLSCertFile,
adapterConfig.TLSKeyFile,
router,
) // #nosec G114
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.WithField("err", err).Fatalf("error in server listener")
}
} else {
// No TLS
log.WithField("address", adapterConfig.ListenAddr).Info("listening for HTTP connections")
err = http.ListenAndServe(adapterConfig.ListenAddr, router)
if err != nil && err != http.ErrServerClosed {
err = http.ListenAndServe(adapterConfig.ListenAddr, router) // #nosec G114
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.WithField("err", err).Fatalf("error in server listener")
}
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const (
RawVulnReportMimeType = "application/vnd.scanner.adapter.vuln.report.raw+json"
DockerImageMimeType = "application/vnd.docker.distribution.manifest.v2+json"
OciImageMimeType = "application/vnd.oci.image.manifest.v1+json"
HarborMetadataVulnDbUpdateKey = "harbor.scanner-adapter/vulnerability-database-updated-at"
HarborMetadataVulnDBUpdateKey = "harbor.scanner-adapter/vulnerability-database-updated-at"
HarborMetadataScannerTypeKey = "harbor.scanner-adapter/scanner-type"
AdapterType = "os-package-vulnerability"
AdapterType = "os-package-vulnerability" // #nosec G101
AdapterVersion = "1.0.0"
AdapterVendor = "Anchore Inc."
AdapterName = "Anchore"
Expand All @@ -36,7 +36,7 @@ var AdapterMetadata = harbor.ScannerAdapterMetadata{
},
},
Properties: map[string]string{
HarborMetadataVulnDbUpdateKey: "", // This gets updated in response to requests from Harbor
HarborMetadataVulnDBUpdateKey: "", // This gets updated in response to requests from Harbor
HarborMetadataScannerTypeKey: AdapterType,
},
}
Expand All @@ -45,6 +45,6 @@ var AdapterMetadata = harbor.ScannerAdapterMetadata{
type ScannerAdapter interface {
GetMetadata() (harbor.ScannerAdapterMetadata, error)
Scan(req harbor.ScanRequest) (harbor.ScanResponse, error)
GetHarborVulnerabilityReport(scanId string, includeDescriptions bool) (*harbor.VulnerabilityReport, error)
GetRawVulnerabilityReport(scanId string) (harbor.RawReport, error)
GetHarborVulnerabilityReport(scanID string, includeDescriptions bool) (*harbor.VulnerabilityReport, error)
GetRawVulnerabilityReport(scanID string) (harbor.RawReport, error)
}
179 changes: 98 additions & 81 deletions pkg/adapter/anchore/adapter.go

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions pkg/adapter/anchore/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,14 @@ func TestScanIdToRegistryDigest(t *testing.T) {
}

for _, v := range inputs {
generated, err := GenerateScanId(v[1], v[2])
generated, err := GenerateScanID(v[1], v[2])
if err != nil {
t.Errorf("failed: %v", err)
}

if repo, dig, err := ScanIdToRegistryDigest(generated); (err == nil) != (v[3] == "true") {
if repo, dig, err := ScanIDToRegistryDigest(generated); (err == nil) != (v[3] == "true") {
t.Errorf("Failed test. Repo=%v, Digest=%v err=%v", repo, dig, err)
}

}
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/adapter/anchore/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type CacheConfiguration struct {
VulnDescriptionCacheEnabled bool
VulnDescriptionCacheMaxCount int
VulnDescriptionCacheTTL int
DbUpdateCacheEnabled bool
DbUpdatedCacheTTL int
DBUpdateCacheEnabled bool
DBUpdatedCacheTTL int
VulnReportCacheEnabled bool
VulnReportCacheMaxCount int
VulnReportCacheTTL int
Expand All @@ -38,14 +38,14 @@ type LockingTTLCache struct {
Enabled bool // If true, use the cache, else always bypass
}

// Cache for vulnerability description text since those must be retrieved from the Anchore APIs separately
// DescriptionCache for vulnerability description text since those must be retrieved from the Anchore APIs separately
// This can be removed if/when the Anchore API vulnerability response includes descriptions directly
var DescriptionCache *LockingTTLCache

// Cache for the vulnerability response from the Anchore API
// ReportCache for the vulnerability response from the Anchore API
var ReportCache *LockingTTLCache

// Cache for storing vuln db update timestamps to minimize the calls to get the db timestamp since it isn't part of
// UpdateTimestampCache for storing vuln db update timestamps to minimize the calls to get the db timestamp since it isn't part of
// the vulnerability response
var UpdateTimestampCache *LockingTTLCache

Expand Down Expand Up @@ -113,6 +113,6 @@ func InitCaches(configuration CacheConfiguration) error {
configuration.VulnReportCacheMaxCount,
configuration.VulnReportCacheTTL,
)
UpdateTimestampCache = NewCache(configuration.DbUpdateCacheEnabled, 1, configuration.DbUpdatedCacheTTL)
UpdateTimestampCache = NewCache(configuration.DBUpdateCacheEnabled, 1, configuration.DBUpdatedCacheTTL)
return nil
}
15 changes: 5 additions & 10 deletions pkg/adapter/anchore/cache_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package anchore

import (
"crypto/rand"
"encoding/base64"
"fmt"
"math/rand"
"testing"
"time"

Expand Down Expand Up @@ -47,10 +47,8 @@ func TestGetCachedDbUpdateTime(t *testing.T) {

if t2, ok := UpdateTimestampCache.Get("db"); !ok {
t.Fatal("not cached")
} else {
if t2 != testT {
t.Fatal("wrong ts cached")
}
} else if t2 != testT {
t.Fatal("wrong ts cached")
}
}

Expand Down Expand Up @@ -127,13 +125,12 @@ func TestCacheVulnDescription(t *testing.T) {
if ok {
t.Fatal("should not get value after flush")
}

}

func TestCacheVulnDescriptionTimeout(t *testing.T) {
t.SkipNow()
err := InitCaches(DefaultCacheConfig)
testTTL := time.Duration(3 * time.Second)
testTTL := 3 * time.Second
DescriptionCache.TTL = testTTL
if err != nil {
t.Fatal(err)
Expand All @@ -157,7 +154,6 @@ func TestCacheVulnDescriptionTimeout(t *testing.T) {
if DescriptionCache.Cache.Len() > 0 {
t.Fatal("should not have any cached entries after ttl + request")
}

}

// Test for manual checks of memory usage for various sizes of data
Expand All @@ -167,7 +163,7 @@ func TestVulnDescriptionCacheSize(t *testing.T) {
if err != nil {
t.Fatal(err)
}
var tmp = make([]byte, 1000)
tmp := make([]byte, 1000)

for i := 0; i < 10000; i++ {
_, err := rand.Read(tmp)
Expand All @@ -183,7 +179,6 @@ func TestVulnDescriptionCacheSize(t *testing.T) {
Description: desc,
})
}

}

// Vuln report cache tests
Expand Down
Loading

0 comments on commit fe4ff60

Please sign in to comment.