-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Call fleet-server audit/unenroll endpoint on uninstall
Uninstalling a fleet-managed elastic-agent instance will now do a best-effort attempt to notify fleet-server of the agent removal so the agent may not appear as offiline.
- Loading branch information
1 parent
ef69b58
commit bb4d789
Showing
5 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
changelog/fragments/1723675050-Call-fleet-server-audit-unenroll-endpoint-on-uninstall.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Kind can be one of: | ||
# - breaking-change: a change to previously-documented behavior | ||
# - deprecation: functionality that is being removed in a later release | ||
# - bug-fix: fixes a problem in a previous version | ||
# - enhancement: extends functionality but does not break or fix existing behavior | ||
# - feature: new functionality | ||
# - known-issue: problems that we are aware of in a given version | ||
# - security: impacts on the security of a product or a user’s deployment. | ||
# - upgrade: important information for someone upgrading from a prior version | ||
# - other: does not fit into any of the other categories | ||
kind: feature | ||
|
||
# Change summary; a 80ish characters long description of the change. | ||
summary: Call fleet-server audit/unenroll endpoint on uninstall | ||
|
||
# Long description; in case the summary is not enough to describe the change | ||
# this field accommodate a description without length limits. | ||
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment. | ||
description: | | ||
Uninstalling a fleet-managed elastic-agent instance will now do a | ||
best-effort attempt to notify fleet-server of the agent removal so the | ||
agent may not appear as offiline. | ||
# Affected component; a word indicating the component this changeset affects. | ||
component: | ||
|
||
# PR URL; optional; the PR number that added the changeset. | ||
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added. | ||
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number. | ||
# Please provide it if you are adding a fragment for a different PR. | ||
pr: https://github.com/elastic/elastic-agent/pull/5302 | ||
|
||
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of). | ||
# If not present is automatically filled by the tooling with the issue linked to the PR number. | ||
issue: https://github.com/elastic/elastic-agent/issues/484 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package fleetapi | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/elastic/elastic-agent/internal/pkg/agent/errors" | ||
"github.com/elastic/elastic-agent/internal/pkg/fleetapi/client" | ||
) | ||
|
||
// ReqError is an error wrapper to wrap errors with a request. | ||
// These can include validation or marshalling errors that should not be retried. | ||
type ReqError struct { | ||
err error | ||
} | ||
|
||
func (e *ReqError) Error() string { | ||
return e.err.Error() | ||
} | ||
|
||
func (e *ReqError) Unwrap() error { | ||
return e.err | ||
} | ||
|
||
const auditUnenrollPath = "/api/fleet/agents/%s/audit/unenroll" | ||
|
||
type Reason string | ||
|
||
const ( | ||
ReasonUninstall Reason = "uninstall" | ||
) | ||
|
||
type AuditUnenrollRequest struct { | ||
Reason Reason `json:"reason"` | ||
Timestamp time.Time `json:"timestamp"` | ||
} | ||
|
||
// Validate will ensure the timestamp is set and the reason is an allowed value. | ||
func (e *AuditUnenrollRequest) Validate() error { | ||
if e.Timestamp.IsZero() { | ||
return &ReqError{fmt.Errorf("request timestamp not set")} | ||
} | ||
switch e.Reason { | ||
case ReasonUninstall: | ||
default: | ||
return &ReqError{fmt.Errorf("unsupported reason: %s", e.Reason)} | ||
} | ||
return nil | ||
} | ||
|
||
type AuditUnenrollCmd struct { | ||
client client.Sender | ||
info agentInfo | ||
} | ||
|
||
func NewAuditUnenrollCmd(info agentInfo, client client.Sender) *AuditUnenrollCmd { | ||
return &AuditUnenrollCmd{ | ||
client: client, | ||
info: info, | ||
} | ||
} | ||
|
||
// Execute sends the request to fleet-sever and returns the status code response. | ||
// | ||
// the caller must determine if the call succeeded or if it should be retried. | ||
func (e *AuditUnenrollCmd) Execute(ctx context.Context, r *AuditUnenrollRequest) (int, error) { | ||
if err := r.Validate(); err != nil { | ||
return 0, err | ||
} | ||
p, err := json.Marshal(r) | ||
if err != nil { | ||
return 0, &ReqError{err} | ||
} | ||
path := fmt.Sprintf(auditUnenrollPath, e.info.AgentID()) | ||
resp, err := e.client.Send(ctx, http.MethodPost, path, nil, nil, bytes.NewBuffer(p)) | ||
if err != nil { | ||
return 0, errors.New(err, | ||
"fail to notify audit/unenroll on fleet-server", | ||
errors.TypeNetwork, | ||
errors.M(errors.MetaKeyURI, path)) | ||
} | ||
resp.Body.Close() | ||
return resp.StatusCode, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package fleetapi | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/elastic/elastic-agent/internal/pkg/fleetapi/client" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_AuditUnenrollCmd_Execute(t *testing.T) { | ||
const withAPIKey = "secret" | ||
agentInfo := &agentinfo{} | ||
|
||
t.Run("test audit/unenroll roundtrip", withServerWithAuthClient( | ||
func(t *testing.T) *http.ServeMux { | ||
mux := http.NewServeMux() | ||
path := fmt.Sprintf(auditUnenrollPath, agentInfo.AgentID()) | ||
mux.HandleFunc(path, authHandler(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
|
||
decoder := json.NewDecoder(r.Body) | ||
defer r.Body.Close() | ||
request := &AuditUnenrollRequest{} | ||
err := decoder.Decode(&request) | ||
require.NoError(t, err) | ||
require.Equal(t, ReasonUninstall, request.Reason) | ||
}, withAPIKey)) | ||
return mux | ||
}, withAPIKey, | ||
func(t *testing.T, client client.Sender) { | ||
cmd := NewAuditUnenrollCmd(agentInfo, client) | ||
request := &AuditUnenrollRequest{ | ||
Reason: ReasonUninstall, | ||
Timestamp: time.Now(), | ||
} | ||
status, err := cmd.Execute(context.Background(), request) | ||
require.NoError(t, err) | ||
require.Equal(t, http.StatusOK, status) | ||
}, | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters