Skip to content

Commit 63fd801

Browse files
committed
Rework sdk
- have the hook request implement the methods in a more straight forward way - pass in ctx into the hook function to keep best practices and ensure the funcs remain cancellable - include a test helper in the sdk to keep hook boilerplate out of the hooks Signed-off-by: Jannik Hollenbach <jannik.hollenbach@iteratec.com>
1 parent 391b8fb commit 63fd801

5 files changed

Lines changed: 237 additions & 103 deletions

File tree

‎hook-sdk/golang/mock.go‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-FileCopyrightText: the secureCodeBox authors
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package hooksdk
6+
7+
import "context"
8+
9+
// HookRequestMock is a configurable HookRequest implementation for hook tests.
10+
// Unconfigured methods return their zero values without an error.
11+
type HookRequestMock struct {
12+
ScanFunc func(context.Context) (*Scan, error)
13+
GetRawResultsFunc func(context.Context) (string, error)
14+
GetFindingsFunc func(context.Context) ([]Finding, error)
15+
UpdateRawResultsFunc func(context.Context, string) error
16+
UpdateFindingsFunc func(context.Context, []Finding) error
17+
}
18+
19+
func (m *HookRequestMock) Scan(ctx context.Context) (*Scan, error) {
20+
if m.ScanFunc != nil {
21+
return m.ScanFunc(ctx)
22+
}
23+
return nil, nil
24+
}
25+
26+
func (m *HookRequestMock) GetRawResults(ctx context.Context) (string, error) {
27+
if m.GetRawResultsFunc != nil {
28+
return m.GetRawResultsFunc(ctx)
29+
}
30+
return "", nil
31+
}
32+
33+
func (m *HookRequestMock) GetFindings(ctx context.Context) ([]Finding, error) {
34+
if m.GetFindingsFunc != nil {
35+
return m.GetFindingsFunc(ctx)
36+
}
37+
return nil, nil
38+
}
39+
40+
func (m *HookRequestMock) UpdateRawResults(ctx context.Context, content string) error {
41+
if m.UpdateRawResultsFunc != nil {
42+
return m.UpdateRawResultsFunc(ctx, content)
43+
}
44+
return nil
45+
}
46+
47+
func (m *HookRequestMock) UpdateFindings(ctx context.Context, findings []Finding) error {
48+
if m.UpdateFindingsFunc != nil {
49+
return m.UpdateFindingsFunc(ctx, findings)
50+
}
51+
return nil
52+
}

‎hook-sdk/golang/sdk.go‎

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,90 @@ type HookHandler interface {
1717
}
1818

1919
type HookRequest interface {
20-
Scan() *Scan
21-
GetRawResults() (string, error)
22-
GetFindings() ([]Finding, error)
23-
UpdateRawResults(content string) error
24-
UpdateFindings(findings []Finding) error
20+
Scan(ctx context.Context) (*Scan, error)
21+
GetRawResults(ctx context.Context) (string, error)
22+
GetFindings(ctx context.Context) ([]Finding, error)
23+
UpdateRawResults(ctx context.Context, content string) error
24+
UpdateFindings(ctx context.Context, findings []Finding) error
2525
}
2626

2727
type hookRequest struct {
28-
scan *Scan
29-
getRawResults func() (string, error)
30-
getFindings func() ([]Finding, error)
31-
updateRawResults func(string) error
32-
updateFindings func([]Finding) error
28+
k8sClient K8sClient
29+
fileClient FileClient
30+
scanName string
31+
namespace string
32+
urls []string
33+
}
34+
35+
func (r *hookRequest) Scan(ctx context.Context) (*Scan, error) {
36+
scan, err := r.k8sClient.GetScan(ctx, r.scanName, r.namespace)
37+
if err != nil {
38+
return nil, fmt.Errorf("get Scan from Kubernetes API: %w", err)
39+
}
40+
return scan, nil
41+
}
42+
43+
func (r *hookRequest) GetRawResults(ctx context.Context) (string, error) {
44+
url := r.urlAt(0)
45+
if url == "" {
46+
return "", fmt.Errorf("raw results download URL not provided")
47+
}
48+
return r.fileClient.DownloadText(ctx, url)
49+
}
50+
51+
func (r *hookRequest) GetFindings(ctx context.Context) ([]Finding, error) {
52+
url := r.urlAt(1)
53+
if url == "" {
54+
return nil, fmt.Errorf("findings download URL not provided")
55+
}
56+
var findings []Finding
57+
if err := r.fileClient.DownloadJSON(ctx, url, &findings); err != nil {
58+
return nil, fmt.Errorf("download findings: %w", err)
59+
}
60+
for index, finding := range findings {
61+
if err := ValidateFinding(finding, index); err != nil {
62+
return nil, err
63+
}
64+
}
65+
return findings, nil
3366
}
3467

