Add convert-config-to-alpha flag to convert existing configuration to alpha structure
This commit is contained in:
		
							parent
							
								
									5b683a7631
								
							
						
					
					
						commit
						b201dbb2d3
					
				
							
								
								
									
										31
									
								
								main.go
								
								
								
								
							
							
						
						
									
										31
									
								
								main.go
								
								
								
								
							|  | @ -9,6 +9,7 @@ import ( | ||||||
| 	"syscall" | 	"syscall" | ||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
|  | 	"github.com/ghodss/yaml" | ||||||
| 	"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options" | 	"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options" | ||||||
| 	"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger" | 	"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger" | ||||||
| 	"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/validation" | 	"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/validation" | ||||||
|  | @ -21,6 +22,7 @@ func main() { | ||||||
| 	configFlagSet := pflag.NewFlagSet("oauth2-proxy", pflag.ContinueOnError) | 	configFlagSet := pflag.NewFlagSet("oauth2-proxy", pflag.ContinueOnError) | ||||||
| 	config := configFlagSet.String("config", "", "path to config file") | 	config := configFlagSet.String("config", "", "path to config file") | ||||||
| 	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)") | 	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)") | ||||||
|  | 	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") | ||||||
| 	showVersion := configFlagSet.Bool("version", false, "print version string") | 	showVersion := configFlagSet.Bool("version", false, "print version string") | ||||||
| 	configFlagSet.Parse(os.Args[1:]) | 	configFlagSet.Parse(os.Args[1:]) | ||||||
| 
 | 
 | ||||||
|  | @ -29,12 +31,23 @@ func main() { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	if *convertConfig && *alphaConfig != "" { | ||||||
|  | 		logger.Fatal("cannot use alpha-config and conver-config-to-alpha together") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	opts, err := loadConfiguration(*config, *alphaConfig, configFlagSet, os.Args[1:]) | 	opts, err := loadConfiguration(*config, *alphaConfig, configFlagSet, os.Args[1:]) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		logger.Printf("ERROR: %v", err) | 		logger.Printf("ERROR: %v", err) | ||||||
| 		os.Exit(1) | 		os.Exit(1) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	if *convertConfig { | ||||||
|  | 		if err := printConvertedConfig(opts); err != nil { | ||||||
|  | 			logger.Fatalf("ERROR: could not convert config: %v", err) | ||||||
|  | 		} | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	err = validation.Validate(opts) | 	err = validation.Validate(opts) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		logger.Printf("%s", err) | 		logger.Printf("%s", err) | ||||||
|  | @ -134,3 +147,21 @@ func loadOptions(config string, extraFlags *pflag.FlagSet, args []string) (*opti | ||||||
| 
 | 
 | ||||||
| 	return opts, nil | 	return opts, nil | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | // printConvertedConfig extracts alpha options from the loaded configuration
 | ||||||
|  | // and renders these to stdout in YAML format.
 | ||||||
|  | func printConvertedConfig(opts *options.Options) error { | ||||||
|  | 	alphaConfig := &options.AlphaOptions{} | ||||||
|  | 	alphaConfig.ExtractFrom(opts) | ||||||
|  | 
 | ||||||
|  | 	data, err := yaml.Marshal(alphaConfig) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return fmt.Errorf("unable to marshal config: %v", err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if _, err := os.Stdout.Write(data); err != nil { | ||||||
|  | 		return fmt.Errorf("unable to write output: %v", err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -37,3 +37,11 @@ func (a *AlphaOptions) MergeInto(opts *Options) { | ||||||
| 	opts.InjectRequestHeaders = a.InjectRequestHeaders | 	opts.InjectRequestHeaders = a.InjectRequestHeaders | ||||||
| 	opts.InjectResponseHeaders = a.InjectResponseHeaders | 	opts.InjectResponseHeaders = a.InjectResponseHeaders | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | // ExtractFrom populates the fields in the AlphaOptions with the values from
 | ||||||
|  | // the Options
 | ||||||
|  | func (a *AlphaOptions) ExtractFrom(opts *Options) { | ||||||
|  | 	a.Upstreams = opts.UpstreamServers | ||||||
|  | 	a.InjectRequestHeaders = opts.InjectRequestHeaders | ||||||
|  | 	a.InjectResponseHeaders = opts.InjectResponseHeaders | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue