Support setcap in dev mode (XCADDY_SETCAP)

When I first wrote xcaddy I was on macOS which does not
require permission to bind to low ports. Now I'm on Linux.

The XCADDY_SETCAP env var will cause xcaddy to run setcap
on the generated temporary binary before executing it
so that it can bind to low ports. This requires sudo.
This commit is contained in:
Matthew Holt 2021-01-20 12:17:46 -07:00
parent 623c361726
commit dabafeef18
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
2 changed files with 11 additions and 1 deletions

View file

@ -125,7 +125,7 @@ Because the subcommands and flags are constrained to benefit rapid plugin protot
- `CADDY_VERSION` sets the version of Caddy to build. - `CADDY_VERSION` sets the version of Caddy to build.
- `XCADDY_RACE_DETECTOR=1` enables the Go race detector in the build. - `XCADDY_RACE_DETECTOR=1` enables the Go race detector in the build.
- `XCADDY_SKIP_CLEANUP=1` causes xcaddy to leave build artifacts on disk after exiting. - `XCADDY_SKIP_CLEANUP=1` causes xcaddy to leave build artifacts on disk after exiting.
- `XCADDY_SETCAP=1` will run `sudo setcap cap_net_bind_service=+ep` on the temporary binary before running it when in dev mode.
--- ---

View file

@ -223,6 +223,16 @@ func runDev(ctx context.Context, args []string) error {
return err return err
} }
if os.Getenv("XCADDY_SETCAP") == "1" {
cmd = exec.Command("sudo", "setcap", "cap_net_bind_service=+ep", binOutput)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Printf("[INFO] Setting capabilities (requires admin privileges): %v", cmd.Args)
if err = cmd.Run(); err != nil {
return err
}
}
log.Printf("[INFO] Running %v\n\n", append([]string{binOutput}, args...)) log.Printf("[INFO] Running %v\n\n", append([]string{binOutput}, args...))
cmd = exec.Command(binOutput, args...) cmd = exec.Command(binOutput, args...)