forked from RichardKnop/go-oauth2-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consul.go
105 lines (80 loc) · 2.47 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
package config
import (
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/RichardKnop/go-oauth2-server/log"
"github.com/hashicorp/consul/api"
)
var (
consulEndpoint = "http://localhost:8500"
consulCertFile, consulKeyFile, consulCaFile string
consulConfigPath = "/config/go_oauth2_server.json"
)
type consulBackend struct{}
func (b *consulBackend) InitConfigBackend() {
// Overwrite default values with environment variables if they are set
if os.Getenv("CONSUL_ENDPOINT") != "" {
consulEndpoint = os.Getenv("CONSUL_ENDPOINT")
}
if os.Getenv("CONSUL_CERT_FILE") != "" {
consulCertFile = os.Getenv("CONSUL_CERT_FILE")
}
if os.Getenv("CONSUL_KEY_FILE") != "" {
consulKeyFile = os.Getenv("CONSUL_KEY_FILE")
}
if os.Getenv("CONSUL_CA_FILE") != "" {
consulCaFile = os.Getenv("CONSUL_CA_FILE")
}
if os.Getenv("CONSUL_CONFIG_PATH") != "" {
consulConfigPath = os.Getenv("CONSUL_CONFIG_PATH")
}
}
//LoadConfig gets the JSON from Consul and unmarshals it to the config object
func (b *consulBackend) LoadConfig() (*Config, error) {
cli, err := newConsulClient(consulEndpoint, consulCertFile, consulKeyFile, consulCaFile)
if err != nil {
return nil, err
}
// Read from remote config the first time
resp, _, err := cli.KV().Get(consulConfigPath, nil)
if err != nil {
return nil, err
}
if resp == nil {
return nil, fmt.Errorf("key not found: %s", consulConfigPath)
}
// Unmarshal the config JSON into the cnf object
newCnf := new(Config)
if err := json.Unmarshal(resp.Value, newCnf); err != nil {
return nil, err
}
return newCnf, nil
}
// RefreshConfig sets config through the pointer so config actually gets refreshed
func (b *consulBackend) RefreshConfig(newCnf *Config) {
*Cnf = *newCnf
}
func newConsulClient(theEndpoint, certFile, keyFile, caFile string) (*api.Client, error) {
// Log the consul endpoint for debugging purposes
log.INFO.Printf("CONSUL Endpoint: %s", theEndpoint)
consulConfig := api.DefaultConfig()
consulConfig.Address = theEndpoint
// Optionally, configure TLS transport
if certFile != "" && keyFile != "" && caFile != "" {
tlsConfig, err := api.SetupTLSConfig(&api.TLSConfig{
CertFile: certFile,
KeyFile: keyFile,
CAFile: caFile,
InsecureSkipVerify: true,
})
if err != nil {
return nil, err
}
consulConfig.HttpClient.Transport = &http.Transport{
TLSClientConfig: tlsConfig,
}
}
return api.NewClient(consulConfig)
}