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"
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:])
30 fmt.Printf("oauth2-proxy %s (built with %s)\n", VERSION, runtime.Version())
34 if *convertConfig && *alphaConfig != "" {
35 logger.Fatal("cannot use alpha-config and conver-config-to-alpha together")
38 opts, err := loadConfiguration(*config, *alphaConfig, configFlagSet, os.Args[1:])
40 logger.Printf("ERROR: %v", err)
45 if err := printConvertedConfig(opts); err != nil {
46 logger.Fatalf("ERROR: could not convert config: %v", err)
51 err = validation.Validate(opts)
53 logger.Printf("%s", err)
57 validator := NewValidator(opts.EmailDomains, opts.AuthenticatedEmailsFile)
58 oauthproxy, err := NewOAuthProxy(opts, validator)
60 logger.Errorf("ERROR: Failed to initialise OAuth2 Proxy: %v", err)
64 rand.Seed(time.Now().UnixNano())
69 stop: make(chan struct{}, 1),
71 // Observe signals in background goroutine.
73 sigint := make(chan os.Signal, 1)
74 signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
76 s.stop <- struct{}{} // notify having caught signal
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)
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)
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)
106 opts, err := legacyOpts.ToOptions()
108 return nil, fmt.Errorf("failed to convert config: %v", err)
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)
120 return nil, fmt.Errorf("failed to load core options: %v", err)
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)
128 alphaOpts.MergeInto(opts)
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)
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)
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)
159 return fmt.Errorf("unable to marshal config: %v", err)
162 if _, err := os.Stdout.Write(data); err != nil {
163 return fmt.Errorf("unable to write output: %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