@@ -9,6 +9,7 @@ import (
99 "syscall"
1010 "time"
1111
12+ "github.com/ghodss/yaml"
1213 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
1314 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
1415 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/validation"
@@ -21,6 +22,7 @@ func main() {
2122 configFlagSet := pflag.NewFlagSet("oauth2-proxy", pflag.ContinueOnError)
2223 config := configFlagSet.String("config", "", "path to config file")
2324 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")
2426 showVersion := configFlagSet.Bool("version", false, "print version string")
2527 configFlagSet.Parse(os.Args[1:])
2628
@@ -29,12 +31,23 @@ func main() {
2931 return
3032 }
3133
34+ if *convertConfig && *alphaConfig != "" {
35+ logger.Fatal("cannot use alpha-config and conver-config-to-alpha together")
36+ }
37+
3238 opts, err := loadConfiguration(*config, *alphaConfig, configFlagSet, os.Args[1:])
3339 if err != nil {
3440 logger.Printf("ERROR: %v", err)
3541 os.Exit(1)
3642 }
3743
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+
3851 err = validation.Validate(opts)
3952 if err != nil {
4053 logger.Printf("%s", err)
@@ -134,3 +147,21 @@ func loadOptions(config string, extraFlags *pflag.FlagSet, args []string) (*opti
134147
135148 return opts, nil
136149}
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+}