Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 56712a4

Browse files
committedJun 13, 2024··
feat: implement container initialisation that chowns related paths and raises capabilities
1 parent 5927401 commit 56712a4

File tree

7 files changed

+499
-0
lines changed

7 files changed

+499
-0
lines changed
 

‎dev-tools/packaging/templates/docker/Dockerfile.elastic-agent.tmpl

+3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ RUN mkdir /app && \
144144
{{- end }}
145145

146146
# Keep this after any chown command, chown resets any applied capabilities
147+
RUN setcap cap_setfcap,cap_chown=ep {{ $beatHome }}/data/elastic-agent-{{ commit_short }}/elastic-agent
147148
RUN setcap cap_net_raw,cap_setuid+p {{ $beatHome }}/data/elastic-agent-{{ commit_short }}/components/heartbeat && \
148149
{{- if .linux_capabilities }}
149150
# Since the beat is stored at the other end of a symlink we must follow the symlink first
@@ -216,6 +217,8 @@ RUN for iter in {1..10}; do \
216217
(exit $exit_code)
217218

218219
{{- end }}
220+
# root group no more for elastic-agent user
221+
RUN gpasswd --delete {{ .user }} root
219222
USER {{ .user }}
220223

221224

‎go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ require (
8282
k8s.io/apimachinery v0.29.5
8383
k8s.io/client-go v0.29.5
8484
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
85+
kernel.org/pub/linux/libs/security/libcap/cap v1.2.70
8586
)
8687

8788
require (
@@ -276,6 +277,7 @@ require (
276277
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
277278
howett.net/plist v1.0.1 // indirect
278279
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
280+
kernel.org/pub/linux/libs/security/libcap/psx v1.2.70 // indirect
279281
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
280282
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
281283
sigs.k8s.io/yaml v1.3.0 // indirect

‎go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -3113,6 +3113,10 @@ k8s.io/utils v0.0.0-20221107191617-1a15be271d1d/go.mod h1:OLgZIPagt7ERELqWJFomSt
31133113
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
31143114
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
31153115
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
3116+
kernel.org/pub/linux/libs/security/libcap/cap v1.2.70 h1:QnLPkuDWWbD5C+3DUA2IUXai5TK6w2zff+MAGccqdsw=
3117+
kernel.org/pub/linux/libs/security/libcap/cap v1.2.70/go.mod h1:/iBwcj9nbLejQitYvUm9caurITQ6WyNHibJk6Q9fiS4=
3118+
kernel.org/pub/linux/libs/security/libcap/psx v1.2.70 h1:HsB2G/rEQiYyo1bGoQqHZ/Bvd6x1rERQTNdPr1FyWjI=
3119+
kernel.org/pub/linux/libs/security/libcap/psx v1.2.70/go.mod h1:+l6Ee2F59XiJ2I6WR5ObpC1utCQJZ/VLsEbQCD8RG24=
31163120
oras.land/oras-go v1.2.2/go.mod h1:Apa81sKoZPpP7CDciE006tSZ0x3Q3+dOoBcMZ/aNxvw=
31173121
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
31183122
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

‎internal/pkg/agent/cmd/container.go

+16
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const (
4343
defaultRequestRetrySleep = "1s" // sleep 1 sec between retries for HTTP requests
4444
defaultMaxRequestRetries = "30" // maximum number of retries for HTTP requests
4545
defaultStateDirectory = "/usr/share/elastic-agent/state" // directory that will hold the state data
46+
agentBaseDirectory = "/usr/share/elastic-agent" // directory that holds all elastic-agent related files
47+
48+
skipFileCapabilitiesFlag = "skip-file-capabilities"
4649

4750
logsPathPerms = 0775
4851
)
@@ -51,6 +54,8 @@ var (
5154
// Used to strip the appended ({uuid}) from the name of an enrollment token. This makes much easier for
5255
// a container to reference a token by name, without having to know what the generated UUID is for that name.
5356
tokenNameStrip = regexp.MustCompile(`\s\([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\)$`)
57+
58+
skipFileCapabilities bool
5459
)
5560

5661
func newContainerCommand(_ []string, streams *cli.IOStreams) *cobra.Command {
@@ -145,6 +150,9 @@ occurs on every start of the container set FLEET_FORCE to 1.
145150
}
146151
},
147152
}
153+
154+
cmd.Flags().BoolVar(&skipFileCapabilities, skipFileCapabilitiesFlag, false, "")
155+
148156
return &cmd
149157
}
150158

@@ -157,6 +165,14 @@ func logInfo(streams *cli.IOStreams, a ...interface{}) {
157165
}
158166

159167
func logContainerCmd(streams *cli.IOStreams) error {
168+
cmd, err := initContainer(streams)
169+
if err != nil {
170+
return err
171+
}
172+
if cmd != nil {
173+
return cmd.Run()
174+
}
175+
160176
logsPath := envWithDefault("", "LOGS_PATH")
161177
if logsPath != "" {
162178
// log this entire command to a file as well as to the passed streams

0 commit comments

Comments
 (0)
Please sign in to comment.