git.iko.soy/typeable/oauth2-proxy
on master help sign in
fix: systemd socket support build handling for windows
commit8dd2cbec4dd1c00b41d027489e78d579a695e3cf authortuunit <jan@larwig.com> dateSun 12 Jan 2025, 16:57 (2 years ago) parentae5b5dc
4 files changed+77−41
CHANGELOG.md+1 pkg/http/server.go+4−41 pkg/http/systemd_socket.go+55 pkg/http/systemd_unsupported.go+17
CHANGELOG.md+1
@@ -33,6 +33,7 @@
3333- [#2800](https://github.com/oauth2-proxy/oauth2-proxy/pull/2800) doc: add some opencontainer labels to docker image (@halkeye)
3434- [#2755](https://github.com/oauth2-proxy/oauth2-proxy/pull/2755) feat: add X-Envoy-External-Address as supported header (@bjencks)
3535- [#1985](https://github.com/oauth2-proxy/oauth2-proxy/pull/1985) feat: add support for systemd socket (@isodude)
36+- [#2916](https://github.com/oauth2-proxy/oauth2-proxy/pull/2916) fix: systemd socket support build handling for windows (@tuunit)
3637- [#2300](https://github.com/oauth2-proxy/oauth2-proxy/pull/2300) fix: add fix for websocket path rewrite (@rekup)
3738- [#2821](https://github.com/oauth2-proxy/oauth2-proxy/pull/2821) feat: add CF-Connecting-IP as supported real ip header (@ondrejsika)
3839- [#2620](https://github.com/oauth2-proxy/oauth2-proxy/pull/2620) fix: update code_verifier to use recommended method (@vishvananda)
pkg/http/server.go+4−41
@@ -8,24 +8,14 @@ import (
88 "net"
99 "net/http"
1010 "os"
11− "strconv"
1211 "strings"
1312 "time"
1413
15− "github.com/coreos/go-systemd/activation"
14+ "golang.org/x/sync/errgroup"
15+
1616 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
1717 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options/util"
1818 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
19− "golang.org/x/sync/errgroup"
20−)
21−
22−// listenFdsStart corresponds to `SD_LISTEN_FDS_START`.
23−// Since the 3 first file descriptors in every linux process is
24−// stdin, stdout and stderr. The first usable file descriptor is 3.
25−// systemd-socket-activate will always assume that the first socket will be
26−// 3 and the rest follow.
27−const (
28− listenFdsStart = 3
2919)
3020
3121// Server represents an HTTP or HTTPS server.
@@ -83,27 +73,6 @@ type server struct {
8373 fdFiles []*os.File
8474}
8575
86−// convert a string filedescriptor to an actual listener
87−func (s *server) fdToListener(bindAddress string) (net.Listener, error) {
88− fd, err := strconv.Atoi(bindAddress)
89− if err != nil {
90− return nil, fmt.Errorf("listen failed: fd with name is not implemented yet")
91− }
92− fdIndex := fd - listenFdsStart
93−
94− if len(s.fdFiles) == 0 {
95− s.fdFiles = activation.Files(true)
96− }
97−
98− l := len(s.fdFiles)
99−
100− if fdIndex < 0 || fdIndex >= l || l == 0 {
101− return nil, fmt.Errorf("listen failed: fd outside of range of available file descriptors")
102− }
103−
104− return net.FileListener(s.fdFiles[fdIndex])
105−}
106−
10776// setupListener sets the server listener if the HTTP server is enabled.
10877// The HTTP server can be disabled by setting the BindAddress to "-" or by
10978// leaving it empty.
@@ -120,13 +89,7 @@ func (s *server) setupListener(opts Opts) error {
12089 // to the program is indeed a net.Listener and starts using it
12190 // without setting up a new listener.
12291 if strings.HasPrefix(strings.ToLower(opts.BindAddress), "fd:") {
123− listenAddr := opts.BindAddress[3:]
124− listener, err := s.fdToListener(listenAddr)
125− if err != nil {
126− err = fmt.Errorf("listen (%s, %s) failed: %v", "file", listenAddr, err)
127− }
128− s.listener = listener
129− return err
92+ return s.checkSystemdSocketSupport(opts)
13093 }
13194
13295 networkType := getNetworkScheme(opts.BindAddress)
@@ -134,7 +97,7 @@ func (s *server) setupListener(opts Opts) error {
13497
13598 listener, err := net.Listen(networkType, listenAddr)
13699 if err != nil {
137− return fmt.Errorf("listen (%s, %s) failed: %v", networkType, listenAddr, err)
100+ return fmt.Errorf("listen (%s, %s) failed: %w", networkType, listenAddr, err)
138101 }
139102 s.listener = listener
140103
pkg/http/systemd_socket.gonew file+55
@@ -0,0 +1,55 @@
1+//go:build !windows
2+// +build !windows
3+
4+package http
5+
6+import (
7+ "errors"
8+ "fmt"
9+ "net"
10+ "strconv"
11+
12+ "github.com/coreos/go-systemd/activation"
13+)
14+
15+// listenFdsStart corresponds to `SD_LISTEN_FDS_START`.
16+// Since the 3 first file descriptors in every linux process is
17+// stdin, stdout and stderr. The first usable file descriptor is 3.
18+// systemd-socket-activate will always assume that the first socket will be
19+// 3 and the rest follow.
20+const (
21+ listenFdsStart = 3
22+)
23+
24+// convert a string filedescriptor to an actual listener
25+func (s *server) fdToListener(bindAddress string) (net.Listener, error) {
26+ fd, err := strconv.Atoi(bindAddress)
27+ if err != nil {
28+ return nil, errors.New("listen failed: fd with name is not implemented yet")
29+ }
30+ fdIndex := fd - listenFdsStart
31+
32+ if len(s.fdFiles) == 0 {
33+ s.fdFiles = activation.Files(true)
34+ }
35+
36+ l := len(s.fdFiles)
37+
38+ if fdIndex < 0 || fdIndex >= l || l == 0 {
39+ return nil, errors.New("listen failed: fd outside of range of available file descriptors")
40+ }
41+
42+ return net.FileListener(s.fdFiles[fdIndex])
43+}
44+
45+func (s *server) checkSystemdSocketSupport(opts Opts) error {
46+ listenAddr := opts.BindAddress[3:]
47+ listener, err := s.fdToListener(listenAddr)
48+ if err != nil {
49+ err = fmt.Errorf("listen (file, %s) failed: %w", listenAddr, err)
50+ return err
51+ }
52+
53+ s.listener = listener
54+ return nil
55+}
pkg/http/systemd_unsupported.gonew file+17
@@ -0,0 +1,17 @@
1+//go:build windows
2+// +build windows
3+
4+package http
5+
6+import (
7+ "fmt"
8+ "strings"
9+)
10+
11+func (s *server) checkSystemdSocketSupport(opts Opts) error {
12+ if strings.HasPrefix(strings.ToLower(opts.BindAddress), "fd:") {
13+ listenAddr := opts.BindAddress[3:]
14+ return fmt.Errorf("listen (file, %s) failed: systemd sockets are not supported on windows", listenAddr)
15+ }
16+ return nil
17+}