Skip to content

Commit 8947bf7

Browse files
authored
Update workflow PR (#220)
Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
1 parent 4b111f9 commit 8947bf7

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

sourcetool/cmd/status.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,17 @@ command is intended to help maintainers implementing SLSA controls
5858
understand the next steps to secure their repos and progress in their
5959
SLSA journey.
6060
`,
61-
Use: "status",
61+
Use: "status [flags] owner/repo@branch",
6262
SilenceUsage: false,
6363
SilenceErrors: true,
64+
Example: `Check the SLSA tooling status on a repository:
65+
sourcetool status myorg/myrepo
66+
67+
A branch other than the default can be specified by appending it to
68+
the repository slug:
69+
70+
sourcetool status myorg/myrepo@mybranch
71+
`,
6472
PreRunE: func(cmd *cobra.Command, args []string) error {
6573
if len(args) > 0 {
6674
if err := opts.ParseLocator(args[0]); err != nil {
@@ -116,7 +124,10 @@ SLSA journey.
116124
// Compute the maximum level possible:
117125
toplevel := policy.ComputeEligibleSlsaLevel(controls)
118126

119-
title := fmt.Sprintf("SLSA Source Status for %s/%s", opts.owner, opts.repository)
127+
title := fmt.Sprintf(
128+
"SLSA Source Status for %s/%s@%s", opts.owner, opts.repository,
129+
ghcontrol.BranchToFullRef(opts.branch),
130+
)
120131
fmt.Printf("")
121132
fmt.Println(w(title))
122133
fmt.Println(strings.Repeat("=", len(title)))

sourcetool/pkg/sourcetool/implementation.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"io"
9+
"log"
910
"net/http"
1011
"os"
1112
"path/filepath"
@@ -29,23 +30,33 @@ const (
2930

3031
workflowPath = ".github/workflows/compute_slsa_source.yaml"
3132
workflowSource = "git+https://github.com/slsa-"
33+
34+
// workflowCommitMessage will be used as the commit message and the PR title
35+
workflowCommitMessage = "Add SLSA Source Provenance Workflow"
36+
37+
// workflowPRBody is the body of the pull request that adds the provenance workflow
38+
workflowPRBody = `This pull request adds a new workflow to the repository to generate ` +
39+
`[SLSA](https://slsa.dev/) Source provenance data on every push.` + "\n\n" +
40+
`Every time a new commit merges to the specified branch, attestations will ` +
41+
`be automatically signed and stored in git notes in this repository.` + "\n\n" +
42+
`Note: This is an automated PR created using the ` +
43+
`[SLSA sourcetool](https://github.com/slsa-framework/slsa-source-poc) utility.` + "\n"
3244
)
3345

3446
// TODO(puerco): Read this from latest version on the repository
35-
var workflowData = `# SPDX-FileCopyrightText: Copyright 2025 The SLSA Authors
36-
# SPDX-License-Identifier: Apache-2.0
37-
---
47+
var workflowData = `---
3848
name: SLSA Source
3949
on:
4050
push:
4151
branches: [ %q ]
52+
permissions: {}
4253
4354
jobs:
4455
# Whenever new source is pushed recompute the slsa source information.
45-
check-change:
56+
generate-provenance:
4657
permissions:
4758
contents: write # needed for storing the vsa in the repo.
48-
id-token: write
59+
id-token: write # meeded to mint yokens for signing
4960
uses: slsa-framework/slsa-source-poc/.github/workflows/compute_slsa_source.yml@main
5061
`
5162

@@ -68,15 +79,19 @@ type defaultToolImplementation struct{}
6879
func (impl *defaultToolImplementation) GetActiveControls(opts *Options) (slsa.Controls, error) {
6980
ctx := context.Background()
7081

71-
ghc, err := opts.GetGitHubConnection()
72-
if err != nil {
73-
return nil, fmt.Errorf("getting GitHub connection: %w", err)
82+
if err := opts.EnsureBranch(); err != nil {
83+
return nil, err
7484
}
7585

76-
if err := opts.EnsureBranch(); err != nil {
86+
if err := opts.EnsureCommit(); err != nil {
7787
return nil, err
7888
}
7989

90+
ghc, err := opts.GetGitHubConnection()
91+
if err != nil {
92+
return nil, fmt.Errorf("getting GitHub connection: %w", err)
93+
}
94+
8095
// Get the active controls
8196
activeControls, err := ghc.GetBranchControls(ctx, ghcontrol.BranchToFullRef(opts.Branch))
8297
if err != nil {
@@ -92,12 +107,14 @@ func (impl *defaultToolImplementation) GetActiveControls(opts *Options) (slsa.Co
92107
// Fetch the attestation. If found, then add the control:
93108
attestation, _, err := attestor.GetProvenance(ctx, opts.Commit, ghcontrol.BranchToFullRef(opts.Branch))
94109
if err != nil {
95-
return nil, fmt.Errorf("attempting to read provenance from commit: %w", err)
110+
return nil, fmt.Errorf("attempting to read provenance from commit %q: %w", opts.Commit, err)
96111
}
97112
if attestation != nil {
98113
activeControls.AddControl(&slsa.Control{
99114
Name: slsa.ProvenanceAvailable,
100115
})
116+
} else {
117+
log.Printf("No provenance attestation found on %s", opts.Commit)
101118
}
102119

103120
return *activeControls, nil
@@ -282,10 +299,8 @@ func (impl *defaultToolImplementation) CreateWorkflowPR(opts *Options) error {
282299
return fmt.Errorf("adding workflow file to staging area: %w", err)
283300
}
284301

285-
commitMessage := "Add SLSA Source attesting workflow"
286-
287302
// Create the commit
288-
if err := repo.UserCommit(commitMessage); err != nil {
303+
if err := repo.UserCommit(workflowCommitMessage); err != nil {
289304
return fmt.Errorf("committing changes to workflow: %w", err)
290305
}
291306

@@ -295,14 +310,11 @@ func (impl *defaultToolImplementation) CreateWorkflowPR(opts *Options) error {
295310
return fmt.Errorf("pushing %s to %s/%s: %w", kgithub.UserForkName, userForkOrg, userForkRepo, err)
296311
}
297312

298-
prBody := `This pull request adds a workflow to the repository to attest the
299-
SLSA Source compliance on every push.
300-
`
301313
// Create the Pull Request
302314
pr, err := gh.CreatePullRequest(
303315
opts.Owner, opts.Repo, opts.Branch,
304316
fmt.Sprintf("%s:%s", userForkOrg, branchname),
305-
commitMessage, prBody, false,
317+
workflowCommitMessage, workflowPRBody, false,
306318
)
307319
if err != nil {
308320
return fmt.Errorf("creating the pull request in %s: %w", opts.Owner, err)

sourcetool/pkg/sourcetool/tool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (t *Tool) ConfigureControls(configs []ControlConfiguration, funcs ...ooFn)
118118
}
119119
case CONFIG_POLICY:
120120
if err := t.impl.CheckPolicyFork(&opts); err != nil {
121-
return fmt.Errorf("checking policy repo fork")
121+
return fmt.Errorf("checking policy repo fork: %w", err)
122122
}
123123
if err := t.impl.CreatePolicyPR(&opts); err != nil {
124124
return fmt.Errorf("opening the policy pull request: %w", err)

0 commit comments

Comments
 (0)