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

fix audit unenroll #4036

Closed
wants to merge 3 commits into from
Closed
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 internal/pkg/api/handleAudit.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (audit *AuditT) markUnenroll(ctx context.Context, zlog zerolog.Logger, req

now := time.Now().UTC().Format(time.RFC3339)
doc := bulk.UpdateFields{
dl.FieldActive: false,
dl.FieldUnenrolledAt: now,
dl.FieldUpdatedAt: now,
dl.FieldAuditUnenrolledTime: req.Timestamp,
Expand Down
39 changes: 7 additions & 32 deletions internal/pkg/server/fleet_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1485,17 +1485,6 @@ func Test_SmokeTest_AuditUnenroll(t *testing.T) {
require.Equal(t, http.StatusOK, res.StatusCode)
res.Body.Close()

t.Log("Orphaned can replace uninstall")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to test/support this scenario.
It's important for Endpoint to be able to signal that it is orphaned even if the installed agent is removed

req, err = http.NewRequestWithContext(ctx, "POST", srv.baseURL()+"/api/fleet/agents/"+id+"/audit/unenroll", strings.NewReader(orphanBody))
require.NoError(t, err)
req.Header.Set("Authorization", "ApiKey "+key)
req.Header.Set("User-Agent", "elastic agent "+serverVersion)
req.Header.Set("Content-Type", "application/json")
res, err = cli.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, res.StatusCode)
res.Body.Close()

t.Log("Use of audit/unenroll once orphaned should fail.")
req, err = http.NewRequestWithContext(ctx, "POST", srv.baseURL()+"/api/fleet/agents/"+id+"/audit/unenroll", strings.NewReader(orphanBody))
require.NoError(t, err)
Expand All @@ -1504,26 +1493,9 @@ func Test_SmokeTest_AuditUnenroll(t *testing.T) {
req.Header.Set("Content-Type", "application/json")
res, err = cli.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusConflict, res.StatusCode)
require.Equal(t, http.StatusUnauthorized, res.StatusCode)
res.Body.Close()

t.Logf("Fake a checkin for agent %s", id)
req, err = http.NewRequestWithContext(ctx, "POST", srv.baseURL()+"/api/fleet/agents/"+id+"/checkin", strings.NewReader(checkinBody))
require.NoError(t, err)
req.Header.Set("Authorization", "ApiKey "+key)
req.Header.Set("User-Agent", "elastic agent "+serverVersion)
req.Header.Set("Content-Type", "application/json")
res, err = cli.Do(req)
require.NoError(t, err)

require.Equal(t, http.StatusOK, res.StatusCode)
t.Log("Checkin successful, verify body")
p, _ := io.ReadAll(res.Body)
res.Body.Close()
var obj map[string]interface{}
err = json.Unmarshal(p, &obj)
require.NoError(t, err)

require.Eventually(t, func() bool {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:9200/.fleet-agents/_doc/"+id, nil)
require.NoError(t, err)
Expand All @@ -1543,9 +1515,12 @@ func Test_SmokeTest_AuditUnenroll(t *testing.T) {
require.Truef(t, ok, "expected to find _source in: %v", tmp)
obj, ok := o.(map[string]interface{})
require.Truef(t, ok, "expected _source to be an object, was: %T", o)
_, ok = obj["audit_unenrolled_reason"]
return !ok
}, time.Second*20, time.Second, "agent document should not have audit_unenrolled_reason attribute")
activeStr, ok := obj["active"]
require.Truef(t, ok, "expected active to exist")
active, ok := activeStr.(bool)
require.Truef(t, ok, "expected active to be a bool")
return !active
}, time.Second*20, time.Second, "agent document should be inactive")
cancel()
srv.waitExit() //nolint:errcheck // test case
}
Loading