diff --git a/src/common/http/client.go b/src/common/http/client.go index 099ee1612..d74ad5211 100644 --- a/src/common/http/client.go +++ b/src/common/http/client.go @@ -127,7 +127,6 @@ func (c *Client) Post(url string, v ...interface{}) error { func (c *Client) Put(url string, v ...interface{}) error { var reader io.Reader if len(v) > 0 { - data := []byte{} data, err := json.Marshal(v[0]) if err != nil { return err diff --git a/src/controller/p2p/preheat/enforcer.go b/src/controller/p2p/preheat/enforcer.go index 836db1f86..717531df5 100644 --- a/src/controller/p2p/preheat/enforcer.go +++ b/src/controller/p2p/preheat/enforcer.go @@ -41,6 +41,8 @@ import ( proModels "github.com/goharbor/harbor/src/pkg/project/models" "github.com/goharbor/harbor/src/pkg/scan/vuln" "github.com/goharbor/harbor/src/pkg/task" + "golang.org/x/text/cases" + "golang.org/x/text/language" ) func init() { @@ -625,7 +627,8 @@ func overrideSecuritySettings(p *pol.Schema, pro *proModels.Project) [][]interfa // Append vulnerability filter if vulnerability severity config is set at project configurations if v, ok := pro.Metadata[proMetaKeyVulnerability]; ok && v == "true" { if se, ok := pro.Metadata[proMetaKeySeverity]; ok && len(se) > 0 { - se = strings.Title(strings.ToLower(se)) + title := cases.Title(language.Und) + se = title.String(strings.ToLower(se)) code := vuln.Severity(se).Code() filters = append(filters, &pol.Filter{ Type: pol.FilterTypeVulnerability, diff --git a/src/core/controllers/oidc.go b/src/core/controllers/oidc.go index 9a8e9bf34..6f09a637a 100644 --- a/src/core/controllers/oidc.go +++ b/src/core/controllers/oidc.go @@ -159,6 +159,10 @@ func (oc *OIDCController) Callback() { return } _, t, err := secretAndToken(tokenBytes) + if err != nil { + oc.SendInternalServerError(err) + return + } oidcUser := um.OIDCUserMeta oidcUser.Token = t if err := ctluser.Ctl.UpdateOIDCMeta(ctx, oidcUser); err != nil { diff --git a/src/jobservice/hook/hook_agent.go b/src/jobservice/hook/hook_agent.go index a2db6b917..a14ed57f9 100644 --- a/src/jobservice/hook/hook_agent.go +++ b/src/jobservice/hook/hook_agent.go @@ -17,6 +17,7 @@ package hook import ( "context" "encoding/json" + "github.com/goharbor/harbor/src/lib/log" "net/url" "time" @@ -244,6 +245,7 @@ func (ba *basicAgent) isOutdated(evt *Event) (bool, error) { case st.After(est): return true, nil case st.Equal(est): + log.Debugf("ignore the consistent status: %v", est) // Continue to compare check in at timestamp } diff --git a/src/jobservice/job/impl/gc/garbage_collection.go b/src/jobservice/job/impl/gc/garbage_collection.go index 87643ddd0..79752d18f 100644 --- a/src/jobservice/job/impl/gc/garbage_collection.go +++ b/src/jobservice/job/impl/gc/garbage_collection.go @@ -402,14 +402,12 @@ func (gc *GarbageCollector) deletedArt(ctx job.Context) (map[string][]model.Arti // allTrashedArts contains the artifacts that actual removed and simulate removed(for dry run). allTrashedArts := make([]model.ArtifactTrash, 0) - untaggedArts := make([]*artifact.Artifact, 0) - var err error // artMap : map[digest : []ArtifactTrash list] artMap := make(map[string][]model.ArtifactTrash) // handle the optional ones, and the artifact controller will move them into trash. if gc.deleteUntagged { - untaggedArts, err = gc.artCtl.List(ctx.SystemContext(), &q.Query{ + untaggedArts, err := gc.artCtl.List(ctx.SystemContext(), &q.Query{ Keywords: map[string]interface{}{ "Tags": "nil", }, diff --git a/src/jobservice/job/tracker.go b/src/jobservice/job/tracker.go index 08f989be2..bf00bedd3 100644 --- a/src/jobservice/job/tracker.go +++ b/src/jobservice/job/tracker.go @@ -231,14 +231,17 @@ func (bt *basicTracker) CheckIn(message string) error { current := Status(bt.jobStats.Info.Status) bt.refresh(current, message) - err := bt.fireHookEvent(current, message) - err = bt.Update( + errFireHE := bt.fireHookEvent(current, message) + err := bt.Update( // skip checkin data here "check_in_at", now, "update_time", now, ) + if err != nil { + return errors.Wrap(err, errFireHE.Error()) + } - return err + return nil } // Run job diff --git a/src/jobservice/runtime/bootstrap.go b/src/jobservice/runtime/bootstrap.go index c6573c6fc..5af9d6210 100644 --- a/src/jobservice/runtime/bootstrap.go +++ b/src/jobservice/runtime/bootstrap.go @@ -219,7 +219,7 @@ func (bs *Bootstrap) LoadAndRun(ctx context.Context, cancel context.CancelFunc) // Listen to the system signals sig := make(chan os.Signal, 1) - signal.Notify(sig, os.Interrupt, syscall.SIGTERM, os.Kill) + signal.Notify(sig, os.Interrupt, syscall.SIGTERM) terminated := false go func(errChan chan error) { defer func() { diff --git a/src/lib/config/userconfig.go b/src/lib/config/userconfig.go index eafadaa03..a80502323 100644 --- a/src/lib/config/userconfig.go +++ b/src/lib/config/userconfig.go @@ -173,7 +173,7 @@ func OIDCSetting(ctx context.Context) (*cfgModels.OIDCSetting, error) { return nil, err } scopeStr := mgr.Get(ctx, common.OIDCScope).GetString() - extEndpoint := strings.TrimSuffix(mgr.Get(nil, common.ExtEndpoint).GetString(), "/") + extEndpoint := strings.TrimSuffix(mgr.Get(context.Background(), common.ExtEndpoint).GetString(), "/") scope := SplitAndTrim(scopeStr, ",") return &cfgModels.OIDCSetting{ Name: mgr.Get(ctx, common.OIDCName).GetString(), diff --git a/src/lib/errors/errors.go b/src/lib/errors/errors.go index 12cdf7271..195dc91fa 100644 --- a/src/lib/errors/errors.go +++ b/src/lib/errors/errors.go @@ -130,8 +130,6 @@ func New(in interface{}) *Error { switch in := in.(type) { case error: err = in - case *Error: - err = in.Cause default: err = fmt.Errorf("%v", in) } diff --git a/src/lib/redis/redisclient.go b/src/lib/redis/redisclient.go index 9282898ac..634d7001e 100644 --- a/src/lib/redis/redisclient.go +++ b/src/lib/redis/redisclient.go @@ -81,7 +81,7 @@ func GetRedisPool(name string, rawurl string, param *PoolParam) (*redis.Pool, er knownPool.Store(name, pool) return pool, nil } else if u.Scheme == "redis+sentinel" { - pool, err := getSentinelPool(u, param, err, name) + pool, err := getSentinelPool(u, param, name) if err != nil { return nil, err } @@ -92,7 +92,7 @@ func GetRedisPool(name string, rawurl string, param *PoolParam) (*redis.Pool, er } } -func getSentinelPool(u *url.URL, param *PoolParam, err error, name string) (*redis.Pool, error) { +func getSentinelPool(u *url.URL, param *PoolParam, name string) (*redis.Pool, error) { ps := strings.Split(u.Path, "/") if len(ps) < 2 { return nil, fmt.Errorf("bad redis sentinel url: no master name, %s %s", name, u) @@ -126,7 +126,7 @@ func getSentinelPool(u *url.URL, param *PoolParam, err error, name string) (*red // sentinel doesn't need select db db := 0 if len(ps) > 2 { - db, err = strconv.Atoi(ps[2]) + db, err := strconv.Atoi(ps[2]) if err != nil { return nil, fmt.Errorf("invalid redis db: %s, %s", ps[1], name) } diff --git a/src/pkg/reg/adapter/aliacr/auth.go b/src/pkg/reg/adapter/aliacr/auth.go index 45b01ad13..1871b36be 100644 --- a/src/pkg/reg/adapter/aliacr/auth.go +++ b/src/pkg/reg/adapter/aliacr/auth.go @@ -70,7 +70,7 @@ func (a *aliyunAuthCredential) Modify(r *http.Request) (err error) { } func (a *aliyunAuthCredential) isCacheTokenValid() bool { - if &a.cacheTokenExpiredAt == nil { + if a.cacheTokenExpiredAt.IsZero() { return false } if a.cacheToken == nil { diff --git a/src/pkg/reg/adapter/quay/auth/authorizer.go b/src/pkg/reg/adapter/quay/auth/authorizer.go index f568750b4..0b388ca00 100644 --- a/src/pkg/reg/adapter/quay/auth/authorizer.go +++ b/src/pkg/reg/adapter/quay/auth/authorizer.go @@ -47,7 +47,7 @@ func (a *authorizer) Modify(req *http.Request) error { // resume path req.URL.Path = oldPath }() - req.URL.Path = strings.TrimRight(req.URL.Path, "_catalog") + req.URL.Path = strings.TrimSuffix(req.URL.Path, "_catalog") } return a.innerAuthorizer.Modify(req) diff --git a/src/pkg/reg/adapter/tencentcr/adapter.go b/src/pkg/reg/adapter/tencentcr/adapter.go index 8c27be60a..cae4832ae 100644 --- a/src/pkg/reg/adapter/tencentcr/adapter.go +++ b/src/pkg/reg/adapter/tencentcr/adapter.go @@ -219,7 +219,6 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) (err error) { if err != nil { return } - return } return diff --git a/src/pkg/reg/adapter/tencentcr/auth.go b/src/pkg/reg/adapter/tencentcr/auth.go index 869b4a833..84df98ddc 100644 --- a/src/pkg/reg/adapter/tencentcr/auth.go +++ b/src/pkg/reg/adapter/tencentcr/auth.go @@ -29,7 +29,7 @@ func (q *qcloudAuthCredential) Modify(r *http.Request) (err error) { } func (q *qcloudAuthCredential) isCacheTokenValid() (ok bool) { - if &q.cacheTokenExpiredAt == nil { + if q.cacheTokenExpiredAt.IsZero() { return } if q.cacheTokener == nil { diff --git a/src/pkg/reg/filter/artifact.go b/src/pkg/reg/filter/artifact.go index f9969afe0..d3bf5984a 100644 --- a/src/pkg/reg/filter/artifact.go +++ b/src/pkg/reg/filter/artifact.go @@ -91,7 +91,7 @@ func (a *artifactTypeFilter) Filter(artifacts []*model.Artifact) ([]*model.Artif var result []*model.Artifact for _, artifact := range artifacts { for _, t := range a.types { - if strings.ToLower(artifact.Type) == strings.ToLower(t) { + if strings.EqualFold(strings.ToLower(artifact.Type), strings.ToLower(t)) { result = append(result, artifact) continue } diff --git a/src/pkg/scan/rest/v1/client_pool.go b/src/pkg/scan/rest/v1/client_pool.go index eb93c5b6f..27ff01a16 100644 --- a/src/pkg/scan/rest/v1/client_pool.go +++ b/src/pkg/scan/rest/v1/client_pool.go @@ -128,7 +128,7 @@ func (bcp *basicClientPool) deadCheck(key string, item *poolItem) { // As we do not have a global context, let's watch the system signal to // exit the goroutine correctly. sig := make(chan os.Signal, 1) - signal.Notify(sig, os.Interrupt, syscall.SIGTERM, os.Kill) + signal.Notify(sig, os.Interrupt, syscall.SIGTERM) tk := time.NewTicker(bcp.config.DeadCheckInterval) defer tk.Stop() diff --git a/src/pkg/scan/vuln/severity.go b/src/pkg/scan/vuln/severity.go index 75c39bbdc..a3c2e1384 100644 --- a/src/pkg/scan/vuln/severity.go +++ b/src/pkg/scan/vuln/severity.go @@ -15,7 +15,8 @@ package vuln import ( - "strings" + "golang.org/x/text/cases" + "golang.org/x/text/language" ) const ( @@ -73,7 +74,8 @@ func (s Severity) String() string { // ParseSeverityVersion3 returns severity of CVSS v3.0 Ratings func ParseSeverityVersion3(str string) Severity { - severity := Severity(strings.Title(str)) + title := cases.Title(language.Und) + severity := Severity(title.String(str)) // There are `None`, `Low`, `Medium`, `High` and `Critical` severity rankings in CVSS v3.0 Ratings, // so map `negligible` severity to `none` diff --git a/src/server/middleware/quota/copy_artifact.go b/src/server/middleware/quota/copy_artifact.go index 5e0758794..5b626fdba 100644 --- a/src/server/middleware/quota/copy_artifact.go +++ b/src/server/middleware/quota/copy_artifact.go @@ -64,7 +64,7 @@ func parseRepositoryName(p string) string { return strings.TrimSuffix(parts[1], "/artifacts") } -func copyArtifactResources(r *http.Request, reference, referenceID string) (types.ResourceList, error) { +func copyArtifactResources(r *http.Request, _, referenceID string) (types.ResourceList, error) { query := r.URL.Query() from := query.Get("from") if from == "" { @@ -132,7 +132,7 @@ func copyArtifactResources(r *http.Request, reference, referenceID string) (type } func copyArtifactResourcesEvent(level int) func(*http.Request, string, string, string) event.Metadata { - return func(r *http.Request, reference, referenceID string, message string) event.Metadata { + return func(r *http.Request, _, referenceID string, message string) event.Metadata { ctx := r.Context() logger := log.G(ctx).WithFields(log.Fields{"middleware": "quota", "action": "request", "url": r.URL.Path})