Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apiserver/apiclient: lint #2695

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions pkg/apiserver/apic.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,12 +908,19 @@
}

func setAlertScenario(alert *models.Alert, addCounters map[string]map[string]int, deleteCounters map[string]map[string]int) {
if *alert.Source.Scope == types.CAPIOrigin {
switch *alert.Source.Scope {
case types.CAPIOrigin:
*alert.Source.Scope = types.CommunityBlocklistPullSourceScope
alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs", addCounters[types.CAPIOrigin]["all"], deleteCounters[types.CAPIOrigin]["all"]))
} else if *alert.Source.Scope == types.ListOrigin {
alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs",
addCounters[types.CAPIOrigin]["all"],
deleteCounters[types.CAPIOrigin]["all"]),
)
case types.ListOrigin:
*alert.Source.Scope = fmt.Sprintf("%s:%s", types.ListOrigin, *alert.Scenario)
alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs", addCounters[types.ListOrigin][*alert.Scenario], deleteCounters[types.ListOrigin][*alert.Scenario]))
alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs",
addCounters[types.ListOrigin][*alert.Scenario],
deleteCounters[types.ListOrigin][*alert.Scenario]),
)
}
}

Expand Down Expand Up @@ -985,11 +992,12 @@
}

func updateCounterForDecision(counter map[string]map[string]int, origin *string, scenario *string, totalDecisions int) {
if *origin == types.CAPIOrigin {
switch *origin {
case types.CAPIOrigin:
counter[*origin]["all"] += totalDecisions
} else if *origin == types.ListOrigin {
case types.ListOrigin:
counter[*origin][*scenario] += totalDecisions
} else {
default:

Check warning on line 1000 in pkg/apiserver/apic.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apic.go#L1000

Added line #L1000 was not covered by tests
log.Warningf("Unknown origin %s", *origin)
}
}
3 changes: 1 addition & 2 deletions pkg/apiserver/controllers/v1/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,7 @@ func (c *Controller) DeleteAlertByID(gctx *gin.Context) {
return
}

decisionIDStr := gctx.Param("alert_id")
decisionID, err := strconv.Atoi(decisionIDStr)
decisionID, err := strconv.Atoi(gctx.Param("alert_id"))
if err != nil {
gctx.JSON(http.StatusBadRequest, gin.H{"message": "alert_id must be valid integer"})
return
Expand Down
39 changes: 23 additions & 16 deletions pkg/apiserver/controllers/v1/decisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -138,22 +137,25 @@
// respBuffer := bytes.NewBuffer([]byte{})
limit := 30000 //FIXME : make it configurable
needComma := false
lastId := 0
lastID := 0

Check warning on line 140 in pkg/apiserver/controllers/v1/decisions.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/controllers/v1/decisions.go#L140

Added line #L140 was not covered by tests

limitStr := fmt.Sprintf("%d", limit)
limitStr := strconv.Itoa(limit)

Check warning on line 142 in pkg/apiserver/controllers/v1/decisions.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/controllers/v1/decisions.go#L142

Added line #L142 was not covered by tests
filters["limit"] = []string{limitStr}

Check warning on line 144 in pkg/apiserver/controllers/v1/decisions.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/controllers/v1/decisions.go#L144

Added line #L144 was not covered by tests
for {
if lastId > 0 {
lastIdStr := fmt.Sprintf("%d", lastId)
filters["id_gt"] = []string{lastIdStr}
if lastID > 0 {
lastIDStr := strconv.Itoa(lastID)
filters["id_gt"] = []string{lastIDStr}

Check warning on line 148 in pkg/apiserver/controllers/v1/decisions.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/controllers/v1/decisions.go#L146-L148

Added lines #L146 - L148 were not covered by tests
}

data, err := dbFunc(filters)
if err != nil {
return err
}

if len(data) > 0 {
lastId = data[len(data)-1].ID
lastID = data[len(data)-1].ID

Check warning on line 158 in pkg/apiserver/controllers/v1/decisions.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/controllers/v1/decisions.go#L157-L158

Added lines #L157 - L158 were not covered by tests
results := FormatDecisions(data)
for _, decision := range results {
decisionJSON, _ := json.Marshal(decision)
Expand All @@ -175,7 +177,9 @@
//respBuffer.Reset()
}
}
log.Debugf("startup: %d decisions returned (limit: %d, lastid: %d)", len(data), limit, lastId)

log.Debugf("startup: %d decisions returned (limit: %d, lastid: %d)", len(data), limit, lastID)

Check warning on line 182 in pkg/apiserver/controllers/v1/decisions.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/controllers/v1/decisions.go#L181-L182

Added lines #L181 - L182 were not covered by tests
if len(data) < limit {
gctx.Writer.Flush()

Expand All @@ -190,22 +194,23 @@
//respBuffer := bytes.NewBuffer([]byte{})
limit := 30000 //FIXME : make it configurable
needComma := false
lastId := 0
lastID := 0

filters["limit"] = []string{strconv.Itoa(limit)}

Check warning on line 199 in pkg/apiserver/controllers/v1/decisions.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/controllers/v1/decisions.go#L197-L199

Added lines #L197 - L199 were not covered by tests

limitStr := fmt.Sprintf("%d", limit)
filters["limit"] = []string{limitStr}
for {
if lastId > 0 {
lastIdStr := fmt.Sprintf("%d", lastId)
filters["id_gt"] = []string{lastIdStr}
if lastID > 0 {
filters["id_gt"] = []string{strconv.Itoa(lastID)}

Check warning on line 203 in pkg/apiserver/controllers/v1/decisions.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/controllers/v1/decisions.go#L202-L203

Added lines #L202 - L203 were not covered by tests
}

data, err := dbFunc(lastPull, filters)
if err != nil {
return err
}

if len(data) > 0 {
lastId = data[len(data)-1].ID
lastID = data[len(data)-1].ID

Check warning on line 213 in pkg/apiserver/controllers/v1/decisions.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/controllers/v1/decisions.go#L212-L213

Added lines #L212 - L213 were not covered by tests
results := FormatDecisions(data)
for _, decision := range results {
decisionJSON, _ := json.Marshal(decision)
Expand All @@ -227,7 +232,9 @@
//respBuffer.Reset()
}
}
log.Debugf("startup: %d decisions returned (limit: %d, lastid: %d)", len(data), limit, lastId)

log.Debugf("startup: %d decisions returned (limit: %d, lastid: %d)", len(data), limit, lastID)

Check warning on line 237 in pkg/apiserver/controllers/v1/decisions.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/controllers/v1/decisions.go#L236-L237

Added lines #L236 - L237 were not covered by tests
if len(data) < limit {
gctx.Writer.Flush()

Expand Down
3 changes: 2 additions & 1 deletion pkg/apiserver/middlewares/v1/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -53,7 +54,7 @@ func HashSHA512(str string) string {
hashedKey := sha512.New()
hashedKey.Write([]byte(str))

hashStr := fmt.Sprintf("%x", hashedKey.Sum(nil))
hashStr := hex.EncodeToString(hashedKey.Sum(nil))

return hashStr
}
Expand Down
17 changes: 7 additions & 10 deletions pkg/apiserver/middlewares/v1/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,25 +276,21 @@
}

func NewJWT(dbClient *database.Client) (*JWT, error) {
// Get secret from environment variable "SECRET"
var (
secret []byte
err error
)
var err error

// Please be aware that brute force HS256 is possible.
// PLEASE choose a STRONG secret
secretString := os.Getenv("CS_LAPI_SECRET")
secret = []byte(secretString)
secret := []byte(secretString)

switch l := len(secret); {
case l == 0:
secret, err = randomSecret()
if err != nil {
return &JWT{}, err
return nil, err

Check warning on line 290 in pkg/apiserver/middlewares/v1/jwt.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/middlewares/v1/jwt.go#L290

Added line #L290 was not covered by tests
}
case l < 64:
return &JWT{}, errors.New("CS_LAPI_SECRET not strong enough")
return nil, errors.New("CS_LAPI_SECRET not strong enough")
}

jwtMiddleware := &JWT{
Expand All @@ -318,13 +314,14 @@
TimeFunc: time.Now,
})
if err != nil {
return &JWT{}, err
return nil, err

Check warning on line 317 in pkg/apiserver/middlewares/v1/jwt.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/middlewares/v1/jwt.go#L317

Added line #L317 was not covered by tests
}

errInit := ret.MiddlewareInit()
if errInit != nil {
return &JWT{}, fmt.Errorf("authMiddleware.MiddlewareInit() Error:" + errInit.Error())
return nil, fmt.Errorf("authMiddleware.MiddlewareInit() Error:" + errInit.Error())

Check warning on line 322 in pkg/apiserver/middlewares/v1/jwt.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/middlewares/v1/jwt.go#L322

Added line #L322 was not covered by tests
}

jwtMiddleware.Middleware = ret

return jwtMiddleware, nil
Expand Down
47 changes: 28 additions & 19 deletions pkg/apiserver/papi.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@
}

func NewPAPI(apic *apic, dbClient *database.Client, consoleConfig *csconfig.ConsoleConfig, logLevel log.Level) (*Papi, error) {

logger := log.New()
if err := types.ConfigureLogger(logger); err != nil {
return &Papi{}, fmt.Errorf("creating papi logger: %s", err)
return &Papi{}, fmt.Errorf("creating papi logger: %w", err)

Check warning on line 92 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L92

Added line #L92 was not covered by tests
}

logger.SetLevel(logLevel)

papiUrl := *apic.apiClient.PapiURL
papiUrl.Path = fmt.Sprintf("%s%s", types.PAPIVersion, types.PAPIPollUrl)
papiURL := *apic.apiClient.PapiURL
papiURL.Path = fmt.Sprintf("%s%s", types.PAPIVersion, types.PAPIPollUrl)

Check warning on line 98 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L97-L98

Added lines #L97 - L98 were not covered by tests
longPollClient, err := longpollclient.NewLongPollClient(longpollclient.LongPollClientConfig{
Url: papiUrl,
Url: papiURL,

Check warning on line 100 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L100

Added line #L100 was not covered by tests
Logger: logger,
HttpClient: apic.apiClient.GetClient(),
})
Expand Down Expand Up @@ -132,55 +132,64 @@
func (p *Papi) handleEvent(event longpollclient.Event, sync bool) error {
logger := p.Logger.WithField("request-id", event.RequestId)
logger.Debugf("message received: %+v", event.Data)

Check warning on line 135 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L135

Added line #L135 was not covered by tests
message := &Message{}
if err := json.Unmarshal([]byte(event.Data), message); err != nil {
return fmt.Errorf("polling papi message format is not compatible: %+v: %s", event.Data, err)
return fmt.Errorf("polling papi message format is not compatible: %+v: %w", event.Data, err)

Check warning on line 138 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L138

Added line #L138 was not covered by tests
}

if message.Header == nil {
return fmt.Errorf("no header in message, skipping")
}

if message.Header.Source == nil {
return fmt.Errorf("no source user in header message, skipping")
}

if operationFunc, ok := operationMap[message.Header.OperationType]; ok {
logger.Debugf("Calling operation '%s'", message.Header.OperationType)
err := operationFunc(message, p, sync)
if err != nil {
return fmt.Errorf("'%s %s failed: %s", message.Header.OperationType, message.Header.OperationCmd, err)

if err := operationFunc(message, p, sync); err != nil {
return fmt.Errorf("'%s %s failed: %w", message.Header.OperationType, message.Header.OperationCmd, err)

Check warning on line 153 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L151-L153

Added lines #L151 - L153 were not covered by tests
}
} else {
return fmt.Errorf("operation '%s' unknown, continue", message.Header.OperationType)
}

return nil
}

func (p *Papi) GetPermissions() (PapiPermCheckSuccess, error) {
httpClient := p.apiClient.GetClient()
papiCheckUrl := fmt.Sprintf("%s%s%s", p.URL, types.PAPIVersion, types.PAPIPermissionsUrl)
req, err := http.NewRequest(http.MethodGet, papiCheckUrl, nil)
papiCheckURL := fmt.Sprintf("%s%s%s", p.URL, types.PAPIVersion, types.PAPIPermissionsUrl)

req, err := http.NewRequest(http.MethodGet, papiCheckURL, nil)

Check warning on line 166 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L164-L166

Added lines #L164 - L166 were not covered by tests
if err != nil {
return PapiPermCheckSuccess{}, fmt.Errorf("failed to create request : %s", err)
return PapiPermCheckSuccess{}, fmt.Errorf("failed to create request: %w", err)

Check warning on line 168 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L168

Added line #L168 was not covered by tests
}

resp, err := httpClient.Do(req)
if err != nil {
log.Fatalf("failed to get response : %s", err)
// XXX: fatal?
log.Fatalf("failed to get response: %s", err)

Check warning on line 174 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L173-L174

Added lines #L173 - L174 were not covered by tests
}

defer resp.Body.Close()

Check warning on line 178 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L178

Added line #L178 was not covered by tests
if resp.StatusCode != http.StatusOK {
errResp := PapiPermCheckError{}
err = json.NewDecoder(resp.Body).Decode(&errResp)
if err != nil {
return PapiPermCheckSuccess{}, fmt.Errorf("failed to decode response : %s", err)
if err = json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
return PapiPermCheckSuccess{}, fmt.Errorf("failed to decode response: %w", err)

Check warning on line 182 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L181-L182

Added lines #L181 - L182 were not covered by tests
}

return PapiPermCheckSuccess{}, fmt.Errorf("unable to query PAPI : %s (%d)", errResp.Error, resp.StatusCode)
}

respBody := PapiPermCheckSuccess{}
err = json.NewDecoder(resp.Body).Decode(&respBody)
if err != nil {
return PapiPermCheckSuccess{}, fmt.Errorf("failed to decode response : %s", err)
if err = json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
return PapiPermCheckSuccess{}, fmt.Errorf("failed to decode response: %w", err)

Check warning on line 190 in pkg/apiserver/papi.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi.go#L189-L190

Added lines #L189 - L190 were not covered by tests
}

return respBody, nil
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/apiserver/papi_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,17 @@
if err != nil {
return err
}

forcePullMsg := forcePull{}
if err := json.Unmarshal(data, &forcePullMsg); err != nil {
return fmt.Errorf("message for '%s' contains bad data format: %s", message.Header.OperationType, err)
return fmt.Errorf("message for '%s' contains bad data format: %w", message.Header.OperationType, err)

Check warning on line 182 in pkg/apiserver/papi_cmd.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi_cmd.go#L182

Added line #L182 was not covered by tests
}

if forcePullMsg.Blocklist == nil {
log.Infof("Received force_pull command from PAPI, pulling community and 3rd-party blocklists")
err = p.apic.PullTop(true)
if err != nil {
return fmt.Errorf("failed to force pull operation: %s", err)

if err = p.apic.PullTop(true); err != nil {
return fmt.Errorf("failed to force pull operation: %w", err)

Check warning on line 189 in pkg/apiserver/papi_cmd.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/papi_cmd.go#L187-L189

Added lines #L187 - L189 were not covered by tests
}
} else {
log.Infof("Received force_pull command from PAPI, pulling blocklist %s", forcePullMsg.Blocklist.Name)
Expand Down
Loading