Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix download mode leak for latest and list handlers #1826

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions download.example.hcl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
downloadURL = "https://proxy.golang.org"

mode = "async_redirect"
mode = "sync"

download "github.com/gomods/*" {
mode = "sync"
mode = "async_redirect"
}

download "golang.org/x/*" {
15 changes: 14 additions & 1 deletion pkg/download/protocol.go
Original file line number Diff line number Diff line change
@@ -89,6 +89,12 @@ func (p *protocol) List(ctx context.Context, mod string) ([]string, error) {
var sErr, goErr error
var wg sync.WaitGroup

// if download mode is none for the specific mod, just return an error.
downloadMode := p.df.Match(mod)
if downloadMode == mode.None {
return nil, errors.E(op, errors.M(mod), errors.KindNotFound)
}

/*
TODO: potential refactor:

@@ -183,9 +189,16 @@ func (p *protocol) Latest(ctx context.Context, mod string) (*storage.RevInfo, er
const op errors.Op = "protocol.Latest"
ctx, span := observ.StartSpan(ctx, op.String())
defer span.End()

// if download mode is none for the specific mod, just return an error.
downloadMode := p.df.Match(mod)
if downloadMode == mode.None {
return nil, errors.E(op, errors.M(mod), errors.KindNotFound)
}

if p.networkMode == Offline {
// Go never pings the /@latest endpoint _first_. It always tries /list and if that
// endpoint returns an empty list then it fallsback to calling /@latest.
// endpoint returns an empty list then it fallbacks to calling /@latest.
return nil, errors.E(op, "Athens is in offline mode, use /list endpoint", errors.KindNotFound)
}
lr, _, err := p.lister.List(ctx, mod)
54 changes: 49 additions & 5 deletions pkg/download/protocol_test.go
Original file line number Diff line number Diff line change
@@ -28,7 +28,8 @@ import (
)

var (
testConfigPath = filepath.Join("..", "..", "config.dev.toml")
testConfigPath = filepath.Join("..", "..", "config.dev.toml")
testDownloadModeFilePath = filepath.Join("..", "..", "download.example.hcl")
)

func getDP(t *testing.T) Protocol {
@@ -37,6 +38,12 @@ func getDP(t *testing.T) Protocol {
if err != nil {
t.Fatalf("Unable to parse config file: %s", err.Error())
}

df, err := mode.NewFile(mode.Mode("file:"+testDownloadModeFilePath), conf.DownloadURL)
if err != nil {
t.Fatalf("Unable to parse download mode file: %s", err.Error())
}

goBin := conf.GoBinary
fs := afero.NewOsFs()
mf, err := module.NewGoGetFetcher(goBin, conf.GoGetDir, conf.GoBinaryEnvVars, fs)
@@ -49,10 +56,11 @@ func getDP(t *testing.T) Protocol {
}
st := stash.New(mf, s, nop.New())
return New(&Opts{
Storage: s,
Stasher: st,
Lister: module.NewVCSLister(goBin, conf.GoBinaryEnvVars, fs),
NetworkMode: Strict,
Storage: s,
Stasher: st,
Lister: module.NewVCSLister(goBin, conf.GoBinaryEnvVars, fs),
NetworkMode: Strict,
DownloadFile: df,
})
}

@@ -149,10 +157,33 @@ var listModeTests = []listModeTest{
wantTags: nil,
wantErr: true,
},
{
name: "download mode is none for module",
networkmode: Strict,
path: "golang.org/x/crypto",
storageTags: []string{"v0.1.0"},
upstreamList: []string{"v0.1.0", "v0.2.0", "v0.3.0", "v0.4.0", "v0.5.0", "v0.6.0"},
wantTags: nil,
wantErr: true,
},
{
name: "download mode is async_redirect for module",
networkmode: Strict,
path: "github.com/gomods/athens",
storageTags: []string{"v0.1.0"},
upstreamList: []string{"v0.1.0", "v0.2.0", "v0.3.0"},
wantTags: []string{"v0.1.0", "v0.2.0", "v0.3.0"},
wantErr: true,
},
}

func TestListMode(t *testing.T) {
ctx := context.Background()
df, err := mode.NewFile(mode.Mode("file:"+testDownloadModeFilePath), "")
if err != nil {
t.Fatalf("Unable to parse download mode file: %s", err.Error())
}

for _, tc := range listModeTests {
strg, err := mem.NewStorage()
require.NoError(t, err)
@@ -164,6 +195,7 @@ func TestListMode(t *testing.T) {
storage: strg,
lister: ml,
networkMode: tc.networkmode,
df: df,
}
for _, tag := range tc.storageTags {
err := strg.Save(ctx, tc.path, tag, []byte("mod"), bytes.NewReader([]byte("zip")), []byte("info"))
@@ -237,6 +269,12 @@ var latestTests = []latestTest{
Time: time.Date(2018, 8, 3, 17, 16, 00, 0, time.UTC),
},
},
{
name: "download mode is none",
path: "golang.org/x/crypto",
info: nil,
err: true,
},
}

func TestLatest(t *testing.T) {
@@ -330,6 +368,12 @@ var modTests = []modTest{
version: "v1.0.0",
err: true,
},
{
name: "download mode is none",
path: "golang.org/x/crypto",
version: "v0.6.0",
err: true,
},
}

func rmNewLine(input string) string {