-
Notifications
You must be signed in to change notification settings - Fork 180
Pre submit #16
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
Pre submit #16
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3f81336
Add pre-submit for tests
ianlewis 656991c
Add basic test for HostedActionsProvenance
ianlewis b657423
Add a couple more tests
ianlewis 2b368ad
Fix pre-submit step name
ianlewis aefd7f4
Remove err as it's not used
ianlewis c577132
Add better error checking in tests
ianlewis 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
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,25 @@ | ||
| name: Pre submits | ||
| on: [pull_request, workflow_dispatch] | ||
|
|
||
| permissions: read-all | ||
|
|
||
| jobs: | ||
| pre-submit: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579 # v2.3.4 | ||
|
|
||
| - name: setup-go | ||
| uses: actions/setup-go@bfdd3570ce990073878bf10f6b2d79082de49492 # v2.2.0 | ||
| with: | ||
| go-version: "1.18" | ||
|
|
||
| - name: Run tests | ||
| run: | | ||
| set -euo pipefail | ||
|
|
||
| # Download dependencies. | ||
| go mod vendor | ||
| # Test. | ||
| go test -mod=vendor -v ./... | ||
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,71 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "errors" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| ) | ||
|
|
||
| func TestRequestOIDCToken(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| audience string | ||
| expected *OIDCToken | ||
| raw string | ||
| err error | ||
| }{ | ||
| { | ||
| name: "basic token", | ||
| audience: "hoge", | ||
| expected: &OIDCToken{JobWorkflowRef: "hoge"}, | ||
| }, | ||
| { | ||
| name: "invalid response", | ||
| audience: "hoge", | ||
| raw: `not json`, | ||
| err: errResponseJSON, | ||
| }, | ||
| { | ||
| name: "invalid parts", | ||
| audience: "hoge", | ||
| raw: `{"value": "part1"}`, | ||
| err: errInvalidToken, | ||
| }, | ||
| { | ||
| name: "invalid base64", | ||
| audience: "hoge", | ||
| raw: `{"value": "part1.part2.part3"}`, | ||
| err: errInvalidTokenB64, | ||
| }, | ||
| { | ||
| name: "invalid json", | ||
| audience: "hoge", | ||
| raw: fmt.Sprintf(`{"value": "part1.%s.part3"}`, base64.RawURLEncoding.EncodeToString([]byte("not json"))), | ||
| err: errInvalidTokenJSON, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| if tc.expected != nil { | ||
| _, stop := NewTestOIDCServer(tc.expected) | ||
| defer stop() | ||
| } else { | ||
| _, stop := newRawTestOIDCServer(tc.raw) | ||
| defer stop() | ||
| } | ||
|
|
||
| token, err := RequestOIDCToken(tc.audience) | ||
| if !errors.Is(err, tc.err) { | ||
| t.Fatalf("unexpected error: %v", cmp.Diff(err, tc.err, cmpopts.EquateErrors())) | ||
| } | ||
| if want, got := tc.expected, token; !cmp.Equal(want, got) { | ||
| t.Errorf("unexpected workflow ref\nwant: %#v\ngot: %#v\ndiff: %#v", want, got, cmp.Diff(want, got)) | ||
| } | ||
| }) | ||
| } | ||
| } |
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,41 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| ) | ||
|
|
||
| // NewTestOIDCServer returns an httptest.Server that can be used as the OIDC | ||
| // server in tests and a cleanup function that can be used to stop and clean up | ||
| // the server. The server returns the given token when queried. | ||
| func NewTestOIDCServer(t *OIDCToken) (*httptest.Server, func()) { | ||
| b, err := json.Marshal(t) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| rawResponse := fmt.Sprintf(`{"value": "part1.%s.part3"}`, base64.RawURLEncoding.EncodeToString(b)) | ||
| return newRawTestOIDCServer(rawResponse) | ||
| } | ||
|
|
||
| func newRawTestOIDCServer(raw string) (*httptest.Server, func()) { | ||
| s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| // Respond with a very basic 3-part JWT token. | ||
| fmt.Fprintln(w, raw) | ||
| })) | ||
| oldEnv, ok := os.LookupEnv(requestURLEnvKey) | ||
| // NOTE: httptest.Server.URL has no trailing slash. | ||
| os.Setenv(requestURLEnvKey, s.URL+"/") | ||
| return s, func() { | ||
| s.Close() | ||
| if ok { | ||
| os.Setenv(requestURLEnvKey, oldEnv) | ||
| } else { | ||
| os.Unsetenv(requestURLEnvKey) | ||
| } | ||
| } | ||
| } |
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,51 @@ | ||
| package slsa | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| intoto "github.com/in-toto/in-toto-golang/in_toto" | ||
| slsa "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v0.2" | ||
|
|
||
| "github.com/slsa-framework/slsa-github-generator/github" | ||
| ) | ||
|
|
||
| func TestHostedActionsProvenance(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| r WorkflowRun | ||
| expected *intoto.ProvenanceStatement | ||
| }{ | ||
| { | ||
| name: "empty", | ||
| r: WorkflowRun{}, | ||
| expected: &intoto.ProvenanceStatement{ | ||
| StatementHeader: intoto.StatementHeader{ | ||
| Type: intoto.StatementInTotoV01, | ||
| PredicateType: slsa.PredicateSLSAProvenance, | ||
| }, | ||
| Predicate: slsa.ProvenancePredicate{ | ||
| Builder: slsa.ProvenanceBuilder{ | ||
| ID: GithubHostedActionsBuilderID, | ||
| }, | ||
| Metadata: &slsa.ProvenanceMetadata{}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| _, stop := github.NewTestOIDCServer(nil) | ||
| defer stop() | ||
|
|
||
| t.Run(tc.name, func(t *testing.T) { | ||
| if p, err := HostedActionsProvenance(tc.r); err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } else { | ||
| if want, got := tc.expected, p; !reflect.DeepEqual(want, got) { | ||
| t.Errorf("unexpected result, want: %#v, got: %#v", want, got) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
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.