Skip to content

Commit 4a8e7ad

Browse files
author
Ian Lewis
authored
Fix support for --signature="" (slsa-framework#615)
1 parent 9019505 commit 4a8e7ad

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

.github/workflows/generator_generic_slsa3.yml

+7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ jobs:
114114
UNTRUSTED_ATTESTATION_NAME: "${{ inputs.attestation-name }}"
115115
run: |
116116
set -euo pipefail
117+
# NOTE: The generator binary allows the attestation to be "" in which
118+
# case it does not sign or generate provenance. However, this workflow
119+
# requires it to be non-empty so we validate it here.
120+
if [ "$UNTRUSTED_ATTESTATION_NAME" == "" ]; then
121+
echo "attestation-name cannot be empty."
122+
exit 5
123+
fi
117124
# Create and sign provenance.
118125
# Note: The builder verifies that the UNTRUSTED_ATTESTATION_NAME is located
119126
# in the current directory.

internal/builders/generic/attest.go

+19-13
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (b *provenanceOnlyBuild) URI() string {
133133
}
134134

135135
// attestCmd returns the 'attest' command.
136-
func attestCmd() *cobra.Command {
136+
func attestCmd(provider slsa.ClientProvider) *cobra.Command {
137137
var predicatePath string
138138
var attPath string
139139
var subjects string
@@ -148,15 +148,13 @@ run in the context of a Github Actions workflow.`,
148148
Run: func(cmd *cobra.Command, args []string) {
149149
ghContext, err := github.GetWorkflowContext()
150150
check(err)
151-
152-
// Verify the extension path and extension.
153-
err = utils.VerifyAttestationPath(attPath)
154-
check(err)
155-
156151
var parsedSubjects []intoto.Subject
157152
// We don't actually care about the subjects if we aren't writing an attestation.
158153
if attPath != "" {
159-
var err error
154+
// Verify the extension path and extension.
155+
err = utils.VerifyAttestationPath(attPath)
156+
check(err)
157+
160158
parsedSubjects, err = parseSubjects(subjects)
161159
check(err)
162160

@@ -170,15 +168,23 @@ run in the context of a Github Actions workflow.`,
170168
b := provenanceOnlyBuild{
171169
GithubActionsBuild: slsa.NewGithubActionsBuild(parsedSubjects, ghContext),
172170
}
173-
// TODO(github.com/slsa-framework/slsa-github-generator/issues/124): Remove
174-
if utils.IsPresubmitTests() {
175-
b.WithClients(&slsa.NilClientProvider{})
171+
if provider != nil {
172+
b.WithClients(provider)
173+
} else {
174+
// TODO(github.com/slsa-framework/slsa-github-generator/issues/124): Remove
175+
if utils.IsPresubmitTests() {
176+
b.WithClients(&slsa.NilClientProvider{})
177+
}
176178
}
177179

178180
g := slsa.NewHostedActionsGenerator(&b)
179-
// TODO(github.com/slsa-framework/slsa-github-generator/issues/124): Remove
180-
if utils.IsPresubmitTests() {
181-
g.WithClients(&slsa.NilClientProvider{})
181+
if provider != nil {
182+
g.WithClients(provider)
183+
} else {
184+
// TODO(github.com/slsa-framework/slsa-github-generator/issues/124): Remove
185+
if utils.IsPresubmitTests() {
186+
g.WithClients(&slsa.NilClientProvider{})
187+
}
182188
}
183189

184190
p, err := g.Generate(ctx)

internal/builders/generic/attest_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"testing"
56

67
"github.com/google/go-cmp/cmp"
@@ -9,6 +10,7 @@ import (
910
slsav02 "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v0.2"
1011

1112
"github.com/slsa-framework/slsa-github-generator/internal/errors"
13+
"github.com/slsa-framework/slsa-github-generator/slsa"
1214
)
1315

1416
// TestParseSubjects tests the parseSubjects function.
@@ -145,3 +147,17 @@ func TestParseSubjects(t *testing.T) {
145147
})
146148
}
147149
}
150+
151+
// Test_attestCmd tests the attest command.
152+
func Test_attestCmd(t *testing.T) {
153+
t.Run("empty attestation path", func(t *testing.T) {
154+
t.Setenv("GITHUB_CONTEXT", "{}")
155+
156+
c := attestCmd(&slsa.NilClientProvider{})
157+
c.SetOut(new(bytes.Buffer))
158+
c.SetArgs([]string{"--signature", ""})
159+
if err := c.Execute(); err != nil {
160+
t.Errorf("unexpected failure: %v", err)
161+
}
162+
})
163+
}

internal/builders/generic/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ For more information on SLSA, visit https://slsa.dev`,
4444
},
4545
}
4646
c.AddCommand(versionCmd())
47-
c.AddCommand(attestCmd())
47+
c.AddCommand(attestCmd(nil))
4848
return c
4949
}
5050

0 commit comments

Comments
 (0)