Skip to content

Commit

Permalink
Merge pull request #11 from influxdata/10-configuration-env-vars
Browse files Browse the repository at this point in the history
Made cmd/wirey configurable with env variables
  • Loading branch information
fntlnz authored Apr 30, 2018
2 parents 79bbf50 + 4d8b8eb commit 0848436
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ Example usage:
./bin/wirey --endpoint 192.168.33.12 --ipaddr 10.30.0.80 --http http://192.168.33.10:8080 --httpbasicauth "time:series"
```

Example usage using env variables:

```bash
export WIREY_ENDPOINT="192.168.33.12"
export WIREY_IPADDR="10.30.0.80"
export WIREY_HTTP="http://192.168.33.10:8080"
export WIREY_HTTPBASICAUTH="time:series"
./bin/wirey
```

#### HTTP Server endpoints
You can find an example of http server in [examples/httpbackend](examples/httpbackend)

Expand Down
55 changes: 36 additions & 19 deletions cmd/wirey/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@ import (

"github.com/influxdata/wirey/backend"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var endpoint string
var endpointPort string
var ipAddr string
var privateKeyPath string
var ifname string
var etcdBackend []string
var httpBackend string
var httpBackendBasicAuth string

var rootCmd = &cobra.Command{
Use: "wirey",
Short: "manage local wireguard interfaces in a distributed system",
Expand All @@ -30,13 +22,18 @@ var rootCmd = &cobra.Command{
log.Fatal(err)
}

privateKeyPath := viper.GetString("privatekeypath")
privKeyBaseDir := filepath.Dir(privateKeyPath)
if _, err := os.Stat(privKeyBaseDir); os.IsNotExist(err) {
if err := os.Mkdir(privKeyBaseDir, 0600); err != nil {
log.Fatalf("Unable to create the base directory for the wirey private key: %s - %s", privKeyBaseDir, err.Error())
}
}

ifname := viper.GetString("ifname")
endpoint := viper.GetString("endpoint")
endpointPort := viper.GetString("endpoint-port")
ipAddr := viper.GetString("ipaddr")
i, err := backend.NewInterface(b, ifname, fmt.Sprintf("%s:%s", endpoint, endpointPort), ipAddr, privateKeyPath)

if err != nil {
Expand All @@ -49,20 +46,22 @@ var rootCmd = &cobra.Command{

func backendFactory() (backend.Backend, error) {
// etcd backend
if etcdBackend != nil {
etcdBackend := viper.GetStringSlice("etcd")
if len(etcdBackend) > 0 {
b, err := backend.NewEtcdBackend(etcdBackend)
if err != nil {
return nil, err
}
return b, nil
}

// http backend with optional basic auth
httpBackend := viper.GetString("http")
if len(httpBackend) != 0 {
b, err := backend.NewHTTPBackend(httpBackend)
if err != nil {
return nil, err
}
httpBackendBasicAuth := viper.GetString("httpbasicauth")
if len(httpBackendBasicAuth) > 2 {
splitted := strings.Split(httpBackendBasicAuth, ":")
username, password := splitted[0], splitted[1]
Expand All @@ -87,12 +86,30 @@ func Execute() {
}

func init() {
rootCmd.PersistentFlags().StringVar(&endpoint, "endpoint", "", "endpoint for this machine, e.g: 192.168.1.3")
rootCmd.PersistentFlags().StringVar(&endpointPort, "endpoint-port", "2345", "endpoint port for this machine")
rootCmd.PersistentFlags().StringVar(&ipAddr, "ipaddr", "", "the ip for this node inside the tunnel, e.g: 10.0.0.3")
rootCmd.PersistentFlags().StringVar(&privateKeyPath, "privatekeypath", "/etc/wirey/privkey", "the local path where to load the private key from, if empty, a private key will be generated.")
rootCmd.PersistentFlags().StringVar(&ifname, "ifname", "wg0", "the name to use for the interface (must be the same in all the peers)")
rootCmd.PersistentFlags().StringVar(&httpBackend, "http", "", "the http backend endpoint to use as backend, see also httpbasicauth if you need basic authentication")
rootCmd.PersistentFlags().StringVar(&httpBackendBasicAuth, "httpbasicauth", "", "basic auth for the http backend, in form username:password")
rootCmd.PersistentFlags().StringArrayVar(&etcdBackend, "etcd", nil, "array of etcd servers to connect to")

pflags := rootCmd.PersistentFlags()
pflags.String("endpoint", "", "endpoint for this machine, e.g: 192.168.1.3")
pflags.String("endpoint-port", "2345", "endpoint port for this machine")
pflags.StringSlice("etcd", nil, "array of etcd servers to connect to")
pflags.String("http", "", "the http backend endpoint to use as backend, see also httpbasicauth if you need basic authentication")
pflags.String("httpbasicauth", "", "basic auth for the http backend, in form username:password")
pflags.String("ifname", "wg0", "the name to use for the interface (must be the same in all the peers)")
pflags.String("ipaddr", "", "the ip for this node inside the tunnel, e.g: 10.0.0.3")
pflags.String("privatekeypath", "/etc/wirey/privkey", "the local path where to load the private key from, if empty, a private key will be generated.")

rootCmd.MarkFlagRequired("endpoint")
rootCmd.MarkFlagRequired("ipaddr")

viper.BindPFlag("endpoint", pflags.Lookup("endpoint"))
viper.BindPFlag("endpoint-port", pflags.Lookup("endpoint-port"))
viper.BindPFlag("etcd", pflags.Lookup("etcd"))
viper.BindPFlag("http", pflags.Lookup("http"))
viper.BindPFlag("httpbasicauth", pflags.Lookup("httpbasicauth"))
viper.BindPFlag("ifname", pflags.Lookup("ifname"))
viper.BindPFlag("ipaddr", pflags.Lookup("ipaddr"))
viper.BindPFlag("privatekeypath", pflags.Lookup("privatekeypath"))

viper.SetEnvPrefix("wirey")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
}

0 comments on commit 0848436

Please sign in to comment.