Skip to content

Commit b0db151

Browse files
author
Ian Lewis
authored
Fix log message for tlog upload (#773)
* Temporarily disable Run test. Signed-off-by: Ian Lewis <[email protected]> * Fix log message for tlog upload Signed-off-by: Ian Lewis <[email protected]> * Fix unit tests run during pre-submit Signed-off-by: Ian Lewis <[email protected]> Signed-off-by: Ian Lewis <[email protected]>
1 parent f3a107f commit b0db151

File tree

3 files changed

+91
-9
lines changed

3 files changed

+91
-9
lines changed

internal/builders/go/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func runProvenanceGeneration(subject, digest, commands, envs, workingDir, rekor
7878
r := sigstore.NewRekor(rekor)
7979
s := sigstore.NewDefaultFulcio()
8080
attBytes, err := pkg.GenerateProvenance(subject, digest,
81-
commands, envs, workingDir, s, r)
81+
commands, envs, workingDir, s, r, nil)
8282
if err != nil {
8383
return err
8484
}

internal/builders/go/pkg/provenance.go

+25-8
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (b *goProvenanceBuild) BuildConfig(context.Context) (interface{}, error) {
6464
// GenerateProvenance translates github context into a SLSA provenance
6565
// attestation.
6666
// Spec: https://slsa.dev/provenance/v0.2
67-
func GenerateProvenance(name, digest, command, envs, workingDir string, s signing.Signer, r signing.TransparencyLog) ([]byte, error) {
67+
func GenerateProvenance(name, digest, command, envs, workingDir string, s signing.Signer, r signing.TransparencyLog, provider slsa.ClientProvider) ([]byte, error) {
6868
gh, err := github.GetWorkflowContext()
6969
if err != nil {
7070
return nil, err
@@ -84,6 +84,11 @@ func GenerateProvenance(name, digest, command, envs, workingDir string, s signin
8484
return nil, err
8585
}
8686

87+
var cmd []string
88+
if len(com) > 0 {
89+
cmd = []string{com[0], "mod", "vendor"}
90+
}
91+
8792
b := goProvenanceBuild{
8893
GithubActionsBuild: slsa.NewGithubActionsBuild([]intoto.Subject{
8994
{
@@ -101,7 +106,7 @@ func GenerateProvenance(name, digest, command, envs, workingDir string, s signin
101106
// Note: vendoring and compilation are
102107
// performed in the same VM, so the compiler is
103108
// the same.
104-
Command: []string{com[0], "mod", "vendor"},
109+
Command: cmd,
105110
WorkingDir: workingDir,
106111
// Note: No user-defined env set for this step.
107112
},
@@ -116,15 +121,25 @@ func GenerateProvenance(name, digest, command, envs, workingDir string, s signin
116121
}
117122

118123
// Pre-submit tests don't have access to write OIDC token.
119-
if utils.IsPresubmitTests() {
120-
b.GithubActionsBuild.WithClients(&slsa.NilClientProvider{})
124+
if provider != nil {
125+
b.WithClients(provider)
126+
} else {
127+
// TODO(github.com/slsa-framework/slsa-github-generator/issues/124): Remove
128+
if utils.IsPresubmitTests() {
129+
b.GithubActionsBuild.WithClients(&slsa.NilClientProvider{})
130+
}
121131
}
122132

123133
ctx := context.Background()
124134
g := slsa.NewHostedActionsGenerator(&b)
125135
// Pre-submit tests don't have access to write OIDC token.
126-
if utils.IsPresubmitTests() {
127-
g.WithClients(&slsa.NilClientProvider{})
136+
if provider != nil {
137+
g.WithClients(provider)
138+
} else {
139+
// TODO(github.com/slsa-framework/slsa-github-generator/issues/124): Remove
140+
if utils.IsPresubmitTests() {
141+
g.WithClients(&slsa.NilClientProvider{})
142+
}
128143
}
129144
p, err := g.Generate(ctx)
130145
if err != nil {
@@ -163,10 +178,12 @@ func GenerateProvenance(name, digest, command, envs, workingDir string, s signin
163178
}
164179

165180
// Upload the signed attestation to rekor.
166-
if logEntry, err := r.Upload(ctx, att); err != nil {
167-
fmt.Printf("Uploaded signed attestation to rekor with UUID %s.\n", logEntry.UUID())
181+
logEntry, err := r.Upload(ctx, att)
182+
if err != nil {
168183
return nil, err
169184
}
170185

186+
fmt.Printf("Uploaded signed attestation to rekor with UUID %s.\n", logEntry.UUID())
187+
171188
return att.Bytes(), nil
172189
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,66 @@
1+
// Copyright 2022 SLSA Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package pkg
16+
17+
import (
18+
"context"
19+
"errors"
20+
"fmt"
21+
"testing"
22+
23+
intoto "github.com/in-toto/in-toto-golang/in_toto"
24+
"github.com/slsa-framework/slsa-github-generator/signing"
25+
"github.com/slsa-framework/slsa-github-generator/slsa"
26+
)
27+
28+
type testAttestation struct {
29+
cert []byte
30+
bytes []byte
31+
}
32+
33+
func (a *testAttestation) Cert() []byte {
34+
return a.cert
35+
}
36+
37+
func (a *testAttestation) Bytes() []byte {
38+
return a.bytes
39+
}
40+
41+
type testSigner struct{}
42+
43+
func (s testSigner) Sign(context.Context, *intoto.Statement) (signing.Attestation, error) {
44+
return &testAttestation{}, nil
45+
}
46+
47+
type tLogWithErr struct{}
48+
49+
var errTransparencyLog = errors.New("transparency log error")
50+
51+
func (tLogWithErr) Upload(context.Context, signing.Attestation) (signing.LogEntry, error) {
52+
fmt.Printf("Upload")
53+
return nil, errTransparencyLog
54+
}
55+
56+
func TestGenerateProvenance_withErr(t *testing.T) {
57+
// Disable pre-submit detection.
58+
// TODO(github.com/slsa-framework/slsa-github-generator/issues/124): Remove
59+
t.Setenv("GITHUB_EVENT_NAME", "non_event")
60+
t.Setenv("GITHUB_CONTEXT", "{}")
61+
sha256 := "2e0390eb024a52963db7b95e84a9c2b12c004054a7bad9a97ec0c7c89d4681d2"
62+
_, err := GenerateProvenance("foo", sha256, "", "", "/home/foo", &testSigner{}, &tLogWithErr{}, &slsa.NilClientProvider{})
63+
if want, got := errTransparencyLog, err; want != got {
64+
t.Errorf("expected error, want: %v, got: %v", want, got)
65+
}
66+
}

0 commit comments

Comments
 (0)