From b3c5137a2ff6856bbf793fab20430c85ed1dac04 Mon Sep 17 00:00:00 2001 From: wang yan Date: Wed, 3 Jul 2019 16:03:20 +0800 Subject: [PATCH] add copyright and fix codecy Signed-off-by: wang yan --- src/core/middlewares/blobquota/handler.go | 16 ++++++++++++ src/core/middlewares/chain.go | 18 ++++++++++++- src/core/middlewares/config.go | 16 +++++++++++- src/core/middlewares/contenttrust/handler.go | 17 +++++++++++++ src/core/middlewares/inlet.go | 14 +++++++++++ src/core/middlewares/interface.go | 15 +++++++++++ src/core/middlewares/listrepo/handler.go | 16 ++++++++++++ .../middlewares/multiplmanifest/handler.go | 23 ++++++++++++++--- src/core/middlewares/readonly/hanlder.go | 16 ++++++++++++ src/core/middlewares/registryproxy/handler.go | 16 ++++++++++++ src/core/middlewares/regquota/handler.go | 25 +++++++++++++++---- src/core/middlewares/url/handler.go | 16 ++++++++++++ src/core/middlewares/util/util.go | 20 +++++++++++++-- src/core/middlewares/vulnerable/handler.go | 16 ++++++++++++ 14 files changed, 231 insertions(+), 13 deletions(-) diff --git a/src/core/middlewares/blobquota/handler.go b/src/core/middlewares/blobquota/handler.go index 876fd2ef3..91558fa57 100644 --- a/src/core/middlewares/blobquota/handler.go +++ b/src/core/middlewares/blobquota/handler.go @@ -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 import ( @@ -17,12 +31,14 @@ type blobQuotaHandler struct { next http.Handler } +// New ... func New(next http.Handler) http.Handler { return &blobQuotaHandler{ next: next, } } +// ServeHTTP ... func (bqh blobQuotaHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { if req.Method == http.MethodPut { match, _ := util.MatchPutBlobURL(req) diff --git a/src/core/middlewares/chain.go b/src/core/middlewares/chain.go index 69d15a2af..bde524ecd 100644 --- a/src/core/middlewares/chain.go +++ b/src/core/middlewares/chain.go @@ -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 import ( @@ -14,17 +28,19 @@ import ( "net/http" ) +// DefaultCreator ... type DefaultCreator struct { middlewares []string } +// New ... func New(middlewares []string) *DefaultCreator { return &DefaultCreator{ middlewares: middlewares, } } -// CreateChain ... +// Create creates a middleware chain ... func (b *DefaultCreator) Create() *alice.Chain { chain := alice.New() for _, mName := range b.middlewares { diff --git a/src/core/middlewares/config.go b/src/core/middlewares/config.go index c881b5075..633d97f0e 100644 --- a/src/core/middlewares/config.go +++ b/src/core/middlewares/config.go @@ -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 // const variables @@ -12,5 +26,5 @@ const ( BLOBQUOTA = "blobquota" ) -// sequential organization +// Middlewares with sequential organization var Middlewares = []string{READONLY, URL, MUITIPLEMANIFEST, LISTREPO, CONTENTTRUST, VULNERABLE, BLOBQUOTA, REGQUOTA} diff --git a/src/core/middlewares/contenttrust/handler.go b/src/core/middlewares/contenttrust/handler.go index 5b9d7b4ed..82ded4e5b 100644 --- a/src/core/middlewares/contenttrust/handler.go +++ b/src/core/middlewares/contenttrust/handler.go @@ -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 import ( @@ -9,18 +23,21 @@ import ( "strings" ) +// NotaryEndpoint ... var NotaryEndpoint = "" type contentTrustHandler struct { next http.Handler } +// New ... func New(next http.Handler) http.Handler { return &contentTrustHandler{ next: next, } } +// ServeHTTP ... func (cth contentTrustHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { imgRaw := req.Context().Value(util.ImageInfoCtxKey) if imgRaw == nil || !config.WithNotary() { diff --git a/src/core/middlewares/inlet.go b/src/core/middlewares/inlet.go index 55f579a06..d7a505613 100644 --- a/src/core/middlewares/inlet.go +++ b/src/core/middlewares/inlet.go @@ -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 import ( diff --git a/src/core/middlewares/interface.go b/src/core/middlewares/interface.go index 86381d8cd..4ca772f43 100644 --- a/src/core/middlewares/interface.go +++ b/src/core/middlewares/interface.go @@ -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 import "github.com/justinas/alice" +// ChainCreator ... type ChainCreator interface { Create(middlewares []string) *alice.Chain } diff --git a/src/core/middlewares/listrepo/handler.go b/src/core/middlewares/listrepo/handler.go index ea41a6557..9cc2a2ae0 100644 --- a/src/core/middlewares/listrepo/handler.go +++ b/src/core/middlewares/listrepo/handler.go @@ -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 import ( @@ -19,12 +33,14 @@ type listReposHandler struct { next http.Handler } +// New ... func New(next http.Handler) http.Handler { return &listReposHandler{ next: next, } } +// ServeHTTP ... func (lrh listReposHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { var rec *httptest.ResponseRecorder listReposFlag := matchListRepos(req) diff --git a/src/core/middlewares/multiplmanifest/handler.go b/src/core/middlewares/multiplmanifest/handler.go index bd18e09f2..1cecbc1ff 100644 --- a/src/core/middlewares/multiplmanifest/handler.go +++ b/src/core/middlewares/multiplmanifest/handler.go @@ -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 import ( @@ -7,18 +21,19 @@ import ( "strings" ) -type MultipleManifestHandler struct { +type multipleManifestHandler struct { next http.Handler } +// New ... func New(next http.Handler) http.Handler { - return &MultipleManifestHandler{ + return &multipleManifestHandler{ next: next, } } -// 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) { +// 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) { match, _, _ := util.MatchManifestURL(req) if match { contentType := req.Header.Get("Content-type") diff --git a/src/core/middlewares/readonly/hanlder.go b/src/core/middlewares/readonly/hanlder.go index 1bdb8b659..be77ac285 100644 --- a/src/core/middlewares/readonly/hanlder.go +++ b/src/core/middlewares/readonly/hanlder.go @@ -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 import ( @@ -11,12 +25,14 @@ type readonlyHandler struct { next http.Handler } +// New ... func New(next http.Handler) http.Handler { return &readonlyHandler{ next: next, } } +// ServeHTTP ... func (rh readonlyHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { if config.ReadOnly() { if req.Method == http.MethodDelete || req.Method == http.MethodPost || req.Method == http.MethodPatch || req.Method == http.MethodPut { diff --git a/src/core/middlewares/registryproxy/handler.go b/src/core/middlewares/registryproxy/handler.go index eb0e43b05..2558b9717 100644 --- a/src/core/middlewares/registryproxy/handler.go +++ b/src/core/middlewares/registryproxy/handler.go @@ -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 import ( @@ -14,6 +28,7 @@ type proxyHandler struct { handler http.Handler } +// New ... func New(urls ...string) http.Handler { var registryURL string var err error @@ -102,6 +117,7 @@ func singleJoiningSlash(a, b string) string { return a + b } +// ServeHTTP ... func (ph proxyHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { ph.handler.ServeHTTP(rw, req) } diff --git a/src/core/middlewares/regquota/handler.go b/src/core/middlewares/regquota/handler.go index fe2b132ea..bf61bb6ac 100644 --- a/src/core/middlewares/regquota/handler.go +++ b/src/core/middlewares/regquota/handler.go @@ -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 import ( @@ -14,13 +28,14 @@ type regQuotaHandler struct { next http.Handler } +// New ... func New(next http.Handler) http.Handler { return ®QuotaHandler{ next: next, } } -//PATCH manifest ... +// ServeHTTP PATCH manifest ... func (rqh regQuotaHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { match, _, _ := util.MatchManifestURL(req) 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" { data, err := ioutil.ReadAll(req.Body) if err != nil { - log.Warningf("Error occured 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) + log.Warningf("Error occurred when to copy manifest body %v", err) + http.Error(rw, util.MarshalError("InternalServerError", fmt.Sprintf("Error occurred when to decode manifest body %v", err)), http.StatusInternalServerError) return } req.Body = ioutil.NopCloser(bytes.NewBuffer(data)) _, desc, err := distribution.UnmarshalManifest(mediaType, data) if err != nil { - log.Warningf("Error occured when to Unmarshal Manifest %v", err) - http.Error(rw, util.MarshalError("InternalServerError", fmt.Sprintf("Error occured when to Unmarshal Manifest %v", err)), http.StatusInternalServerError) + log.Warningf("Error occurred when to Unmarshal Manifest %v", err) + http.Error(rw, util.MarshalError("InternalServerError", fmt.Sprintf("Error occurred when to Unmarshal Manifest %v", err)), http.StatusInternalServerError) return } mfDigest = desc.Digest.String() diff --git a/src/core/middlewares/url/handler.go b/src/core/middlewares/url/handler.go index cfd893ef3..07e1a0f3f 100644 --- a/src/core/middlewares/url/handler.go +++ b/src/core/middlewares/url/handler.go @@ -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 import ( @@ -14,12 +28,14 @@ type urlHandler struct { next http.Handler } +// New ... func New(next http.Handler) http.Handler { return &urlHandler{ next: next, } } +// ServeHTTP ... func (uh urlHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { log.Debugf("in url handler, path: %s", req.URL.Path) flag, repository, reference := util.MatchPullManifest(req) diff --git a/src/core/middlewares/util/util.go b/src/core/middlewares/util/util.go index 6eb6ade6a..e06ae5397 100644 --- a/src/core/middlewares/util/util.go +++ b/src/core/middlewares/util/util.go @@ -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 import ( @@ -18,12 +32,14 @@ type contextKey string const ( manifestURLPattern = `^/v2/((?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)+)manifests/([\w][\w.:-]{0,127})` blobURLPattern = `^/v2/((?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)+)blobs/uploads/` - ImageInfoCtxKey = contextKey("ImageInfo") + // ImageInfoCtxKey the context key for image information + ImageInfoCtxKey = contextKey("ImageInfo") + // TokenUsername ... // TODO: temp solution, remove after vmware/harbor#2242 is resolved. TokenUsername = "harbor-core" ) -// ImageInfo +// ImageInfo ... type ImageInfo struct { Repository string Reference string diff --git a/src/core/middlewares/vulnerable/handler.go b/src/core/middlewares/vulnerable/handler.go index 44762ac5e..c31946b22 100644 --- a/src/core/middlewares/vulnerable/handler.go +++ b/src/core/middlewares/vulnerable/handler.go @@ -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 import ( @@ -14,12 +28,14 @@ type vulnerableHandler struct { next http.Handler } +// New ... func New(next http.Handler) http.Handler { return &vulnerableHandler{ next: next, } } +// ServeHTTP ... func (vh vulnerableHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { imgRaw := req.Context().Value(util.ImageInfoCtxKey) if imgRaw == nil || !config.WithClair() {