Skip to content
Open
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
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ Guiding principles and expectations: [CONTRIBUTING.md](./CONTRIBUTING.md).
- **Update docs and tests** when behavior, configuration, or operator-facing workflow changes.
- **Make non-obvious intent clear** through naming, structure, or brief “why” comments when needed.
- **Formatting:** All go files must be formatted with `go fmt`, and imports must be ordereed with `goimports` which can be done with the `mage check:imports` target.
- **Go Fix:** All .go files must be updated with `go fix`, this can be done with the `mage check:fix` target.
- **Changelog:** For notable changes, add a fragment using **[elastic-agent-changelog-tool](https://github.com/elastic/elastic-agent-changelog-tool)**. Typical usage: `elastic-agent-changelog-tool new "$TITLE"` (see the tool’s [usage docs](https://github.com/elastic/elastic-agent-changelog-tool/blob/main/docs/usage.md)). PRs may use the **`skip-changelog`** label when appropriate; see `changelog/` for examples.
- **`go.mod` / NOTICE:** If you change **`go.mod`** or add/update Go dependencies, regenerate **`NOTICE.txt`** and **`NOTICE-fips.txt`** with `mage check:notice`
- **Before opening a PR:** `mage check:all` and `mage test:unit` must pass at minimum; integration or E2E tests must also pass when behavior depends on Elasticsearch or full HTTP flows (see **Testing** above).
Expand Down
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
28 changes: 14 additions & 14 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 @@ -276,7 +276,7 @@ func TestHandleAckEvents(t *testing.T) {
}},
res: newAckResponse(false, []AckResponseItem{{
Status: http.StatusOK,
Message: ptr(http.StatusText(http.StatusOK)),
Message: new(http.StatusText(http.StatusOK)),
}}),
bulker: func(t *testing.T) *ftesting.MockBulk {
return ftesting.NewMockBulk()
Expand All @@ -291,7 +291,7 @@ func TestHandleAckEvents(t *testing.T) {
}},
res: newAckResponse(false, []AckResponseItem{{
Status: http.StatusOK,
Message: ptr(http.StatusText(http.StatusOK)),
Message: new(http.StatusText(http.StatusOK)),
}}),
bulker: func(t *testing.T) *ftesting.MockBulk {
m := ftesting.NewMockBulk()
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestHandleAckEvents(t *testing.T) {
}},
res: newAckResponse(true, []AckResponseItem{{
Status: http.StatusServiceUnavailable,
Message: ptr(http.StatusText(http.StatusServiceUnavailable)),
Message: new(http.StatusText(http.StatusServiceUnavailable)),
}}),
bulker: func(t *testing.T) *ftesting.MockBulk {
m := ftesting.NewMockBulk()
Expand Down Expand Up @@ -405,27 +405,27 @@ func TestHandleAckEvents(t *testing.T) {
res: newAckResponse(true, []AckResponseItem{
{
Status: http.StatusOK,
Message: ptr(http.StatusText(http.StatusOK)),
Message: new(http.StatusText(http.StatusOK)),
},
{
Status: http.StatusOK,
Message: ptr(http.StatusText(http.StatusOK)),
Message: new(http.StatusText(http.StatusOK)),
},
{
Status: http.StatusNotFound,
Message: ptr(http.StatusText(http.StatusNotFound)),
Message: new(http.StatusText(http.StatusNotFound)),
},
{
Status: http.StatusOK,
Message: ptr(http.StatusText(http.StatusOK)),
Message: new(http.StatusText(http.StatusOK)),
},
{
Status: http.StatusOK,
Message: ptr(http.StatusText(http.StatusOK)),
Message: new(http.StatusText(http.StatusOK)),
},
{
Status: http.StatusOK,
Message: ptr(http.StatusText(http.StatusOK)),
Message: new(http.StatusText(http.StatusOK)),
},
}),
bulker: func(t *testing.T) *ftesting.MockBulk {
Expand Down Expand Up @@ -468,7 +468,7 @@ func TestHandleAckEvents(t *testing.T) {
res: newAckResponse(false, []AckResponseItem{
{
Status: http.StatusOK,
Message: ptr(http.StatusText(http.StatusOK)),
Message: new(http.StatusText(http.StatusOK)),
},
}),
bulker: func(t *testing.T) *ftesting.MockBulk {
Expand All @@ -495,7 +495,7 @@ func TestHandleAckEvents(t *testing.T) {
res: newAckResponse(false, []AckResponseItem{
{
Status: http.StatusOK,
Message: ptr(http.StatusText(http.StatusOK)),
Message: new(http.StatusText(http.StatusOK)),
},
}),
bulker: func(t *testing.T) *ftesting.MockBulk {
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 @@ -228,13 +228,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 @@ -691,12 +685,7 @@ func (ct *CheckinT) writeResponse(zlog zerolog.Logger, w http.ResponseWriter, r
// acceptsEncoding reports whether the request includes the passed encoding.
// Only an exact match is checked as that is all the agent will send.
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 @@ -1057,7 +1046,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 @@ -1069,7 +1058,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 @@ -358,8 +358,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 @@ -415,7 +414,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 @@ -1092,7 +1091,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
Loading
Loading