From b67442e484e9ab041244efedecdc1dc9427b164e Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Mon, 14 Sep 2020 10:04:47 -0400 Subject: [PATCH] [Elastic Agent] Add fleet.host.id for sending to endpoint. (#21042) * Add fleet.host.id for sending to endpoint. * Add changelog entry. --- x-pack/elastic-agent/CHANGELOG.next.asciidoc | 1 + .../pkg/agent/application/fleet_decorator.go | 10 ++++++++-- .../pkg/agent/application/managed_mode.go | 11 ++++++++++- .../testdata/endpoint_basic-endpoint-security.yml | 2 ++ .../pkg/agent/program/testdata/endpoint_basic.yml | 2 ++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index 938e1ea25ae2..d50f72369cd6 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -19,3 +19,4 @@ - Users of the Docker image can now pass `FLEET_ENROLL_INSECURE=1` to include the `--insecure` flag with the `elastic-agent enroll` command {issue}20312[20312] {pull}20713[20713] - Add support for dynamic inputs with providers and `{{variable|"default"}}` substitution. {pull}20839[20839] - Add support for EQL based condition on inputs {pull}20994[20994] +- Send `fleet.host.id` to Endpoint Security {pull}21042[21042] diff --git a/x-pack/elastic-agent/pkg/agent/application/fleet_decorator.go b/x-pack/elastic-agent/pkg/agent/application/fleet_decorator.go index ebb676cb2cd9..6402b89e7424 100644 --- a/x-pack/elastic-agent/pkg/agent/application/fleet_decorator.go +++ b/x-pack/elastic-agent/pkg/agent/application/fleet_decorator.go @@ -7,12 +7,14 @@ package application import ( "fmt" + "github.com/elastic/go-sysinfo/types" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/transpiler" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" ) -func injectFleet(cfg *config.Config) func(*logger.Logger, *transpiler.AST) error { +func injectFleet(cfg *config.Config, hostInfo types.HostInfo) func(*logger.Logger, *transpiler.AST) error { return func(logger *logger.Logger, rootAst *transpiler.AST) error { config, err := cfg.ToMapStr() if err != nil { @@ -37,7 +39,11 @@ func injectFleet(cfg *config.Config) func(*logger.Logger, *transpiler.AST) error return fmt.Errorf("failed to get agent key from fleet config") } - fleet := transpiler.NewDict([]transpiler.Node{agent, token, kbn}) + host := transpiler.NewKey("host", transpiler.NewDict([]transpiler.Node{ + transpiler.NewKey("id", transpiler.NewStrVal(hostInfo.UniqueID)), + })) + + fleet := transpiler.NewDict([]transpiler.Node{agent, token, kbn, host}) err = transpiler.Insert(rootAst, fleet, "fleet") if err != nil { return err diff --git a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go index 2b5c517d9703..76d9d0bed46b 100644 --- a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go @@ -11,6 +11,8 @@ import ( "net/http" "net/url" + "github.com/elastic/go-sysinfo" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/filters" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/info" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configuration" @@ -113,6 +115,13 @@ func newManaged( errors.M(errors.MetaKeyURI, cfg.Fleet.Kibana.Host)) } + sysInfo, err := sysinfo.Host() + if err != nil { + return nil, errors.New(err, + "fail to get system information", + errors.TypeUnexpected) + } + managedApplication := &Managed{ log: log, agentInfo: agentInfo, @@ -160,7 +169,7 @@ func newManaged( router, &configModifiers{ Decorators: []decoratorFunc{injectMonitoring}, - Filters: []filterFunc{filters.StreamChecker, injectFleet(config)}, + Filters: []filterFunc{filters.StreamChecker, injectFleet(config, sysInfo.Info())}, }, monitor, ) diff --git a/x-pack/elastic-agent/pkg/agent/program/testdata/endpoint_basic-endpoint-security.yml b/x-pack/elastic-agent/pkg/agent/program/testdata/endpoint_basic-endpoint-security.yml index f0505a5f3237..b77a83633aef 100644 --- a/x-pack/elastic-agent/pkg/agent/program/testdata/endpoint_basic-endpoint-security.yml +++ b/x-pack/elastic-agent/pkg/agent/program/testdata/endpoint_basic-endpoint-security.yml @@ -2,6 +2,8 @@ revision: 5 fleet: agent: id: fleet-agent-id + host: + id: host-agent-id api: access_api_key: VuaCfGcBCdbkQm-e5aOx:ui2lp2axTNmsyakw9tvNnw kibana: diff --git a/x-pack/elastic-agent/pkg/agent/program/testdata/endpoint_basic.yml b/x-pack/elastic-agent/pkg/agent/program/testdata/endpoint_basic.yml index 832dc4704de9..9eee9b3a01db 100644 --- a/x-pack/elastic-agent/pkg/agent/program/testdata/endpoint_basic.yml +++ b/x-pack/elastic-agent/pkg/agent/program/testdata/endpoint_basic.yml @@ -3,6 +3,8 @@ name: Endpoint Host fleet: agent: id: fleet-agent-id + host: + id: host-agent-id access_api_key: VuaCfGcBCdbkQm-e5aOx:ui2lp2axTNmsyakw9tvNnw kibana: protocol: https