Skip to content

Commit

Permalink
Merge pull request #9 from rchincha/gorilla-mux
Browse files Browse the repository at this point in the history
router: move to gorilla/mux to support multiple name path components
  • Loading branch information
rchamarthy authored Jul 11, 2019
2 parents 131e19c + 066bf1b commit 6146f2f
Show file tree
Hide file tree
Showing 15 changed files with 660 additions and 418 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ binary: doc

.PHONY: debug
debug: doc
go build -v -gcflags '-N -l' -o bin/zot-debug -tags=jsoniter ./cmd/zot
go build -v -gcflags all='-N -l' -o bin/zot-debug -tags=jsoniter ./cmd/zot

.PHONY: test
test:
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
# Caveats

* go 1.12+
* Image name consists of only one path component, for example, _busybox:latest_ instead _ubuntu/busybox:latest_
* The OCI distribution spec is still WIP, and we try to keep up
32 changes: 28 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ go_repository(

go_repository(
name = "com_github_smartystreets_goconvey",
commit = "68dc04aab96a",
commit = "9d28bd7c0945",
importpath = "github.com/smartystreets/goconvey",
)

Expand Down Expand Up @@ -648,7 +648,7 @@ go_repository(
go_repository(
name = "com_github_swaggo_swag",
importpath = "github.com/swaggo/swag",
tag = "v1.5.1",
tag = "v1.6.2",
)

go_repository(
Expand All @@ -660,7 +660,7 @@ go_repository(
go_repository(
name = "com_github_ugorji_go",
importpath = "github.com/ugorji/go",
tag = "v1.1.5-pre",
tag = "v1.1.4",
)

go_repository(
Expand Down Expand Up @@ -761,7 +761,7 @@ go_repository(

go_repository(
name = "org_golang_x_crypto",
commit = "ea8f1a30c443",
commit = "4def268fd1a4",
importpath = "golang.org/x/crypto",
)

Expand Down Expand Up @@ -830,3 +830,27 @@ go_repository(
importpath = "go.uber.org/zap",
tag = "v1.10.0",
)

go_repository(
name = "com_github_gorilla_mux",
importpath = "github.com/gorilla/mux",
tag = "v1.7.3",
)

go_repository(
name = "com_github_kylebanks_depth",
importpath = "github.com/KyleBanks/depth",
tag = "v1.2.1",
)

go_repository(
name = "com_github_swaggo_files",
commit = "630677cd5c14",
importpath = "github.com/swaggo/files",
)

go_repository(
name = "com_github_swaggo_http_swagger",
commit = "c2865af9083e",
importpath = "github.com/swaggo/http-swagger",
)
2 changes: 1 addition & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2019-06-21 14:49:20.043038483 -0700 PDT m=+0.069174432
// 2019-07-10 17:20:00.064076444 -0700 PDT m=+0.118699568

package docs

Expand Down
2 changes: 1 addition & 1 deletion examples/config-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"rootDirectory":"/tmp/zot"
},
"http": {
"address":"127.0.0.1",
"address":"0.0.0.0",
"port":"8080"
},
"log":{
Expand Down
15 changes: 9 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ go 1.12

require (
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc
github.com/gin-gonic/gin v1.4.0
github.com/gofrs/uuid v3.2.0+incompatible
github.com/gorilla/mux v1.7.3
github.com/json-iterator/go v1.1.6
github.com/mitchellh/mapstructure v1.1.2
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/opencontainers/distribution-spec v1.0.0-rc0
github.com/opencontainers/go-digest v1.0.0-rc1
github.com/opencontainers/image-spec v1.0.1
github.com/rs/zerolog v1.14.3
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a
github.com/smartystreets/goconvey v0.0.0-20190710185942-9d28bd7c0945
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/swaggo/gin-swagger v1.1.0
github.com/swaggo/swag v1.5.1
github.com/ugorji/go v1.1.5-pre // indirect
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14 // indirect
github.com/swaggo/http-swagger v0.0.0-20190614090009-c2865af9083e
github.com/swaggo/swag v1.6.2
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
gopkg.in/resty.v1 v1.12.0
)
59 changes: 14 additions & 45 deletions go.sum

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions pkg/api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
"controller.go",
"errors.go",
"log.go",
"regexp.go",
"routes.go",
],
importpath = "github.com/anuvu/zot/pkg/api",
Expand All @@ -16,12 +17,12 @@ go_library(
"//docs:go_default_library",
"//errors:go_default_library",
"//pkg/storage:go_default_library",
"@com_github_gin_gonic_gin//:go_default_library",
"@com_github_gorilla_mux//:go_default_library",
"@com_github_json_iterator_go//:go_default_library",
"@com_github_opencontainers_distribution_spec//:go_default_library",
"@com_github_opencontainers_image_spec//specs-go/v1:go_default_library",
"@com_github_rs_zerolog//:go_default_library",
"@com_github_swaggo_gin_swagger//:go_default_library",
"@com_github_swaggo_gin_swagger//swaggerFiles:go_default_library",
"@com_github_swaggo_http_swagger//:go_default_library",
"@org_golang_x_crypto//bcrypt:go_default_library",
],
)
Expand Down
86 changes: 48 additions & 38 deletions pkg/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ import (
"strings"
"time"

"github.com/gin-gonic/gin"
"github.com/gorilla/mux"
"golang.org/x/crypto/bcrypt"
)

func authFail(ginCtx *gin.Context, realm string, delay int) {
func authFail(w http.ResponseWriter, realm string, delay int) {
time.Sleep(time.Duration(delay) * time.Second)
ginCtx.Header("WWW-Authenticate", realm)
ginCtx.AbortWithStatusJSON(http.StatusUnauthorized, NewError(UNAUTHORIZED))
w.Header().Set("WWW-Authenticate", realm)
w.Header().Set("Content-Type", "application/json")
WriteJSON(w, http.StatusUnauthorized, NewError(UNAUTHORIZED))
}

func BasicAuthHandler(c *Controller) gin.HandlerFunc {
func BasicAuthHandler(c *Controller) mux.MiddlewareFunc {
if c.Config.HTTP.Auth.HTPasswd.Path == "" {
// no authentication
return func(ginCtx *gin.Context) {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Process request
next.ServeHTTP(w, r)
})
}
}

Expand All @@ -49,43 +54,48 @@ func BasicAuthHandler(c *Controller) gin.HandlerFunc {
credMap[tokens[0]] = tokens[1]
}

return func(ginCtx *gin.Context) {
basicAuth := ginCtx.Request.Header.Get("Authorization")
if basicAuth == "" {
authFail(ginCtx, realm, delay)
return
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
basicAuth := r.Header.Get("Authorization")
if basicAuth == "" {
authFail(w, realm, delay)
return
}

s := strings.SplitN(basicAuth, " ", 2)
if len(s) != 2 || strings.ToLower(s[0]) != "basic" {
authFail(ginCtx, realm, delay)
return
}
s := strings.SplitN(basicAuth, " ", 2)
if len(s) != 2 || strings.ToLower(s[0]) != "basic" {
authFail(w, realm, delay)
return
}

b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
authFail(ginCtx, realm, delay)
return
}
b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
authFail(w, realm, delay)
return
}

pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
authFail(ginCtx, realm, delay)
return
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
authFail(w, realm, delay)
return
}

username := pair[0]
passphrase := pair[1]
username := pair[0]
passphrase := pair[1]

passphraseHash, ok := credMap[username]
if !ok {
authFail(ginCtx, realm, delay)
return
}
passphraseHash, ok := credMap[username]
if !ok {
authFail(w, realm, delay)
return
}

if err := bcrypt.CompareHashAndPassword([]byte(passphraseHash), []byte(passphrase)); err != nil {
authFail(ginCtx, realm, delay)
return
}
if err := bcrypt.CompareHashAndPassword([]byte(passphraseHash), []byte(passphrase)); err != nil {
authFail(w, realm, delay)
return
}

// Process request
next.ServeHTTP(w, r)
})
}
}
13 changes: 4 additions & 9 deletions pkg/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"net/http"

