Skip to content

Commit 5237d40

Browse files
authored
Merge pull request #206 from metraction/PH-205/allow-contextlabels-in-reporter
Ph 205/allow contextlabels in reporter<<< action:create-release
2 parents 6d5e2f3 + 3700260 commit 5237d40

13 files changed

Lines changed: 48 additions & 24 deletions

File tree

.github/workflows/build.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ on:
1212
- 'PH-196/*'
1313
- 'PH-198/*'
1414
- 'PH-201/*'
15+
- 'PH-205/*'
1516
pull_request:
1617
branches:
1718
- main

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ require (
5050
github.com/dustinkirkland/golang-petname v0.0.0-20240428194347-eebcea082ee0
5151
github.com/go-chi/chi/v5 v5.2.3
5252
github.com/go-git/go-git/v5 v5.16.3
53-
github.com/metraction/handwheel v0.0.3
53+
github.com/metraction/handwheel v0.0.4
5454
github.com/otiai10/copy v1.14.1
5555
github.com/prometheus/client_golang v1.23.2
5656
github.com/theory/jsonpath v0.10.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
202202
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
203203
github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs=
204204
github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
205-
github.com/metraction/handwheel v0.0.3 h1:Gyw38RnqzJzwMMYw5EBJ4XDXnGvPz8CF0cOZkXT3RIk=
206-
github.com/metraction/handwheel v0.0.3/go.mod h1:Yh3ep7n59UXW8m9hMIuEzJ+T72Z6pVoO6hDFx94+A6M=
205+
github.com/metraction/handwheel v0.0.4 h1:dHacUX93NMZoFXH39bUQdmb/fHNYfJFNeqdtlY94tjY=
206+
github.com/metraction/handwheel v0.0.4/go.mod h1:Yh3ep7n59UXW8m9hMIuEzJ+T72Z6pVoO6hDFx94+A6M=
207207
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
208208
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
209209
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=

helm/pharos/templates/configmap.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ data:
88
.pharos.yaml: |
99
{{- if .Values.redis.enabled }}
1010
redis:
11-
dsn: {{ .Release.Namespace }}-redis.{{ .Release.Namespace }}.svc.cluster.local:6379
11+
dsn: {{ .Release.Name }}-redis.{{ .Release.Namespace }}.svc.cluster.local:6379
1212
{{- else }}
1313
redis:
1414
dsn: {{ .Values.externalRedis.host | default "localhost" }}:{{ .Values.externalRedis.port | default 6379 }}
1515
{{- end }}
1616
{{- if .Values.redis.enabled }}
1717
scanner:
18-
cacheEndpoint: redis://{{ .Release.Namespace }}-redis.{{ .Release.Namespace }}.svc.cluster.local:6379
18+
cacheEndpoint: redis://{{ .Release.Name }}-redis.{{ .Release.Namespace }}.svc.cluster.local:6379
1919
{{- else }}
2020
scanner:
2121
cacheEndpoint: redis://{{ .Values.externalRedis.host | default "localhost" }}:{{ .Values.externalRedis.port | default 6379 }}

helm/pharos/templates/deployment-reporter.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ spec:
4848
value: {{ .Values.prometheus.query }}
4949
- name: PHAROS_PROMETHEUS_INTERVAL
5050
value: {{ .Values.prometheus.interval }}
51+
{{- if .Values.prometheus.contextLabels}}
52+
- name: PHAROS_PROMETHEUS_CONTEXTLABELS
53+
value: {{ .Values.prometheus.contextLabels | quote }}
54+
{{- end }}
5155
- name: PHAROS_PROMETHEUS_NAMESPACE
5256
valueFrom:
5357
fieldRef:

helm/pharos/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ prometheus:
221221
query: 'kube_pod_container_info{}'
222222
# -- Interval for scanning images
223223
interval: 10m
224-
# -- Context labels to add to the Prometheus context
224+
# -- Context labels to add to the Prometheus context, separate by comma
225225
contextLabels: "namespace"
226226
# -- Time to live for the scan results, defaults to 12 hours
227227
ttl: "12h"

pkg/grype/grype.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ func (rx *GrypeScanner) VulnScanSbom(sbom []byte) (grypetype.GrypeScanType, []by
178178

179179
rx.wgDbUpdate.Wait() // wait in case of running db update
180180

181-
ctx, cancel := context.WithTimeout(context.Background(), rx.ScanTimeout)
181+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
182182
defer cancel()
183183

184184
elapsed := utils.ElapsedFunc()
185-
cmd := exec.Command(rx.ScannerBin, "-o", "json") // cyclonedx-json has no "fixed" state ;-(
185+
cmd := exec.CommandContext(ctx, rx.ScannerBin, "-o", "json") // cyclonedx-json has no "fixed" state ;-(
186186
cmd.Stdin = bytes.NewReader(sbom)
187187
cmd.Stdout = &stdout
188188
cmd.Stderr = &stderr

pkg/grype/grype_scan.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"time"
78

89
"github.com/dustin/go-humanize"
910
"github.com/metraction/pharos/internal/integrations/cache"
@@ -43,7 +44,9 @@ func ScanImage(task model.PharosScanTask, scanEngine *GrypeScanner, kvc *cache.P
4344
return lo.Substring(digest, 0, 39) + ".grype.sbom"
4445
}
4546

46-
ctx := context.Background()
47+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
48+
defer cancel()
49+
4750
var sbomEngine *syft.SyftSbomCreator
4851

4952
result.ScanTask.Status = "get-digest"

pkg/grype/helper.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package grype
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"os"
@@ -75,7 +76,9 @@ func GrypeExeOutput[T any](cmd *exec.Cmd) (T, error) {
7576
// check grype binary version
7677
func GetScannerVersion(scannerBin string) (string, error) {
7778

78-
cmd := exec.Command(scannerBin, "version", "-o", "json")
79+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
80+
defer cancel()
81+
cmd := exec.CommandContext(ctx, scannerBin, "version", "-o", "json")
7982
cmd.Env = append(cmd.Env, "GRYPE_CHECK_FOR_APP_UPDATE=false")
8083

8184
result, err := GrypeExeOutput[GrypeVersion](cmd)
@@ -88,7 +91,9 @@ func GetScannerVersion(scannerBin string) (string, error) {
8891
// check grype local database status, update DbState
8992
func GetDatabaseStatus(scannerBin string, targetDir string) (string, time.Time, error) {
9093

91-
cmd := exec.Command(scannerBin, "db", "status", "-o", "json")
94+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
95+
defer cancel()
96+
cmd := exec.CommandContext(ctx, scannerBin, "db", "status", "-o", "json")
9297
cmd.Env = append(cmd.Env, "GRYPE_CHECK_FOR_APP_UPDATE=false")
9398
cmd.Env = append(cmd.Env, "GRYPE_DB_CACHE_DIR="+targetDir)
9499

@@ -102,7 +107,9 @@ func GetDatabaseStatus(scannerBin string, targetDir string) (string, time.Time,
102107
// check if local db in targetDir requires an update
103108
func GrypeUpdateRequired(scannerBin, targetDir string) bool {
104109

105-
cmd := exec.Command(scannerBin, "db", "check", "-o", "json")
110+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
111+
defer cancel()
112+
cmd := exec.CommandContext(ctx, scannerBin, "db", "check", "-o", "json")
106113
cmd.Env = append(cmd.Env, "GRYPE_CHECK_FOR_APP_UPDATE=false")
107114
cmd.Env = append(cmd.Env, "GRYPE_DB_CACHE_DIR="+targetDir)
108115

@@ -118,7 +125,9 @@ func GetGrypeUpdate(scannerBin, targetDir string) error {
118125

119126
// do update
120127
var stdout, stderr bytes.Buffer
121-
cmd := exec.Command(scannerBin, "db", "update", "--quiet")
128+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
129+
defer cancel()
130+
cmd := exec.CommandContext(ctx, scannerBin, "db", "update", "--quiet")
122131
cmd.Env = append(cmd.Env, "GRYPE_CHECK_FOR_APP_UPDATE=false")
123132
cmd.Env = append(cmd.Env, "GRYPE_DB_CACHE_DIR="+targetDir)
124133
cmd.Stdout = &stdout
@@ -137,7 +146,9 @@ func GrypeTestScan(scannerBin, targetDir string) error {
137146

138147
var stdout, stderr bytes.Buffer
139148

140-
cmd := exec.Command(scannerBin, "-o", "json")
149+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
150+
defer cancel()
151+
cmd := exec.CommandContext(ctx, scannerBin, "-o", "json")
141152
cmd.Stdin = bytes.NewReader(sampleSbom)
142153
cmd.Stdout = &stdout
143154
cmd.Stderr = &stderr

pkg/images/digest.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package images
22

33
import (
4+
"context"
45
"crypto/tls"
56
"encoding/json"
67
"fmt"
78
"net/http"
89
"strings"
10+
"time"
911

1012
"github.com/google/go-containerregistry/pkg/authn"
1113
"github.com/google/go-containerregistry/pkg/name"
@@ -44,7 +46,11 @@ func GetImageDigests(task model.PharosScanTask) (string, string, string, error)
4446
digest := sbom.Source.Metadata.Digests[0].Algorithm + ":" + sbom.Source.Metadata.Digests[0].Value
4547
return digest, digest, "", nil
4648
}
49+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1)
50+
defer cancel()
51+
4752
var options []remote.Option
53+
options = append(options, remote.WithContext(ctx))
4854
platform := task.Platform // TODO: Empty platform -> default? What if @sha:.. is given, so no platform needed
4955

5056
ref, err := name.ParseReference(task.ImageSpec)

0 commit comments

Comments
 (0)