35-
func (r *hookRequest) Scan() *Scan { return r.scan }
36-
func (r *hookRequest) GetRawResults() (string, error) { return r.getRawResults() }
37-
func (r *hookRequest) GetFindings() ([]Finding, error) { return r.getFindings() }
38-
func (r *hookRequest) UpdateRawResults(content string) error { return r.updateRawResults(content) }
39-
func (r *hookRequest) UpdateFindings(findings []Finding) error {
40-
return r.updateFindings(findings)
68+
func (r *hookRequest) UpdateRawResults(ctx context.Context, content string) error {
69+
url := r.urlAt(2)
70+
if url == "" {
71+
return fmt.Errorf("cannot update raw results in a ReadOnly hook")
72+
}
73+
return r.fileClient.Upload(ctx, url, "", []byte(content))
74+
}
75+
76+
func (r *hookRequest) UpdateFindings(ctx context.Context, findings []Finding) error {
77+
url := r.urlAt(3)
78+
if url == "" {
79+
return fmt.Errorf("cannot update findings in a ReadOnly hook")
80+
}
81+
for index, finding := range findings {
82+
if err := ValidateFinding(finding, index); err != nil {
83+
return err
84+
}
85+
}
86+
body, err := json.Marshal(findings)
87+
if err != nil {
88+
return fmt.Errorf("marshal findings: %w", err)
89+
}
90+
if err := r.fileClient.Upload(ctx, url, "", body); err != nil {
91+
return fmt.Errorf("upload findings: %w", err)
92+
}
93+
if err := r.k8sClient.PatchScanStatus(ctx, r.scanName, r.namespace, findings); err != nil {
94+
return fmt.Errorf("update scan status: %w", err)
95+
}
96+
return nil
97+
}
98+
99+
func (r *hookRequest) urlAt(index int) string {
100+
if index < len(r.urls) {
101+
return r.urls[index]
102+
}
103+
return ""
41104
}
42105

43106
type Client struct {
@@ -85,70 +148,7 @@ func NewClient(opts ...Option) (*Client, error) {
85148
}
86149

87150
func (c *Client) Run(ctx context.Context, handler HookHandler) error {
88-
scan, err := c.k8sClient.GetScan(ctx, c.scanName, c.namespace)
89-
if err != nil {
90-
return fmt.Errorf("get Scan from Kubernetes API: %w", err)
91-
}
92-
urls := c.args[1:]
93-
urlAt := func(index int) string {
94-
if index < len(urls) {
95-
return urls[index]
96-
}
97-
return ""
98-
}
99-
getRawResults := func() (string, error) {
100-
url := urlAt(0)
101-
if url == "" {
102-
return "", fmt.Errorf("raw results download URL not provided")
103-
}
104-
return c.fileClient.DownloadText(ctx, url)
105-
}
106-
getFindings := func() ([]Finding, error) {
107-
url := urlAt(1)
108-
if url == "" {
109-
return nil, fmt.Errorf("findings download URL not provided")
110-
}
111-
var findings []Finding
112-
if err := c.fileClient.DownloadJSON(ctx, url, &findings); err != nil {
113-
return nil, fmt.Errorf("download findings: %w", err)
114-
}
115-
for index, finding := range findings {
116-
if err := ValidateFinding(finding, index); err != nil {
117-
return nil, err
118-
}
119-
}
120-
return findings, nil
121-
}
122-
updateRawResults := func(content string) error {
123-
url := urlAt(2)
124-
if url == "" {
125-
return fmt.Errorf("cannot update raw results in a ReadOnly hook")
126-
}
127-
return c.fileClient.Upload(ctx, url, "", []byte(content))
128-
}
129-
updateFindings := func(findings []Finding) error {
130-
url := urlAt(3)
131-
if url == "" {
132-
return fmt.Errorf("cannot update findings in a ReadOnly hook")
133-
}
134-
for index, finding := range findings {
135-
if err := ValidateFinding(finding, index); err != nil {
136-
return err
137-
}
138-
}
139-
body, err := json.Marshal(findings)
140-
if err != nil {
141-
return fmt.Errorf("marshal findings: %w", err)
142-
}
143-
if err := c.fileClient.Upload(ctx, url, "", body); err != nil {
144-
return fmt.Errorf("upload findings: %w", err)
145-
}
146-
if err := c.k8sClient.PatchScanStatus(ctx, c.scanName, c.namespace, findings); err != nil {
147-
return fmt.Errorf("update scan status: %w", err)
148-
}
149-
return nil
150-
}
151-
request := &hookRequest{scan: scan, getRawResults: getRawResults, getFindings: getFindings, updateRawResults: updateRawResults, updateFindings: updateFindings}
151+
request := &hookRequest{k8sClient: c.k8sClient, fileClient: c.fileClient, scanName: c.scanName, namespace: c.namespace, urls: c.args[1:]}
152152
if err := handler.Handle(ctx, request); err != nil {
153153
return fmt.Errorf("run hook handler: %w", err)
154154
}

‎hook-sdk/golang/sdk_test.go‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// SPDX-FileCopyrightText: the secureCodeBox authors
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package hooksdk
6+
7+
import (
8+
"context"
9+
"encoding/json"
10+
"testing"
11+
)
12+
13+
type contextKey struct{}
14+
15+
type stubK8sClient struct {
16+
scanCtx context.Context
17+
patchCtx context.Context
18+
}
19+
20+
func (s *stubK8sClient) GetScan(ctx context.Context, _, _ string) (*Scan, error) {
21+
s.scanCtx = ctx
22+
return &Scan{Name: "scan"}, nil
23+
}
24+
25+
func (s *stubK8sClient) PatchScanStatus(ctx context.Context, _, _ string, _ []Finding) error {
26+
s.patchCtx = ctx
27+
return nil
28+
}
29+
30+
type stubFileClient struct {
31+
downloadTextCtx context.Context
32+
downloadJSONCtx context.Context
33+
uploadCtx context.Context
34+
}
35+
36+
func (s *stubFileClient) DownloadText(ctx context.Context, _ string) (string, error) {
37+
s.downloadTextCtx = ctx
38+
return "raw results", nil
39+
}
40+
41+
func (s *stubFileClient) DownloadJSON(ctx context.Context, _ string, value any) error {
42+
s.downloadJSONCtx = ctx
43+
data, err := json.Marshal([]Finding{validFinding()})
44+
if err != nil {
45+
return err
46+
}
47+
return json.Unmarshal(data, value)
48+
}
49+
50+
func (s *stubFileClient) Upload(ctx context.Context, _ string, _ string, _ []byte) error {
51+
s.uploadCtx = ctx
52+
return nil
53+
}
54+
55+
func TestHookRequestDefersOperationsAndPassesContext(t *testing.T) {
56+
k8sClient := &stubK8sClient{}
57+
fileClient := &stubFileClient{}
58+
request := &hookRequest{
59+
k8sClient: k8sClient,
60+
fileClient: fileClient,
61+
scanName: "scan",
62+
namespace: "default",
63+
urls: []string{"raw", "findings", "raw-upload", "findings-upload"},
64+
}
65+
ctx := context.WithValue(context.Background(), contextKey{}, "hook context")
66+
67+
if _, err := request.Scan(ctx); err != nil {
68+
t.Fatal(err)
69+
}
70+
if _, err := request.GetRawResults(ctx); err != nil {
71+
t.Fatal(err)
72+
}
73+
findings, err := request.GetFindings(ctx)
74+
if err != nil {
75+
t.Fatal(err)
76+
}
77+
if err := request.UpdateRawResults(ctx, "updated raw results"); err != nil {
78+
t.Fatal(err)
79+
}
80+
if err := request.UpdateFindings(ctx, findings); err != nil {
81+
t.Fatal(err)
82+
}
83+
84+
for name, actual := range map[string]context.Context{
85+
"GetScan": k8sClient.scanCtx,
86+
"DownloadText": fileClient.downloadTextCtx,
87+
"DownloadJSON": fileClient.downloadJSONCtx,
88+
"Upload": fileClient.uploadCtx,
89+
"PatchScanStatus": k8sClient.patchCtx,
90+
} {
91+
if actual != ctx {
92+
t.Errorf("%s received a different context", name)
93+
}
94+
}
95+
}

‎hooks/finding-post-processing/hook/handler.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ func newHandler(rulesJSON string) (*handler, error) {
6868
return &handler{rules: rules}, nil
6969
}
7070

71-
func (h *handler) Handle(_ context.Context, request hooksdk.HookRequest) error {
72-
findings, err := request.GetFindings()
71+
func (h *handler) Handle(ctx context.Context, request hooksdk.HookRequest) error {
72+
findings, err := request.GetFindings(ctx)
7373
if err != nil {
7474
return err
7575
}
@@ -83,7 +83,7 @@ func (h *handler) Handle(_ context.Context, request hooksdk.HookRequest) error {
8383
}
8484
}
8585
if matched {
86-
return request.UpdateFindings(findings)
86+
return request.UpdateFindings(ctx, findings)
8787
}
8888
return nil
8989
}

0 commit comments

Comments
 (0)