Skip to content

Commit 84792f7

Browse files
fix: don't override log level from state for standalone agents (#8784) (#8858)
* fix: don't override log level from state for standalone agents * doc: add changelog fragment * fix: add unit test for NewAgentInfoWithLog * fix: derive unprivileged from utils.HasRoot (cherry picked from commit e8f759b) Co-authored-by: Panos Koutsovasilis <[email protected]>
1 parent d4d6c65 commit 84792f7

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: bug-fix
2+
summary: Ensure standalone Elastic Agent uses log level from configuration instead of persisted state
3+
component: elastic-agent
4+
pr: https://github.com/elastic/elastic-agent/pull/8784
5+
issue: https://github.com/elastic/elastic-agent/issues/8137

internal/pkg/agent/application/info/agent_info.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,17 @@ type AgentInfo struct {
6060
esHeaders map[string]string
6161
}
6262

63+
// for unit testing
64+
var doLoadAgentInfoWithBackoff = loadAgentInfoWithBackoff
65+
6366
// NewAgentInfoWithLog creates a new agent information.
6467
// In case when agent ID was already created it returns,
6568
// this created ID otherwise it generates
6669
// new unique identifier for agent.
6770
// If agent config file does not exist it gets created.
6871
// Initiates log level to predefined value.
6972
func NewAgentInfoWithLog(ctx context.Context, level string, createAgentID bool) (*AgentInfo, error) {
70-
agentInfo, isStandalone, err := loadAgentInfoWithBackoff(ctx, false, level, createAgentID)
73+
agentInfo, isStandalone, err := doLoadAgentInfoWithBackoff(ctx, false, level, createAgentID)
7174
if err != nil {
7275
return nil, err
7376
}
@@ -76,9 +79,13 @@ func NewAgentInfoWithLog(ctx context.Context, level string, createAgentID bool)
7679
return nil, fmt.Errorf("failed to determine root/Administrator: %w", err)
7780
}
7881

82+
if !isStandalone {
83+
level = agentInfo.LogLevel
84+
}
85+
7986
return &AgentInfo{
8087
agentID: agentInfo.ID,
81-
logLevel: agentInfo.LogLevel,
88+
logLevel: level,
8289
unprivileged: !isRoot,
8390
esHeaders: agentInfo.Headers,
8491
isStandalone: isStandalone,
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License 2.0;
3+
// you may not use this file except in compliance with the Elastic License 2.0.
4+
5+
package info
6+
7+
import (
8+
"context"
9+
"testing"
10+
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/elastic/elastic-agent/pkg/utils"
14+
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func TestNewAgentInfoWithLog(t *testing.T) {
19+
hasRoot, err := utils.HasRoot()
20+
require.NoError(t, err, "failed to check for root")
21+
22+
for _, tc := range []struct {
23+
name string
24+
levelFromConfig string
25+
isStandalone bool
26+
persistentAgentInfo *persistentAgentInfo
27+
expected *AgentInfo
28+
}{
29+
{
30+
name: "standalone agent",
31+
levelFromConfig: "debug",
32+
isStandalone: true,
33+
persistentAgentInfo: &persistentAgentInfo{
34+
ID: "testID",
35+
Headers: nil,
36+
LogLevel: "info",
37+
MonitoringHTTP: nil,
38+
},
39+
expected: &AgentInfo{
40+
agentID: "testID",
41+
logLevel: "debug",
42+
unprivileged: !hasRoot,
43+
esHeaders: nil,
44+
isStandalone: true,
45+
},
46+
},
47+
{
48+
name: "fleet managed agent",
49+
levelFromConfig: "debug",
50+
isStandalone: false,
51+
persistentAgentInfo: &persistentAgentInfo{
52+
ID: "testID",
53+
Headers: nil,
54+
LogLevel: "info",
55+
MonitoringHTTP: nil,
56+
},
57+
expected: &AgentInfo{
58+
agentID: "testID",
59+
logLevel: "info",
60+
unprivileged: !hasRoot,
61+
esHeaders: nil,
62+
isStandalone: false,
63+
},
64+
},
65+
} {
66+
t.Run(tc.name, func(t *testing.T) {
67+
prevDoLoadAgentInfoWithBackoff := doLoadAgentInfoWithBackoff
68+
defer func() {
69+
doLoadAgentInfoWithBackoff = prevDoLoadAgentInfoWithBackoff
70+
}()
71+
doLoadAgentInfoWithBackoff = func(ctx context.Context, forceUpdate bool, logLevel string, createAgentID bool) (*persistentAgentInfo, bool, error) {
72+
return tc.persistentAgentInfo, tc.isStandalone, nil
73+
}
74+
75+
ai, err := NewAgentInfoWithLog(context.Background(), tc.levelFromConfig, true)
76+
assert.NoError(t, err, "could not create agent info")
77+
assert.Equal(t, tc.expected, ai, "agent info does not match")
78+
})
79+
}
80+
}

0 commit comments

Comments
 (0)