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

[Elastic Agent] Enroll with Fleet Server #23865

Merged
merged 7 commits into from
Feb 16, 2021
Merged
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 x-pack/elastic-agent/CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@
- Push log level downstream {pull}22815[22815]
- Add metrics collection for Agent {pull}22793[22793]
- Add support for Fleet Server {pull}23736[23736]
- Add support for enrollment with local bootstrap of Fleet Server {pull}23865[23865]
72 changes: 68 additions & 4 deletions x-pack/elastic-agent/pkg/agent/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ package application

import (
"context"
"fmt"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/storage"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/status"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/info"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/upgrade"
Expand All @@ -31,7 +36,7 @@ type upgraderControl interface {
}

// New creates a new Agent and bootstrap the required subsystem.
func New(log *logger.Logger, pathConfigFile string, reexec reexecManager, uc upgraderControl, agentInfo *info.AgentInfo) (Application, error) {
func New(log *logger.Logger, pathConfigFile string, reexec reexecManager, statusCtrl status.Controller, uc upgraderControl, agentInfo *info.AgentInfo) (Application, error) {
// Load configuration from disk to understand in which mode of operation
// we must start the elastic-agent, the mode of operation cannot be changed without restarting the
// elastic-agent.
Expand All @@ -44,14 +49,15 @@ func New(log *logger.Logger, pathConfigFile string, reexec reexecManager, uc upg
return nil, err
}

return createApplication(log, pathConfigFile, rawConfig, reexec, uc, agentInfo)
return createApplication(log, pathConfigFile, rawConfig, reexec, statusCtrl, uc, agentInfo)
}

func createApplication(
log *logger.Logger,
pathConfigFile string,
rawConfig *config.Config,
reexec reexecManager,
statusCtrl status.Controller,
uc upgraderControl,
agentInfo *info.AgentInfo,
) (Application, error) {
Expand All @@ -66,14 +72,72 @@ func createApplication(

if IsStandalone(cfg.Fleet) {
log.Info("Agent is managed locally")
return newLocal(ctx, log, pathConfigFile, rawConfig, reexec, uc, agentInfo)
return newLocal(ctx, log, pathConfigFile, rawConfig, reexec, statusCtrl, uc, agentInfo)
}

// not in standalone; both modes require reading the fleet.yml configuration file
var store storage.Store
store, cfg, err = mergeFleetConfig(rawConfig)

if IsFleetServerBootstrap(cfg.Fleet) {
log.Info("Agent is in Fleet Server bootstrap mode")
return newFleetServerBootstrap(ctx, log, pathConfigFile, rawConfig, statusCtrl, agentInfo)
}

log.Info("Agent is managed by Fleet")
return newManaged(ctx, log, rawConfig, reexec, agentInfo)
return newManaged(ctx, log, store, cfg, rawConfig, reexec, statusCtrl, agentInfo)
}

// IsStandalone decides based on missing of fleet.enabled: true or fleet.{access_token,kibana} will place Elastic Agent into standalone mode.
func IsStandalone(cfg *configuration.FleetAgentConfig) bool {
return cfg == nil || !cfg.Enabled
}

// IsFleetServerBootstrap decides if Elastic Agent is started in bootstrap mode.
func IsFleetServerBootstrap(cfg *configuration.FleetAgentConfig) bool {
return cfg != nil && cfg.Server != nil && cfg.Server.Bootstrap
}

func mergeFleetConfig(rawConfig *config.Config) (storage.Store, *configuration.Configuration, error) {
path := info.AgentConfigFile()
store := storage.NewDiskStore(path)
reader, err := store.Load()
if err != nil {
return store, nil, errors.New(err, "could not initialize config store",
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, path))
}
config, err := config.NewConfigFrom(reader)
if err != nil {
return store, nil, errors.New(err,
fmt.Sprintf("fail to read configuration %s for the elastic-agent", path),
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, path))
}

// merge local configuration and configuration persisted from fleet.
err = rawConfig.Merge(config)
if err != nil {
return store, nil, errors.New(err,
fmt.Sprintf("fail to merge configuration with %s for the elastic-agent", path),
errors.TypeConfig,
errors.M(errors.MetaKeyPath, path))
}

cfg, err := configuration.NewFromConfig(rawConfig)
if err != nil {
return store, nil, errors.New(err,
fmt.Sprintf("fail to unpack configuration from %s", path),
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, path))
}

if err := cfg.Fleet.Valid(); err != nil {
return store, nil, errors.New(err,
"fleet configuration is invalid",
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, path))
}

return store, cfg, nil
}
23 changes: 23 additions & 0 deletions x-pack/elastic-agent/pkg/agent/application/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,26 @@ func createFleetConfigFromEnroll(accessAPIKey string, kbn *kibana.Config) (*conf
}
return cfg, nil
}

func createFleetServerBootstrapConfig(connStr string, policyID string) (*configuration.FleetAgentConfig, error) {
es, err := configuration.ElasticsearchFromConnStr(connStr)
if err != nil {
return nil, err
}
cfg := configuration.DefaultFleetAgentConfig()
cfg.Enabled = true
cfg.Server = &configuration.FleetServerConfig{
Bootstrap: true,
Output: configuration.FleetServerOutputConfig{
Elasticsearch: es,
},
}
if policyID != "" {
cfg.Server.Policy = &configuration.FleetServerPolicyConfig{ID: policyID}
}

if err := cfg.Valid(); err != nil {
return nil, errors.New(err, "invalid enrollment options", errors.TypeConfig)
}
return cfg, nil
}
Loading