This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
consul_config_definer.go
166 lines (141 loc) · 5.2 KB
/
consul_config_definer.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package config
import (
"crypto/sha1"
"encoding/base64"
"path/filepath"
"golang.org/x/crypto/pbkdf2"
)
type ConsulConfig struct {
Server bool `json:"server"`
Domain string `json:"domain"`
Datacenter string `json:"datacenter"`
DataDir string `json:"data_dir"`
LogLevel string `json:"log_level"`
NodeName string `json:"node_name"`
Ports ConsulConfigPorts `json:"ports"`
RejoinAfterLeave bool `json:"rejoin_after_leave"`
BindAddr string `json:"bind_addr"`
DisableRemoteExec bool `json:"disable_remote_exec"`
DisableUpdateCheck bool `json:"disable_update_check"`
Protocol int `json:"protocol"`
VerifyOutgoing *bool `json:"verify_outgoing,omitempty"`
VerifyIncoming *bool `json:"verify_incoming,omitempty"`
VerifyServerHostname *bool `json:"verify_server_hostname,omitempty"`
CAFile *string `json:"ca_file,omitempty"`
KeyFile *string `json:"key_file,omitempty"`
CertFile *string `json:"cert_file,omitempty"`
Encrypt *string `json:"encrypt,omitempty"`
DnsConfig ConsulConfigDnsConfig `json:"dns_config"`
Bootstrap *bool `json:"bootstrap,omitempty"`
Performance ConsulConfigPerformance `json:"performance"`
Telemetry *ConsulConfigTelemetry `json:"telemetry,omitempty"`
TLSMinVersion string `json:"tls_min_version"`
}
type ConsulConfigPorts struct {
DNS int `json:"dns,omitempty"`
HTTP int `json:"http,omitempty"`
HTTPS int `json:"https,omitempty"`
}
type ConsulConfigDnsConfig struct {
AllowStale bool `json:"allow_stale"`
MaxStale string `json:"max_stale"`
RecursorTimeout string `json:"recursor_timeout"`
ServiceTTL ConsulConfigServiceTTL `json:"service_ttl"`
}
type ConsulConfigServiceTTL struct {
AllServices string `json:"*"`
}
type ConsulConfigPerformance struct {
RaftMultiplier int `json:"raft_multiplier"`
}
type ConsulConfigTelemetry struct {
StatsdAddress string `json:"statsd_address,omitempty"`
}
func GenerateConfiguration(config Config, configDir, nodeName string) ConsulConfig {
lan := config.Consul.Agent.Servers.LAN
if lan == nil {
lan = []string{}
}
wan := config.Consul.Agent.Servers.WAN
if wan == nil {
wan = []string{}
}
isServer := config.Consul.Agent.Mode == "server"
dns := config.Consul.Agent.Ports.DNS
if dns == 0 {
dns = 53
}
consulConfig := ConsulConfig{
Server: isServer,
Domain: config.Consul.Agent.Domain,
Datacenter: config.Consul.Agent.Datacenter,
DataDir: config.Path.DataDir,
LogLevel: config.Consul.Agent.LogLevel,
NodeName: nodeName,
RejoinAfterLeave: true,
BindAddr: config.Node.ExternalIP,
DisableRemoteExec: true,
DisableUpdateCheck: true,
Protocol: config.Consul.Agent.ProtocolVersion,
Ports: ConsulConfigPorts{
DNS: dns,
},
DnsConfig: ConsulConfigDnsConfig{
AllowStale: config.Consul.Agent.DnsConfig.AllowStale,
MaxStale: config.Consul.Agent.DnsConfig.MaxStale,
RecursorTimeout: config.Consul.Agent.DnsConfig.RecursorTimeout,
ServiceTTL: ConsulConfigServiceTTL{
AllServices: config.Consul.Agent.DnsConfig.ServiceTTL,
},
},
Performance: ConsulConfigPerformance{
RaftMultiplier: 1,
},
TLSMinVersion: "tls12",
}
if config.Consul.Agent.Telemetry.StatsdAddress != "" {
consulConfig.Telemetry = &ConsulConfigTelemetry{
StatsdAddress: config.Consul.Agent.Telemetry.StatsdAddress,
}
}
if config.Consul.Agent.RequireSSL {
consulConfig.Ports.HTTP = -1
consulConfig.Ports.HTTPS = 8500
}
consulConfig.VerifyOutgoing = boolPtr(true)
consulConfig.VerifyIncoming = boolPtr(true)
consulConfig.VerifyServerHostname = boolPtr(true)
certsDir := filepath.Join(configDir, "certs")
consulConfig.CAFile = strPtr(filepath.Join(certsDir, "ca.crt"))
if isServer {
consulConfig.KeyFile = strPtr(filepath.Join(certsDir, "server.key"))
consulConfig.CertFile = strPtr(filepath.Join(certsDir, "server.crt"))
} else {
consulConfig.KeyFile = strPtr(filepath.Join(certsDir, "agent.key"))
consulConfig.CertFile = strPtr(filepath.Join(certsDir, "agent.crt"))
}
if len(config.Consul.EncryptKeys) > 0 {
consulConfig.Encrypt = encryptKey(config.Consul.EncryptKeys[0])
}
if isServer {
consulConfig.Bootstrap = boolPtr(config.Consul.Agent.Bootstrap)
}
return consulConfig
}
func encryptKey(key string) *string {
decodedKey, err := base64.StdEncoding.DecodeString(key)
if err != nil || len(decodedKey) != 16 {
return strPtr(base64.StdEncoding.EncodeToString(pbkdf2.Key([]byte(key), []byte(""), 20000, 16, sha1.New)))
} else {
return strPtr(key)
}
}
func intPtr(i int) *int {
return &i
}
func boolPtr(b bool) *bool {
return &b
}
func strPtr(s string) *string {
return &s
}