Skip to content

Commit

Permalink
Fix support for --signature="" (slsa-framework#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianlewis committed Jul 25, 2022
1 parent 9019505 commit 4a8e7ad
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/generator_generic_slsa3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ jobs:
UNTRUSTED_ATTESTATION_NAME: "${{ inputs.attestation-name }}"
run: |
set -euo pipefail
# NOTE: The generator binary allows the attestation to be "" in which
# case it does not sign or generate provenance. However, this workflow
# requires it to be non-empty so we validate it here.
if [ "$UNTRUSTED_ATTESTATION_NAME" == "" ]; then
echo "attestation-name cannot be empty."
exit 5
fi
# Create and sign provenance.
# Note: The builder verifies that the UNTRUSTED_ATTESTATION_NAME is located
# in the current directory.
Expand Down
32 changes: 19 additions & 13 deletions internal/builders/generic/attest.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (b *provenanceOnlyBuild) URI() string {
}

// attestCmd returns the 'attest' command.
func attestCmd() *cobra.Command {
func attestCmd(provider slsa.ClientProvider) *cobra.Command {
var predicatePath string
var attPath string
var subjects string
Expand All @@ -148,15 +148,13 @@ run in the context of a Github Actions workflow.`,
Run: func(cmd *cobra.Command, args []string) {
ghContext, err := github.GetWorkflowContext()
check(err)

// Verify the extension path and extension.
err = utils.VerifyAttestationPath(attPath)
check(err)

var parsedSubjects []intoto.Subject
// We don't actually care about the subjects if we aren't writing an attestation.
if attPath != "" {
var err error
// Verify the extension path and extension.
err = utils.VerifyAttestationPath(attPath)
check(err)

parsedSubjects, err = parseSubjects(subjects)
check(err)

Expand All @@ -170,15 +168,23 @@ run in the context of a Github Actions workflow.`,
b := provenanceOnlyBuild{
GithubActionsBuild: slsa.NewGithubActionsBuild(parsedSubjects, ghContext),
}
// TODO(github.com/slsa-framework/slsa-github-generator/issues/124): Remove
if utils.IsPresubmitTests() {
b.WithClients(&slsa.NilClientProvider{})
if provider != nil {
b.WithClients(provider)
} else {
// TODO(github.com/slsa-framework/slsa-github-generator/issues/124): Remove
if utils.IsPresubmitTests() {
b.WithClients(&slsa.NilClientProvider{})
}
}

g := slsa.NewHostedActionsGenerator(&b)
// TODO(github.com/slsa-framework/slsa-github-generator/issues/124): Remove
if utils.IsPresubmitTests() {
g.WithClients(&slsa.NilClientProvider{})
if provider != nil {
g.WithClients(provider)
} else {
// TODO(github.com/slsa-framework/slsa-github-generator/issues/124): Remove
if utils.IsPresubmitTests() {
g.WithClients(&slsa.NilClientProvider{})
}
}

p, err := g.Generate(ctx)
Expand Down
16 changes: 16 additions & 0 deletions internal/builders/generic/attest_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"testing"

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

"github.com/slsa-framework/slsa-github-generator/internal/errors"
"github.com/slsa-framework/slsa-github-generator/slsa"
)

// TestParseSubjects tests the parseSubjects function.
Expand Down Expand Up @@ -145,3 +147,17 @@ func TestParseSubjects(t *testing.T) {
})
}
}

// Test_attestCmd tests the attest command.
func Test_attestCmd(t *testing.T) {
t.Run("empty attestation path", func(t *testing.T) {
t.Setenv("GITHUB_CONTEXT", "{}")

c := attestCmd(&slsa.NilClientProvider{})
c.SetOut(new(bytes.Buffer))
c.SetArgs([]string{"--signature", ""})
if err := c.Execute(); err != nil {
t.Errorf("unexpected failure: %v", err)
}
})
}
2 changes: 1 addition & 1 deletion internal/builders/generic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ For more information on SLSA, visit https://slsa.dev`,
},
}
c.AddCommand(versionCmd())
c.AddCommand(attestCmd())
c.AddCommand(attestCmd(nil))
return c
}

Expand Down

0 comments on commit 4a8e7ad

Please sign in to comment.