/
consul.go
131 lines (113 loc) · 2.9 KB
/
consul.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package libkv
import (
"fmt"
"net/url"
"os"
"time"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
"github.com/docker/libkv"
"github.com/docker/libkv/store"
"github.com/docker/libkv/store/consul"
"github.com/hairyhenderson/gomplate/conv"
"github.com/hairyhenderson/gomplate/env"
"github.com/hairyhenderson/gomplate/vault"
consulapi "github.com/hashicorp/consul/api"
)
const (
http = "http"
https = "https"
)
// NewConsul - instantiate a new Consul datasource handler
func NewConsul(u *url.URL) *LibKV {
consul.Register()
c, err := consulURL(u)
if err != nil {
logFatal(err)
}
config := consulConfig(c.Scheme == https)
if role := env.Getenv("CONSUL_VAULT_ROLE", ""); role != "" {
mount := env.Getenv("CONSUL_VAULT_MOUNT", "consul")
client := vault.New(nil)
client.Login()
path := fmt.Sprintf("%s/creds/%s", mount, role)
var data []byte
data, err = client.Read(path)
if err != nil {
logFatal("vault consul auth failed", err)
}
decoded := make(map[string]interface{})
err = yaml.Unmarshal(data, &decoded)
if err != nil {
logFatal("Unable to unmarshal object", err)
}
token := decoded["token"].(string)
client.Logout()
// nolint: gosec
_ = os.Setenv("CONSUL_HTTP_TOKEN", token)
}
var kv store.Store
kv, err = libkv.NewStore(store.CONSUL, []string{c.String()}, config)
if err != nil {
logFatal("Consul setup failed", err)
}
return &LibKV{kv}
}
// -- converts a gomplate datasource URL into a usable Consul URL
func consulURL(u *url.URL) (*url.URL, error) {
addrEnv := env.Getenv("CONSUL_HTTP_ADDR")
c, err := url.Parse(addrEnv)
if err != nil {
return nil, errors.Wrapf(err, "invalid URL '%s' in CONSUL_HTTP_ADDR", addrEnv)
}
if c.Scheme == "" {
c.Scheme = u.Scheme
}
switch c.Scheme {
case "consul+http", http:
c.Scheme = http
case "consul+https", https:
c.Scheme = https
case "consul":
if conv.Bool(env.Getenv("CONSUL_HTTP_SSL")) {
c.Scheme = https
} else {
c.Scheme = http
}
}
if c.Host == "" && u.Host == "" {
c.Host = "localhost:8500"
} else if c.Host == "" {
c.Host = u.Host
}
return c, nil
}
func consulConfig(useTLS bool) *store.Config {
t := conv.MustAtoi(env.Getenv("CONSUL_TIMEOUT"))
config := &store.Config{
ConnectionTimeout: time.Duration(t) * time.Second,
}
if useTLS {
tconf := setupTLS("CONSUL")
var err error
config.TLS, err = consulapi.SetupTLSConfig(tconf)
if err != nil {
logFatal("TLS Config setup failed", err)
}
}
return config
}
func setupTLS(prefix string) *consulapi.TLSConfig {
tlsConfig := &consulapi.TLSConfig{
Address: env.Getenv(prefix + "_TLS_SERVER_NAME"),
CAFile: env.Getenv(prefix + "_CACERT"),
CAPath: env.Getenv(prefix + "_CAPATH"),
CertFile: env.Getenv(prefix + "_CLIENT_CERT"),
KeyFile: env.Getenv(prefix + "_CLIENT_KEY"),
}
if v := env.Getenv(prefix + "_HTTP_SSL_VERIFY"); v != "" {
verify := conv.Bool(v)
tlsConfig.InsecureSkipVerify = !verify
}
return tlsConfig
}