Skip to content

Commit eddeb82

Browse files
authored
Add option to bind to a Unix socket instead of a TCP port (#703)
* feat: add ability to listen on unix socket/named pipe Add a -socket option that configures the server to listen on a Unix-domain socket or Windows named pipe instead of a TCP port. This allows webhook to be used behind a reverse proxy on multi-tenant shared hosting without the need to choose (and the permission to bind to) a free port number. On Windows, -socket is expected to be a named pipe such as \\.\pipe\webhook, and the code uses https://github.com/microsoft/go-winio to bind the listening socket. On other platforms, -socket is the path to a Unix domain socket such as /tmp/webhook.sock, or an abstract socket name starting with @, bound using the regular net.Listen function with the "network" parameter set to "unix". Note: this pushes our minimum Go version up to 1.21 as that is what go-winio requires, but that is already the minimum version against which we are testing in the CI matrix. * tests: add test for the -socket option Refactored webhook_test so that the test HTTP requests are made using an explicitly-provided http.Client, so we can run at least one test with the server bound to a socket instead of a port number, using an http.Client whose transport has been configured with a suitable Unix-domain or Windows named pipe dialer function. * tests: use GOROOT to find go command This should ensure that, even if a developer or CI server has multiple versions of go installed, the version used to build the tools under test will be the same version that is running the test harness. * fix: clean up Unix socket file before exit If webhook is restarted with the same settings but the socket file has not been deleted, webhook will be unable to bind and will exit with an error. * docs: add -socket option to documentation * docs: add a note about reverse proxies - README mentions the idea of using webhook behind a reverse proxy, including with the -socket flag - added a note in Hook-Rules that the ip-whitelist rule type does not work as expected behind a reverse proxy, and you should configure IP restrictions at the proxy level instead
1 parent b6f24d0 commit eddeb82

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+4483
-79
lines changed

Diff for: .github/workflows/build.yml

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ name: build
22
on: [push, pull_request]
33
jobs:
44
build:
5+
env:
6+
# The special value "local" tells Go to use the bundled Go
7+
# version rather than trying to fetch one according to a
8+
# `toolchain` value in `go.mod`. This ensures that we're
9+
# really running the Go version in the CI matrix rather than
10+
# one that the Go command has upgraded to automatically.
11+
GOTOOLCHAIN: local
512
strategy:
613
matrix:
714
go-version: [1.21.x, 1.22.x]

Diff for: README.md

+5
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ In either case, the given file part will be parsed as JSON and added to the `pay
109109

110110
TLS version and cipher suite selection flags are available from the command line. To list available cipher suites, use the `-list-cipher-suites` flag. The `-tls-min-version` flag can be used with `-list-cipher-suites`.
111111

112+
## Running behind a reverse proxy
113+
[webhook][w] may be run behind a "reverse proxy" - another web-facing server such as [Apache httpd](https://httpd.apache.org) or [Nginx](https://nginx.org) that accepts requests from clients and forwards them on to [webhook][h]. You can have [webhook][w] listen on a regular TCP port or on a Unix domain socket (with the `-socket` flag), then configure your proxy to send requests for a specific host name or sub-path over that port or socket to [webhook][w].
114+
115+
Note that when running in this mode the [`ip-whitelist`](docs/Hook-Rules.md#match-whitelisted-ip-range) trigger rule will not work as expected, since it will be checking the address of the _proxy_, not the _client_. Client IP restrictions will need to be enforced within the proxy, before it decides whether to forward the request to [webhook][w].
116+
112117
## CORS Headers
113118
If you want to set CORS headers, you can use the `-header name=value` flag while starting [webhook][w] to set the appropriate CORS headers that will be returned with each response.
114119

Diff for: docs/Hook-Rules.md

+2
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ The IP can be IPv4- or IPv6-formatted, using [CIDR notation](https://en.wikipedi
269269
}
270270
```
271271

272+
Note this does not work if webhook is running behind a reverse proxy, as the "client IP" will either not be available at all (if webhook is using a Unix socket or named pipe) or it will be the address of the _proxy_, not of the real client. You will probably need to enforce client IP restrictions in the reverse proxy itself, before forwarding the requests to webhook.
273+
272274
### Match scalr-signature
273275

274276
The trigger rule checks the scalr signature and also checks that the request was signed less than 5 minutes before it was received.

Diff for: docs/Webhook-Parameters.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Usage of webhook:
1414
-hotreload
1515
watch hooks file for changes and reload them automatically
1616
-http-methods string
17-
globally restrict allowed HTTP methods; separate methods with comma
17+
set default allowed HTTP methods (ie. "POST"); separate methods with comma
1818
-ip string
1919
ip the webhook should serve hooks on (default "0.0.0.0")
2020
-key string
@@ -23,6 +23,8 @@ Usage of webhook:
2323
list available TLS cipher suites
2424
-logfile string
2525
send log output to a file; implicitly enables verbose logging
26+
-max-multipart-mem int
27+
maximum memory in bytes for parsing multipart form data before disk caching (default 1048576)
2628
-nopanic
2729
do not panic if hooks cannot be loaded when webhook is not running in verbose mode
2830
-pidfile string
@@ -35,6 +37,8 @@ Usage of webhook:
3537
set group ID after opening listening port; must be used with setuid
3638
-setuid int
3739
set user ID after opening listening port; must be used with setgid
40+
-socket string
41+
path to a Unix socket (e.g. /tmp/webhook.sock) or Windows named pipe (e.g. \\.\pipe\webhook) to use instead of listening on an ip and port; if specified, the ip and port options are ignored
3842
-template
3943
parse hooks file as a Go template
4044
-tls-min-version string

Diff for: go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
module github.com/adnanh/webhook
22

3-
go 1.17
3+
go 1.21
4+
5+
toolchain go1.22.0
46

57
require (
8+
github.com/Microsoft/go-winio v0.6.2
69
github.com/clbanning/mxj/v2 v2.7.0
710
github.com/dustin/go-humanize v1.0.1
811
github.com/fsnotify/fsnotify v1.7.0

Diff for: go.sum

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
2+
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
13
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
24
github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
35
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
@@ -19,7 +21,6 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
1921
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
2022
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
2123
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
22-
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2324
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
2425
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
2526
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

Diff for: platform_unix.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package main
5+
6+
import (
7+
"flag"
8+
"fmt"
9+
"net"
10+
)
11+
12+
func platformFlags() {
13+
flag.StringVar(&socket, "socket", "", "path to a Unix socket (e.g. /tmp/webhook.sock) to use instead of listening on an ip and port; if specified, the ip and port options are ignored")
14+
}
15+
16+
func trySocketListener() (net.Listener, error) {
17+
if socket != "" {
18+
addr = fmt.Sprintf("{unix:%s}", socket)
19+
return net.Listen("unix", socket)
20+
}
21+
return nil, nil
22+
}

Diff for: platform_windows.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build windows
2+
// +build windows
3+
4+
package main
5+
6+
import (
7+
"flag"
8+
"fmt"
9+
"github.com/Microsoft/go-winio"
10+
"net"
11+
)
12+
13+
func platformFlags() {
14+
flag.StringVar(&socket, "socket", "", "path to a Windows named pipe (e.g. \\\\.\\pipe\\webhook) to use instead of listening on an ip and port; if specified, the ip and port options are ignored")
15+
}
16+
17+
func trySocketListener() (net.Listener, error) {
18+
if socket != "" {
19+
addr = fmt.Sprintf("{pipe:%s}", socket)
20+
return winio.ListenPipe(socket, nil)
21+
}
22+
return nil, nil
23+
}

Diff for: signals.go

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !windows
12
// +build !windows
23

34
package main
@@ -6,6 +7,7 @@ import (
67
"log"
78
"os"
89
"os/signal"
10+
"strings"
911
"syscall"
1012
)
1113

@@ -43,6 +45,15 @@ func watchForSignals() {
4345
log.Print(err)
4446
}
4547
}
48+
if socket != "" && !strings.HasPrefix(socket, "@") {
49+
// we've been listening on a named Unix socket, delete it
50+
// before we exit so subsequent runs can re-bind the same
51+
// socket path
52+
err := os.Remove(socket)
53+
if err != nil {
54+
log.Printf("Failed to remove socket file %s: %v", socket, err)
55+
}
56+
}
4657
os.Exit(0)
4758

4859
default:

Diff for: testutils.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package main
5+
6+
import (
7+
"context"
8+
"io/ioutil"
9+
"net"
10+
"net/http"
11+
"os"
12+
"path"
13+
)
14+
15+
func prepareTestSocket(_ string) (socketPath string, transport *http.Transport, cleanup func(), err error) {
16+
tmp, err := ioutil.TempDir("", "webhook-socket-")
17+
if err != nil {
18+
return "", nil, nil, err
19+
}
20+
cleanup = func() { os.RemoveAll(tmp) }
21+
socketPath = path.Join(tmp, "webhook.sock")
22+
socketDialer := &net.Dialer{}
23+
transport = &http.Transport{
24+
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
25+
return socketDialer.DialContext(ctx, "unix", socketPath)
26+
},
27+
}
28+
29+
return socketPath, transport, cleanup, nil
30+
}

Diff for: testutils_windows.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build windows
2+
// +build windows
3+
4+
package main
5+
6+
import (
7+
"context"
8+
"github.com/Microsoft/go-winio"
9+
"net"
10+
"net/http"
11+
)
12+
13+
func prepareTestSocket(hookTmpl string) (socketPath string, transport *http.Transport, cleanup func(), err error) {
14+
socketPath = "\\\\.\\pipe\\webhook-" + hookTmpl
15+
transport = &http.Transport{
16+
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
17+
return winio.DialPipeContext(ctx, socketPath)
18+
},
19+
}
20+
21+
return socketPath, transport, nil, nil
22+
}

Diff for: vendor/github.com/Microsoft/go-winio/.gitattributes

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: vendor/github.com/Microsoft/go-winio/.gitignore

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: vendor/github.com/Microsoft/go-winio/.golangci.yml

+147
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: vendor/github.com/Microsoft/go-winio/CODEOWNERS

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)