git.iko.soy/typeable/oauth2-proxy
on 71dc702 help sign in
115 lines 3.2 kB history raw
1package main
3import (
4 "fmt"
5 "math/rand"
6 "net"
7 "os"
8 "os/signal"
9 "runtime"
10 "strings"
11 "syscall"
12 "time"
14 "github.com/justinas/alice"
15 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
16 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
17 "github.com/oauth2-proxy/oauth2-proxy/pkg/middleware"
18 "github.com/oauth2-proxy/oauth2-proxy/pkg/validation"
21func main() {
22 logger.SetFlags(logger.Lshortfile)
23 flagSet := options.NewFlagSet()
25 config := flagSet.String("config", "", "path to config file")
26 showVersion := flagSet.Bool("version", false, "print version string")
28 flagSet.Parse(os.Args[1:])
30 if *showVersion {
31 fmt.Printf("oauth2-proxy %s (built with %s)\n", VERSION, runtime.Version())
32 return
33 }
35 legacyOpts := options.NewLegacyOptions()
36 err := options.Load(*config, flagSet, legacyOpts)
37 if err != nil {
38 logger.Printf("ERROR: Failed to load config: %v", err)
39 os.Exit(1)
40 }
42 opts, err := legacyOpts.ToOptions()
43 if err != nil {
44 logger.Printf("ERROR: Failed to convert config: %v", err)
45 os.Exit(1)
46 }
48 err = validation.Validate(opts)
49 if err != nil {
50 logger.Printf("%s", err)
51 os.Exit(1)
52 }
54 validator := NewValidator(opts.EmailDomains, opts.AuthenticatedEmailsFile)
55 oauthproxy, err := NewOAuthProxy(opts, validator)
56 if err != nil {
57 logger.Printf("ERROR: Failed to initialise OAuth2 Proxy: %v", err)
58 os.Exit(1)
59 }
61 if len(opts.Banner) >= 1 {
62 if opts.Banner == "-" {
63 oauthproxy.SignInMessage = ""
64 } else {
65 oauthproxy.SignInMessage = opts.Banner
66 }
67 } else if len(opts.EmailDomains) != 0 && opts.AuthenticatedEmailsFile == "" {
68 if len(opts.EmailDomains) > 1 {
69 oauthproxy.SignInMessage = fmt.Sprintf("Authenticate using one of the following domains: %v", strings.Join(opts.EmailDomains, ", "))
70 } else if opts.EmailDomains[0] != "*" {
71 oauthproxy.SignInMessage = fmt.Sprintf("Authenticate using %v", opts.EmailDomains[0])
72 }
73 }
75 rand.Seed(time.Now().UnixNano())
77 chain := alice.New()
79 if opts.ForceHTTPS {
80 _, httpsPort, err := net.SplitHostPort(opts.HTTPSAddress)
81 if err != nil {
82 logger.Fatalf("FATAL: invalid HTTPS address %q: %v", opts.HTTPAddress, err)
83 }
84 chain = chain.Append(middleware.NewRedirectToHTTPS(httpsPort))
85 }
87 healthCheckPaths := []string{opts.PingPath}
88 healthCheckUserAgents := []string{opts.PingUserAgent}
89 if opts.GCPHealthChecks {
90 healthCheckPaths = append(healthCheckPaths, "/liveness_check", "/readiness_check")
91 healthCheckUserAgents = append(healthCheckUserAgents, "GoogleHC/1.0")
92 }
94 // To silence logging of health checks, register the health check handler before
95 // the logging handler
96 if opts.Logging.SilencePing {
97 chain = chain.Append(middleware.NewHealthCheck(healthCheckPaths, healthCheckUserAgents), LoggingHandler)
98 } else {
99 chain = chain.Append(LoggingHandler, middleware.NewHealthCheck(healthCheckPaths, healthCheckUserAgents))
100 }
102 s := &Server{
103 Handler: chain.Then(oauthproxy),
104 Opts: opts,
105 stop: make(chan struct{}, 1),
106 }
107 // Observe signals in background goroutine.
108 go func() {
109 sigint := make(chan os.Signal, 1)
110 signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
111 <-sigint
112 s.stop <- struct{}{} // notify having caught signal
113 }()
114 s.ListenAndServe()