Skip to content

Commit

Permalink
Avoid OIDC client creation errors on PRs (slsa-framework#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianlewis authored Jul 1, 2022
1 parent 628e3ff commit 37ccd28
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
26 changes: 13 additions & 13 deletions .github/actions/detect-workflow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import (
)

type action struct {
getenv func(string) string
event map[string]any
client *github.OIDCClient
getenv func(string) string
event map[string]any
getClient func() (*github.OIDCClient, error)
}

// TODO(github.com/slsa-framework/slsa-github-generator/issues/164): use the github context via the shared library

func newAction(getenv func(string) string, c *github.OIDCClient) (*action, error) {
func newAction(getenv func(string) string, getClient func() (*github.OIDCClient, error)) (*action, error) {
eventPath := getenv("GITHUB_EVENT_PATH")
if eventPath == "" {
return nil, errors.New("GITHUB_EVENT_PATH not set")
Expand All @@ -52,9 +52,9 @@ func newAction(getenv func(string) string, c *github.OIDCClient) (*action, error
}

return &action{
getenv: getenv,
event: event,
client: c,
getenv: getenv,
event: event,
getClient: getClient,
}, nil
}

Expand Down Expand Up @@ -107,7 +107,11 @@ func (a *action) getRepoRef(ctx context.Context) (string, string, error) {
}
audience = path.Join(audience, "detect-workflow")

t, err := a.client.Token(ctx, []string{audience})
client, err := a.getClient()
if err != nil {
return "", "", fmt.Errorf("creating OIDC client: %w", err)
}
t, err := client.Token(ctx, []string{audience})
if err != nil {
return "", "", fmt.Errorf("getting OIDC token: %w", err)
}
Expand Down Expand Up @@ -136,11 +140,7 @@ func (a *action) getRepoRef(ctx context.Context) (string, string, error) {
}

func main() {
c, err := github.NewOIDCClient()
if err != nil {
log.Fatal(err)
}
a, err := newAction(os.Getenv, c)
a, err := newAction(os.Getenv, github.NewOIDCClient)
if err != nil {
log.Fatal(err)
}
Expand Down
8 changes: 6 additions & 2 deletions .github/actions/detect-workflow/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ func Test_action_getRepoRef(t *testing.T) {
}
return ""
},
client: c,
getClient: func() (*github.OIDCClient, error) {
return c, nil
},
}

repo, ref, err := a.getRepoRef(context.Background())
Expand Down Expand Up @@ -151,7 +153,9 @@ func Test_action_getRepoRef(t *testing.T) {
}
return env[k]
},
client: c,
getClient: func() (*github.OIDCClient, error) {
return c, nil
},
event: map[string]any{
"pull_request": map[string]any{
"head": map[string]any{
Expand Down

0 comments on commit 37ccd28

Please sign in to comment.