Skip to content

Commit 1cafb16

Browse files
committed
build: fix linter issues after upgrade
1 parent eef7df5 commit 1cafb16

6 files changed

Lines changed: 41 additions & 25 deletions

File tree

base/database/database.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ func OnConflictUpdate(db *gorm.DB, key string, updateCols ...string) *gorm.DB {
1212

1313
// Appends `ON CONFLICT (key...) DO UPDATE SET (fields) to following insert query with multiple key fields
1414
func OnConflictUpdateMulti(db *gorm.DB, keys []string, updateCols ...string) *gorm.DB {
15-
confilctColumns := []clause.Column{}
16-
for _, key := range keys {
17-
confilctColumns = append(confilctColumns, clause.Column{Name: key})
15+
conflictColumns := make([]clause.Column, len(keys))
16+
for i, key := range keys {
17+
conflictColumns[i] = clause.Column{Name: key}
1818
}
19-
onConflict := clause.OnConflict{Columns: confilctColumns}
19+
onConflict := clause.OnConflict{Columns: conflictColumns}
2020
if len(updateCols) > 0 {
2121
onConflict.DoUpdates = clause.AssignmentColumns(updateCols)
2222
} else {
@@ -31,17 +31,17 @@ type UpExpr struct {
3131
}
3232

3333
func OnConflictDoUpdateExpr(db *gorm.DB, keys []string, updateExprs ...UpExpr) *gorm.DB {
34-
updateColsValues := make(map[string]interface{})
34+
updateColsValues := make(map[string]interface{}, len(updateExprs))
3535
for _, v := range updateExprs {
3636
updateColsValues[v.Name] = v.Expr
3737
}
38-
confilctColumns := []clause.Column{}
39-
for _, key := range keys {
40-
confilctColumns = append(confilctColumns, clause.Column{Name: key})
38+
conflictColumns := make([]clause.Column, len(keys))
39+
for i, key := range keys {
40+
conflictColumns[i] = clause.Column{Name: key}
4141
}
4242
if len(updateColsValues) > 0 {
4343
return db.Clauses(clause.OnConflict{
44-
Columns: confilctColumns,
44+
Columns: conflictColumns,
4545
DoUpdates: clause.Assignments(updateColsValues),
4646
})
4747
}

base/database/setup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type PostgreSQLConfig struct {
6767
Port int
6868
User string
6969
Database string
70-
Passwd string
70+
Passwd string // #nosec G117
7171
SSLMode string
7272
SSLRootCert string
7373
Debug bool

base/utils/awscloudwatch.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"os"
99
"time"
1010

11+
// nolint: staticcheck
1112
"github.com/aws/aws-sdk-go/aws"
13+
// nolint: staticcheck
1214
"github.com/aws/aws-sdk-go/aws/credentials"
1315
lc "github.com/redhatinsights/platform-go-middlewares/logging/cloudwatch"
1416
log "github.com/sirupsen/logrus"

base/utils/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func CallAPI(client *http.Client, request *http.Request, debugEnabled bool) (*ht
5656
LogDebug("dump", fmt.Sprintf("\n%s\n", string(dump)), "http call")
5757
}
5858

59-
resp, err := client.Do(request)
59+
resp, err := client.Do(request) // #nosec G704
6060
if err != nil {
6161
return resp, err
6262
}

manager/middlewares/logger.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ func RequestResponseLogger() gin.HandlerFunc {
1515
return func(c *gin.Context) {
1616
tStart := time.Now()
1717
c.Next()
18-
var fields []interface{}
18+
// 17 is the number of fields that are always logged
19+
// 2 is the number of fields for each parameter
20+
var fields = make([]any, 0, 17+2*len(c.Params))
1921

2022
duration := time.Since(tStart).Nanoseconds() / 1e6
2123
fields = append(fields, "durationMs", duration,

turnpike/controllers/pprof.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8-
"regexp"
8+
"slices"
99
"time"
1010

1111
"github.com/gin-gonic/gin"
@@ -21,7 +21,7 @@ import (
2121
// @Failure 500 {object} map[string]interface{}
2222
// @Router /pprof/evaluator_upload/{param} [get]
2323
func GetEvaluatorUploadPprof(c *gin.Context) {
24-
pprofHandler(c, utils.CoreCfg.EvaluatorUploadPrivateAddress)
24+
pprofHandler(c, "evaluator-upload")
2525
}
2626

2727
// @Summary Get profile info
@@ -34,7 +34,7 @@ func GetEvaluatorUploadPprof(c *gin.Context) {
3434
// @Failure 500 {object} map[string]interface{}
3535
// @Router /pprof/evaluator_recalc/{param} [get]
3636
func GetEvaluatorRecalcPprof(c *gin.Context) {
37-
pprofHandler(c, utils.CoreCfg.EvaluatorRecalcPrivateAddress)
37+
pprofHandler(c, "evaluator-recalc")
3838
}
3939

4040
// @Summary Get profile info
@@ -47,7 +47,7 @@ func GetEvaluatorRecalcPprof(c *gin.Context) {
4747
// @Failure 500 {object} map[string]interface{}
4848
// @Router /pprof/listener/{param} [get]
4949
func GetListenerPprof(c *gin.Context) {
50-
pprofHandler(c, utils.CoreCfg.ListenerPrivateAddress)
50+
pprofHandler(c, "listener")
5151
}
5252

5353
// @Summary Get profile info
@@ -60,20 +60,19 @@ func GetListenerPprof(c *gin.Context) {
6060
// @Failure 500 {object} map[string]interface{}
6161
// @Router /pprof/manager/{param} [get]
6262
func GetManagerPprof(c *gin.Context) {
63-
pprofHandler(c, utils.CoreCfg.ManagerPrivateAddress)
63+
pprofHandler(c, "manager")
6464
}
6565

66-
var paramRegexp = regexp.MustCompile("^(heap|profile|block|mutex|trace)$")
66+
var allowedParams = []string{"heap", "profile", "block", "mutex", "trace"}
6767

68-
func pprofHandler(c *gin.Context, address string) {
68+
func pprofHandler(c *gin.Context, serviceName string) {
6969
query := c.Request.URL.RawQuery
7070
param := c.Param("param")
71-
match := paramRegexp.FindStringSubmatch(param)
72-
if len(match) < 1 {
71+
if !slices.Contains(allowedParams, param) {
7372
c.Status(http.StatusBadRequest)
7473
return
7574
}
76-
data, err := getPprof(address, match[0], query)
75+
data, err := getPprof(serviceName, param, query)
7776
if err != nil {
7877
c.JSON(http.StatusInternalServerError, gin.H{"err": err.Error()})
7978
return
@@ -82,19 +81,32 @@ func pprofHandler(c *gin.Context, address string) {
8281
c.Data(http.StatusOK, "application/octet-stream", data)
8382
}
8483

85-
func getPprof(address, param, query string) ([]byte, error) {
84+
func getPprof(serviceName, param, query string) ([]byte, error) {
8685
client := &http.Client{
8786
Timeout: time.Second * 60,
8887
}
8988
if len(query) > 0 {
9089
param = param + "?" + query
9190
}
91+
var address string
92+
switch serviceName {
93+
case "manager":
94+
address = utils.CoreCfg.ManagerPrivateAddress
95+
case "listener":
96+
address = utils.CoreCfg.ListenerPrivateAddress
97+
case "evaluator-upload":
98+
address = utils.CoreCfg.EvaluatorUploadPrivateAddress
99+
case "evaluator-recalc":
100+
address = utils.CoreCfg.EvaluatorRecalcPrivateAddress
101+
default:
102+
return nil, fmt.Errorf("invalid service name: %s", serviceName)
103+
}
92104
urlPath := fmt.Sprintf("%s/debug/pprof/%s", address, param)
93-
req, err := http.NewRequest(http.MethodGet, urlPath, nil)
105+
req, err := http.NewRequest(http.MethodGet, urlPath, nil) // #nosec G704
94106
if err != nil {
95107
return nil, err
96108
}
97-
res, err := client.Do(req)
109+
res, err := client.Do(req) // #nosec G704
98110
if err != nil {
99111
return nil, err
100112
}

0 commit comments

Comments
 (0)