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

Add support for systemd socket activation #704

Merged
merged 3 commits into from
Oct 25, 2024

Conversation

ianroberts
Copy link
Contributor

@ianroberts ianroberts commented Oct 25, 2024

Systemd has a feature whereby it can manage the listening sockets for a network service, and launch the service in response to the first incoming connection, passing the open socket to the service process via an open file descriptor. This PR adds support for socket activation to webhook.

This PR builds on my previous #703 that added platform-specific logic to the socket binding process. On compatible platforms (i.e. not Windows) we now check whether we have been launched with the standard environment variables that indicate systemd socket activation - if so, we simply use the already-open socket we have been given by systemd instead of opening our own. In this mode any -port and -socket options are ignored completely in favour of the systemd socket.

As per the discussion on #703 I've also made it a fatal error to try and combine the -socket and -setuid options. Socket activation provides a safer way to achieve the equivalent behaviour of running webhook as an unprivileged user but have it listening on a Unix socket owned by root.

Testing

I can't really see an easy way to add tests for this feature, but I have been using my own build of this branch in production to run webhook with socket activation.

If webhook has been launched via systemd socket activation, simply use the systemd-provided socket rather than opening our own.
The setuid and setgid flags do not work on Windows, so moved them to platform_unix so they are only added to the flag set on compatible platforms.

Also disallow the use of setuid and setgid in combination with -socket, since a setuid webhook process would not be able to clean up a socket that was created while running as root.  If you _need_ to have the socket owned by root but the webhook process running as a normal user, you can achieve the same effect with systemd socket activation.
@@ -0,0 +1,61 @@
# Using systemd socket activation

_New in v2.9.0_
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change this to whatever the next release version number will be...

Comment on lines +15 to +16
flag.IntVar(&setGID, "setgid", 0, "set group ID after opening listening port; must be used with setuid, not permitted with -socket")
flag.IntVar(&setUID, "setuid", 0, "set user ID after opening listening port; must be used with setgid, not permitted with -socket")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since setuid and setgid don't work on Windows anyway, I've moved then in here so that they are only available as flags for non-Windows builds.

@adnanh adnanh merged commit 98cf5d0 into adnanh:master Oct 25, 2024
6 checks passed
@adnanh
Copy link
Owner

adnanh commented Oct 25, 2024

Perfect!

@ianroberts ianroberts deleted the systemd-activation branch October 25, 2024 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants