fix: fix codeql alerts

Signed-off-by: chlins <chenyuzh@vmware.com>
This commit is contained in:
chlins 2022-02-14 15:09:14 +08:00
parent 6f1f2c0395
commit b37a987073
9 changed files with 23 additions and 18 deletions

View File

@ -24,6 +24,8 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
"fmt" "fmt"
"strconv"
"github.com/goharbor/harbor/src/jobservice/common/query" "github.com/goharbor/harbor/src/jobservice/common/query"
"github.com/goharbor/harbor/src/jobservice/common/utils" "github.com/goharbor/harbor/src/jobservice/common/utils"
"github.com/goharbor/harbor/src/jobservice/core" "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/job"
"github.com/goharbor/harbor/src/jobservice/logger" "github.com/goharbor/harbor/src/jobservice/logger"
"github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/errors"
"strconv"
) )
const ( const (

View File

@ -78,7 +78,7 @@ func (br *BaseRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if authErr == nil { if authErr == nil {
authErr = errors.Errorf("unauthorized: %s", err) 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) w.WriteHeader(http.StatusUnauthorized)
writeDate(w, []byte(authErr.Error())) writeDate(w, []byte(authErr.Error()))
return return

View File

@ -119,10 +119,10 @@ func (bs *basicScheduler) UnSchedule(policyID string) error {
// Failure errors will be only logged here // Failure errors will be only logged here
eKey := rds.KeyUpstreamJobAndExecutions(bs.namespace, policyID) eKey := rds.KeyUpstreamJobAndExecutions(bs.namespace, policyID)
if eIDs, err := getPeriodicExecutions(conn, eKey); err != nil { 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 { } else {
if len(eIDs) == 0 { if len(eIDs) == 0 {
logger.Debugf("no stopped executions: %s", policyID) logger.Debugf("no stopped executions: %q", policyID)
} }
for _, eID := range eIDs { for _, eID := range eIDs {
@ -150,7 +150,7 @@ func (bs *basicScheduler) UnSchedule(policyID string) error {
if err := eTracker.Stop(); err != nil { if err := eTracker.Stop(); err != nil {
logger.Errorf("Stop execution %s error: %s", eID, err) logger.Errorf("Stop execution %s error: %s", eID, err)
} else { } 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 { 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 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 // Switch the job stats to stopped if the job stats existing
// Should not block the next clear action // Should not block the next clear action
if err := tracker.Stop(); err != nil { 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() return tracker.NumericID()

View File

@ -163,7 +163,7 @@ func (e *enqueuer) scheduleNextJobs(p *Policy, conn redis.Conn) {
// The cron spec should be already checked at upper layers. // The cron spec should be already checked at upper layers.
// Just in cases, if error occurred, ignore it // Just in cases, if error occurred, ignore it
e.lastEnqueueErr = err 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 { } else {
for t := schedule.Next(nowTime); t.Before(horizon); t = schedule.Next(t) { for t := schedule.Next(nowTime); t.Before(horizon); t = schedule.Next(t) {
epoch := t.Unix() epoch := t.Unix()
@ -222,7 +222,7 @@ func (e *enqueuer) scheduleNextJobs(p *Policy, conn redis.Conn) {
break // Probably redis connection is broken 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)
} }
} }
} }

View File

@ -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 // 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 { if err := w.client.DeleteScheduledJob(t.Job().Info.RunAt, jobID); err != nil {
// Job is already running? // 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)
} }
} }

View File

@ -3,13 +3,13 @@ package client
import ( import (
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
"time" "time"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log" "github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/provider/auth" "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 { if (res.StatusCode / 100) != 2 {
// Return the server error content in the error. // 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 return bytes, nil
@ -194,7 +194,7 @@ func (hc *HTTPClient) post(url string, cred *auth.Credential, body interface{},
if (res.StatusCode / 100) != 2 { if (res.StatusCode / 100) != 2 {
// Return the server error content in the error. // 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 { } else if res.StatusCode == http.StatusAlreadyReported {
// Currently because if image was already preheated at least once, Dragonfly will return 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. // And we should preserve http status code info to process this case later.

View File

@ -23,10 +23,12 @@ import (
"net/http" "net/http"
"strings" "strings"
"net/url"
common_http "github.com/goharbor/harbor/src/common/http" 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/filter"
"github.com/goharbor/harbor/src/pkg/reg/model" "github.com/goharbor/harbor/src/pkg/reg/model"
"net/url"
) )
type label struct { type label struct {
@ -179,7 +181,7 @@ func (a *Adapter) DownloadChart(name, version, contentURL string) (io.ReadCloser
if err != nil { if err != nil {
return nil, err 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 return resp.Body, nil
} }

View File

@ -16,12 +16,13 @@ package helmhub
import ( import (
"fmt" "fmt"
"github.com/goharbor/harbor/src/pkg/reg/filter"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
"github.com/goharbor/harbor/src/pkg/reg/filter"
"github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log" "github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/reg/model" "github.com/goharbor/harbor/src/pkg/reg/model"
@ -138,7 +139,7 @@ func (a *adapter) download(version *chartVersion) (io.ReadCloser, error) {
if err != nil { if err != nil {
return nil, err 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 return resp.Body, nil
} }

View File

@ -10,6 +10,7 @@ import (
"strings" "strings"
commonhttp "github.com/goharbor/harbor/src/common/http" commonhttp "github.com/goharbor/harbor/src/common/http"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log" "github.com/goharbor/harbor/src/lib/log"
adp "github.com/goharbor/harbor/src/pkg/reg/adapter" adp "github.com/goharbor/harbor/src/pkg/reg/adapter"
"github.com/goharbor/harbor/src/pkg/reg/filter" "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 { if err != nil {
return 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 return
} }