"github.com/anuvu/zot/pkg/storage"
"github.com/gin-gonic/gin"
"github.com/gorilla/mux"
"github.com/rs/zerolog"
)

type Controller struct {
Config *Config
Router *gin.Engine
Router *mux.Router
ImageStore *storage.ImageStore
Log zerolog.Logger
Server *http.Server
Expand All @@ -26,13 +26,8 @@ func NewController(config *Config) *Controller {
}

func (c *Controller) Run() error {
if c.Config.Log.Level == "debug" {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
engine := gin.New()
engine.Use(gin.Recovery(), Logger(c.Log))
engine := mux.NewRouter()
engine.Use(Logger(c.Log))
c.Router = engine
_ = NewRouteHandler(c)

Expand Down
6 changes: 3 additions & 3 deletions pkg/api/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestBasicAuth(t *testing.T) {
}()

// without creds, should get access error
resp, err := resty.R().Get(BaseURL1)
resp, err := resty.R().Get(BaseURL1 + "/v2/")
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
So(resp.StatusCode(), ShouldEqual, 401)
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestTLSWithBasicAuth(t *testing.T) {
So(resp.StatusCode(), ShouldEqual, 400)

// without creds, should get access error
resp, err = resty.R().Get(BaseSecureURL2)
resp, err = resty.R().Get(BaseSecureURL2 + "/v2/")
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
So(resp.StatusCode(), ShouldEqual, 401)
Expand Down Expand Up @@ -220,7 +220,7 @@ func TestTLSMutualAuth(t *testing.T) {
defer func() { resty.SetCertificates(tls.Certificate{}) }()

// with client certs but without creds, should get access error
resp, err = resty.R().Get(BaseSecureURL2)
resp, err = resty.R().Get(BaseSecureURL2 + "/v2/")
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
So(resp.StatusCode(), ShouldEqual, 401)
Expand Down
Loading

0 comments on commit 6146f2f

Please sign in to comment.