Skip to content

Commit f9231ea

Browse files
feat: utilise unix.Exec to re-exec elastic-agent when file capabilities are updated
1 parent 6f9adb0 commit f9231ea

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

internal/pkg/agent/cmd/container.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ func logInfo(streams *cli.IOStreams, a ...interface{}) {
165165
}
166166

167167
func logContainerCmd(streams *cli.IOStreams) error {
168-
cmd, err := initContainer(streams)
168+
shouldExit, err := initContainer(streams)
169169
if err != nil {
170170
return err
171171
}
172-
if cmd != nil {
173-
return cmd.Run()
172+
if shouldExit {
173+
return nil
174174
}
175175

176176
logsPath := envWithDefault("", "LOGS_PATH")

internal/pkg/agent/cmd/container_init_linux.go

+10-14
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
"fmt"
1010
"io/fs"
1111
"os"
12-
"os/exec"
1312
"path/filepath"
1413
"strings"
1514
"syscall"
1615

16+
"golang.org/x/sys/unix"
1717
"kernel.org/pub/linux/libs/security/libcap/cap"
1818

1919
"github.com/elastic/elastic-agent/internal/pkg/cli"
@@ -33,42 +33,38 @@ var (
3333
// - chown all agent-related paths if DAC_OVERRIDE capability is not in the Effective set
3434
// If new binary capabilities are set then the returned cmd will be not nil. Note that it is up to caller to invoke
3535
// the returned cmd and spawn an agent instance with all the capabilities.
36-
func initContainer(streams *cli.IOStreams) (*exec.Cmd, error) {
36+
func initContainer(streams *cli.IOStreams) (shouldExit bool, err error) {
3737
isRoot, _ := utils.HasRoot()
3838
if !skipFileCapabilities && !isRoot {
3939
executable, err := os.Executable()
4040
if err != nil {
41-
return nil, err
41+
return true, err
4242
}
4343

4444
logInfo(streams, "agent container initialisation - file capabilities")
4545
updated, err := updateFileCapsFromBoundingSet(executable)
4646
if err != nil {
47-
return nil, err
47+
return true, err
4848
}
4949

5050
if updated {
5151
// new capabilities were added thus we need to re-exec agent to pick them up
52-
var args []string
52+
args := []string{filepath.Base(executable)}
5353
if len(os.Args) > 1 {
5454
args = append(args, os.Args[1:]...)
5555
}
5656
// add skipFileCapabilitiesFlag flag to skip reapplying the file capabilities
5757
args = append(args, fmt.Sprintf("--%s", skipFileCapabilitiesFlag))
5858

59-
cmd := exec.Command(executable, args...)
60-
cmd.Stdout = os.Stdout
61-
cmd.Stderr = os.Stderr
62-
cmd.Env = os.Environ()
63-
return cmd, nil
59+
return true, unix.Exec(executable, args, os.Environ())
6460
}
6561
}
6662

6763
if !isRoot {
6864
// if we are not root, we need to raise the ambient capabilities
6965
logInfo(streams, "agent container initialisation - ambient capabilities")
7066
if err := raiseAmbientCapabilities(); err != nil {
71-
return nil, err
67+
return true, err
7268
}
7369
}
7470

@@ -78,18 +74,18 @@ func initContainer(streams *cli.IOStreams) (*exec.Cmd, error) {
7874
procSet := capProc()
7975
hasOverride, err := procSet.GetFlag(cap.Effective, cap.DAC_OVERRIDE)
8076
if err != nil {
81-
return nil, err
77+
return true, err
8278
}
8379
if !hasOverride {
8480
// we need to chown all paths
8581
logInfo(streams, "agent container initialisation - chown paths")
8682

8783
if err = chownPaths(); err != nil {
88-
return nil, err
84+
return true, err
8985
}
9086
}
9187

92-
return nil, nil
88+
return false, nil
9389
}
9490

9591
// raiseAmbientCapabilities will attempt to raise all capabilities present in the Effective set of the running process

internal/pkg/agent/cmd/container_init_other.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
package cmd
88

99
import (
10-
"os/exec"
11-
1210
"github.com/elastic/elastic-agent/internal/pkg/cli"
1311
)
1412

15-
func initContainer(_ *cli.IOStreams) (cmd *exec.Cmd, err error) {
16-
return nil, nil
13+
func initContainer(streams *cli.IOStreams) (shouldExit bool, err error) {
14+
return false, nil
1715
}

0 commit comments

Comments
 (0)