14 "golang.org/x/sync/errgroup"
16 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
17 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options/util"
18 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
21// Server represents an HTTP or HTTPS server.
22type Server interface {
23 // Start blocks and runs the server.
24 Start(ctx context.Context) error
27// Opts contains the information required to set up the server.
29 // Handler is the http.Handler to be used to serve http pages by the server.
32 // BindAddress is the address the HTTP server should listen on.
35 // SecureBindAddress is the address the HTTPS server should listen on.
36 SecureBindAddress string
38 // TLS is the TLS configuration for the server.
41 // Let testing infrastructure circumvent parsing file descriptors
45// NewServer creates a new Server from the options given.
46func NewServer(opts Opts) (Server, error) {
48 handler: opts.Handler,
51 if len(opts.fdFiles) > 0 {
52 s.fdFiles = opts.fdFiles
55 if err := s.setupListener(opts); err != nil {
56 return nil, fmt.Errorf("error setting up listener: %v", err)
58 if err := s.setupTLSListener(opts); err != nil {
59 return nil, fmt.Errorf("error setting up TLS listener: %v", err)
65// server is an implementation of the Server interface.
70 tlsListener net.Listener
72 // ensure activation.Files are called once
76// setupListener sets the server listener if the HTTP server is enabled.
77// The HTTP server can be disabled by setting the BindAddress to "-" or by
79func (s *server) setupListener(opts Opts) error {
80 if opts.BindAddress == "" || opts.BindAddress == "-" {
81 // No HTTP listener required
85 // Use fd: as a prefix for systemd socket activation, it's generic
87 // The most common usage would be --http-address fd:3.
88 // This causes oauth2-proxy to just assume that the third fd passed
89 // to the program is indeed a net.Listener and starts using it
90 // without setting up a new listener.
91 if strings.HasPrefix(strings.ToLower(opts.BindAddress), "fd:") {
92 return s.checkSystemdSocketSupport(opts)
95 networkType := getNetworkScheme(opts.BindAddress)
96 listenAddr := getListenAddress(opts.BindAddress)
98 listener, err := net.Listen(networkType, listenAddr)
100 return fmt.Errorf("listen (%s, %s) failed: %w", networkType, listenAddr, err)
102 s.listener = listener
107func parseCipherSuites(names []string) ([]uint16, error) {
108 cipherNameMap := make(map[string]uint16)
110 for _, cipherSuite := range tls.CipherSuites() {
111 cipherNameMap[cipherSuite.Name] = cipherSuite.ID
113 for _, cipherSuite := range tls.InsecureCipherSuites() {
114 cipherNameMap[cipherSuite.Name] = cipherSuite.ID
117 result := make([]uint16, len(names))
118 for i, name := range names {
119 id, present := cipherNameMap[name]
121 return nil, fmt.Errorf("unknown TLS cipher suite name specified %q", name)
128// setupTLSListener sets the server TLS listener if the HTTPS server is enabled.
129// The HTTPS server can be disabled by setting the SecureBindAddress to "-" or by
131func (s *server) setupTLSListener(opts Opts) error {
132 if opts.SecureBindAddress == "" || opts.SecureBindAddress == "-" {
133 // No HTTPS listener required
137 config := &tls.Config{
138 MinVersion: tls.VersionTLS12, // default, override below
139 MaxVersion: tls.VersionTLS13,
140 NextProtos: []string{"http/1.1"},
143 return errors.New("no TLS config provided")
145 cert, err := getCertificate(opts.TLS)
147 return fmt.Errorf("could not load certificate: %v", err)
149 config.Certificates = []tls.Certificate{cert}
151 if len(opts.TLS.CipherSuites) > 0 {
152 cipherSuites, err := parseCipherSuites(opts.TLS.CipherSuites)
154 return fmt.Errorf("could not parse cipher suites: %v", err)
156 config.CipherSuites = cipherSuites
159 if len(opts.TLS.MinVersion) > 0 {
160 switch opts.TLS.MinVersion {
162 config.MinVersion = tls.VersionTLS12
164 config.MinVersion = tls.VersionTLS13
166 return errors.New("unknown TLS MinVersion config provided")
170 listenAddr := getListenAddress(opts.SecureBindAddress)
172 listener, err := net.Listen("tcp", listenAddr)
174 return fmt.Errorf("listen (%s) failed: %v", listenAddr, err)
177 s.tlsListener = tls.NewListener(tcpKeepAliveListener{listener.(*net.TCPListener)}, config)
181// Start starts the HTTP and HTTPS server if applicable.
182// It will block until the context is cancelled.
183// If any errors occur, only the first error will be returned.
184func (s *server) Start(ctx context.Context) error {
185 g, groupCtx := errgroup.WithContext(ctx)
187 if s.listener != nil {
189 if err := s.startServer(groupCtx, s.listener); err != nil {
190 return fmt.Errorf("error starting insecure server: %v", err)
196 if s.tlsListener != nil {
198 if err := s.startServer(groupCtx, s.tlsListener); err != nil {
199 return fmt.Errorf("error starting secure server: %v", err)
208// startServer creates and starts a new server with the given listener.
209// When the given context is cancelled the server will be shutdown.
210// If any errors occur, only the first error will be returned.
211func (s *server) startServer(ctx context.Context, listener net.Listener) error {
212 srv := &http.Server{Handler: s.handler, ReadHeaderTimeout: time.Minute}
213 g, groupCtx := errgroup.WithContext(ctx)
218 if err := srv.Shutdown(context.Background()); err != nil {
219 return fmt.Errorf("error shutting down server: %v", err)
225 if err := srv.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
226 return fmt.Errorf("could not start server: %v", err)
234// getNetworkScheme gets the scheme for the HTTP server.
235func getNetworkScheme(addr string) string {
237 i := strings.Index(addr, "://")
250// getListenAddress gets the address for the HTTP server.
251func getListenAddress(addr string) string {
252 slice := strings.SplitN(addr, "//", 2)
253 return slice[len(slice)-1]
256// getCertificate loads the certificate data from the TLS config.
257func getCertificate(opts *options.TLS) (tls.Certificate, error) {
258 keyData, err := getSecretValue(opts.Key)
260 return tls.Certificate{}, fmt.Errorf("could not load key data: %v", err)
263 certData, err := getSecretValue(opts.Cert)
265 return tls.Certificate{}, fmt.Errorf("could not load cert data: %v", err)
268 cert, err := tls.X509KeyPair(certData, keyData)
270 return tls.Certificate{}, fmt.Errorf("could not parse certificate data: %v", err)
276// getSecretValue wraps util.GetSecretValue so that we can return an error if no
277// source is provided.
278func getSecretValue(src *options.SecretSource) ([]byte, error) {
280 return nil, errors.New("no configuration provided")
282 return util.GetSecretValue(src)
285// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
286// connections. It's used by so that dead TCP connections (e.g. closing laptop
287// mid-download) eventually go away.
288type tcpKeepAliveListener struct {
292// Accept implements the TCPListener interface.
293// It sets the keep alive period to 3 minutes for each connection.
294func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
295 tc, err := ln.AcceptTCP()
299 err = tc.SetKeepAlive(true)
301 logger.Errorf("Error setting Keep-Alive: %v", err)
303 err = tc.SetKeepAlivePeriod(3 * time.Minute)
305 logger.Printf("Error setting Keep-Alive period: %v", err)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list