git.iko.soy/typeable/oauth2-proxy
on v6.0.0 help sign in
109 lines 3.1 kB history raw
1package main
3import (
4 "fmt"
5 "math/rand"
6 "os"
7 "os/signal"
8 "runtime"
9 "strings"
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 flagSet.Parse(os.Args[1:])
29 if *showVersion {
30 fmt.Printf("oauth2-proxy %s (built with %s)\n", VERSION, runtime.Version())
31 return
32 }
34 opts := options.NewOptions()
35 err := options.Load(*config, flagSet, opts)
36 if err != nil {
37 logger.Printf("ERROR: Failed to load config: %v", err)
38 os.Exit(1)
39 }
41 err = validation.Validate(opts)
42 if err != nil {
43 logger.Printf("%s", err)
44 os.Exit(1)
45 }
47 validator := NewValidator(opts.EmailDomains, opts.AuthenticatedEmailsFile)
48 oauthproxy := NewOAuthProxy(opts, validator)
50 if len(opts.Banner) >= 1 {
51 if opts.Banner == "-" {
52 oauthproxy.SignInMessage = ""
53 } else {
54 oauthproxy.SignInMessage = opts.Banner
55 }
56 } else if len(opts.EmailDomains) != 0 && opts.AuthenticatedEmailsFile == "" {
57 if len(opts.EmailDomains) > 1 {
58 oauthproxy.SignInMessage = fmt.Sprintf("Authenticate using one of the following domains: %v", strings.Join(opts.EmailDomains, ", "))
59 } else if opts.EmailDomains[0] != "*" {
60 oauthproxy.SignInMessage = fmt.Sprintf("Authenticate using %v", opts.EmailDomains[0])
61 }
62 }
64 if opts.HtpasswdFile != "" {
65 logger.Printf("using htpasswd file %s", opts.HtpasswdFile)
66 oauthproxy.HtpasswdFile, err = NewHtpasswdFromFile(opts.HtpasswdFile)
67 oauthproxy.DisplayHtpasswdForm = opts.DisplayHtpasswdForm
68 if err != nil {
69 logger.Fatalf("FATAL: unable to open %s %s", opts.HtpasswdFile, err)
70 }
71 }
73 rand.Seed(time.Now().UnixNano())
75 chain := alice.New()
77 if opts.ForceHTTPS {
78 chain = chain.Append(newRedirectToHTTPS(opts))
79 }
81 healthCheckPaths := []string{opts.PingPath}
82 healthCheckUserAgents := []string{opts.PingUserAgent}
83 if opts.GCPHealthChecks {
84 healthCheckPaths = append(healthCheckPaths, "/liveness_check", "/readiness_check")
85 healthCheckUserAgents = append(healthCheckUserAgents, "GoogleHC/1.0")
86 }
88 // To silence logging of health checks, register the health check handler before
89 // the logging handler
90 if opts.Logging.SilencePing {
91 chain = chain.Append(middleware.NewHealthCheck(healthCheckPaths, healthCheckUserAgents), LoggingHandler)
92 } else {
93 chain = chain.Append(LoggingHandler, middleware.NewHealthCheck(healthCheckPaths, healthCheckUserAgents))
94 }
96 s := &Server{
97 Handler: chain.Then(oauthproxy),
98 Opts: opts,
99 stop: make(chan struct{}, 1),
100 }
101 // Observe signals in background goroutine.
102 go func() {
103 sigint := make(chan os.Signal, 1)
104 signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
105 <-sigint
106 s.stop <- struct{}{} // notify having caught signal
107 }()
108 s.ListenAndServe()