-
Notifications
You must be signed in to change notification settings - Fork 183
Add golang based hook-sdk #3786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
500d337
Add hook-sdk for golang hooks
J12934 3fcc9da
Resolve review comments
J12934 e07ff46
Properly set defaults and ensure that node.js hook taskfile builds are
J12934 484e15f
Rework sdk
J12934 ead2fbf
Add some docs for the golang hook sdk
J12934 5a8d81f
Have the build context be set more cleanly for new golang based hooks
J12934 e509b8a
Ensure that severities are properly uppercased
J12934 3d52d87
Resolve minor inconsistency with url and arg handling
J12934 3dca955
Add a simple test to the sdk
J12934 b050de3
add license header stuff
J12934 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
- Loading branch information
commit 484e15fb64fbf95a7022cc95ea946a70582c835d
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // SPDX-FileCopyrightText: the secureCodeBox authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package hooksdk | ||
|
|
||
| import "context" | ||
|
|
||
| // HookRequestMock is a configurable HookRequest implementation for hook tests. | ||
| // Unconfigured methods return their zero values without an error. | ||
| type HookRequestMock struct { | ||
| ScanFunc func(context.Context) (*Scan, error) | ||
| GetRawResultsFunc func(context.Context) (string, error) | ||
| GetFindingsFunc func(context.Context) ([]Finding, error) | ||
| UpdateRawResultsFunc func(context.Context, string) error | ||
| UpdateFindingsFunc func(context.Context, []Finding) error | ||
| } | ||
|
|
||
| func (m *HookRequestMock) Scan(ctx context.Context) (*Scan, error) { | ||
| if m.ScanFunc != nil { | ||
| return m.ScanFunc(ctx) | ||
| } | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (m *HookRequestMock) GetRawResults(ctx context.Context) (string, error) { | ||
| if m.GetRawResultsFunc != nil { | ||
| return m.GetRawResultsFunc(ctx) | ||
| } | ||
| return "", nil | ||
| } | ||
|
|
||
| func (m *HookRequestMock) GetFindings(ctx context.Context) ([]Finding, error) { | ||
| if m.GetFindingsFunc != nil { | ||
| return m.GetFindingsFunc(ctx) | ||
| } | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (m *HookRequestMock) UpdateRawResults(ctx context.Context, content string) error { | ||
| if m.UpdateRawResultsFunc != nil { | ||
| return m.UpdateRawResultsFunc(ctx, content) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (m *HookRequestMock) UpdateFindings(ctx context.Context, findings []Finding) error { | ||
| if m.UpdateFindingsFunc != nil { | ||
| return m.UpdateFindingsFunc(ctx, findings) | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // SPDX-FileCopyrightText: the secureCodeBox authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package hooksdk | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "testing" | ||
| ) | ||
|
|
||
| type contextKey struct{} | ||
|
|
||
| type stubK8sClient struct { | ||
| scanCtx context.Context | ||
| patchCtx context.Context | ||
| } | ||
|
|
||
| func (s *stubK8sClient) GetScan(ctx context.Context, _, _ string) (*Scan, error) { | ||
| s.scanCtx = ctx | ||
| return &Scan{Name: "scan"}, nil | ||
| } | ||
|
|
||
| func (s *stubK8sClient) PatchScanStatus(ctx context.Context, _, _ string, _ []Finding) error { | ||
| s.patchCtx = ctx | ||
| return nil | ||
| } | ||
|
|
||
| type stubFileClient struct { | ||
| downloadTextCtx context.Context | ||
| downloadJSONCtx context.Context | ||
| uploadCtx context.Context | ||
| } | ||
|
|
||
| func (s *stubFileClient) DownloadText(ctx context.Context, _ string) (string, error) { | ||
| s.downloadTextCtx = ctx | ||
| return "raw results", nil | ||
| } | ||
|
|
||
| func (s *stubFileClient) DownloadJSON(ctx context.Context, _ string, value any) error { | ||
| s.downloadJSONCtx = ctx | ||
| data, err := json.Marshal([]Finding{validFinding()}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return json.Unmarshal(data, value) | ||
| } | ||
|
|
||
| func (s *stubFileClient) Upload(ctx context.Context, _ string, _ string, _ []byte) error { | ||
| s.uploadCtx = ctx | ||
| return nil | ||
| } | ||
|
|
||
| func TestHookRequestDefersOperationsAndPassesContext(t *testing.T) { | ||
| k8sClient := &stubK8sClient{} | ||
| fileClient := &stubFileClient{} | ||
| request := &hookRequest{ | ||
| k8sClient: k8sClient, | ||
| fileClient: fileClient, | ||
| scanName: "scan", | ||
| namespace: "default", | ||
| urls: []string{"raw", "findings", "raw-upload", "findings-upload"}, | ||
| } | ||
| ctx := context.WithValue(context.Background(), contextKey{}, "hook context") | ||
|
|
||
| if _, err := request.Scan(ctx); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := request.GetRawResults(ctx); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| findings, err := request.GetFindings(ctx) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := request.UpdateRawResults(ctx, "updated raw results"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := request.UpdateFindings(ctx, findings); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| for name, actual := range map[string]context.Context{ | ||
| "GetScan": k8sClient.scanCtx, | ||
| "DownloadText": fileClient.downloadTextCtx, | ||
| "DownloadJSON": fileClient.downloadJSONCtx, | ||
| "Upload": fileClient.uploadCtx, | ||
| "PatchScanStatus": k8sClient.patchCtx, | ||
| } { | ||
| if actual != ctx { | ||
| t.Errorf("%s received a different context", name) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.