git.iko.soy/typeable/oauth2-proxy
on 8cbf921 help sign in
104 lines 2.8 kB history raw
1package main
3import (
4 "fmt"
5 "math/rand"
6 "net"
7 "os"
8 "os/signal"
9 "runtime"
10 "syscall"
11 "time"
13 "github.com/justinas/alice"
14 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
15 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
16 "github.com/oauth2-proxy/oauth2-proxy/pkg/middleware"
17 "github.com/oauth2-proxy/oauth2-proxy/pkg/validation"
20func main() {
21 logger.SetFlags(logger.Lshortfile)
22 flagSet := options.NewFlagSet()
24 config := flagSet.String("config", "", "path to config file")
25 showVersion := flagSet.Bool("version", false, "print version string")
27 err := flagSet.Parse(os.Args[1:])
28 if err != nil {
29 logger.Printf("ERROR: Failed to parse flags: %v", err)
30 os.Exit(1)
31 }
33 if *showVersion {
34 fmt.Printf("oauth2-proxy %s (built with %s)\n", VERSION, runtime.Version())
35 return
36 }
38 legacyOpts := options.NewLegacyOptions()
39 err = options.Load(*config, flagSet, legacyOpts)
40 if err != nil {
41 logger.Errorf("ERROR: Failed to load config: %v", err)
42 os.Exit(1)
43 }
45 opts, err := legacyOpts.ToOptions()
46 if err != nil {
47 logger.Errorf("ERROR: Failed to convert config: %v", err)
48 os.Exit(1)
49 }
51 err = validation.Validate(opts)
52 if err != nil {
53 logger.Printf("%s", err)
54 os.Exit(1)
55 }
57 validator := NewValidator(opts.EmailDomains, opts.AuthenticatedEmailsFile)
58 oauthproxy, err := NewOAuthProxy(opts, validator)
59 if err != nil {
60 logger.Errorf("ERROR: Failed to initialise OAuth2 Proxy: %v", err)
61 os.Exit(1)
62 }
64 rand.Seed(time.Now().UnixNano())
66 chain := alice.New()
68 if opts.ForceHTTPS {
69 _, httpsPort, err := net.SplitHostPort(opts.HTTPSAddress)
70 if err != nil {
71 logger.Fatalf("FATAL: invalid HTTPS address %q: %v", opts.HTTPAddress, err)
72 }
73 chain = chain.Append(middleware.NewRedirectToHTTPS(httpsPort))
74 }
76 healthCheckPaths := []string{opts.PingPath}
77 healthCheckUserAgents := []string{opts.PingUserAgent}
78 if opts.GCPHealthChecks {
79 healthCheckPaths = append(healthCheckPaths, "/liveness_check", "/readiness_check")
80 healthCheckUserAgents = append(healthCheckUserAgents, "GoogleHC/1.0")
81 }
83 // To silence logging of health checks, register the health check handler before
84 // the logging handler
85 if opts.Logging.SilencePing {
86 chain = chain.Append(middleware.NewHealthCheck(healthCheckPaths, healthCheckUserAgents), LoggingHandler)
87 } else {
88 chain = chain.Append(LoggingHandler, middleware.NewHealthCheck(healthCheckPaths, healthCheckUserAgents))
89 }
91 s := &Server{
92 Handler: chain.Then(oauthproxy),
93 Opts: opts,
94 stop: make(chan struct{}, 1),
95 }
96 // Observe signals in background goroutine.
97 go func() {
98 sigint := make(chan os.Signal, 1)
99 signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
100 <-sigint
101 s.stop <- struct{}{} // notify having caught signal
102 }()
103 s.ListenAndServe()