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

[Heartbeat] Unpack beats at build time on docker #202

Merged
merged 2 commits into from
Mar 16, 2022

Conversation

emilioalvap
Copy link
Contributor

@emilioalvap emilioalvap commented Mar 14, 2022

What does this PR do?

This PR enables unpacking of beats inside the container at build time, so that required cap_net_raw, cap_setuid capabilities can be assigned to the binary.

Why is it important?

Without the required capabilities, heartbeat cannot execute ICMP pings or setuid calls. As it is now, agent is unpacking beats at runtime, most likely with a user that doesn't have permission to assign capabilities.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files

Author's Checklist

  • [ ]

How to test this PR locally

  • Build elastic-agent containers, run:
    DEV=true SNAPSHOT=true PLATFORMS=linux/amd64 TYPES=docker mage package
  • Run one of the built containers and provide some heartbeat configuration:
docker run --name agent -it -u root --env FLEET_ENROLL=1 --env \
FLEET_URL=<url> --env \ 
FLEET_ENROLLMENT_TOKEN=<token> \ 
docker.elastic.co/beats/elastic-agent:8.2.0-SNAPSHOT

Related issues

Screenshots

image

Logs

15:38:37.323
elastic_agent.heartbeat
[elastic_agent.heartbeat][info] heartbeat start running.
15:38:37.323
elastic_agent.heartbeat
[elastic_agent.heartbeat][warn] BETA: Fleet management is enabled
15:38:37.323
elastic_agent.heartbeat
[elastic_agent.heartbeat][info] Starting fleet management service
15:38:37.323
elastic_agent.heartbeat
[elastic_agent.heartbeat][info] heartbeat is running! Hit CTRL-C to stop it.
15:38:37.323
elastic_agent.heartbeat
[elastic_agent.heartbeat][info] Effective user/group ids: 1000/1000, with groups: [0]

@mergify
Copy link
Contributor

mergify bot commented Mar 14, 2022

This pull request does not have a backport label. Could you fix it @emilioalvap? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v./d./d./d is the label to automatically backport to the 8./d branch. /d is the digit

NOTE: backport-skip has been added to this pull request.

@emilioalvap emilioalvap added Team:Elastic-Agent Label for the Agent team Team:Uptime Label for the Uptime team bug Something isn't working v8.2.0 and removed backport-skip labels Mar 14, 2022
@mergify
Copy link
Contributor

mergify bot commented Mar 14, 2022

This pull request does not have a backport label. Could you fix it @emilioalvap? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v./d./d./d is the label to automatically backport to the 8./d branch. /d is the digit

NOTE: backport-skip has been added to this pull request.

@elasticmachine
Copy link
Contributor

elasticmachine commented Mar 14, 2022

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2022-03-14T16:05:23.423+0000

  • Duration: 37 min 43 sec

🤖 GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages.

  • run integration tests : Run the Elastic Agent Integration tests.

  • run end-to-end tests : Generate the packages and run the E2E Tests.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

Copy link
Contributor

@ph ph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This look OK to me

chown -R {{ .user }}:{{ .user }} {{ $beatHome }}/data/{{.BeatName}}-{{ commit_short }}/{{ .beats_install_path }} && \
chown -R root:root {{ $beatHome }}/data/{{.BeatName}}-{{ commit_short }}/{{ .beats_install_path }}/*/*.yml && \
chmod 0644 {{ $beatHome }}/data/{{.BeatName}}-{{ commit_short }}/{{ .beats_install_path }}/*/*.yml && \
setcap cap_net_raw,cap_setuid+p {{ $beatHome }}/data/{{.BeatName}}-{{ commit_short }}/{{ .beats_install_path }}/heartbeat-*/heartbeat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment about why these permission are necessary for heartbeat?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, added a short explanation on setcap

Copy link
Contributor

@blakerouse blakerouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

This should be backported to 7.17+

@lucasfcosta
Copy link
Contributor

I've managed to run ICMP pings to my localhost from the container as @emilioalvap described.

Screenshot 2022-04-20 at 11 27 40

Even when pings timed out, there wasn't an error message about capabilities.

Checking the binaries capability also seems correct as per my understanding of how capabilities work:

elastic-agent@a1eb84cf2c41:~$ getpcaps 58
58: = cap_net_raw+ep

Reference: https://blog.container-solutions.com/linux-capabilities-in-practice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-skip bug Something isn't working Team:Elastic-Agent Label for the Agent team Team:Uptime Label for the Uptime team v8.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants