Skip to content

Commit a73c72a

Browse files
authored
bug: always use the default GITHUB_TOKEN for signing (#898)
* update * update * update * update * update * update
1 parent 68bf5b3 commit a73c72a

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ linters:
5656
- misspell
5757
- nakedret
5858
- nestif
59-
- noctx
6059
- nolintlint
6160
- paralleltest
6261
- predeclared

github/github.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func (c *Client) ParseFromURL(baseRepoURL, repoName string) (RepoInfo, error) {
9191
}
9292

9393
log.Printf("getting repo info from URL: %s", repoURL.String())
94+
//nolint:noctx
9495
req, err := http.NewRequestWithContext(
9596
c.ctx,
9697
http.MethodGet,

main.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,20 @@ func main() {
4141
}
4242

4343
// Sign json results.
44-
if err = signing.SignScorecardResult("results.json"); err != nil {
44+
// Always use the default GitHub token, never a PAT.
45+
accessToken := os.Getenv(options.EnvInputInternalRepoToken)
46+
s, err := signing.New(accessToken)
47+
if err != nil {
48+
log.Fatalf("error SigningNew: %v", err)
49+
}
50+
if err = s.SignScorecardResult("results.json"); err != nil {
4551
log.Fatalf("error signing scorecard json results: %v", err)
4652
}
4753

4854
// Processes json results.
4955
repoName := os.Getenv(options.EnvGithubRepository)
5056
repoRef := os.Getenv(options.EnvGithubRef)
51-
accessToken := os.Getenv(options.EnvInputRepoToken)
52-
if err := signing.ProcessSignature(jsonPayload, repoName, repoRef, accessToken); err != nil {
57+
if err := s.ProcessSignature(jsonPayload, repoName, repoRef); err != nil {
5358
log.Fatalf("error processing signature: %v", err)
5459
}
5560
}

signing/signing.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ import (
2020
"bytes"
2121
"context"
2222
"encoding/json"
23+
"errors"
2324
"fmt"
2425
"io"
2526
"net/http"
2627
"net/url"
2728
"os"
29+
"strings"
2830
"time"
2931

3032
sigOpts "github.com/sigstore/cosign/cmd/cosign/cli/options"
@@ -34,15 +36,45 @@ import (
3436
"github.com/ossf/scorecard-action/options"
3537
)
3638

37-
// SignScorecardResult signs the results file and uploads the attestation to the Rekor transparency log.
38-
func SignScorecardResult(scorecardResultsFile string) error {
39+
var (
40+
errorEmptyToken = errors.New("error token empty")
41+
errorInvalidToken = errors.New("invalid token")
42+
)
43+
44+
// Signing is a signing structure.
45+
type Signing struct {
46+
token string
47+
}
48+
49+
// New creates a new Signing instance.
50+
func New(token string) (*Signing, error) {
51+
// Set the default GITHUB_TOKEN, because it's not available by default
52+
// in a GitHub Action. We need it for OIDC.
53+
if token == "" {
54+
return nil, fmt.Errorf("%w", errorEmptyToken)
55+
}
56+
57+
// Check for a workflow secret.
58+
if !strings.HasPrefix(token, "ghs_") {
59+
return nil, fmt.Errorf("%w: not a default GITHUB_TOKEN", errorInvalidToken)
60+
}
61+
if err := os.Setenv("GITHUB_TOKEN", token); err != nil {
62+
return nil, fmt.Errorf("error setting GITHUB_TOKEN env var: %w", err)
63+
}
64+
3965
if err := os.Setenv("COSIGN_EXPERIMENTAL", "true"); err != nil {
40-
return fmt.Errorf("error setting COSIGN_EXPERIMENTAL env var: %w", err)
66+
return nil, fmt.Errorf("error setting COSIGN_EXPERIMENTAL env var: %w", err)
4167
}
4268

69+
return &Signing{
70+
token: token,
71+
}, nil
72+
}
73+
74+
// SignScorecardResult signs the results file and uploads the attestation to the Rekor transparency log.
75+
func (s *Signing) SignScorecardResult(scorecardResultsFile string) error {
4376
// Prepare settings for SignBlobCmd.
4477
rootOpts := &sigOpts.RootOptions{Timeout: sigOpts.DefaultTimeout} // Just the timeout.
45-
4678
keyOpts := sigOpts.KeyOpts{
4779
FulcioURL: sigOpts.DefaultFulcioURL, // Signing certificate provider.
4880
RekorURL: sigOpts.DefaultRekorURL, // Transparency log.
@@ -87,7 +119,7 @@ func GetJSONScorecardResults() ([]byte, error) {
87119
}
88120

89121
// ProcessSignature calls scorecard-api to process & upload signed scorecard results.
90-
func ProcessSignature(jsonPayload []byte, repoName, repoRef, accessToken string) error {
122+
func (s *Signing) ProcessSignature(jsonPayload []byte, repoName, repoRef string) error {
91123
// Prepare HTTP request body for scorecard-webapp-api call.
92124
// TODO: Use the `ScorecardResult` struct from `scorecard-webapp`.
93125
resultsPayload := struct {
@@ -97,7 +129,7 @@ func ProcessSignature(jsonPayload []byte, repoName, repoRef, accessToken string)
97129
}{
98130
Result: string(jsonPayload),
99131
Branch: repoRef,
100-
AccessToken: accessToken,
132+
AccessToken: s.token,
101133
}
102134

103135
payloadBytes, err := json.Marshal(resultsPayload)
@@ -113,7 +145,7 @@ func ProcessSignature(jsonPayload []byte, repoName, repoRef, accessToken string)
113145
if err != nil {
114146
return fmt.Errorf("parsing Scorecard API endpoint: %w", err)
115147
}
116-
req, err := http.NewRequest("POST", parsedURL.String(), bytes.NewBuffer(payloadBytes)) //nolint
148+
req, err := http.NewRequest("POST", parsedURL.String(), bytes.NewBuffer(payloadBytes))
117149
if err != nil {
118150
return fmt.Errorf("creating HTTP request: %w", err)
119151
}

signing/signing_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package signing
1818

1919
import (
20+
"fmt"
2021
"os"
2122
"testing"
2223

@@ -88,7 +89,11 @@ func Test_ProcessSignature(t *testing.T) {
8889
t.Errorf("Error reading testdata:, %v", err)
8990
}
9091

91-
if err := ProcessSignature(jsonPayload, repoName, repoRef, accessToken); err != nil {
92+
s, err := New(accessToken)
93+
if err != nil {
94+
panic(fmt.Sprintf("error SigningNew: %v", err))
95+
}
96+
if err := s.ProcessSignature(jsonPayload, repoName, repoRef); err != nil {
9297
t.Errorf("ProcessSignature() error:, %v", err)
9398
return
9499
}

0 commit comments

Comments
 (0)