Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions pkg/attest/attester.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func NewAttester(fn ...optFn) (*Attester, error) {

// createCurrentProvenance creates the provenance statement for the specified commit
// without any context from the previous provenance (if any).
func (a *Attester) createCurrentProvenance(ctx context.Context, branch *models.Branch, commit, prevCommit *models.Commit) (*intoto.Statement, error) {
func (a *Attester) createCurrentProvenance(ctx context.Context, branch *models.Branch, commit, prevCommit *models.Commit, opts *provenanceOptions) (*intoto.Statement, error) {
// Get the active controls
controlStatus, err := a.backend.GetBranchControlsAtCommit(ctx, branch, commit)
if err != nil {
Expand All @@ -180,19 +180,59 @@ func (a *Attester) createCurrentProvenance(ctx context.Context, branch *models.B
Controls: controlStatus.ToProvenanceControls(),
}

addAttestationControls(&curProvPred, opts)

return addPredToStatement(&curProvPred, provenance.SourceProvPredicateType, commit.SHA)
}

// provenanceOptions tunes how the source provenance of a commit is built.
type provenanceOptions struct {
// vsaIssued is true when a VSA for the commit is generated together
// with the provenance.
vsaIssued bool
}

// ProvenanceOption is a functional option for CreateSourceProvenance.
type ProvenanceOption func(*provenanceOptions)

// WithVSAIssued tells the attester that a VSA for the commit is being issued
// together with the provenance, which makes SLSA_SOURCE_SCS_VSA an active
// control of the revision.
func WithVSAIssued(yesno bool) ProvenanceOption {
return func(o *provenanceOptions) {
o.vsaIssued = yesno
}
}

// addAttestationControls adds to the predicate the controls that are satisfied
// by the attestations being generated for the commit. The VCS backend can only
// report them when the commit already has attestations, which is never the case
// the first time a revision is attested.
func addAttestationControls(pred *provenance.SourceProvenancePred, opts *provenanceOptions) {
// At the very least provenance is available starting now. :)
// ... indeed, but don't set the `since`` date because doing so breaks
// checking against policies.
// See https://github.com/slsa-framework/source-tool/issues/272
if provenance.GetControl(&curProvPred, slsa.SLSA_SOURCE_SCS_PROVENANCE.String()) == nil {
provenance.AddControl(&curProvPred,
if provenance.GetControl(pred, slsa.SLSA_SOURCE_SCS_PROVENANCE.String()) == nil {
provenance.AddControl(pred,
&provenance.Control{
Name: slsa.SLSA_SOURCE_SCS_PROVENANCE.String(),
},
)
}

return addPredToStatement(&curProvPred, provenance.SourceProvPredicateType, commit.SHA)
// Same for the VSA: every level requires it, but the commit cannot have
// one before we attest it. Mark the control as active when the VSA is
// about to be issued, otherwise no revision could ever go above the
// level 1 fallback.
if opts != nil && opts.vsaIssued &&
provenance.GetControl(pred, slsa.SLSA_SOURCE_SCS_VSA.String()) == nil {
provenance.AddControl(pred,
&provenance.Control{
Name: slsa.SLSA_SOURCE_SCS_VSA.String(),
},
)
}
}

// addPredToStatement generates a new statement and adds the provenance predicate
Expand Down
63 changes: 63 additions & 0 deletions pkg/attest/attester_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-FileCopyrightText: Copyright 2025 The SLSA Authors
// SPDX-License-Identifier: Apache-2.0

package attest

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/slsa-framework/source-tool/pkg/provenance"
"github.com/slsa-framework/source-tool/pkg/slsa"
)

// A commit has no attestations the first time it is attested, so the backend
// never reports the provenance and VSA controls. They have to be added when the
// attestations are about to be issued or the revision is stuck at level 1.
func TestAddAttestationControls(t *testing.T) {
t.Parallel()

// All the level 3 controls a backend can report on a fresh commit.
backendControls := func() *provenance.SourceProvenancePred {
pred := &provenance.SourceProvenancePred{}
for _, name := range slsa.Level3 {
if name == slsa.SLSA_SOURCE_SCS_PROVENANCE || name == slsa.SLSA_SOURCE_SCS_VSA {
continue
}
provenance.AddControl(pred, &provenance.Control{Name: name.String()})
}
return pred
}

for _, tc := range []struct {
name string
opts *provenanceOptions
expectVSA bool
}{
{name: "vsa-issued", opts: &provenanceOptions{vsaIssued: true}, expectVSA: true},
{name: "vsa-not-issued", opts: &provenanceOptions{}, expectVSA: false},
{name: "nil-options", opts: nil, expectVSA: false},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
pred := backendControls()
addAttestationControls(pred, tc.opts)

require.NotNil(t, provenance.GetControl(pred, slsa.SLSA_SOURCE_SCS_PROVENANCE.String()))
require.Equal(t, tc.expectVSA, provenance.GetControl(pred, slsa.SLSA_SOURCE_SCS_VSA.String()) != nil)

controls := slsa.NewControlSetFromProvanenaceControls(pred.GetControls())
require.Equal(t, tc.expectVSA, controls.AreControlsAvailable(slsa.Level3))
})
}

t.Run("no-duplicates", func(t *testing.T) {
t.Parallel()
pred := backendControls()
addAttestationControls(pred, &provenanceOptions{vsaIssued: true})
count := len(pred.GetControls())
addAttestationControls(pred, &provenanceOptions{vsaIssued: true})
require.Len(t, pred.GetControls(), count)
})
}
9 changes: 7 additions & 2 deletions pkg/attest/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,12 @@ func (a *Attester) GetRevisionProvenance(ctx context.Context, branch *models.Bra
}

// prevAttPath string
func (a *Attester) CreateSourceProvenance(ctx context.Context, branch *models.Branch, commit *models.Commit) (*intoto.Statement, error) {
func (a *Attester) CreateSourceProvenance(ctx context.Context, branch *models.Branch, commit *models.Commit, fn ...ProvenanceOption) (*intoto.Statement, error) {
opts := &provenanceOptions{}
for _, f := range fn {
f(opts)
}

// Get the previous commit
prevCommit, err := a.backend.GetPreviousCommit(ctx, branch, commit)
if err != nil {
Expand All @@ -305,7 +310,7 @@ func (a *Attester) CreateSourceProvenance(ctx context.Context, branch *models.Br
// Source provenance is based on
// 1. The current control situation (we assume 'commit' has _just_ occurred).
// 2. How long the properties have been enforced according to the previous provenance.
curProv, err := a.createCurrentProvenance(ctx, branch, commit, prevCommit)
curProv, err := a.createCurrentProvenance(ctx, branch, commit, prevCommit, opts)
if err != nil {
return nil, fmt.Errorf("creating provenance predicate: %w", err)
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/sourcetool/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,11 @@ func (t *Tool) AttestRevision(
if isCommit {
// Create the provenance attestation. It is always built: it is emitted
// when requested and it is the basis for the policy evaluation below.
prov, err := t.Attester().CreateSourceProvenance(ctx, branch, rev.GetCommit())
// When the VSA is issued in this same run, the provenance records
// the VSA control as active.
prov, err := t.Attester().CreateSourceProvenance(
ctx, branch, rev.GetCommit(), attest.WithVSAIssued(opts.VSA),
)
if err != nil {
return nil, err
}
Expand Down
Loading