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

Extend agent container initialisation logic #4925

Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
56712a4
feat: implement container initialisation that chowns related paths an…
pkoutsovasilis Jun 13, 2024
bcc9f18
doc: add changelog fragment
pkoutsovasilis Jun 13, 2024
61763a8
fix: update NOTICE.txt
pkoutsovasilis Jun 13, 2024
de2a26f
fix: typo in container_init_test.go
pkoutsovasilis Jun 13, 2024
6f9adb0
feat: add more unit-tests to increase coverage
pkoutsovasilis Jun 13, 2024
f9231ea
feat: utilise unix.Exec to re-exec elastic-agent when file capabiliti…
pkoutsovasilis Jun 13, 2024
f62c056
fix: handle err from utils.HasRoot
pkoutsovasilis Jun 13, 2024
dc53f87
fix: refactor skip-file-capabilities flag
pkoutsovasilis Jun 13, 2024
84b98e1
fix: add cap_setpcap at elastic-agent binary
pkoutsovasilis Jun 17, 2024
cc6f8df
feat: make npm install work with elastic-agent user
pkoutsovasilis Jun 18, 2024
77bbf4c
fix: remove --skip-file-capabilities flag
pkoutsovasilis Jun 18, 2024
b61c1db
fix: allow CAP_CHOWN in Ambient set
pkoutsovasilis Jun 18, 2024
dae5b91
fix: remove redundant comment from Dockerfile.elastic-agent.tmpl
pkoutsovasilis Jun 18, 2024
1b47337
Merge branch 'main' into pkoutsovasilis/container_capabilities_chown
pkoutsovasilis Jun 18, 2024
0de171b
Merge remote-tracking branch 'origin/main' into pkoutsovasilis/contai…
pkoutsovasilis Jun 28, 2024
839ae37
fix: remove IDE auto-corrections from NOTICE.txt
pkoutsovasilis Jun 28, 2024
d38cbf6
Merge remote-tracking branch 'refs/remotes/origin/main' into pkoutsov…
pkoutsovasilis Jul 13, 2024
7611317
chore: go mod tidy after merge
pkoutsovasilis Jul 13, 2024
5d0fccf
feat: refactor to eliminate disrupting effects
pkoutsovasilis Jul 15, 2024
164d938
Merge remote-tracking branch 'refs/remotes/origin/main' into pkoutsov…
pkoutsovasilis Jul 15, 2024
ce329c6
fix: correct import order
pkoutsovasilis Jul 15, 2024
10031d6
fix: move logWarning inside container_init_linux.go
pkoutsovasilis Jul 15, 2024
53292e6
fix: update NOTICE.txt
pkoutsovasilis Jul 15, 2024
0d006fe
Merge branch 'refs/heads/main' into pkoutsovasilis/container_capabili…
pkoutsovasilis Jul 19, 2024
f163d3f
fix: call initContainer before tryContainerLoadPaths for elastic-agen…
pkoutsovasilis Jul 23, 2024
f8b85eb
fix: revert npm installation changes in agent container image
pkoutsovasilis Jul 23, 2024
bb03305
fix: chown also symlinks
pkoutsovasilis Jul 23, 2024
4e4dad8
fix: introduce support for inside kubernetes tests
pkoutsovasilis Jul 23, 2024
2bae3dd
fix: permission for configuration configmap mount to support rootless…
pkoutsovasilis Jul 23, 2024
cb9a825
Merge remote-tracking branch 'refs/remotes/origin/main' into pkoutsov…
pkoutsovasilis Jul 23, 2024
0f8b308
fix: code format
pkoutsovasilis Jul 23, 2024
508323c
feat: revisit the runAsUser and runAsGroup in k8s test
pkoutsovasilis Jul 23, 2024
45c83ba
fix: go mod tidy
pkoutsovasilis Jul 23, 2024
2fe8637
fix: wrap errors in fmt.Errorf
pkoutsovasilis Jul 23, 2024
f4af164
fix: update NOTICE.txt
pkoutsovasilis Jul 23, 2024
852589e
feat: add comments in container_init_linux.go
pkoutsovasilis Jul 23, 2024
10075a7
fix: adjust artifact_paths to extract all artifacts
pkoutsovasilis Jul 23, 2024
f263d3f
fix: permission for configuration configmap mount to support rootless…
pkoutsovasilis Jul 23, 2024
0e38c8a
fix: log stderr for k8s inner tests
pkoutsovasilis Jul 23, 2024
1f419c0
fix: disable CGO for k8s inner tests
pkoutsovasilis Jul 23, 2024
1d54afc
Merge remote-tracking branch 'refs/remotes/origin/main' into pkoutsov…
pkoutsovasilis Jul 23, 2024
c23bcb6
fix: merge conflicts
pkoutsovasilis Jul 23, 2024
6f3b7b6
fix: permission for configuration configmap mount to support rootless…
pkoutsovasilis Jul 23, 2024
44193ba
Merge branch 'main' into pkoutsovasilis/container_capabilities_chown
pkoutsovasilis Jul 24, 2024
de8b24d
fix: improve logging the output of status command when elastic-agent …
pkoutsovasilis Jul 24, 2024
50cd3a9
fix: revert back to pre-1.22 golang for loop to make linter happy
pkoutsovasilis Jul 24, 2024
809421c
fix: don't set runAsGroup inside k8s tests for deployment with root a…
pkoutsovasilis Jul 24, 2024
d27d12d
fix: add DAC_READ_SEARCH capability in k8s tests for deployment with …
pkoutsovasilis Jul 24, 2024
51c2683
fix: add comment to elaborate why call initContainer before tryContai…
pkoutsovasilis Jul 25, 2024
191c1a0
Merge branch 'main' into pkoutsovasilis/container_capabilities_chown
pkoutsovasilis Jul 29, 2024
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
816 changes: 816 additions & 0 deletions NOTICE.txt

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions changelog/fragments/1718273218-container-init-capabilities.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Kind can be one of:
# - breaking-change: a change to previously-documented behavior
# - deprecation: functionality that is being removed in a later release
# - bug-fix: fixes a problem in a previous version
# - enhancement: extends functionality but does not break or fix existing behavior
# - feature: new functionality
# - known-issue: problems that we are aware of in a given version
# - security: impacts on the security of a product or a user’s deployment.
# - upgrade: important information for someone upgrading from a prior version
# - other: does not fit into any of the other categories
kind: feature

