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

[8.18](backport #4514) Add unit test for fips metadata attribute in enroll/checkin #4522

Open
wants to merge 1 commit into
base: 8.18
Choose a base branch
from
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
21 changes: 19 additions & 2 deletions internal/pkg/api/handleCheckin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -981,17 +998,17 @@ 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
// we will end up with false positives.
assert.Equal(t, tc.expErr.Error(), err.Error())
assert.ErrorAs(t, err, &tc.expErr)
}
assert.Equal(t, tc.expValid, valid)
})
}
}
21 changes: 15 additions & 6 deletions internal/pkg/api/handleEnroll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
})
}
11 changes: 11 additions & 0 deletions internal/pkg/checkin/bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading