From d1a876ab9aed2a1e31a1c538840826aa5aed64b1 Mon Sep 17 00:00:00 2001 From: Michel Laterman <82832767+michel-laterman@users.noreply.github.com> Date: Wed, 26 Feb 2025 08:32:59 -0800 Subject: [PATCH] Add unit test for fips metadata attribute in enroll/checkin (#4514) (cherry picked from commit ba68a24383abfa328cce0cc3eddce1b73b497d8a) --- internal/pkg/api/handleCheckin_test.go | 21 +++++++++++++++++++-- internal/pkg/api/handleEnroll_test.go | 21 +++++++++++++++------ internal/pkg/checkin/bulk_test.go | 11 +++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/internal/pkg/api/handleCheckin_test.go b/internal/pkg/api/handleCheckin_test.go index f39926eaf..cc5652f2d 100644 --- a/internal/pkg/api/handleCheckin_test.go +++ b/internal/pkg/api/handleCheckin_test.go @@ -973,6 +973,23 @@ func TestValidateCheckinRequest(t *testing.T) { }, expValid: validatedCheckin{}, }, + { + name: "local metadata has fips attribute", + req: &http.Request{ + Body: io.NopCloser(strings.NewReader(`{"status": "online", "message": "test message", "local_metadata": {"elastic": {"agent": {"id": "testid", "fips": true}}}}`)), + }, + expErr: nil, + cfg: &config.Server{ + Limits: config.ServerLimits{ + CheckinLimit: config.Limit{ + MaxBody: 0, + }, + }, + }, + expValid: validatedCheckin{ + rawMeta: []byte(`{"elastic": {"agent": {"id": "testid", "fips": true}}}`), + }, + }, } for _, tc := range tests { @@ -981,9 +998,10 @@ func TestValidateCheckinRequest(t *testing.T) { assert.NoError(t, err) wr := httptest.NewRecorder() logger := testlog.SetLogger(t) - valid, err := checkin.validateRequest(logger, wr, tc.req, time.Time{}, nil) + valid, err := checkin.validateRequest(logger, wr, tc.req, time.Time{}, &model.Agent{LocalMetadata: json.RawMessage(`{}`)}) if tc.expErr == nil { assert.NoError(t, err) + assert.Equal(t, tc.expValid.rawMeta, valid.rawMeta) } else { // Asserting error messages prior to ErrorAs becuase ErrorAs modifies // the target error. If we assert error messages after calling ErrorAs @@ -991,7 +1009,6 @@ func TestValidateCheckinRequest(t *testing.T) { assert.Equal(t, tc.expErr.Error(), err.Error()) assert.ErrorAs(t, err, &tc.expErr) } - assert.Equal(t, tc.expValid, valid) }) } } diff --git a/internal/pkg/api/handleEnroll_test.go b/internal/pkg/api/handleEnroll_test.go index 7094ecdfe..1580c888e 100644 --- a/internal/pkg/api/handleEnroll_test.go +++ b/internal/pkg/api/handleEnroll_test.go @@ -13,6 +13,10 @@ import ( "strings" "testing" + "github.com/rs/zerolog" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/elastic/fleet-server/v7/internal/pkg/apikey" "github.com/elastic/fleet-server/v7/internal/pkg/bulk" "github.com/elastic/fleet-server/v7/internal/pkg/cache" @@ -21,9 +25,6 @@ import ( "github.com/elastic/fleet-server/v7/internal/pkg/model" "github.com/elastic/fleet-server/v7/internal/pkg/rollback" ftesting "github.com/elastic/fleet-server/v7/internal/pkg/testing" - "github.com/rs/zerolog" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" ) func TestRemoveDuplicateStr(t *testing.T) { @@ -250,7 +251,15 @@ func TestEnrollerT_retrieveStaticTokenEnrollmentToken(t *testing.T) { } func TestValidateEnrollRequest(t *testing.T) { - req, err := validateRequest(context.Background(), strings.NewReader("not a json")) - assert.Equal(t, "Bad request: unable to decode enroll request", err.Error()) - assert.Nil(t, req) + t.Run("invalid json", func(t *testing.T) { + req, err := validateRequest(context.Background(), strings.NewReader("not a json")) + assert.Equal(t, "Bad request: unable to decode enroll request", err.Error()) + assert.Nil(t, req) + }) + t.Run("fips attribute in local metadata", func(t *testing.T) { + req, err := validateRequest(context.Background(), strings.NewReader(`{"type": "PERMANENT", "metadata": {"local": {"elastic": {"agent": {"fips": true, "snapshot": false}}}}}`)) + assert.NoError(t, err) + assert.Equal(t, PERMANENT, req.Type) + assert.Equal(t, json.RawMessage(`{"elastic": {"agent": {"fips": true, "snapshot": false}}}`), req.Metadata.Local) + }) } diff --git a/internal/pkg/checkin/bulk_test.go b/internal/pkg/checkin/bulk_test.go index 8597c2306..1772f5eef 100644 --- a/internal/pkg/checkin/bulk_test.go +++ b/internal/pkg/checkin/bulk_test.go @@ -107,6 +107,17 @@ func TestBulkSimple(t *testing.T) { "", nil, }, + { + "has meta with fips attribute", + "metaCaseID", + "online", + "message", + []byte(`{"fips":true,"snapshot":false}`), + nil, + nil, + "", + nil, + }, { "Singled field case", "singleFieldId",