git.iko.soy/typeable/oauth2-proxy
on master help sign in
Add convert-config-to-alpha flag to convert existing configuration to alpha structure
b201dbbJoel Speed
@@ -9,6 +9,7 @@ import (
9 "syscall"
9 "syscall"
10 "time"
10 "time"
11
11
12+ "github.com/ghodss/yaml"
12 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
13 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
13 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
14 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
14 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/validation"
15 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/validation"
@@ -21,6 +22,7 @@ func main() {
21 configFlagSet := pflag.NewFlagSet("oauth2-proxy", pflag.ContinueOnError)
22 configFlagSet := pflag.NewFlagSet("oauth2-proxy", pflag.ContinueOnError)
22 config := configFlagSet.String("config", "", "path to config file")
23 config := configFlagSet.String("config", "", "path to config file")
23 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)")
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")
24 showVersion := configFlagSet.Bool("version", false, "print version string")
26 showVersion := configFlagSet.Bool("version", false, "print version string")
25 configFlagSet.Parse(os.Args[1:])
27 configFlagSet.Parse(os.Args[1:])
26
28
@@ -29,12 +31,23 @@ func main() {
29 return
31 return
30 }
32 }
31
33
34+ if *convertConfig && *alphaConfig != "" {
35+ logger.Fatal("cannot use alpha-config and conver-config-to-alpha together")
36+ }
37+
32 opts, err := loadConfiguration(*config, *alphaConfig, configFlagSet, os.Args[1:])
38 opts, err := loadConfiguration(*config, *alphaConfig, configFlagSet, os.Args[1:])
33 if err != nil {
39 if err != nil {
34 logger.Printf("ERROR: %v", err)
40 logger.Printf("ERROR: %v", err)
35 os.Exit(1)
41 os.Exit(1)
36 }
42 }
37
43
44+ if *convertConfig {
45+ if err := printConvertedConfig(opts); err != nil {
46+ logger.Fatalf("ERROR: could not convert config: %v", err)
47+ }
48+ return
49+ }
50+
38 err = validation.Validate(opts)
51 err = validation.Validate(opts)
39 if err != nil {
52 if err != nil {
40 logger.Printf("%s", err)
53 logger.Printf("%s", err)
@@ -134,3 +147,21 @@ func loadOptions(config string, extraFlags *pflag.FlagSet, args []string) (*opti
134
147
135 return opts, nil
148 return opts, nil
136}
149}
150+
151+// printConvertedConfig extracts alpha options from the loaded configuration
152+// and renders these to stdout in YAML format.
153+func printConvertedConfig(opts *options.Options) error {
154+ alphaConfig := &options.AlphaOptions{}
155+ alphaConfig.ExtractFrom(opts)
156+
157+ data, err := yaml.Marshal(alphaConfig)
158+ if err != nil {
159+ return fmt.Errorf("unable to marshal config: %v", err)
160+ }
161+
162+ if _, err := os.Stdout.Write(data); err != nil {
163+ return fmt.Errorf("unable to write output: %v", err)
164+ }
165+
166+ return nil
167+}
pkg/apis/options/alpha_options.go+8
@@ -37,3 +37,11 @@ func (a *AlphaOptions) MergeInto(opts *Options) {
37 opts.InjectRequestHeaders = a.InjectRequestHeaders
37 opts.InjectRequestHeaders = a.InjectRequestHeaders
38 opts.InjectResponseHeaders = a.InjectResponseHeaders
38 opts.InjectResponseHeaders = a.InjectResponseHeaders
39}
39}
40+
41+// ExtractFrom populates the fields in the AlphaOptions with the values from
42+// the Options
43+func (a *AlphaOptions) ExtractFrom(opts *Options) {
44+ a.Upstreams = opts.UpstreamServers
45+ a.InjectRequestHeaders = opts.InjectRequestHeaders
46+ a.InjectResponseHeaders = opts.InjectResponseHeaders
47+}