Skip to content

Commit

Permalink
feat: simpler config loading; separate paths for windows/nix
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan committed Oct 17, 2023
1 parent b973e2e commit de17905
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 52 deletions.
84 changes: 37 additions & 47 deletions bin/vproxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func fileExists(name string) bool {
return true
}

// findConfig locates a config file at the given locations with either a .conf or .toml extension
// (file format must be TOML, however)
func findConfig(files ...string) string {
for _, config := range files {
if config != "" {
Expand All @@ -70,14 +72,6 @@ func homeConfPath() string {
return ""
}

func findClientConfig(path string) string {
return findConfig(path, ".vproxy.conf", homeConfPath(), "/usr/local/etc/vproxy.conf", "/etc/vproxy.conf")
}

func findDaemonConfig(path string) string {
return findConfig(path, homeConfPath(), "/usr/local/etc/vproxy.conf", "/etc/vproxy.conf")
}

func loadConfigFile(path string) (*Config, error) {
t, err := toml.LoadFile(path)
if err != nil {
Expand All @@ -102,48 +96,21 @@ func cleanListenAddr(c *cli.Context) {
}

func loadClientConfig(c *cli.Context) error {
conf := findClientConfig(c.String("config"))
if cf := c.String("config"); c.IsSet("config") && conf != cf {
log.Fatalf("error: config file not found: %s\n", cf)
}
if conf == "" {
return nil
}
verbose(c, "Loading config file %s", conf)
config, err := loadConfigFile(conf)
if err != nil {
return err
}
if config != nil {
if v := (config.Client.Verbose || config.Verbose); v && !c.IsSet("verbose") {
c.Lineage()[1].Set("verbose", "true")
verbose(c, "Loading config file %s", conf)
verbose(c, "via conf: verbose=true")
}
if v := config.Client.Host; v != "" && !c.IsSet("host") {
verbose(c, "via conf: host=%s", v)
c.Set("host", v)
}
if v := config.Client.HTTP; v > 0 && !c.IsSet("http") {
verbose(c, "via conf: http=%d", v)
c.Set("http", strconv.Itoa(v))
}
if v := config.Client.Bind; v != "" && !c.IsSet("bind") {
verbose(c, "via conf: bind=%s", v)
c.Set("bind", v)
}
if v := config.Server.CaRootPath; v != "" {
os.Setenv("CAROOT_PATH", v)
verbose(c, "via conf: CAROOT_PATH=%s", v)
}
}
return nil
conf := findConfigFile(c.String("config"), false)
return loadConfig(c, conf)
}

func loadDaemonConfig(c *cli.Context) error {
conf := findClientConfig(c.String("config"))
if cf := c.String("config"); c.IsSet("config") && conf != cf {
log.Fatalf("error: config file not found: %s\n", cf)
conf := findConfigFile(c.String("config"), true)
return loadConfig(c, conf)
}

func loadConfig(c *cli.Context, conf string) error {
if c.IsSet("config") {
if cf := c.String("config"); conf != cf {
// config flag was passed but file does not exist
log.Fatalf("error: config file not found: %s\n", cf)
}
}
if conf == "" {
return nil
Expand Down Expand Up @@ -181,6 +148,29 @@ func loadDaemonConfig(c *cli.Context) error {
os.Setenv("CERT_PATH", v)
verbose(c, "via conf: CERT_PATH=%s", v)
}

// client configs
if v := (config.Client.Verbose || config.Verbose); v && !c.IsSet("verbose") {
c.Lineage()[1].Set("verbose", "true")
verbose(c, "Loading config file %s", conf)
verbose(c, "via conf: verbose=true")
}
if v := config.Client.Host; v != "" && !c.IsSet("host") {
verbose(c, "via conf: host=%s", v)
c.Set("host", v)
}
if v := config.Client.HTTP; v > 0 && !c.IsSet("http") {
verbose(c, "via conf: http=%d", v)
c.Set("http", strconv.Itoa(v))
}
if v := config.Client.Bind; v != "" && !c.IsSet("bind") {
verbose(c, "via conf: bind=%s", v)
c.Set("bind", v)
}
if v := config.Server.CaRootPath; v != "" {
os.Setenv("CAROOT_PATH", v)
verbose(c, "via conf: CAROOT_PATH=%s", v)
}
}
cleanListenAddr(c)
return nil
Expand Down
13 changes: 13 additions & 0 deletions bin/vproxy/config_nix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build linux || darwin

package main

func findConfigFile(path string, isDaemon bool) string {
paths := []string{path}
if !isDaemon {
// look for dot file only for clients
paths = append(paths, ".vproxy.conf")
}
paths = append(paths, homeConfPath(), "/usr/local/etc/vproxy.conf", "/etc/vproxy.conf")
return findConfig(paths...)
}
13 changes: 13 additions & 0 deletions bin/vproxy/config_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build windows

package main

func findConfigFile(path string, isDaemon bool) string {
paths := []string{path}
if !isDaemon {
// look for dot file only for clients
paths = append(paths, ".vproxy.conf")
}
paths = append(paths, homeConfPath())
return findConfig(paths...)
}
9 changes: 5 additions & 4 deletions bin/vproxy/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,11 @@ vproxy connect hello.local:8888 -- vproxy hello
},
},
{
Name: "info",
Usage: "Print vproxy configuration",
Before: loadDaemonConfig,
Action: printInfo,
Name: "info",
Usage: "Print vproxy configuration",
Description: `More verbose info: vproxy -v info`,
Before: loadDaemonConfig,
Action: printInfo,
},
{
Name: "hello",
Expand Down
10 changes: 9 additions & 1 deletion bin/vproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,16 @@ func printInfo(c *cli.Context) error {
printVersion(c)
fmt.Printf(" CAROOT=%s\n", vproxy.CARootPath())
fmt.Printf(" CERT_PATH=%s\n", vproxy.CertPath())

confFile := findConfigFile(c.String("config"), false)
if confFile == "" {
fmt.Printf("\n Config file: [not found]\n")
} else {
fmt.Printf("\n Detected Config file: %s (loaded)\n", confFile)
}

certs, _ := filepath.Glob(filepath.Join(vproxy.CertPath(), "*-key.pem"))
fmt.Printf("\n Nubmer of installed certs: %d\n", len(certs))
fmt.Printf("\n Number of installed certs: %d\n", len(certs))
fmt.Println(" Certs:")
for _, cert := range certs {
host := strings.TrimPrefix(strings.TrimSuffix(cert, "-key.pem"), vproxy.CertPath()+string(filepath.Separator))
Expand Down

0 comments on commit de17905

Please sign in to comment.