Skip to content
Draft
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
8 changes: 2 additions & 6 deletions internal/pkg/api/apiVersion.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
"regexp"
"slices"
"strings"
)

Expand Down Expand Up @@ -47,12 +48,7 @@ func (a *apiVersion) validateVersionFormat(version string) error {
}

func (a *apiVersion) isVersionSupported(version string) bool {
for _, vers := range a.supportedVersions {
if vers == version {
return true
}
}
return false
return slices.Contains(a.supportedVersions, version)
}

func (a *apiVersion) middleware(next http.Handler) http.Handler {
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/api/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ func Test_ErrorResp(t *testing.T) {
tests := []struct {
name string
err error
expectedTags map[string]interface{}
expectedTags map[string]any
}{{
name: "generic error",
err: fmt.Errorf("generic error"),
}, {
name: "elastic error",
err: &es.ErrElastic{},
expectedTags: map[string]interface{}{
expectedTags: map[string]any{
"error_type": "ErrElastic",
},
}, {
name: "wrapped elastic error",
err: fmt.Errorf("wrapped error: %w", &es.ErrElastic{}),
expectedTags: map[string]interface{}{
expectedTags: map[string]any{
"error_type": "ErrElastic",
},
}}
Expand Down Expand Up @@ -64,7 +64,7 @@ func Test_ErrorResp(t *testing.T) {
require.Len(t, payloads.Transactions, 1)
require.Len(t, payloads.Errors, 1)

tags := make(map[string]interface{})
tags := make(map[string]any)
for _, tag := range payloads.Transactions[0].Context.Tags {
tags[tag.Key] = tag.Value
}
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/api/handleAck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestMakeUpdatePolicyBody(t *testing.T) {

data := makeUpdatePolicyBody(policyID, newRev)

var i interface{}
var i any
err := json.Unmarshal(data, &i)

if err != nil {
Expand Down Expand Up @@ -706,7 +706,7 @@ func TestAckHandleUpgrade(t *testing.T) {
bulker: func(t *testing.T) *ftesting.MockBulk {
m := ftesting.NewMockBulk()
m.On("Update", mock.Anything, mock.Anything, mock.Anything, mock.MatchedBy(func(p []byte) bool {
var body map[string]map[string]interface{}
var body map[string]map[string]any
if err := json.Unmarshal(p, &body); err != nil {
t.Fatal(err)
}
Expand All @@ -725,7 +725,7 @@ func TestAckHandleUpgrade(t *testing.T) {
bulker: func(t *testing.T) *ftesting.MockBulk {
m := ftesting.NewMockBulk()
m.On("Update", mock.Anything, mock.Anything, mock.Anything, mock.MatchedBy(func(p []byte) bool {
var body map[string]map[string]interface{}
var body map[string]map[string]any
if err := json.Unmarshal(p, &body); err != nil {
t.Fatal(err)
}
Expand Down
19 changes: 4 additions & 15 deletions internal/pkg/api/handleCheckin.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,7 @@ func (ct *CheckinT) validateRequest(zlog zerolog.Logger, w http.ResponseWriter,
// sets timeout is set to max(1m, min(pDur-2m, max poll time))
// sets the response write timeout to max(2m, timeout+1m)
if pDur != time.Duration(0) {
pollDuration = pDur - (2 * time.Minute)
if pollDuration > ct.cfg.Timeouts.CheckinMaxPoll {
pollDuration = ct.cfg.Timeouts.CheckinMaxPoll
}
if pollDuration < time.Minute {
pollDuration = time.Minute
}
pollDuration = max(min(pDur-(2*time.Minute), ct.cfg.Timeouts.CheckinMaxPoll), time.Minute)

wTime := pollDuration + time.Minute
rc := http.NewResponseController(w)
Expand Down Expand Up @@ -678,12 +672,7 @@ func (ct *CheckinT) writeResponse(zlog zerolog.Logger, w http.ResponseWriter, r
}

func acceptsEncoding(r *http.Request, encoding string) bool {
for _, v := range r.Header.Values("Accept-Encoding") {
if v == encoding {
return true
}
}
return false
return slices.Contains(r.Header.Values("Accept-Encoding"), encoding)
}

// Resolve AckToken from request, fallback on the agent record
Expand Down Expand Up @@ -1044,7 +1033,7 @@ func parseMeta(zlog zerolog.Logger, agent *model.Agent, req *CheckinRequest) ([]
}

// Deserialize the request metadata
var reqLocalMeta interface{}
var reqLocalMeta any
if err := json.Unmarshal(req.LocalMetadata, &reqLocalMeta); err != nil {
return nil, fmt.Errorf("parseMeta request: %w", err)
}
Expand All @@ -1056,7 +1045,7 @@ func parseMeta(zlog zerolog.Logger, agent *model.Agent, req *CheckinRequest) ([]

// Deserialize the agent's metadata copy. If it fails, it's ignored as it will just
// be replaced with the correct contents from the clients checkin.
var agentLocalMeta interface{}
var agentLocalMeta any
if err := json.Unmarshal(agent.LocalMetadata, &agentLocalMeta); err != nil {
zlog.Warn().Err(err).Msg("local_metadata in document invalid; ignoring it")
}
Expand Down
7 changes: 3 additions & 4 deletions internal/pkg/api/handleCheckin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@ func TestResolveSeqNo(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// setup mock CheckinT
logger := testlog.SetLogger(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
verCon := mustBuildConstraints("8.0.0")
cfg := &config.Server{}
c, _ := cache.New(config.Cache{NumCounters: 100, MaxCost: 100000})
Expand Down Expand Up @@ -413,7 +412,7 @@ func TestProcessUpgradeDetails(t *testing.T) {
mBulk := ftesting.NewMockBulk()
mBulk.On("Update", mock.Anything, dl.FleetAgents, "doc-ID", mock.MatchedBy(func(p []byte) bool {
doc := struct {
Doc map[string]interface{} `json:"doc"`
Doc map[string]any `json:"doc"`
}{}
if err := json.Unmarshal(p, &doc); err != nil {
t.Logf("bulk match unmarshal error: %v", err)
Expand Down Expand Up @@ -1090,7 +1089,7 @@ func TestParseComponents(t *testing.T) {
}
}

func requireMarshalJSON(t *testing.T, obj interface{}) json.RawMessage {
func requireMarshalJSON(t *testing.T, obj any) json.RawMessage {
data, err := json.Marshal(obj)
require.NoError(t, err)
return data
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/api/handleEnroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,16 +607,16 @@ func updateLocalMetaAgentID(data []byte, agentID string) ([]byte, error) {
return data, nil
}

var m map[string]interface{}
var m map[string]any
err := json.Unmarshal(data, &m)
if err != nil {
return nil, err
}

if v, ok := m["elastic"]; ok {
if sm, ok := v.(map[string]interface{}); ok {
if sm, ok := v.(map[string]any); ok {
if v, ok = sm["agent"]; ok {
if sm, ok = v.(map[string]interface{}); ok {
if sm, ok = v.(map[string]any); ok {
if _, ok = sm["id"]; ok {
sm["id"] = agentID
data, err = json.Marshal(m)
Expand Down
24 changes: 8 additions & 16 deletions internal/pkg/api/handleEnroll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ func TestRemoveDuplicateStr(t *testing.T) {
}

func TestEnroll(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
rb := &rollback.Rollback{}
zlog := zerolog.Logger{}
enrollmentID := "1234"
Expand Down Expand Up @@ -102,8 +101,7 @@ func TestEnroll(t *testing.T) {
}

func TestEnrollWithAgentID(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
rb := &rollback.Rollback{}
zlog := zerolog.Logger{}
agentID := "1234"
Expand Down Expand Up @@ -144,8 +142,7 @@ func TestEnrollWithAgentID(t *testing.T) {
}

func TestEnrollWithAgentIDExistingNonActive(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
rb := &rollback.Rollback{}
zlog := zerolog.Logger{}
agentID := "1234"
Expand Down Expand Up @@ -191,8 +188,7 @@ func TestEnrollWithAgentIDExistingNonActive(t *testing.T) {
}

func TestEnrollWithAgentIDExistingActive_NotReplaceable(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
rb := &rollback.Rollback{}
zlog := zerolog.Logger{}
agentID := "1234"
Expand Down Expand Up @@ -226,8 +222,7 @@ func TestEnrollWithAgentIDExistingActive_NotReplaceable(t *testing.T) {
}

func TestEnrollWithAgentIDExistingActive_InvalidReplaceToken_Missing(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
rb := &rollback.Rollback{}
zlog := zerolog.Logger{}
agentID := "1234"
Expand Down Expand Up @@ -269,8 +264,7 @@ func TestEnrollWithAgentIDExistingActive_InvalidReplaceToken_Missing(t *testing.
}

func TestEnrollWithAgentIDExistingActive_InvalidReplaceToken_Mismatch(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
rb := &rollback.Rollback{}
zlog := zerolog.Logger{}
agentID := "1234"
Expand Down Expand Up @@ -314,8 +308,7 @@ func TestEnrollWithAgentIDExistingActive_InvalidReplaceToken_Mismatch(t *testing
}

func TestEnrollWithAgentIDExistingActive_WrongPolicy(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
rb := &rollback.Rollback{}
zlog := zerolog.Logger{}
agentID := "1234"
Expand Down Expand Up @@ -359,8 +352,7 @@ func TestEnrollWithAgentIDExistingActive_WrongPolicy(t *testing.T) {
}

func TestEnrollWithAgentIDExistingActive(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
rb := &rollback.Rollback{}
zlog := zerolog.Logger{}
agentID := "1234"
Expand Down
28 changes: 14 additions & 14 deletions internal/pkg/api/handleFileDelivery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ func TestFileDelivery(t *testing.T) {
SeqNo: 1,
Version: 1,
Index: fmt.Sprintf(delivery.FileDataIndexPattern, "endpoint"),
Fields: map[string]interface{}{
file.FieldBaseID: []interface{}{"X"},
file.FieldLast: []interface{}{true},
Fields: map[string]any{
file.FieldBaseID: []any{"X"},
file.FieldLast: []any{true},
},
},
},
Expand Down Expand Up @@ -222,18 +222,18 @@ func TestFileDeliveryMultipleChunks(t *testing.T) {
SeqNo: 1,
Version: 1,
Index: fmt.Sprintf(delivery.FileDataIndexPattern, "endpoint"),
Fields: map[string]interface{}{
file.FieldBaseID: []interface{}{"X"},
Fields: map[string]any{
file.FieldBaseID: []any{"X"},
},
},
{
ID: "X.1",
SeqNo: 1,
Version: 1,
Index: fmt.Sprintf(delivery.FileDataIndexPattern, "endpoint"),
Fields: map[string]interface{}{
file.FieldBaseID: []interface{}{"X"},
file.FieldLast: []interface{}{true},
Fields: map[string]any{
file.FieldBaseID: []any{"X"},
file.FieldLast: []any{true},
},
},
},
Expand Down Expand Up @@ -301,9 +301,9 @@ func TestFileDeliverySetsHeaders(t *testing.T) {
SeqNo: 1,
Version: 1,
Index: fmt.Sprintf(delivery.FileDataIndexPattern, "endpoint"),
Fields: map[string]interface{}{
file.FieldBaseID: []interface{}{"X"},
file.FieldLast: []interface{}{true},
Fields: map[string]any{
file.FieldBaseID: []any{"X"},
file.FieldLast: []any{true},
},
},
},
Expand Down Expand Up @@ -362,9 +362,9 @@ func TestFileDeliverySetsHashWhenPresent(t *testing.T) {
SeqNo: 1,
Version: 1,
Index: fmt.Sprintf(delivery.FileDataIndexPattern, "endpoint"),
Fields: map[string]interface{}{
file.FieldBaseID: []interface{}{"X"},
file.FieldLast: []interface{}{true},
Fields: map[string]any{
file.FieldBaseID: []any{"X"},
file.FieldLast: []any{true},
},
},
},
Expand Down
Loading