Skip to content

Commit 97f0442

Browse files
authored
Even SourceTool API context (#361)
* Even SourceTool API context This commit modifies the sourcetool api to make all functions take a context. As we are preparing for third party backend, we need to ensure tools can get the tools context if they need it. Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]> * Bump go and linter Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]> --------- Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
1 parent 5a65471 commit 97f0442

File tree

10 files changed

+32
-27
lines changed

10 files changed

+32
-27
lines changed

.github/workflows/golangci-lint.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ jobs:
3939
- name: Run golangci-lint
4040
uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0
4141
with:
42-
version: v2.8
42+
version: v2.10

.golangci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ linters:
171171
gosec:
172172
excludes:
173173
- G304
174+
- G704
174175
nolintlint:
175176
# Enable to ensure that nolint directives are all used. Default is true.
176177
allow-unused: false

internal/cmd/policy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ func displayPolicy(opts repoOptions, pcy *policy.RepoPolicy) error {
311311
// ensureOrCreatePolicyFork checks the user has a fork of the policy repo.
312312
// In case they do not, asks to create a fork in their GitHub account.
313313
func ensureOrCreatePolicyFork(srctool *sourcetool.Tool) error {
314-
found, err := srctool.CheckPolicyRepoFork()
314+
found, err := srctool.CheckPolicyRepoFork(context.Background())
315315
if err != nil {
316316
return fmt.Errorf("checking for policy repo fork: %w", err)
317317
}

internal/cmd/setup.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Alternatively, to enable each control individually use: sourcetool setup control
158158
models.CONFIG_TAG_RULES, models.CONFIG_GEN_PROVENANCE, models.CONFIG_BRANCH_RULES,
159159
} {
160160
ok, actionDescr, remediateFn, err := srctool.ControlPrecheck(
161-
opts.GetBranch().Repository, []*models.Branch{opts.GetBranch()}, cc,
161+
cmd.Context(), opts.GetBranch().Repository, []*models.Branch{opts.GetBranch()}, cc,
162162
)
163163
if err != nil {
164164
return fmt.Errorf("checking prerequisites for %s: %w", cc, err)
@@ -217,7 +217,7 @@ sourcetool is about to perform the following actions on your behalf:
217217
}
218218

219219
err = srctool.OnboardRepository(
220-
opts.GetBranch().Repository, []*models.Branch{opts.GetBranch()},
220+
cmd.Context(), opts.GetBranch().Repository, []*models.Branch{opts.GetBranch()},
221221
)
222222
if err != nil {
223223
return fmt.Errorf("onboarding repo: %w", err)
@@ -371,7 +371,7 @@ a fork of the repository you want to protect.
371371

372372
// Check the control prerequisites
373373
ok, actionDescr, remediateFn, err := srctool.ControlPrecheck(
374-
opts.GetBranch().Repository, []*models.Branch{opts.GetBranch()}, cc,
374+
cmd.Context(), opts.GetBranch().Repository, []*models.Branch{opts.GetBranch()}, cc,
375375
)
376376
if err != nil {
377377
return fmt.Errorf("checking prerequisites for %s: %w", cc, err)
@@ -426,7 +426,9 @@ a fork of the repository you want to protect.
426426
for _, c := range opts.configs {
427427
cc := models.ControlConfiguration(c)
428428
// Run the prerequisites and run any remediations
429-
ok, _, remediateFn, err := srctool.ControlPrecheck(opts.GetBranch().Repository, []*models.Branch{opts.GetBranch()}, cc)
429+
ok, _, remediateFn, err := srctool.ControlPrecheck(
430+
cmd.Context(), opts.GetBranch().Repository, []*models.Branch{opts.GetBranch()}, cc,
431+
)
430432
if err != nil {
431433
return fmt.Errorf("checking prerequisites for %q: %w", cc, err)
432434
}
@@ -441,7 +443,7 @@ a fork of the repository you want to protect.
441443
}
442444
}
443445
err = srctool.ConfigureControls(
444-
opts.GetBranch().Repository, []*models.Branch{opts.GetBranch()}, cs,
446+
cmd.Context(), opts.GetBranch().Repository, []*models.Branch{opts.GetBranch()}, cs,
445447
)
446448
if err != nil {
447449
// if strings.Contains(err.Error(), models.ErrProtectionAlreadyInPlace.Error()) {

internal/cmd/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ sourcetool status myorg/myrepo@mybranch
110110
}
111111

112112
// Get the active repository controls
113-
controls, err := srctool.GetBranchControls(opts.GetRepository(), opts.GetBranch())
113+
controls, err := srctool.GetBranchControls(cmd.Context(), opts.GetRepository(), opts.GetBranch())
114114
if err != nil {
115115
return fmt.Errorf("fetching active controls: %w", err)
116116
}

pkg/attest/log.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ import (
99
)
1010

1111
func Debugf(format string, args ...any) {
12+
//nolint:gosec // G706 This is feneral purpose logger
1213
slog.Debug(fmt.Sprintf(format, args...))
1314
}

pkg/auth/authenticator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type DeviceCodeResponse struct {
5454

5555
// TokenResponse is the data structure returned when exchanging tokens
5656
type TokenResponse struct {
57-
AccessToken string `json:"access_token"`
57+
AccessToken string `json:"access_token"` //nolint:gosec // G117 This is the github struct
5858
TokenType string `json:"token_type"`
5959
Scope string `json:"scope"`
6060
Error string `json:"error"`

pkg/policy/policy_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,12 +692,12 @@ func assertProtectedBranchEquals(t *testing.T, got, expected *ProtectedBranch, i
692692

693693
if !reflect.DeepEqual(&actualCopy, &expectedCopy) || !sinceMatch {
694694
var errorMessage strings.Builder
695-
errorMessage.WriteString(fmt.Sprintf("ProtectedBranch structs not equal:\nExpected: %+v\nGot: %+v", expected, actual))
695+
fmt.Fprintf(&errorMessage, "ProtectedBranch structs not equal:\nExpected: %+v\nGot: %+v", expected, actual)
696696
if !sinceMatch {
697-
errorMessage.WriteString(fmt.Sprintf("\nSpecifically, 'Since' fields were not equal (Expected.Since: %v, Got.Since: %v)", expected.GetSince(), actual.GetSince()))
697+
fmt.Fprintf(&errorMessage, "\nSpecifically, 'Since' fields were not equal (Expected.Since: %v, Got.Since: %v)", expected.GetSince(), actual.GetSince())
698698
}
699699
if ignoreSince && actual.GetSince().AsTime() != (time.Time{}) { // Add note only if Since was ignored AND original got.Since was not zero
700-
errorMessage.WriteString(fmt.Sprintf("\n(Note: 'Since' field was ignored in comparison as requested. Original Expected.Since: %v, Original Got.Since: %v)", expected.GetSince(), actual.GetSince()))
700+
fmt.Fprintf(&errorMessage, "\n(Note: 'Since' field was ignored in comparison as requested. Original Expected.Since: %v, Original Got.Since: %v)", expected.GetSince(), actual.GetSince())
701701
}
702702
t.Error(errorMessage.String())
703703
}

pkg/sourcetool/tool.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ type Tool struct {
5252
}
5353

5454
// GetRepoControls returns the controls that are enabled in a repository branch.
55-
func (t *Tool) GetBranchControls(r *models.Repository, branch *models.Branch) (*slsa.ControlSetStatus, error) {
56-
ctx := context.Background()
55+
func (t *Tool) GetBranchControls(ctx context.Context, r *models.Repository, branch *models.Branch) (*slsa.ControlSetStatus, error) {
5756
backend, err := t.impl.GetVcsBackend(r)
5857
if err != nil {
5958
return nil, fmt.Errorf("getting VCS backend: %w", err)
6059
}
6160

61+
// Get the control status in the branch. Backends are expected to
62+
// return the full SLSA Source control catalog
6263
controls, err := t.impl.GetBranchControls(ctx, backend, r, branch)
6364
if err != nil {
6465
return nil, fmt.Errorf("getting branch controls: %w", err)
@@ -77,7 +78,7 @@ func (t *Tool) GetBranchControls(r *models.Repository, branch *models.Branch) (*
7778

7879
// OnboardRepository configures a repository to set up the required controls
7980
// to meet SLSA Source L3.
80-
func (t *Tool) OnboardRepository(repo *models.Repository, branches []*models.Branch) error {
81+
func (t *Tool) OnboardRepository(ctx context.Context, repo *models.Repository, branches []*models.Branch) error {
8182
backend, err := t.impl.GetVcsBackend(repo)
8283
if err != nil {
8384
return fmt.Errorf("getting VCS backend: %w", err)
@@ -99,7 +100,7 @@ func (t *Tool) OnboardRepository(repo *models.Repository, branches []*models.Bra
99100
}
100101

101102
// ConfigureControls sets up a control in the repo
102-
func (t *Tool) ConfigureControls(repo *models.Repository, branches []*models.Branch, configs []models.ControlConfiguration) error {
103+
func (t *Tool) ConfigureControls(ctx context.Context, repo *models.Repository, branches []*models.Branch, configs []models.ControlConfiguration) error {
103104
backend, err := t.impl.GetVcsBackend(repo)
104105
if err != nil {
105106
return fmt.Errorf("getting VCS backend: %w", err)
@@ -112,7 +113,7 @@ func (t *Tool) ConfigureControls(repo *models.Repository, branches []*models.Bra
112113
}
113114

114115
// Build the policy here:
115-
pcy, err := t.CreateBranchPolicy(context.Background(), repo, branches)
116+
pcy, err := t.CreateBranchPolicy(ctx, repo, branches)
116117
if err != nil {
117118
return fmt.Errorf("creating policy for: %w", err)
118119
}
@@ -135,7 +136,7 @@ func (t *Tool) ControlConfigurationDescr(branch *models.Branch, config models.Co
135136
return backend.ControlConfigurationDescr(branch, config)
136137
}
137138

138-
func (t *Tool) FindPolicyPR(repo *models.Repository) (*models.PullRequest, error) {
139+
func (t *Tool) FindPolicyPR(ctx context.Context, repo *models.Repository) (*models.PullRequest, error) {
139140
policyRepoOwner := policy.SourcePolicyRepoOwner
140141
policyRepoRepo := policy.SourcePolicyRepo
141142
o, r, ok := strings.Cut(t.Options.PolicyRepo, "/")
@@ -144,7 +145,7 @@ func (t *Tool) FindPolicyPR(repo *models.Repository) (*models.PullRequest, error
144145
policyRepoRepo = r
145146
}
146147

147-
pr, err := t.impl.SearchPullRequest(context.Background(), t.Authenticator, &models.Repository{
148+
pr, err := t.impl.SearchPullRequest(ctx, t.Authenticator, &models.Repository{
148149
Hostname: "github.com",
149150
Path: fmt.Sprintf("%s/%s", policyRepoOwner, policyRepoRepo),
150151
}, fmt.Sprintf("Add %s SLSA Source policy file", repo.Path))
@@ -157,7 +158,7 @@ func (t *Tool) FindPolicyPR(repo *models.Repository) (*models.PullRequest, error
157158

158159
// CheckPolicyRepoFork checks that the logged in user has a fork
159160
// of the configured policy repo.
160-
func (t *Tool) CheckPolicyRepoFork() (bool, error) {
161+
func (t *Tool) CheckPolicyRepoFork(_ context.Context) (bool, error) {
161162
if err := t.impl.CheckPolicyFork(&t.Options); err != nil {
162163
if strings.Contains(err.Error(), "404 Not Found") {
163164
return false, nil
@@ -277,7 +278,7 @@ func (t *Tool) CreatePolicyRepoFork(ctx context.Context) error {
277278
// Backend may optionally return a remediation function to correct the
278279
// prerequisite which the CLI can before attempting to enable the control.
279280
func (t *Tool) ControlPrecheck(
280-
r *models.Repository, branches []*models.Branch, config models.ControlConfiguration,
281+
_ context.Context, r *models.Repository, branches []*models.Branch, config models.ControlConfiguration,
281282
) (ok bool, remediationMessage string, remediateFn models.ControlPreRemediationFn, err error) {
282283
backend, err := t.impl.GetVcsBackend(r)
283284
if err != nil {

pkg/sourcetool/tool_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestGetBranchControls(t *testing.T) {
3535
tool := &Tool{
3636
impl: i,
3737
}
38-
res, err := tool.GetBranchControls(&models.Repository{}, &models.Branch{})
38+
res, err := tool.GetBranchControls(t.Context(), &models.Repository{}, &models.Branch{})
3939
require.NotNil(t, res)
4040
// This always has one more as we add the synyhetic policy check
4141
require.Len(t, res.Controls, 2)
@@ -48,7 +48,7 @@ func TestGetBranchControls(t *testing.T) {
4848
tool := &Tool{
4949
impl: i,
5050
}
51-
_, err := tool.GetBranchControls(&models.Repository{}, &models.Branch{})
51+
_, err := tool.GetBranchControls(t.Context(), &models.Repository{}, &models.Branch{})
5252
require.Error(t, err)
5353
})
5454
}
@@ -121,7 +121,7 @@ func TestConfigureControls(t *testing.T) {
121121
impl: i,
122122
}
123123

124-
err := tool.ConfigureControls(&models.Repository{}, []*models.Branch{{}}, tc.controls)
124+
err := tool.ConfigureControls(t.Context(), &models.Repository{}, []*models.Branch{{}}, tc.controls)
125125
if tc.mustErr {
126126
require.Error(t, err)
127127
return
@@ -195,7 +195,7 @@ func TestFindPolicyPR(t *testing.T) {
195195
impl: tc.prepare(t),
196196
}
197197

198-
prd, err := tool.FindPolicyPR(&models.Repository{})
198+
prd, err := tool.FindPolicyPR(t.Context(), &models.Repository{})
199199
if tc.mustErr {
200200
require.Error(t, err)
201201
return
@@ -241,7 +241,7 @@ func TestCheckPolicyFork(t *testing.T) {
241241
t.Run(tc.name, func(t *testing.T) {
242242
t.Parallel()
243243
tool := Tool{impl: tc.prepare(t)}
244-
found, err := tool.CheckPolicyRepoFork()
244+
found, err := tool.CheckPolicyRepoFork(t.Context())
245245
if tc.mustErr {
246246
require.Error(t, err)
247247
return
@@ -335,7 +335,7 @@ func TestOnboardRepository(t *testing.T) {
335335
tool, err := New()
336336
require.NoError(t, err)
337337
tool.impl = impl
338-
err = tool.OnboardRepository(&models.Repository{Path: "example/repo"}, []*models.Branch{{Name: "main"}})
338+
err = tool.OnboardRepository(t.Context(), &models.Repository{Path: "example/repo"}, []*models.Branch{{Name: "main"}})
339339
if tt.mustErr {
340340
require.Error(t, err)
341341
return

0 commit comments

Comments
 (0)