github.com/mailgun/holster/v4@v4.20.0/consul/config.go (about) 1 package consul 2 3 import ( 4 "os" 5 "strconv" 6 "strings" 7 8 "github.com/hashicorp/consul/api" 9 "github.com/mailgun/holster/v4/errors" 10 "github.com/mailgun/holster/v4/setter" 11 ) 12 13 // EnvHasConsulConfig returns true if there are items in the local environment 14 // that have the prefix `CONSUL_` 15 func EnvHasConsulConfig() bool { 16 for _, i := range os.Environ() { 17 if strings.HasPrefix(i, "CONSUL_") { 18 return true 19 } 20 } 21 return false 22 } 23 24 // NewClient creates a new consul api.Client with the specified config, call 25 // NewConfig to complete the configuration by reading the local environment 26 // `CONSUL_` variables. 27 // 28 // If no environment variables are set, and the passed cfg is nil, returns 29 // a client connected to 127.0.0.1:8500 30 func NewClient(cfg *api.Config) (*api.Client, error) { 31 var err error 32 if cfg, err = NewConfig(cfg); err != nil { 33 return nil, errors.Wrap(err, "failed to build consul config") 34 } 35 36 etcdClt, err := api.NewClient(cfg) 37 if err != nil { 38 return nil, errors.Wrap(err, "failed to create consul client") 39 } 40 return etcdClt, nil 41 } 42 43 // NewConfig creates a new api.Config using environment variables. If an 44 // existing config is passed, it will fill in missing configuration using 45 // environment variables or defaults if they exists on the local system. 46 // 47 // The config mirrors the same environment variables used by the consul CLI 48 // as documented here https://www.consul.io/commands 49 // 50 // If no environment variables are set, and the passed cfg is nil, returns 51 // a default config pointing to 127.0.0.1:8500 52 func NewConfig(cfg *api.Config) (*api.Config, error) { 53 setter.SetDefault(&cfg, api.DefaultConfig()) 54 55 auth := os.Getenv("CONSUL_HTTP_AUTH") 56 if auth != "" { 57 parts := strings.Split(auth, ":") 58 if len(parts) != 2 { 59 return nil, errors.Errorf("invalid format for 'CONSUL_HTTP_AUTH'; "+ 60 "expected 'user:pass' got '%s'", auth) 61 } 62 cfg.HttpAuth = &api.HttpBasicAuth{ 63 Username: parts[0], 64 Password: parts[1], 65 } 66 } 67 68 setter.SetDefault(&cfg.Address, os.Getenv("CONSUL_HTTP_ADDR")) 69 setter.SetDefault(&cfg.Datacenter, os.Getenv("CONSUL_DATACENTER")) 70 setter.SetDefault(&cfg.Token, os.Getenv("CONSUL_HTTP_TOKEN")) 71 setter.SetDefault(&cfg.TokenFile, os.Getenv("CONSUL_HTTP_TOKEN_FILE")) 72 setter.SetDefault(&cfg.Namespace, os.Getenv("CONSUL_NAMESPACE")) 73 setter.SetDefault(&cfg.TLSConfig.CertFile, os.Getenv("CONSUL_CLIENT_CERT")) 74 setter.SetDefault(&cfg.TLSConfig.KeyFile, os.Getenv("CONSUL_CLIENT_KEY")) 75 setter.SetDefault(&cfg.TLSConfig.CAFile, os.Getenv("CONSUL_CACERT")) 76 setter.SetDefault(&cfg.TLSConfig.InsecureSkipVerify, getEnvBool("CONSUL_HTTP_SSL_VERIFY")) 77 return cfg, nil 78 } 79 80 func getEnvBool(name string) bool { 81 v := os.Getenv(name) 82 if v == "" { 83 return false 84 } 85 b, err := strconv.ParseBool(v) 86 if err != nil { 87 return false 88 } 89 return b 90 }