From b37a9870734575f0cb55b401e752d694b21589a5 Mon Sep 17 00:00:00 2001 From: chlins Date: Mon, 14 Feb 2022 15:09:14 +0800 Subject: [PATCH] fix: fix codeql alerts Signed-off-by: chlins --- src/jobservice/api/handler.go | 3 ++- src/jobservice/api/router.go | 2 +- src/jobservice/period/basic_scheduler.go | 10 +++++----- src/jobservice/period/enqueuer.go | 4 ++-- src/jobservice/worker/cworker/c_worker.go | 2 +- src/pkg/p2p/preheat/provider/client/http_client.go | 6 +++--- src/pkg/reg/adapter/harbor/base/chart_registry.go | 6 ++++-- src/pkg/reg/adapter/helmhub/chart_registry.go | 5 +++-- src/pkg/reg/adapter/tencentcr/chart_registry.go | 3 ++- 9 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/jobservice/api/handler.go b/src/jobservice/api/handler.go index 456914cce..70fbd1861 100644 --- a/src/jobservice/api/handler.go +++ b/src/jobservice/api/handler.go @@ -24,6 +24,8 @@ import ( "github.com/gorilla/mux" "fmt" + "strconv" + "github.com/goharbor/harbor/src/jobservice/common/query" "github.com/goharbor/harbor/src/jobservice/common/utils" "github.com/goharbor/harbor/src/jobservice/core" @@ -31,7 +33,6 @@ import ( "github.com/goharbor/harbor/src/jobservice/job" "github.com/goharbor/harbor/src/jobservice/logger" "github.com/goharbor/harbor/src/lib/errors" - "strconv" ) const ( diff --git a/src/jobservice/api/router.go b/src/jobservice/api/router.go index d0557e944..78b665241 100644 --- a/src/jobservice/api/router.go +++ b/src/jobservice/api/router.go @@ -78,7 +78,7 @@ func (br *BaseRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { if authErr == nil { authErr = errors.Errorf("unauthorized: %s", err) } - logger.Errorf("Serve http request '%s %s' failed with error: %s", req.Method, req.URL.String(), authErr.Error()) + logger.Errorf("Serve http request '%q %q' failed with error: %s", req.Method, req.URL.String(), authErr.Error()) w.WriteHeader(http.StatusUnauthorized) writeDate(w, []byte(authErr.Error())) return diff --git a/src/jobservice/period/basic_scheduler.go b/src/jobservice/period/basic_scheduler.go index 324e6c123..3f39fc763 100644 --- a/src/jobservice/period/basic_scheduler.go +++ b/src/jobservice/period/basic_scheduler.go @@ -119,10 +119,10 @@ func (bs *basicScheduler) UnSchedule(policyID string) error { // Failure errors will be only logged here eKey := rds.KeyUpstreamJobAndExecutions(bs.namespace, policyID) if eIDs, err := getPeriodicExecutions(conn, eKey); err != nil { - logger.Errorf("Get executions for periodic job %s error: %s", policyID, err) + logger.Errorf("Get executions for periodic job %q error: %s", policyID, err) } else { if len(eIDs) == 0 { - logger.Debugf("no stopped executions: %s", policyID) + logger.Debugf("no stopped executions: %q", policyID) } for _, eID := range eIDs { @@ -150,7 +150,7 @@ func (bs *basicScheduler) UnSchedule(policyID string) error { if err := eTracker.Stop(); err != nil { logger.Errorf("Stop execution %s error: %s", eID, err) } else { - logger.Debugf("Stop execution %s of periodic job %s", eID, policyID) + logger.Debugf("Stop execution %q of periodic job %s", eID, policyID) } } } @@ -164,7 +164,7 @@ func (bs *basicScheduler) UnSchedule(policyID string) error { } if removed == 0 { - logger.Warningf("No periodic job with ID=%s and numeric ID=%d removed from the periodic job policy set", policyID, numericID) + logger.Warningf("No periodic job with ID=%q and numeric ID=%d removed from the periodic job policy set", policyID, numericID) } return nil @@ -199,7 +199,7 @@ func (bs *basicScheduler) locatePolicy(policyID string, conn redis.Conn) (int64, // Switch the job stats to stopped if the job stats existing // Should not block the next clear action if err := tracker.Stop(); err != nil { - logger.Errorf("Stop periodic job %s failed with error: %s", policyID, err) + logger.Errorf("Stop periodic job %q failed with error: %s", policyID, err) } return tracker.NumericID() diff --git a/src/jobservice/period/enqueuer.go b/src/jobservice/period/enqueuer.go index 256aafb17..704b9faa4 100644 --- a/src/jobservice/period/enqueuer.go +++ b/src/jobservice/period/enqueuer.go @@ -163,7 +163,7 @@ func (e *enqueuer) scheduleNextJobs(p *Policy, conn redis.Conn) { // The cron spec should be already checked at upper layers. // Just in cases, if error occurred, ignore it e.lastEnqueueErr = err - logger.Errorf("Invalid corn spec in periodic policy %s %s: %s", p.JobName, p.ID, err) + logger.Errorf("Invalid corn spec in periodic policy %q %s: %s", p.JobName, p.ID, err) } else { for t := schedule.Next(nowTime); t.Before(horizon); t = schedule.Next(t) { epoch := t.Unix() @@ -222,7 +222,7 @@ func (e *enqueuer) scheduleNextJobs(p *Policy, conn redis.Conn) { break // Probably redis connection is broken } - logger.Debugf("Scheduled execution for periodic job %s:%s at %d", j.Name, p.ID, epoch) + logger.Debugf("Scheduled execution for periodic job %q:%s at %d", j.Name, p.ID, epoch) } } } diff --git a/src/jobservice/worker/cworker/c_worker.go b/src/jobservice/worker/cworker/c_worker.go index df7ee987a..fd9eeb2be 100644 --- a/src/jobservice/worker/cworker/c_worker.go +++ b/src/jobservice/worker/cworker/c_worker.go @@ -360,7 +360,7 @@ func (w *basicWorker) StopJob(jobID string) error { // We need to delete the scheduled job in the queue if it is not running yet if err := w.client.DeleteScheduledJob(t.Job().Info.RunAt, jobID); err != nil { // Job is already running? - logger.Warningf("scheduled job %s (run at = %d) is not found in the queue, is it running?", jobID, t.Job().Info.RunAt) + logger.Warningf("scheduled job %q (run at = %d) is not found in the queue, is it running?", jobID, t.Job().Info.RunAt) } } diff --git a/src/pkg/p2p/preheat/provider/client/http_client.go b/src/pkg/p2p/preheat/provider/client/http_client.go index 2c82a36f7..ed7461023 100644 --- a/src/pkg/p2p/preheat/provider/client/http_client.go +++ b/src/pkg/p2p/preheat/provider/client/http_client.go @@ -3,13 +3,13 @@ package client import ( "crypto/tls" "encoding/json" - "errors" "fmt" "io/ioutil" "net/http" "strings" "time" + "github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/log" "github.com/goharbor/harbor/src/pkg/p2p/preheat/provider/auth" @@ -128,7 +128,7 @@ func (hc *HTTPClient) get(url string, cred *auth.Credential, parmas map[string]s if (res.StatusCode / 100) != 2 { // Return the server error content in the error. - return nil, fmt.Errorf("%s '%s' error: %s %s", http.MethodGet, res.Request.URL.String(), res.Status, bytes) + return nil, errors.Errorf("%s %q error: %s %s", http.MethodGet, res.Request.URL.String(), res.Status, bytes) } return bytes, nil @@ -194,7 +194,7 @@ func (hc *HTTPClient) post(url string, cred *auth.Credential, body interface{}, if (res.StatusCode / 100) != 2 { // Return the server error content in the error. - return nil, fmt.Errorf("%s '%s' error: %s %s", http.MethodPost, res.Request.URL.String(), res.Status, bytes) + return nil, errors.Errorf("%s %q error: %s %s", http.MethodPost, res.Request.URL.String(), res.Status, bytes) } else if res.StatusCode == http.StatusAlreadyReported { // Currently because if image was already preheated at least once, Dragonfly will return StatusAlreadyReported. // And we should preserve http status code info to process this case later. diff --git a/src/pkg/reg/adapter/harbor/base/chart_registry.go b/src/pkg/reg/adapter/harbor/base/chart_registry.go index 33ff99a65..4078958a3 100644 --- a/src/pkg/reg/adapter/harbor/base/chart_registry.go +++ b/src/pkg/reg/adapter/harbor/base/chart_registry.go @@ -23,10 +23,12 @@ import ( "net/http" "strings" + "net/url" + common_http "github.com/goharbor/harbor/src/common/http" + "github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/pkg/reg/filter" "github.com/goharbor/harbor/src/pkg/reg/model" - "net/url" ) type label struct { @@ -179,7 +181,7 @@ func (a *Adapter) DownloadChart(name, version, contentURL string) (io.ReadCloser if err != nil { return nil, err } - return nil, fmt.Errorf("failed to download the chart %s: %d %s", req.URL.String(), resp.StatusCode, string(body)) + return nil, errors.Errorf("failed to download the chart %q: %d %s", req.URL.String(), resp.StatusCode, string(body)) } return resp.Body, nil } diff --git a/src/pkg/reg/adapter/helmhub/chart_registry.go b/src/pkg/reg/adapter/helmhub/chart_registry.go index a2854bea8..b566178be 100644 --- a/src/pkg/reg/adapter/helmhub/chart_registry.go +++ b/src/pkg/reg/adapter/helmhub/chart_registry.go @@ -16,12 +16,13 @@ package helmhub import ( "fmt" - "github.com/goharbor/harbor/src/pkg/reg/filter" "io" "io/ioutil" "net/http" "strings" + "github.com/goharbor/harbor/src/pkg/reg/filter" + "github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/log" "github.com/goharbor/harbor/src/pkg/reg/model" @@ -138,7 +139,7 @@ func (a *adapter) download(version *chartVersion) (io.ReadCloser, error) { if err != nil { return nil, err } - return nil, fmt.Errorf("failed to download the chart %s: %d %s", req.URL.String(), resp.StatusCode, string(body)) + return nil, errors.Errorf("failed to download the chart %q: %d %s", req.URL.String(), resp.StatusCode, string(body)) } return resp.Body, nil } diff --git a/src/pkg/reg/adapter/tencentcr/chart_registry.go b/src/pkg/reg/adapter/tencentcr/chart_registry.go index 9ed8a3cae..3f9b119a9 100644 --- a/src/pkg/reg/adapter/tencentcr/chart_registry.go +++ b/src/pkg/reg/adapter/tencentcr/chart_registry.go @@ -10,6 +10,7 @@ import ( "strings" commonhttp "github.com/goharbor/harbor/src/common/http" + "github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/log" adp "github.com/goharbor/harbor/src/pkg/reg/adapter" "github.com/goharbor/harbor/src/pkg/reg/filter" @@ -183,7 +184,7 @@ func (a *adapter) DownloadChart(name, version, contentURL string) (rc io.ReadClo if err != nil { return } - err = fmt.Errorf("[tencent-tcr.DownloadChart.failed] chart=%s, status=%d, body=%s", req.URL.String(), resp.StatusCode, string(body)) + err = errors.Errorf("[tencent-tcr.DownloadChart.failed] chart=%q, status=%d, body=%s", req.URL.String(), resp.StatusCode, string(body)) return }