add copyright and fix codecy

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
wang yan 2019-07-03 16:03:20 +08:00
parent 57821b1b4c
commit b3c5137a2f
14 changed files with 231 additions and 13 deletions

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package blobquota package blobquota
import ( import (
@ -17,12 +31,14 @@ type blobQuotaHandler struct {
next http.Handler next http.Handler
} }
// New ...
func New(next http.Handler) http.Handler { func New(next http.Handler) http.Handler {
return &blobQuotaHandler{ return &blobQuotaHandler{
next: next, next: next,
} }
} }
// ServeHTTP ...
func (bqh blobQuotaHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (bqh blobQuotaHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodPut { if req.Method == http.MethodPut {
match, _ := util.MatchPutBlobURL(req) match, _ := util.MatchPutBlobURL(req)

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package middlewares package middlewares
import ( import (
@ -14,17 +28,19 @@ import (
"net/http" "net/http"
) )
// DefaultCreator ...
type DefaultCreator struct { type DefaultCreator struct {
middlewares []string middlewares []string
} }
// New ...
func New(middlewares []string) *DefaultCreator { func New(middlewares []string) *DefaultCreator {
return &DefaultCreator{ return &DefaultCreator{
middlewares: middlewares, middlewares: middlewares,
} }
} }
// CreateChain ... // Create creates a middleware chain ...
func (b *DefaultCreator) Create() *alice.Chain { func (b *DefaultCreator) Create() *alice.Chain {
chain := alice.New() chain := alice.New()
for _, mName := range b.middlewares { for _, mName := range b.middlewares {

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package middlewares package middlewares
// const variables // const variables
@ -12,5 +26,5 @@ const (
BLOBQUOTA = "blobquota" BLOBQUOTA = "blobquota"
) )
// sequential organization // Middlewares with sequential organization
var Middlewares = []string{READONLY, URL, MUITIPLEMANIFEST, LISTREPO, CONTENTTRUST, VULNERABLE, BLOBQUOTA, REGQUOTA} var Middlewares = []string{READONLY, URL, MUITIPLEMANIFEST, LISTREPO, CONTENTTRUST, VULNERABLE, BLOBQUOTA, REGQUOTA}

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package contenttrust package contenttrust
import ( import (
@ -9,18 +23,21 @@ import (
"strings" "strings"
) )
// NotaryEndpoint ...
var NotaryEndpoint = "" var NotaryEndpoint = ""
type contentTrustHandler struct { type contentTrustHandler struct {
next http.Handler next http.Handler
} }
// New ...
func New(next http.Handler) http.Handler { func New(next http.Handler) http.Handler {
return &contentTrustHandler{ return &contentTrustHandler{
next: next, next: next,
} }
} }
// ServeHTTP ...
func (cth contentTrustHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (cth contentTrustHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
imgRaw := req.Context().Value(util.ImageInfoCtxKey) imgRaw := req.Context().Value(util.ImageInfoCtxKey)
if imgRaw == nil || !config.WithNotary() { if imgRaw == nil || !config.WithNotary() {

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package middlewares package middlewares
import ( import (

View File

@ -1,7 +1,22 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package middlewares package middlewares
import "github.com/justinas/alice" import "github.com/justinas/alice"
// ChainCreator ...
type ChainCreator interface { type ChainCreator interface {
Create(middlewares []string) *alice.Chain Create(middlewares []string) *alice.Chain
} }

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package listrepo package listrepo
import ( import (
@ -19,12 +33,14 @@ type listReposHandler struct {
next http.Handler next http.Handler
} }
// New ...
func New(next http.Handler) http.Handler { func New(next http.Handler) http.Handler {
return &listReposHandler{ return &listReposHandler{
next: next, next: next,
} }
} }
// ServeHTTP ...
func (lrh listReposHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (lrh listReposHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
var rec *httptest.ResponseRecorder var rec *httptest.ResponseRecorder
listReposFlag := matchListRepos(req) listReposFlag := matchListRepos(req)

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package multiplmanifest package multiplmanifest
import ( import (
@ -7,18 +21,19 @@ import (
"strings" "strings"
) )
type MultipleManifestHandler struct { type multipleManifestHandler struct {
next http.Handler next http.Handler
} }
// New ...
func New(next http.Handler) http.Handler { func New(next http.Handler) http.Handler {
return &MultipleManifestHandler{ return &multipleManifestHandler{
next: next, next: next,
} }
} }
// The handler is responsible for blocking request to upload manifest list by docker client, which is not supported so far by Harbor. // ServeHTTP The handler is responsible for blocking request to upload manifest list by docker client, which is not supported so far by Harbor.
func (mh MultipleManifestHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (mh multipleManifestHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
match, _, _ := util.MatchManifestURL(req) match, _, _ := util.MatchManifestURL(req)
if match { if match {
contentType := req.Header.Get("Content-type") contentType := req.Header.Get("Content-type")

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package readonly package readonly
import ( import (
@ -11,12 +25,14 @@ type readonlyHandler struct {
next http.Handler next http.Handler
} }
// New ...
func New(next http.Handler) http.Handler { func New(next http.Handler) http.Handler {
return &readonlyHandler{ return &readonlyHandler{
next: next, next: next,
} }
} }
// ServeHTTP ...
func (rh readonlyHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (rh readonlyHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if config.ReadOnly() { if config.ReadOnly() {
if req.Method == http.MethodDelete || req.Method == http.MethodPost || req.Method == http.MethodPatch || req.Method == http.MethodPut { if req.Method == http.MethodDelete || req.Method == http.MethodPost || req.Method == http.MethodPatch || req.Method == http.MethodPut {

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package registryproxy package registryproxy
import ( import (
@ -14,6 +28,7 @@ type proxyHandler struct {
handler http.Handler handler http.Handler
} }
// New ...
func New(urls ...string) http.Handler { func New(urls ...string) http.Handler {
var registryURL string var registryURL string
var err error var err error
@ -102,6 +117,7 @@ func singleJoiningSlash(a, b string) string {
return a + b return a + b
} }
// ServeHTTP ...
func (ph proxyHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (ph proxyHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
ph.handler.ServeHTTP(rw, req) ph.handler.ServeHTTP(rw, req)
} }

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package regquota package regquota
import ( import (
@ -14,13 +28,14 @@ type regQuotaHandler struct {
next http.Handler next http.Handler
} }
// New ...
func New(next http.Handler) http.Handler { func New(next http.Handler) http.Handler {
return &regQuotaHandler{ return &regQuotaHandler{
next: next, next: next,
} }
} }
//PATCH manifest ... // ServeHTTP PATCH manifest ...
func (rqh regQuotaHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (rqh regQuotaHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
match, _, _ := util.MatchManifestURL(req) match, _, _ := util.MatchManifestURL(req)
if match { if match {
@ -30,16 +45,16 @@ func (rqh regQuotaHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)
if req.Method == http.MethodPut && mediaType == "application/vnd.docker.distribution.manifest.v2+json" { if req.Method == http.MethodPut && mediaType == "application/vnd.docker.distribution.manifest.v2+json" {
data, err := ioutil.ReadAll(req.Body) data, err := ioutil.ReadAll(req.Body)
if err != nil { if err != nil {
log.Warningf("Error occured when to copy manifest body %v", err) log.Warningf("Error occurred when to copy manifest body %v", err)
http.Error(rw, util.MarshalError("InternalServerError", fmt.Sprintf("Error occured when to decode manifest body %v", err)), http.StatusInternalServerError) http.Error(rw, util.MarshalError("InternalServerError", fmt.Sprintf("Error occurred when to decode manifest body %v", err)), http.StatusInternalServerError)
return return
} }
req.Body = ioutil.NopCloser(bytes.NewBuffer(data)) req.Body = ioutil.NopCloser(bytes.NewBuffer(data))
_, desc, err := distribution.UnmarshalManifest(mediaType, data) _, desc, err := distribution.UnmarshalManifest(mediaType, data)
if err != nil { if err != nil {
log.Warningf("Error occured when to Unmarshal Manifest %v", err) log.Warningf("Error occurred when to Unmarshal Manifest %v", err)
http.Error(rw, util.MarshalError("InternalServerError", fmt.Sprintf("Error occured when to Unmarshal Manifest %v", err)), http.StatusInternalServerError) http.Error(rw, util.MarshalError("InternalServerError", fmt.Sprintf("Error occurred when to Unmarshal Manifest %v", err)), http.StatusInternalServerError)
return return
} }
mfDigest = desc.Digest.String() mfDigest = desc.Digest.String()

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package url package url
import ( import (
@ -14,12 +28,14 @@ type urlHandler struct {
next http.Handler next http.Handler
} }
// New ...
func New(next http.Handler) http.Handler { func New(next http.Handler) http.Handler {
return &urlHandler{ return &urlHandler{
next: next, next: next,
} }
} }
// ServeHTTP ...
func (uh urlHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (uh urlHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
log.Debugf("in url handler, path: %s", req.URL.Path) log.Debugf("in url handler, path: %s", req.URL.Path)
flag, repository, reference := util.MatchPullManifest(req) flag, repository, reference := util.MatchPullManifest(req)

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util package util
import ( import (
@ -18,12 +32,14 @@ type contextKey string
const ( const (
manifestURLPattern = `^/v2/((?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)+)manifests/([\w][\w.:-]{0,127})` manifestURLPattern = `^/v2/((?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)+)manifests/([\w][\w.:-]{0,127})`
blobURLPattern = `^/v2/((?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)+)blobs/uploads/` blobURLPattern = `^/v2/((?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)+)blobs/uploads/`
// ImageInfoCtxKey the context key for image information
ImageInfoCtxKey = contextKey("ImageInfo") ImageInfoCtxKey = contextKey("ImageInfo")
// TokenUsername ...
// TODO: temp solution, remove after vmware/harbor#2242 is resolved. // TODO: temp solution, remove after vmware/harbor#2242 is resolved.
TokenUsername = "harbor-core" TokenUsername = "harbor-core"
) )
// ImageInfo // ImageInfo ...
type ImageInfo struct { type ImageInfo struct {
Repository string Repository string
Reference string Reference string

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vulnerable package vulnerable
import ( import (
@ -14,12 +28,14 @@ type vulnerableHandler struct {
next http.Handler next http.Handler
} }
// New ...
func New(next http.Handler) http.Handler { func New(next http.Handler) http.Handler {
return &vulnerableHandler{ return &vulnerableHandler{
next: next, next: next,
} }
} }
// ServeHTTP ...
func (vh vulnerableHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (vh vulnerableHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
imgRaw := req.Context().Value(util.ImageInfoCtxKey) imgRaw := req.Context().Value(util.ImageInfoCtxKey)
if imgRaw == nil || !config.WithClair() { if imgRaw == nil || !config.WithClair() {