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] Handle migration of agent_info to agent in Fleet configuration #19488

Closed
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 x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@
- Agent now runs the GRPC server and spawned application connect by to Agent {pull}18973[18973]
- Rename input.type logs to logfile {pull}19360[19360]
- Agent now installs/uninstalls Elastic Endpoint {pull}19248[19248]
- Handle upgrade of Fleet.yml format transparently {pull}19488[19488]
30 changes: 25 additions & 5 deletions x-pack/elastic-agent/pkg/agent/application/info/agent_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import (

// defaultAgentConfigFile is a name of file used to store agent information
const defaultAgentConfigFile = "fleet.yml"
const agentInfoKey = "agent"
const agentInfoKey = "agent_info"
const agentKey = "agent"

// defaultAgentActionStoreFile is the file that will contains the action that can be replayed after restart.
const defaultAgentActionStoreFile = "action_store.yml"

type persistentAgentInfo struct {
ID string `json:"id" yaml:"id" config:"id"`
ID string `json:"id" yaml:"id" config:"id"`
CapID string `json:"ID,omitempty" yaml:"ID,omitempty" config:"ID"`
}

type ioStore interface {
Expand Down Expand Up @@ -105,9 +107,13 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) {
errors.TypeFilesystem)
}

agentInfoSubMap, found := configMap[agentInfoKey]
// handle migration of `agent_info` to `agent` in config
agentInfoSubMap, found := configMap[agentKey]
if !found {
return &persistentAgentInfo{}, nil
agentInfoSubMap, found = configMap[agentInfoKey]
if !found {
return &persistentAgentInfo{}, nil
}
}

cc, err := config.NewConfigFrom(agentInfoSubMap)
Expand All @@ -120,6 +126,12 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) {
return nil, errors.New(err, "failed to unpack stored config to map")
}

// handle migration of agent ID from `ID` to `id` in YAML.
if pid.ID == "" && pid.CapID != "" {
pid.ID = pid.CapID
pid.CapID = ""
}

return pid, nil
}

Expand All @@ -146,7 +158,15 @@ func updateAgentInfo(s ioStore, agentInfo *persistentAgentInfo) error {
return errors.New(err, "failed to unpack stored config to map")
}

configMap[agentInfoKey] = agentInfo
// handle migration of agent ID from `ID` to `id` in YAML.
if agentInfo.ID == "" && agentInfo.CapID != "" {
agentInfo.ID = agentInfo.CapID
agentInfo.CapID = ""
}

// set `agent` and remove old `agent_info`
configMap[agentKey] = agentInfo
delete(configMap, agentInfoKey)

r, err := yamlToReader(configMap)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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 info

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/storage"
)

func TestLoadAgentInfo(t *testing.T) {
defer cleanupFile()

agentInfo, err := loadAgentInfo(false)
require.NoError(t, err)
assert.NotEqual(t, "", agentInfo.ID)
assert.Equal(t, "", agentInfo.CapID)
}

func TestLoadAgentInfoForce(t *testing.T) {
defer cleanupFile()

agentInfo, err := loadAgentInfo(false)
require.NoError(t, err)
assert.NotEqual(t, "", agentInfo.ID)
assert.Equal(t, "", agentInfo.CapID)

agentInfo2, err := loadAgentInfo(true)
require.NoError(t, err)
assert.NotEqual(t, agentInfo.ID, agentInfo2.ID)
}

func TestLoadAgentInfoUpgrade(t *testing.T) {
defer cleanupFile()

// write an old agent info
agentConfigFile := AgentConfigFile()
s := storage.NewEncryptedDiskStore(agentConfigFile, []byte(""))
id, err := generateAgentID()
require.NoError(t, err)
err = writeOldAgentInfo(s, id)
require.NoError(t, err)

// load agent info, will handle upgrade
agentInfo, err := loadAgentInfo(false)
require.NoError(t, err)
assert.Equal(t, id, agentInfo.ID)
assert.Equal(t, "", agentInfo.CapID)
}

func cleanupFile() {
os.Remove(AgentConfigFile())
}

func writeOldAgentInfo(s ioStore, id string) error {
configMap := make(map[string]interface{})
configMap[agentInfoKey] = struct {
ID string `json:"ID" yaml:"ID" config:"ID"`
}{
ID: id,
}

r, err := yamlToReader(configMap)
if err != nil {
return err
}

return s.Save(r)
}