git.iko.soy/typeable/oauth2-proxy
on ea261ca help sign in
156 lines 5.4 kB history raw
1package main
3import (
4 "fmt"
5 "math/rand"
6 "os"
7 "runtime"
8 "time"
10 "github.com/ghodss/yaml"
11 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
12 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
13 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/validation"
14 "github.com/spf13/pflag"
17func main() {
18 logger.SetFlags(logger.Lshortfile)
20 configFlagSet := pflag.NewFlagSet("oauth2-proxy", pflag.ContinueOnError)
22 // Because we parse early to determine alpha vs legacy config, we have to
23 // ignore any unknown flags for now
24 configFlagSet.ParseErrorsWhitelist.UnknownFlags = true
26 config := configFlagSet.String("config", "", "path to config file")
27 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)")
28 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")
29 showVersion := configFlagSet.Bool("version", false, "print version string")
30 configFlagSet.Parse(os.Args[1:])
32 if *showVersion {
33 fmt.Printf("oauth2-proxy %s (built with %s)\n", VERSION, runtime.Version())
34 return
35 }
37 if *convertConfig && *alphaConfig != "" {
38 logger.Fatal("cannot use alpha-config and conver-config-to-alpha together")
39 }
41 opts, err := loadConfiguration(*config, *alphaConfig, configFlagSet, os.Args[1:])
42 if err != nil {
43 logger.Fatalf("ERROR: %v", err)
44 }
46 if *convertConfig {
47 if err := printConvertedConfig(opts); err != nil {
48 logger.Fatalf("ERROR: could not convert config: %v", err)
49 }
50 return
51 }
53 if err = validation.Validate(opts); err != nil {
54 logger.Fatalf("%s", err)
55 }
57 validator := NewValidator(opts.EmailDomains, opts.AuthenticatedEmailsFile)
58 oauthproxy, err := NewOAuthProxy(opts, validator)
59 if err != nil {
60 logger.Fatalf("ERROR: Failed to initialise OAuth2 Proxy: %v", err)
61 }
63 rand.Seed(time.Now().UnixNano())
65 if err := oauthproxy.Start(); err != nil {
66 logger.Fatalf("ERROR: Failed to start OAuth2 Proxy: %v", err)
67 }
70// loadConfiguration will load in the user's configuration.
71// It will either load the alpha configuration (if alphaConfig is given)
72// or the legacy configuration.
73func loadConfiguration(config, alphaConfig string, extraFlags *pflag.FlagSet, args []string) (*options.Options, error) {
74 if alphaConfig != "" {
75 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.")
76 return loadAlphaOptions(config, alphaConfig, extraFlags, args)
77 }
78 return loadLegacyOptions(config, extraFlags, args)
81// loadLegacyOptions loads the old toml options using the legacy flagset
82// and legacy options struct.
83func loadLegacyOptions(config string, extraFlags *pflag.FlagSet, args []string) (*options.Options, error) {
84 optionsFlagSet := options.NewLegacyFlagSet()
85 optionsFlagSet.AddFlagSet(extraFlags)
86 if err := optionsFlagSet.Parse(args); err != nil {
87 return nil, fmt.Errorf("failed to parse flags: %v", err)
88 }
90 legacyOpts := options.NewLegacyOptions()
91 if err := options.Load(config, optionsFlagSet, legacyOpts); err != nil {
92 return nil, fmt.Errorf("failed to load config: %v", err)
93 }
95 opts, err := legacyOpts.ToOptions()
96 if err != nil {
97 return nil, fmt.Errorf("failed to convert config: %v", err)
98 }
100 return opts, nil
103// loadAlphaOptions loads the old style config excluding options converted to
104// the new alpha format, then merges the alpha options, loaded from YAML,
105// into the core configuration.
106func loadAlphaOptions(config, alphaConfig string, extraFlags *pflag.FlagSet, args []string) (*options.Options, error) {
107 opts, err := loadOptions(config, extraFlags, args)
108 if err != nil {
109 return nil, fmt.Errorf("failed to load core options: %v", err)
110 }
112 alphaOpts := &options.AlphaOptions{}
113 if err := options.LoadYAML(alphaConfig, alphaOpts); err != nil {
114 return nil, fmt.Errorf("failed to load alpha options: %v", err)
115 }
117 alphaOpts.MergeInto(opts)
118 return opts, nil
121// loadOptions loads the configuration using the old style format into the
122// core options.Options struct.
123// This means that none of the options that have been converted to alpha config
124// will be loaded using this method.
125func loadOptions(config string, extraFlags *pflag.FlagSet, args []string) (*options.Options, error) {
126 optionsFlagSet := options.NewFlagSet()
127 optionsFlagSet.AddFlagSet(extraFlags)
128 if err := optionsFlagSet.Parse(args); err != nil {
129 return nil, fmt.Errorf("failed to parse flags: %v", err)
130 }
132 opts := options.NewOptions()
133 if err := options.Load(config, optionsFlagSet, opts); err != nil {
134 return nil, fmt.Errorf("failed to load config: %v", err)
135 }
137 return opts, nil
140// printConvertedConfig extracts alpha options from the loaded configuration
141// and renders these to stdout in YAML format.
142func printConvertedConfig(opts *options.Options) error {
143 alphaConfig := &options.AlphaOptions{}
144 alphaConfig.ExtractFrom(opts)
146 data, err := yaml.Marshal(alphaConfig)
147 if err != nil {
148 return fmt.Errorf("unable to marshal config: %v", err)
149 }
151 if _, err := os.Stdout.Write(data); err != nil {
152 return fmt.Errorf("unable to write output: %v", err)
153 }
155 return nil