Skip to content

Commit e118296

Browse files
run go fix ./..., add as step in bk
1 parent a4f9b5a commit e118296

108 files changed

Lines changed: 744 additions & 853 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

internal/pkg/api/apiVersion.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"net/http"
1111
"regexp"
12+
"slices"
1213
"strings"
1314
)
1415

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

4950
func (a *apiVersion) isVersionSupported(version string) bool {
50-
for _, vers := range a.supportedVersions {
51-
if vers == version {
52-
return true
53-
}
54-
}
55-
return false
51+
return slices.Contains(a.supportedVersions, version)
5652
}
5753

5854
func (a *apiVersion) middleware(next http.Handler) http.Handler {

internal/pkg/api/error_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ func Test_ErrorResp(t *testing.T) {
2222
tests := []struct {
2323
name string
2424
err error
25-
expectedTags map[string]interface{}
25+
expectedTags map[string]any
2626
}{{
2727
name: "generic error",
2828
err: fmt.Errorf("generic error"),
2929
}, {
3030
name: "elastic error",
3131
err: &es.ErrElastic{},
32-
expectedTags: map[string]interface{}{
32+
expectedTags: map[string]any{
3333
"error_type": "ErrElastic",
3434
},
3535
}, {
3636
name: "wrapped elastic error",
3737
err: fmt.Errorf("wrapped error: %w", &es.ErrElastic{}),
38-
expectedTags: map[string]interface{}{
38+
expectedTags: map[string]any{
3939
"error_type": "ErrElastic",
4040
},
4141
}}
@@ -64,7 +64,7 @@ func Test_ErrorResp(t *testing.T) {
6464
require.Len(t, payloads.Transactions, 1)
6565
require.Len(t, payloads.Errors, 1)
6666

67-
tags := make(map[string]interface{})
67+
tags := make(map[string]any)
6868
for _, tag := range payloads.Transactions[0].Context.Tags {
6969
tags[tag.Key] = tag.Value
7070
}

internal/pkg/api/handleAck_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestMakeUpdatePolicyBody(t *testing.T) {
4848

4949
data := makeUpdatePolicyBody(policyID, newRev)
5050

51-
var i interface{}
51+
var i any
5252
err := json.Unmarshal(data, &i)
5353

5454
if err != nil {
@@ -706,7 +706,7 @@ func TestAckHandleUpgrade(t *testing.T) {
706706
bulker: func(t *testing.T) *ftesting.MockBulk {
707707
m := ftesting.NewMockBulk()
708708
m.On("Update", mock.Anything, mock.Anything, mock.Anything, mock.MatchedBy(func(p []byte) bool {
709-
var body map[string]map[string]interface{}
709+
var body map[string]map[string]any
710710
if err := json.Unmarshal(p, &body); err != nil {
711711
t.Fatal(err)
712712
}
@@ -725,7 +725,7 @@ func TestAckHandleUpgrade(t *testing.T) {
725725
bulker: func(t *testing.T) *ftesting.MockBulk {
726726
m := ftesting.NewMockBulk()
727727
m.On("Update", mock.Anything, mock.Anything, mock.Anything, mock.MatchedBy(func(p []byte) bool {
728-
var body map[string]map[string]interface{}
728+
var body map[string]map[string]any
729729
if err := json.Unmarshal(p, &body); err != nil {
730730
t.Fatal(err)
731731
}

internal/pkg/api/handleCheckin.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,7 @@ func (ct *CheckinT) validateRequest(zlog zerolog.Logger, w http.ResponseWriter,
217217
// sets timeout is set to max(1m, min(pDur-2m, max poll time))
218218
// sets the response write timeout to max(2m, timeout+1m)
219219
if pDur != time.Duration(0) {
220-
pollDuration = pDur - (2 * time.Minute)
221-
if pollDuration > ct.cfg.Timeouts.CheckinMaxPoll {
222-
pollDuration = ct.cfg.Timeouts.CheckinMaxPoll
223-
}
224-
if pollDuration < time.Minute {
225-
pollDuration = time.Minute
226-
}
220+
pollDuration = max(min(pDur-(2*time.Minute), ct.cfg.Timeouts.CheckinMaxPoll), time.Minute)
227221

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

680674
func acceptsEncoding(r *http.Request, encoding string) bool {
681-
for _, v := range r.Header.Values("Accept-Encoding") {
682-
if v == encoding {
683-
return true
684-
}
685-
}
686-
return false
675+
return slices.Contains(r.Header.Values("Accept-Encoding"), encoding)
687676
}
688677

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

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

10571046
// Deserialize the agent's metadata copy. If it fails, it's ignored as it will just
10581047
// be replaced with the correct contents from the clients checkin.
1059-
var agentLocalMeta interface{}
1048+
var agentLocalMeta any
10601049
if err := json.Unmarshal(agent.LocalMetadata, &agentLocalMeta); err != nil {
10611050
zlog.Warn().Err(err).Msg("local_metadata in document invalid; ignoring it")
10621051
}

internal/pkg/api/handleCheckin_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,7 @@ func TestResolveSeqNo(t *testing.T) {
356356
t.Run(tc.name, func(t *testing.T) {
357357
// setup mock CheckinT
358358
logger := testlog.SetLogger(t)
359-
ctx, cancel := context.WithCancel(context.Background())
360-
defer cancel()
359+
ctx := t.Context()
361360
verCon := mustBuildConstraints("8.0.0")
362361
cfg := &config.Server{}
363362
c, _ := cache.New(config.Cache{NumCounters: 100, MaxCost: 100000})
@@ -413,7 +412,7 @@ func TestProcessUpgradeDetails(t *testing.T) {
413412
mBulk := ftesting.NewMockBulk()
414413
mBulk.On("Update", mock.Anything, dl.FleetAgents, "doc-ID", mock.MatchedBy(func(p []byte) bool {
415414
doc := struct {
416-
Doc map[string]interface{} `json:"doc"`
415+
Doc map[string]any `json:"doc"`
417416
}{}
418417
if err := json.Unmarshal(p, &doc); err != nil {
419418
t.Logf("bulk match unmarshal error: %v", err)
@@ -1090,7 +1089,7 @@ func TestParseComponents(t *testing.T) {
10901089
}
10911090
}
10921091

1093-
func requireMarshalJSON(t *testing.T, obj interface{}) json.RawMessage {
1092+
func requireMarshalJSON(t *testing.T, obj any) json.RawMessage {
10941093
data, err := json.Marshal(obj)
10951094
require.NoError(t, err)
10961095
return data

internal/pkg/api/handleEnroll.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,16 +607,16 @@ func updateLocalMetaAgentID(data []byte, agentID string) ([]byte, error) {
607607
return data, nil
608608
}
609609

610-
var m map[string]interface{}
610+
var m map[string]any
611611
err := json.Unmarshal(data, &m)
612612
if err != nil {
613613
return nil, err
614614
}
615615

616616
if v, ok := m["elastic"]; ok {
617-
if sm, ok := v.(map[string]interface{}); ok {
617+
if sm, ok := v.(map[string]any); ok {
618618
if v, ok = sm["agent"]; ok {
619-
if sm, ok = v.(map[string]interface{}); ok {
619+
if sm, ok = v.(map[string]any); ok {
620620
if _, ok = sm["id"]; ok {
621621
sm["id"] = agentID
622622
data, err = json.Marshal(m)

internal/pkg/api/handleEnroll_test.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ func TestRemoveDuplicateStr(t *testing.T) {
6363
}
6464

6565
func TestEnroll(t *testing.T) {
66-
ctx, cancel := context.WithCancel(context.Background())
67-
defer cancel()
66+
ctx := t.Context()
6867
rb := &rollback.Rollback{}
6968
zlog := zerolog.Logger{}
7069
enrollmentID := "1234"
@@ -102,8 +101,7 @@ func TestEnroll(t *testing.T) {
102101
}
103102

104103
func TestEnrollWithAgentID(t *testing.T) {
105-
ctx, cancel := context.WithCancel(context.Background())
106-
defer cancel()
104+
ctx := t.Context()
107105
rb := &rollback.Rollback{}
108106
zlog := zerolog.Logger{}
109107
agentID := "1234"
@@ -144,8 +142,7 @@ func TestEnrollWithAgentID(t *testing.T) {
144142
}
145143

146144
func TestEnrollWithAgentIDExistingNonActive(t *testing.T) {
147-
ctx, cancel := context.WithCancel(context.Background())
148-
defer cancel()
145+
ctx := t.Context()
149146
rb := &rollback.Rollback{}
150147
zlog := zerolog.Logger{}
151148
agentID := "1234"
@@ -191,8 +188,7 @@ func TestEnrollWithAgentIDExistingNonActive(t *testing.T) {
191188
}
192189

193190
func TestEnrollWithAgentIDExistingActive_NotReplaceable(t *testing.T) {
194-
ctx, cancel := context.WithCancel(context.Background())
195-
defer cancel()
191+
ctx := t.Context()
196192
rb := &rollback.Rollback{}
197193
zlog := zerolog.Logger{}
198194
agentID := "1234"
@@ -226,8 +222,7 @@ func TestEnrollWithAgentIDExistingActive_NotReplaceable(t *testing.T) {
226222
}
227223

228224
func TestEnrollWithAgentIDExistingActive_InvalidReplaceToken_Missing(t *testing.T) {
229-
ctx, cancel := context.WithCancel(context.Background())
230-
defer cancel()
225+
ctx := t.Context()
231226
rb := &rollback.Rollback{}
232227
zlog := zerolog.Logger{}
233228
agentID := "1234"
@@ -269,8 +264,7 @@ func TestEnrollWithAgentIDExistingActive_InvalidReplaceToken_Missing(t *testing.
269264
}
270265

271266
func TestEnrollWithAgentIDExistingActive_InvalidReplaceToken_Mismatch(t *testing.T) {
272-
ctx, cancel := context.WithCancel(context.Background())
273-
defer cancel()
267+
ctx := t.Context()
274268
rb := &rollback.Rollback{}
275269
zlog := zerolog.Logger{}
276270
agentID := "1234"
@@ -314,8 +308,7 @@ func TestEnrollWithAgentIDExistingActive_InvalidReplaceToken_Mismatch(t *testing
314308
}
315309

316310
func TestEnrollWithAgentIDExistingActive_WrongPolicy(t *testing.T) {
317-
ctx, cancel := context.WithCancel(context.Background())
318-
defer cancel()
311+
ctx := t.Context()
319312
rb := &rollback.Rollback{}
320313
zlog := zerolog.Logger{}
321314
agentID := "1234"
@@ -359,8 +352,7 @@ func TestEnrollWithAgentIDExistingActive_WrongPolicy(t *testing.T) {
359352
}
360353

361354
func TestEnrollWithAgentIDExistingActive(t *testing.T) {
362-
ctx, cancel := context.WithCancel(context.Background())
363-
defer cancel()
355+
ctx := t.Context()
364356
rb := &rollback.Rollback{}
365357
zlog := zerolog.Logger{}
366358
agentID := "1234"

internal/pkg/api/handleFileDelivery_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ func TestFileDelivery(t *testing.T) {
162162
SeqNo: 1,
163163
Version: 1,
164164
Index: fmt.Sprintf(delivery.FileDataIndexPattern, "endpoint"),
165-
Fields: map[string]interface{}{
166-
file.FieldBaseID: []interface{}{"X"},
167-
file.FieldLast: []interface{}{true},
165+
Fields: map[string]any{
166+
file.FieldBaseID: []any{"X"},
167+
file.FieldLast: []any{true},
168168
},
169169
},
170170
},
@@ -222,18 +222,18 @@ func TestFileDeliveryMultipleChunks(t *testing.T) {
222222
SeqNo: 1,
223223
Version: 1,
224224
Index: fmt.Sprintf(delivery.FileDataIndexPattern, "endpoint"),
225-
Fields: map[string]interface{}{
226-
file.FieldBaseID: []interface{}{"X"},
225+
Fields: map[string]any{
226+
file.FieldBaseID: []any{"X"},
227227
},
228228
},
229229
{
230230
ID: "X.1",
231231
SeqNo: 1,
232232
Version: 1,
233233
Index: fmt.Sprintf(delivery.FileDataIndexPattern, "endpoint"),
234-
Fields: map[string]interface{}{
235-
file.FieldBaseID: []interface{}{"X"},
236-
file.FieldLast: []interface{}{true},
234+
Fields: map[string]any{
235+
file.FieldBaseID: []any{"X"},
236+
file.FieldLast: []any{true},
237237
},
238238
},
239239
},
@@ -301,9 +301,9 @@ func TestFileDeliverySetsHeaders(t *testing.T) {
301301
SeqNo: 1,
302302
Version: 1,
303303
Index: fmt.Sprintf(delivery.FileDataIndexPattern, "endpoint"),
304-
Fields: map[string]interface{}{
305-
file.FieldBaseID: []interface{}{"X"},
306-
file.FieldLast: []interface{}{true},
304+
Fields: map[string]any{
305+
file.FieldBaseID: []any{"X"},
306+
file.FieldLast: []any{true},
307307
},
308308
},
309309
},
@@ -362,9 +362,9 @@ func TestFileDeliverySetsHashWhenPresent(t *testing.T) {
362362
SeqNo: 1,
363363
Version: 1,
364364
Index: fmt.Sprintf(delivery.FileDataIndexPattern, "endpoint"),
365-
Fields: map[string]interface{}{
366-
file.FieldBaseID: []interface{}{"X"},
367-
file.FieldLast: []interface{}{true},
365+
Fields: map[string]any{
366+
file.FieldBaseID: []any{"X"},
367+
file.FieldLast: []any{true},
368368
},
369369
},
370370
},

0 commit comments

Comments
 (0)