6
6
"errors"
7
7
"fmt"
8
8
"io"
9
+ "log"
9
10
"net/http"
10
11
"os"
11
12
"path/filepath"
@@ -29,23 +30,33 @@ const (
29
30
30
31
workflowPath = ".github/workflows/compute_slsa_source.yaml"
31
32
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 "
32
44
)
33
45
34
46
// 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 = `---
38
48
name: SLSA Source
39
49
on:
40
50
push:
41
51
branches: [ %q ]
52
+ permissions: {}
42
53
43
54
jobs:
44
55
# Whenever new source is pushed recompute the slsa source information.
45
- check-change :
56
+ generate-provenance :
46
57
permissions:
47
58
contents: write # needed for storing the vsa in the repo.
48
- id-token: write
59
+ id-token: write # meeded to mint yokens for signing
49
60
uses: slsa-framework/slsa-source-poc/.github/workflows/compute_slsa_source.yml@main
50
61
`
51
62
@@ -68,15 +79,19 @@ type defaultToolImplementation struct{}
68
79
func (impl * defaultToolImplementation ) GetActiveControls (opts * Options ) (slsa.Controls , error ) {
69
80
ctx := context .Background ()
70
81
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
74
84
}
75
85
76
- if err := opts .EnsureBranch (); err != nil {
86
+ if err := opts .EnsureCommit (); err != nil {
77
87
return nil , err
78
88
}
79
89
90
+ ghc , err := opts .GetGitHubConnection ()
91
+ if err != nil {
92
+ return nil , fmt .Errorf ("getting GitHub connection: %w" , err )
93
+ }
94
+
80
95
// Get the active controls
81
96
activeControls , err := ghc .GetBranchControls (ctx , ghcontrol .BranchToFullRef (opts .Branch ))
82
97
if err != nil {
@@ -92,12 +107,14 @@ func (impl *defaultToolImplementation) GetActiveControls(opts *Options) (slsa.Co
92
107
// Fetch the attestation. If found, then add the control:
93
108
attestation , _ , err := attestor .GetProvenance (ctx , opts .Commit , ghcontrol .BranchToFullRef (opts .Branch ))
94
109
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 )
96
111
}
97
112
if attestation != nil {
98
113
activeControls .AddControl (& slsa.Control {
99
114
Name : slsa .ProvenanceAvailable ,
100
115
})
116
+ } else {
117
+ log .Printf ("No provenance attestation found on %s" , opts .Commit )
101
118
}
102
119
103
120
return * activeControls , nil
@@ -282,10 +299,8 @@ func (impl *defaultToolImplementation) CreateWorkflowPR(opts *Options) error {
282
299
return fmt .Errorf ("adding workflow file to staging area: %w" , err )
283
300
}
284
301
285
- commitMessage := "Add SLSA Source attesting workflow"
286
-
287
302
// Create the commit
288
- if err := repo .UserCommit (commitMessage ); err != nil {
303
+ if err := repo .UserCommit (workflowCommitMessage ); err != nil {
289
304
return fmt .Errorf ("committing changes to workflow: %w" , err )
290
305
}
291
306
@@ -295,14 +310,11 @@ func (impl *defaultToolImplementation) CreateWorkflowPR(opts *Options) error {
295
310
return fmt .Errorf ("pushing %s to %s/%s: %w" , kgithub .UserForkName , userForkOrg , userForkRepo , err )
296
311
}
297
312
298
- prBody := `This pull request adds a workflow to the repository to attest the
299
- SLSA Source compliance on every push.
300
- `
301
313
// Create the Pull Request
302
314
pr , err := gh .CreatePullRequest (
303
315
opts .Owner , opts .Repo , opts .Branch ,
304
316
fmt .Sprintf ("%s:%s" , userForkOrg , branchname ),
305
- commitMessage , prBody , false ,
317
+ workflowCommitMessage , workflowPRBody , false ,
306
318
)
307
319
if err != nil {
308
320
return fmt .Errorf ("creating the pull request in %s: %w" , opts .Owner , err )
0 commit comments