Skip to content

Commit

Permalink
Filter: fixing after refactoring (gomods#867)
Browse files Browse the repository at this point in the history
* added some clarity

* no need to handle pseudo versions

* gofmt

* tests ok

* reshuffled again
  • Loading branch information
michalpristas authored and manugupt1 committed Nov 6, 2018
1 parent 0b3dc6e commit f53ab92
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
10 changes: 5 additions & 5 deletions cmd/proxy/actions/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ func App(conf *config.Config) (*buffalo.App, error) {

initializeAuth(app)

user, pass, ok := conf.BasicAuth()
if ok {
app.Use(basicAuth(user, pass))
}

if !conf.FilterOff() {
mf, err := module.NewFilter(conf.FilterFile)
if err != nil {
Expand All @@ -132,11 +137,6 @@ func App(conf *config.Config) (*buffalo.App, error) {
app.Use(mw.LogEntryMiddleware(mw.NewValidationMiddleware, lggr, vHook))
}

user, pass, ok := conf.BasicAuth()
if ok {
app.Use(basicAuth(user, pass))
}

if err := addProxyRoutes(app, store, lggr, conf.GoBinary, conf.GoGetWorkers, conf.ProtocolWorkers); err != nil {
err = fmt.Errorf("error adding proxy routes (%s)", err)
return nil, err
Expand Down
28 changes: 9 additions & 19 deletions pkg/middleware/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// NewFilterMiddleware builds a middleware function that implements the
// filters configured in the filter file.
func NewFilterMiddleware(mf *module.Filter, registryEndpoint string) buffalo.MiddlewareFunc {
func NewFilterMiddleware(mf *module.Filter, upstreamEndpoint string) buffalo.MiddlewareFunc {
const op errors.Op = "actions.NewFilterMiddleware"

return func(next buffalo.Handler) buffalo.Handler {
Expand All @@ -25,23 +25,17 @@ func NewFilterMiddleware(mf *module.Filter, registryEndpoint string) buffalo.Mid
return next(c)
}

// not checking the error. Not all requests include a version
// i.e. list requests path is like /{module:.+}/@v/list with no version parameter
version, _ := paths.GetVersion(c)

if isPseudoVersion(version) {
return next(c)
}

rule := mf.Rule(mod)
switch rule {
case module.Exclude:
// Exclude: ignore request for this module
return c.Render(http.StatusForbidden, nil)
case module.Direct:
return next(c)
case module.Include:
// TODO : spin up cache filling worker and serve the request using the cache
newURL := redirectToRegistryURL(registryEndpoint, c.Request().URL)
// Include: please handle this module in a usual way
return next(c)
case module.Direct:
// Direct: do not store modules locally, use upstream proxy
newURL := redirectToUpstreamURL(upstreamEndpoint, c.Request().URL)
return c.Redirect(http.StatusSeeOther, newURL)
}

Expand All @@ -50,10 +44,6 @@ func NewFilterMiddleware(mf *module.Filter, registryEndpoint string) buffalo.Mid
}
}

func isPseudoVersion(version string) bool {
return strings.HasPrefix(version, "v0.0.0-")
}

func redirectToRegistryURL(registryEndpoint string, u *url.URL) string {
return strings.TrimSuffix(registryEndpoint, "/") + u.Path
func redirectToUpstreamURL(upstreamEndpoint string, u *url.URL) string {
return strings.TrimSuffix(upstreamEndpoint, "/") + u.Path
}
4 changes: 2 additions & 2 deletions pkg/middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func newTestFilter(filterFile string) (*module.Filter, error) {
if err != nil {
return nil, err
}
f.AddRule("github.com/gomods/athens/", module.Include)
f.AddRule("github.com/gomods/athens/", module.Direct)
f.AddRule("github.com/athens-artifacts/no-tags", module.Exclude)
f.AddRule("github.com/athens-artifacts", module.Direct)
f.AddRule("github.com/athens-artifacts", module.Include)
return f, nil
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/module/filterRule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package module
type FilterRule int

const (
// Default filter rule does not alter default behavior
// Default filter rule does not alter default/parent behavior
Default FilterRule = iota
// Include filter rule includes package and its children from communication
// Include treats modules the usual way
// Used for reverting Exclude of parent path
Include
// Exclude filter rule excludes package and its children from communication
Exclude
// Direct filter rule forces the package to be fetched directly from the vcs
// Direct filter rule forces the package to be fetched directly from upstream proxy
Direct
)

0 comments on commit f53ab92

Please sign in to comment.