git.iko.soy/typeable/oauth2-proxy
on 597ffeb help sign in
167 lines 5.5 kB history raw
1package main
3import (
4 "fmt"
5 "math/rand"
6 "os"
7 "os/signal"
8 "runtime"
9 "syscall"
10 "time"
12 "github.com/ghodss/yaml"
13 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
14 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
15 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/validation"
16 "github.com/spf13/pflag"
19func main() {
20 logger.SetFlags(logger.Lshortfile)
22 configFlagSet := pflag.NewFlagSet("oauth2-proxy", pflag.ContinueOnError)
23 config := configFlagSet.String("config", "", "path to config file")
24 alphaConfig := configFlagSet.String("alpha-config", "", "path to alpha config file (use at your own risk - the structure in this config file may change between minor releases)")
25 convertConfig := configFlagSet.Bool("convert-config-to-alpha", false, "if true, the proxy will load configuration as normal and convert existing configuration to the alpha config structure, and print it to stdout")
26 showVersion := configFlagSet.Bool("version", false, "print version string")
27 configFlagSet.Parse(os.Args[1:])
29 if *showVersion {
30 fmt.Printf("oauth2-proxy %s (built with %s)\n", VERSION, runtime.Version())
31 return
32 }
34 if *convertConfig && *alphaConfig != "" {
35 logger.Fatal("cannot use alpha-config and conver-config-to-alpha together")
36 }
38 opts, err := loadConfiguration(*config, *alphaConfig, configFlagSet, os.Args[1:])
39 if err != nil {
40 logger.Printf("ERROR: %v", err)
41 os.Exit(1)
42 }
44 if *convertConfig {
45 if err := printConvertedConfig(opts); err != nil {
46 logger.Fatalf("ERROR: could not convert config: %v", err)
47 }
48 return
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 s := &Server{
67 Handler: oauthproxy,
68 Opts: opts,
69 stop: make(chan struct{}, 1),
70 }
71 // Observe signals in background goroutine.
72 go func() {
73 sigint := make(chan os.Signal, 1)
74 signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
75 <-sigint
76 s.stop <- struct{}{} // notify having caught signal
77 }()
78 s.ListenAndServe()
81// loadConfiguration will load in the user's configuration.
82// It will either load the alpha configuration (if alphaConfig is given)
83// or the legacy configuration.
84func loadConfiguration(config, alphaConfig string, extraFlags *pflag.FlagSet, args []string) (*options.Options, error) {
85 if alphaConfig != "" {
86 logger.Printf("WARNING: You are using alpha configuration. The structure in this configuration file may change without notice. You MUST remove conflicting options from your existing configuration.")
87 return loadAlphaOptions(config, alphaConfig, extraFlags, args)
88 }
89 return loadLegacyOptions(config, extraFlags, args)
92// loadLegacyOptions loads the old toml options using the legacy flagset
93// and legacy options struct.
94func loadLegacyOptions(config string, extraFlags *pflag.FlagSet, args []string) (*options.Options, error) {
95 optionsFlagSet := options.NewLegacyFlagSet()
96 optionsFlagSet.AddFlagSet(extraFlags)
97 if err := optionsFlagSet.Parse(args); err != nil {
98 return nil, fmt.Errorf("failed to parse flags: %v", err)
99 }
101 legacyOpts := options.NewLegacyOptions()
102 if err := options.Load(config, optionsFlagSet, legacyOpts); err != nil {
103 return nil, fmt.Errorf("failed to load config: %v", err)
104 }
106 opts, err := legacyOpts.ToOptions()
107 if err != nil {
108 return nil, fmt.Errorf("failed to convert config: %v", err)
109 }
111 return opts, nil
114// loadAlphaOptions loads the old style config excluding options converted to
115// the new alpha format, then merges the alpha options, loaded from YAML,
116// into the core configuration.
117func loadAlphaOptions(config, alphaConfig string, extraFlags *pflag.FlagSet, args []string) (*options.Options, error) {
118 opts, err := loadOptions(config, extraFlags, args)
119 if err != nil {
120 return nil, fmt.Errorf("failed to load core options: %v", err)
121 }
123 alphaOpts := &options.AlphaOptions{}
124 if err := options.LoadYAML(alphaConfig, alphaOpts); err != nil {
125 return nil, fmt.Errorf("failed to load alpha options: %v", err)
126 }
128 alphaOpts.MergeInto(opts)
129 return opts, nil
132// loadOptions loads the configuration using the old style format into the
133// core options.Options struct.
134// This means that none of the options that have been converted to alpha config
135// will be loaded using this method.
136func loadOptions(config string, extraFlags *pflag.FlagSet, args []string) (*options.Options, error) {
137 optionsFlagSet := options.NewFlagSet()
138 optionsFlagSet.AddFlagSet(extraFlags)
139 if err := optionsFlagSet.Parse(args); err != nil {
140 return nil, fmt.Errorf("failed to parse flags: %v", err)
141 }
143 opts := options.NewOptions()
144 if err := options.Load(config, optionsFlagSet, opts); err != nil {
145 return nil, fmt.Errorf("failed to load config: %v", err)
146 }
148 return opts, nil
151// printConvertedConfig extracts alpha options from the loaded configuration
152// and renders these to stdout in YAML format.
153func printConvertedConfig(opts *options.Options) error {
154 alphaConfig := &options.AlphaOptions{}
155 alphaConfig.ExtractFrom(opts)
157 data, err := yaml.Marshal(alphaConfig)
158 if err != nil {
159 return fmt.Errorf("unable to marshal config: %v", err)
160 }
162 if _, err := os.Stdout.Write(data); err != nil {
163 return fmt.Errorf("unable to write output: %v", err)
164 }
166 return nil