# Change summary; a 80ish characters long description of the change.
summary: extend agent container initialisation to chown paths and raise capabilities for non-root

# Long description; in case the summary is not enough to describe the change
# this field accommodate a description without length limits.
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
#description:

# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
component: elastic-agent

# PR URL; optional; the PR number that added the changeset.
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
# Please provide it if you are adding a fragment for a different PR.
pr: https://github.com/elastic/elastic-agent/pull/4925

# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
# If not present is automatically filled by the tooling with the issue linked to the PR number.
#issue: https://github.com/owner/repo/1234
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ RUN mkdir /app && \
{{- end }}

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

{{- end }}
# root group no more for elastic-agent user
RUN gpasswd --delete {{ .user }} root
pkoutsovasilis marked this conversation as resolved.
Show resolved Hide resolved
USER {{ .user }}


Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ require (
k8s.io/apimachinery v0.29.5
k8s.io/client-go v0.29.5
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
kernel.org/pub/linux/libs/security/libcap/cap v1.2.70
)

require (
Expand Down Expand Up @@ -276,6 +277,7 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
howett.net/plist v1.0.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
kernel.org/pub/linux/libs/security/libcap/psx v1.2.70 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3113,6 +3113,10 @@ k8s.io/utils v0.0.0-20221107191617-1a15be271d1d/go.mod h1:OLgZIPagt7ERELqWJFomSt
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
kernel.org/pub/linux/libs/security/libcap/cap v1.2.70 h1:QnLPkuDWWbD5C+3DUA2IUXai5TK6w2zff+MAGccqdsw=
kernel.org/pub/linux/libs/security/libcap/cap v1.2.70/go.mod h1:/iBwcj9nbLejQitYvUm9caurITQ6WyNHibJk6Q9fiS4=
kernel.org/pub/linux/libs/security/libcap/psx v1.2.70 h1:HsB2G/rEQiYyo1bGoQqHZ/Bvd6x1rERQTNdPr1FyWjI=
kernel.org/pub/linux/libs/security/libcap/psx v1.2.70/go.mod h1:+l6Ee2F59XiJ2I6WR5ObpC1utCQJZ/VLsEbQCD8RG24=
oras.land/oras-go v1.2.2/go.mod h1:Apa81sKoZPpP7CDciE006tSZ0x3Q3+dOoBcMZ/aNxvw=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
Expand Down
23 changes: 21 additions & 2 deletions internal/pkg/agent/cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const (
defaultRequestRetrySleep = "1s" // sleep 1 sec between retries for HTTP requests
defaultMaxRequestRetries = "30" // maximum number of retries for HTTP requests
defaultStateDirectory = "/usr/share/elastic-agent/state" // directory that will hold the state data
agentBaseDirectory = "/usr/share/elastic-agent" // directory that holds all elastic-agent related files
pkoutsovasilis marked this conversation as resolved.
Show resolved Hide resolved

skipFileCapabilitiesFlag = "skip-file-capabilities"

logsPathPerms = 0775
)
Expand Down Expand Up @@ -139,12 +142,15 @@ all the above actions will be skipped, because the Elastic Agent has already bee
occurs on every start of the container set FLEET_FORCE to 1.
`,
Run: func(c *cobra.Command, args []string) {
if err := logContainerCmd(streams); err != nil {
if err := logContainerCmd(c, streams); err != nil {
logError(streams, err)
os.Exit(1)
}
},
}

cmd.Flags().Bool(skipFileCapabilitiesFlag, false, "skip setting file capabilities")

return &cmd
}

Expand All @@ -156,7 +162,20 @@ func logInfo(streams *cli.IOStreams, a ...interface{}) {
fmt.Fprintln(streams.Out, a...)
}

func logContainerCmd(streams *cli.IOStreams) error {
func logContainerCmd(cmd *cobra.Command, streams *cli.IOStreams) error {
skipFileCapabilities, err := cmd.Flags().GetBool(skipFileCapabilitiesFlag)
if err != nil {
return err
}

shouldExit, err := initContainer(streams, skipFileCapabilities)
if err != nil {
return err
}
if shouldExit {
return nil
}

logsPath := envWithDefault("", "LOGS_PATH")
if logsPath != "" {
// log this entire command to a file as well as to the passed streams
Expand Down
Loading