git.iko.soy/typeable/oauth2-proxy
on master help sign in
184 lines 6.2 kB history raw
1package main
3import (
4 "fmt"
5 "os"
6 "runtime"
8 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
9 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
10 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/validation"
11 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/version"
12 "github.com/spf13/pflag"
13 "go.yaml.in/yaml/v3"
16func main() {
17 logger.SetFlags(logger.Lshortfile)
19 configFlagSet := pflag.NewFlagSet("oauth2-proxy", pflag.ContinueOnError)
21 // Because we parse early to determine alpha vs legacy config, we have to
22 // ignore any unknown flags for now
23 configFlagSet.ParseErrorsAllowlist.UnknownFlags = true
25 config := configFlagSet.String("config", "", "path to config file")
26 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)")
27 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")
28 showVersion := configFlagSet.Bool("version", false, "print version string")
29 configTest := configFlagSet.Bool("config-test", false, "test the configuration and exit")
30 configFlagSet.Parse(os.Args[1:])
32 if *showVersion {
33 fmt.Printf("oauth2-proxy %s (built with %s)\n", version.VERSION, runtime.Version())
34 return
35 }
37 if *convertConfig && *alphaConfig != "" {
38 logger.Fatal("cannot use alpha-config and convert-config-to-alpha together")
39 }
41 if *configTest && *convertConfig {
42 logger.Fatal("cannot use config-test and convert-config-to-alpha together")
43 }
45 opts, err := loadConfiguration(*config, *alphaConfig, configFlagSet, os.Args[1:])
46 if err != nil {
47 logger.Fatalf("ERROR: %v", err)
48 }
50 if *configTest {
51 if err = validation.Validate(opts); err != nil {
52 logger.Errorf("%s", err)
53 os.Exit(1)
54 }
55 fmt.Println("configuration is valid")
56 return
57 }
59 if *convertConfig {
60 if err := printConvertedConfig(opts); err != nil {
61 logger.Fatalf("ERROR: could not convert config: %v", err)
62 }
63 return
64 }
66 if err = validation.Validate(opts); err != nil {
67 logger.Fatalf("%s", err)
68 }
70 validator := NewValidator(opts.EmailDomains, opts.AuthenticatedEmailsFile)
71 oauthproxy, err := NewOAuthProxy(opts, validator)
72 if err != nil {
73 logger.Fatalf("ERROR: Failed to initialise OAuth2 Proxy: %v", err)
74 }
76 if err := oauthproxy.Start(); err != nil {
77 logger.Fatalf("ERROR: Failed to start OAuth2 Proxy: %v", err)
78 }
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, yamlConfig string, extraFlags *pflag.FlagSet, args []string) (*options.Options, error) {
85 opts, err := loadLegacyOptions(config, extraFlags, args)
86 if err != nil {
87 return nil, fmt.Errorf("failed to load legacy options: %w", err)
88 }
90 if yamlConfig != "" {
91 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.")
92 opts, err = loadYamlOptions(yamlConfig, config, extraFlags, args)
93 if err != nil {
94 return nil, fmt.Errorf("failed to load yaml options: %w", err)
95 }
96 }
98 // Ensure defaults after loading configuration
99 opts.EnsureDefaults()
100 return opts, nil
103// loadLegacyOptions loads the old toml options using the legacy flagset
104// and legacy options struct.
105func loadLegacyOptions(config string, extraFlags *pflag.FlagSet, args []string) (*options.Options, error) {
106 optionsFlagSet := options.NewLegacyFlagSet()
107 optionsFlagSet.AddFlagSet(extraFlags)
108 if err := optionsFlagSet.Parse(args); err != nil {
109 return nil, fmt.Errorf("failed to parse flags: %v", err)
110 }
112 legacyOpts := options.NewLegacyOptions()
113 if err := options.Load(config, optionsFlagSet, legacyOpts); err != nil {
114 return nil, fmt.Errorf("failed to load config: %v", err)
115 }
117 opts, err := legacyOpts.ToOptions()
118 if err != nil {
119 return nil, fmt.Errorf("failed to convert config: %v", err)
120 }
122 return opts, nil
125// loadYamlOptions loads the old style config excluding options converted to
126// the new alpha format, then merges the alpha options, loaded from YAML,
127// into the core configuration.
128func loadYamlOptions(yamlConfig, config string, extraFlags *pflag.FlagSet, args []string) (*options.Options, error) {
129 opts, err := loadOptions(config, extraFlags, args)
130 if err != nil {
131 return nil, fmt.Errorf("failed to load core options: %v", err)
132 }
134 alphaOpts := options.NewAlphaOptions(opts)
135 if err := options.LoadYAML(yamlConfig, alphaOpts); err != nil {
136 return nil, fmt.Errorf("failed to load alpha options: %v", err)
137 }
139 alphaOpts.MergeOptionsWithDefaults(opts)
140 return opts, nil
143// loadOptions loads the configuration using the old style format into the
144// core options.Options struct.
145// This means that none of the options that have been converted to alpha config
146// will be loaded using this method.
147func loadOptions(config string, extraFlags *pflag.FlagSet, args []string) (*options.Options, error) {
148 optionsFlagSet := options.NewFlagSet()
149 optionsFlagSet.AddFlagSet(extraFlags)
150 if err := optionsFlagSet.Parse(args); err != nil {
151 return nil, fmt.Errorf("failed to parse flags: %v", err)
152 }
154 opts := options.NewOptions()
155 if err := options.Load(config, optionsFlagSet, opts); err != nil {
156 return nil, fmt.Errorf("failed to load config: %v", err)
157 }
159 return opts, nil
162// printConvertedConfig extracts alpha options from the loaded configuration
163// and renders these to stdout in YAML format.
164func printConvertedConfig(opts *options.Options) error {
165 alphaConfig := options.NewAlphaOptions(opts)
167 // Generic interface for loading arbitrary yaml structure
168 var buffer map[string]interface{}
170 if err := options.Decode(alphaConfig, &buffer); err != nil {
171 return fmt.Errorf("unable to decode alpha config into interface: %w", err)
172 }
174 data, err := yaml.Marshal(buffer)
175 if err != nil {
176 return fmt.Errorf("unable to marshal config: %v", err)
177 }
179 if _, err := os.Stdout.Write(data); err != nil {
180 return fmt.Errorf("unable to write output: %v", err)
181 }
183 return nil