Skip to content

Commit

Permalink
Plumb contexts everywhere
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Johnson <[email protected]>
  • Loading branch information
jonjohnsonjr committed Feb 11, 2024
1 parent 8bc7984 commit 488b538
Show file tree
Hide file tree
Showing 52 changed files with 248 additions and 241 deletions.
13 changes: 7 additions & 6 deletions pkg/advisory/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package advisory

import (
"context"
"fmt"
"sort"

Expand All @@ -16,7 +17,7 @@ type CreateOptions struct {

// Create creates a new advisory in the `advisories` section of the document at
// the provided path.
func Create(req Request, opts CreateOptions) error {
func Create(ctx context.Context, req Request, opts CreateOptions) error {
err := req.Validate()
if err != nil {
return err
Expand All @@ -28,7 +29,7 @@ func Create(req Request, opts CreateOptions) error {
switch count {
case 0:
// i.e. no advisories file for this package yet
return createAdvisoryConfig(opts.AdvisoryDocs, req)
return createAdvisoryConfig(ctx, opts.AdvisoryDocs, req)

case 1:
newAdvisoryID := req.VulnerabilityID
Expand All @@ -52,13 +53,13 @@ func Create(req Request, opts CreateOptions) error {

return advisories, nil
})
err := documents.Update(u)
err := documents.Update(ctx, u)
if err != nil {
return fmt.Errorf("unable to create advisory %q for %q: %w", newAdvisoryID, req.Package, err)
}

// Update the schema version to the latest version.
err = documents.Update(v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
err = documents.Update(ctx, v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
if err != nil {
return fmt.Errorf("unable to update schema version for %q: %w", req.Package, err)
}
Expand All @@ -69,15 +70,15 @@ func Create(req Request, opts CreateOptions) error {
return fmt.Errorf("cannot create advisory: found %d advisory documents for package %q", count, req.Package)
}

func createAdvisoryConfig(documents *configs.Index[v2.Document], req Request) error {
func createAdvisoryConfig(ctx context.Context, documents *configs.Index[v2.Document], req Request) error {
newAdvisoryID := req.VulnerabilityID
newAdvisory := v2.Advisory{
ID: newAdvisoryID,
Aliases: req.Aliases,
Events: []v2.Event{req.Event},
}

err := documents.Create(fmt.Sprintf("%s.advisories.yaml", req.Package), v2.Document{
err := documents.Create(ctx, fmt.Sprintf("%s.advisories.yaml", req.Package), v2.Document{
SchemaVersion: v2.SchemaVersion,
Package: v2.Package{
Name: req.Package,
Expand Down
5 changes: 3 additions & 2 deletions pkg/advisory/create_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package advisory

import (
"context"
"os"
"testing"
"time"
Expand Down Expand Up @@ -190,10 +191,10 @@ func TestCreate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
// We want a fresh memfs for each test case.
fsys := memfs.New(dirFS)
advisoryDocs, err := v2.NewIndex(fsys)
advisoryDocs, err := v2.NewIndex(context.Background(), fsys)
require.NoError(t, err)

err = Create(tt.req, CreateOptions{
err = Create(context.Background(), tt.req, CreateOptions{
AdvisoryDocs: advisoryDocs,
})

Expand Down
5 changes: 3 additions & 2 deletions pkg/advisory/diff_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package advisory

import (
"context"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -248,9 +249,9 @@ func TestIndexDiff(t *testing.T) {
bDir := filepath.Join("testdata", "diff", tt.name, "b")
aFsys := rwos.DirFS(aDir)
bFsys := rwos.DirFS(bDir)
aIndex, err := v2.NewIndex(aFsys)
aIndex, err := v2.NewIndex(context.Background(), aFsys)
require.NoError(t, err)
bIndex, err := v2.NewIndex(bFsys)
bIndex, err := v2.NewIndex(context.Background(), bFsys)
require.NoError(t, err)

diff := IndexDiff(aIndex, bIndex)
Expand Down
2 changes: 1 addition & 1 deletion pkg/advisory/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (opts DiscoverOptions) discoverMatchesForPackage(ctx context.Context, pkg s

for i := range matches {
match := matches[i]
err := Create(Request{
err := Create(ctx, Request{
Package: pkg,
VulnerabilityID: match.Vulnerability.ID,
Aliases: nil,
Expand Down
8 changes: 4 additions & 4 deletions pkg/advisory/discover_aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ func (opts *DiscoverAliasesOptions) discoverAliasesForAdvisoriesWithCVEIDs(ctx c

return advisories, nil
})
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(u)
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(ctx, u)
if err != nil {
return err
}

// Update the schema version to the latest version.
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(ctx, v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
if err != nil {
return fmt.Errorf("unable to update schema version for %q: %w", doc.Name(), err)
}
Expand Down Expand Up @@ -120,13 +120,13 @@ func (opts *DiscoverAliasesOptions) discoverAliasesForAdvisoriesWithGHSAIDs(ctx

return advisories, nil
})
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(u)
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(ctx, u)
if err != nil {
return err
}

// Update the schema version to the latest version.
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(ctx, v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
if err != nil {
return fmt.Errorf("unable to update schema version for %q: %w", doc.Name(), err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/advisory/discover_aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestDiscoverAliases(t *testing.T) {
// back to disk.
fsys := memfs.New(os.DirFS("testdata/discover_aliases/advisories"))

advisoryDocs, err := v2.NewIndex(fsys)
advisoryDocs, err := v2.NewIndex(context.Background(), fsys)
if err != nil {
t.Fatalf("unable to create advisory docs index: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/advisory/export_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package advisory

import (
"context"
"io"
"os"
"testing"
Expand Down Expand Up @@ -36,7 +37,7 @@ func Test_ExportFuncs(t *testing.T) {
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
advisoryFsys := rwos.DirFS(testdataDir)
advisoryDocs, err := v2.NewIndex(advisoryFsys)
advisoryDocs, err := v2.NewIndex(context.Background(), advisoryFsys)
require.NoError(t, err)
indices := []*configs.Index[v2.Document]{advisoryDocs}

Expand Down
3 changes: 2 additions & 1 deletion pkg/advisory/secdb_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package advisory

import (
"context"
"os"
"testing"

Expand Down Expand Up @@ -65,7 +66,7 @@ func TestBuildSecurityDatabase(t *testing.T) {
indices := make([]*configs.Index[v2.Document], 0, len(tt.advisoryDirs))
for _, dir := range tt.advisoryDirs {
advisoryFsys := rwos.DirFS(dir)
advisoryCfgs, err := v2.NewIndex(advisoryFsys)
advisoryCfgs, err := v2.NewIndex(context.Background(), advisoryFsys)
require.NoError(t, err)
indices = append(indices, advisoryCfgs)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/advisory/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package advisory

import (
"context"
"fmt"
"sort"

Expand All @@ -16,7 +17,7 @@ type UpdateOptions struct {

// Update adds a new event to an existing advisory (named by the vuln parameter)
// in the document at the provided path.
func Update(req Request, opts UpdateOptions) error {
func Update(ctx context.Context, req Request, opts UpdateOptions) error {
vulnID := req.VulnerabilityID

documents := opts.AdvisoryDocs.Select().WhereName(req.Package)
Expand All @@ -40,13 +41,13 @@ func Update(req Request, opts UpdateOptions) error {

return advisories, nil
})
err := documents.Update(u)
err := documents.Update(ctx, u)
if err != nil {
return fmt.Errorf("unable to add entry for advisory %q in %q: %w", vulnID, req.Package, err)
}

// Update the schema version to the latest version.
err = documents.Update(v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
err = documents.Update(ctx, v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
if err != nil {
return fmt.Errorf("unable to update schema version for %q: %w", req.Package, err)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/advisory/update_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package advisory

import (
"context"
"os"
"testing"
"time"
Expand Down Expand Up @@ -122,10 +123,10 @@ func TestUpdate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
// We want a fresh memfs for each test case.
memFS := memfs.New(dirFS)
advisoryDocs, err := v2.NewIndex(memFS)
advisoryDocs, err := v2.NewIndex(context.Background(), memFS)
require.NoError(t, err)

err = Update(tt.req, UpdateOptions{
err = Update(context.Background(), tt.req, UpdateOptions{
AdvisoryDocs: advisoryDocs,
})

Expand Down
18 changes: 9 additions & 9 deletions pkg/advisory/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func TestValidate(t *testing.T) {
bDir := filepath.Join("testdata", "diff", tt.name, "b")
aFsys := rwos.DirFS(aDir)
bFsys := rwos.DirFS(bDir)
aIndex, err := v2.NewIndex(aFsys)
aIndex, err := v2.NewIndex(context.Background(), aFsys)
require.NoError(t, err)
bIndex, err := v2.NewIndex(bFsys)
bIndex, err := v2.NewIndex(context.Background(), bFsys)
require.NoError(t, err)

err = Validate(context.Background(), ValidateOptions{
Expand Down Expand Up @@ -186,9 +186,9 @@ func TestValidate(t *testing.T) {
bDir := filepath.Join("testdata", "diff", tt.name, "b")
aFsys := rwos.DirFS(aDir)
bFsys := rwos.DirFS(bDir)
aIndex, err := v2.NewIndex(aFsys)
aIndex, err := v2.NewIndex(context.Background(), aFsys)
require.NoError(t, err)
bIndex, err := v2.NewIndex(bFsys)
bIndex, err := v2.NewIndex(context.Background(), bFsys)
require.NoError(t, err)

err = Validate(context.Background(), ValidateOptions{
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestValidate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
dir := filepath.Join("testdata", "validate", tt.name)
fsys := rwos.DirFS(dir)
index, err := v2.NewIndex(fsys)
index, err := v2.NewIndex(context.Background(), fsys)
require.NoError(t, err)

err = Validate(context.Background(), ValidateOptions{
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestValidate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
dir := filepath.Join("testdata", "validate", "fixed-version")
fsys := rwos.DirFS(dir)
index, err := v2.NewIndex(fsys)
index, err := v2.NewIndex(context.Background(), fsys)
require.NoError(t, err)

err = Validate(context.Background(), ValidateOptions{
Expand Down Expand Up @@ -359,7 +359,7 @@ func TestValidate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
dir := filepath.Join("testdata", "validate", tt.name)
fsys := rwos.DirFS(dir)
index, err := v2.NewIndex(fsys)
index, err := v2.NewIndex(context.Background(), fsys)
require.NoError(t, err)

err = Validate(context.Background(), ValidateOptions{
Expand All @@ -378,14 +378,14 @@ func TestValidate(t *testing.T) {

func distroWithKo(t *testing.T) *configs.Index[config.Configuration] {
fsys := rwos.DirFS(filepath.Join("testdata", "validate", "package-existence", "distro"))
index, err := build.NewIndex(fsys)
index, err := build.NewIndex(context.Background(), fsys)
require.NoError(t, err)
return index
}

func distroWithNothing(t *testing.T) *configs.Index[config.Configuration] {
fsys := rwos.DirFS(filepath.Join("testdata", "validate", "package-existence", "distro-empty"))
index, err := build.NewIndex(fsys)
index, err := build.NewIndex(context.Background(), fsys)
require.NoError(t, err)
return index
}
16 changes: 8 additions & 8 deletions pkg/checks/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ type CheckUpdateOptions struct {
}

// SetupUpdate will create the options needed to call wolfictl update functions
func SetupUpdate() (*update.Options, lint.EvalRuleErrors) {
o := update.New()
func SetupUpdate(ctx context.Context) (*update.Options, lint.EvalRuleErrors) {
o := update.New(ctx)
o.GithubReleaseQuery = true
o.ReleaseMonitoringQuery = true
o.ErrorMessages = make(map[string]string)
Expand All @@ -47,7 +47,7 @@ func SetupUpdate() (*update.Options, lint.EvalRuleErrors) {

// CheckUpdates will use the melange update config to get the latest versions and validate fetch and git-checkout pipelines
func (o CheckUpdateOptions) CheckUpdates(ctx context.Context, files []string) error {
updateOpts, checkErrors := SetupUpdate()
updateOpts, checkErrors := SetupUpdate(ctx)

changedPackages := GetPackagesToUpdate(files)

Expand Down Expand Up @@ -254,7 +254,7 @@ func (o CheckUpdateOptions) processUpdates(ctx context.Context, latestVersions m
}

// download or git clone sources into a temp folder to validate the update config
verifyPipelines(o, updated, mutations, checkErrors)
verifyPipelines(ctx, o, updated, mutations, checkErrors)
}
return nil
}
Expand All @@ -265,13 +265,13 @@ func applyOverrides(options *CheckUpdateOptions, dryRunConfig *config.Configurat
}
}

func verifyPipelines(o CheckUpdateOptions, updated *config.Configuration, mutations map[string]string, checkErrors *lint.EvalRuleErrors) {
func verifyPipelines(ctx context.Context, o CheckUpdateOptions, updated *config.Configuration, mutations map[string]string, checkErrors *lint.EvalRuleErrors) {
for i := range updated.Pipeline {
var err error
pipeline := updated.Pipeline[i]

if pipeline.Uses == "fetch" {
err = o.verifyFetch(&pipeline, mutations)
err = o.verifyFetch(ctx, &pipeline, mutations)
}
if pipeline.Uses == "git-checkout" {
err = o.verifyGitCheckout(&pipeline, mutations)
Expand All @@ -282,7 +282,7 @@ func verifyPipelines(o CheckUpdateOptions, updated *config.Configuration, mutati
}
}

func (o CheckUpdateOptions) verifyFetch(p *config.Pipeline, m map[string]string) error {
func (o CheckUpdateOptions) verifyFetch(ctx context.Context, p *config.Pipeline, m map[string]string) error {
uriValue := p.With["uri"]
if uriValue == "" {
return fmt.Errorf("no uri to fetch")
Expand All @@ -296,7 +296,7 @@ func (o CheckUpdateOptions) verifyFetch(p *config.Pipeline, m map[string]string)

o.Logger.Printf("downloading sources from %s into a temporary directory, this may take a while", evaluatedURI)

filename, err := util.DownloadFile(context.TODO(), evaluatedURI)
filename, err := util.DownloadFile(ctx, evaluatedURI)
if err != nil {
return fmt.Errorf("failed to verify fetch %s: %w", evaluatedURI, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/advisory_alias_discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ than attempting any kind of merge of the separate advisories.
}

advisoriesFsys := rwos.DirFS(advisoriesRepoDir)
advisoryDocs, err := v2.NewIndex(advisoriesFsys)
advisoryDocs, err := v2.NewIndex(cmd.Context(), advisoriesFsys)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 488b538

Please sign in to comment.