/
config.go
215 lines (193 loc) · 6.61 KB
/
config.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/lego"
"gopkg.in/yaml.v2"
)
const (
envCertDestinationKey = "DOTEGE_CERT_DESTINATION"
envCertDestinationDefault = "/data/certs/"
envCertUserIdKey = "DOTEGE_CERT_UID"
envCertUserIdDefault = -1
envCertGroupIdKey = "DOTEGE_CERT_GID"
envCertGroupIdDefault = -1
envCertModeKey = "DOTEGE_CERT_MODE"
envCertModeDefault = 0600
envDebugKey = "DOTEGE_DEBUG"
envDebugContainersValue = "containers"
envDebugHeadersValue = "headers"
envDebugHostnamesValue = "hostnames"
envDnsProviderKey = "DOTEGE_DNS_PROVIDER"
envAcmeEmailKey = "DOTEGE_ACME_EMAIL"
envAcmeEndpointKey = "DOTEGE_ACME_ENDPOINT"
envAcmeKeyTypeKey = "DOTEGE_ACME_KEY_TYPE"
envAcmeKeyTypeDefault = "P384"
envAcmeCacheLocationKey = "DOTEGE_ACME_CACHE_FILE"
envAcmeCacheLocationDefault = "/data/config/certs.json"
envSignalContainerKey = "DOTEGE_SIGNAL_CONTAINER"
envSignalContainerDefault = ""
envSignalTypeKey = "DOTEGE_SIGNAL_TYPE"
envSignalTypeDefault = "HUP"
envTemplateDestinationKey = "DOTEGE_TEMPLATE_DESTINATION"
envTemplateDestinationDefault = "/data/output/haproxy.cfg"
envTemplateSourceKey = "DOTEGE_TEMPLATE_SOURCE"
envTemplateSourceDefault = "./templates/haproxy.cfg.tpl"
envUsersKey = "DOTEGE_USERS"
envUsersDefault = ""
envWildcardDomainsKey = "DOTEGE_WILDCARD_DOMAINS"
envWildcardDomainsDefault = ""
envProxyTagKey = "DOTEGE_PROXYTAG"
envProxyTagDefault = ""
envCertificateDeploymentKey = "DOTEGE_CERTIFICATE_DEPLOYMENT"
envCertificateDeploymentDefault = CertificateDeploymentCombined
)
const (
CertificateDeploymentCombined = "combined"
CertificateDeploymentSplit = "splitkeys"
CertificateDeploymentDisabled = "disabled"
)
// Config is the user-definable configuration for Dotege.
type Config struct {
Templates []TemplateConfig
Signals []ContainerSignal
DefaultCertDestination string
CertUid int
CertGid int
CertMode os.FileMode
Acme AcmeConfig
WildCardDomains []string
Users []User
ProxyTag string
CertificateDeployment string
DebugContainers bool
DebugHeaders bool
DebugHostnames bool
}
// User holds the details of a single user used for ACL purposes.
type User struct {
Name string `yaml:"name"`
Password string `yaml:"password"`
Groups []string `yaml:"groups"`
}
// TemplateConfig configures a single template for the generator.
type TemplateConfig struct {
Source string
Destination string
}
// ContainerSignal describes a container that should be sent a signal when the config/certs change.
type ContainerSignal struct {
Name string
Signal string
}
// AcmeConfig describes the configuration to use for getting certs using ACME.
type AcmeConfig struct {
Email string
DnsProvider string
Endpoint string
KeyType certcrypto.KeyType
CacheLocation string
}
func requiredStringVar(key string) (value string) {
value, ok := os.LookupEnv(key)
if !ok {
panic(fmt.Errorf("required environmental variable not defined: %s", key))
}
return
}
func optionalStringVar(key string, fallback string) (value string) {
value, ok := os.LookupEnv(key)
if !ok {
value = fallback
}
return
}
func optionalIntVar(key string, fallback int) int {
if value, ok := os.LookupEnv(key); ok {
if num, err := strconv.Atoi(value); err == nil {
return num
}
}
return fallback
}
func optionalFilemodeVar(key string, fallback os.FileMode) os.FileMode {
if value, ok := os.LookupEnv(key); ok {
if num, err := strconv.ParseInt(value, 8, 64); err == nil {
return os.FileMode(num)
}
}
return fallback
}
func createSignalConfig() []ContainerSignal {
name := optionalStringVar(envSignalContainerKey, envSignalContainerDefault)
if name == envSignalContainerDefault {
return []ContainerSignal{}
} else {
return []ContainerSignal{
{
Name: name,
Signal: optionalStringVar(envSignalTypeKey, envSignalTypeDefault),
},
}
}
}
func createConfig() *Config {
debug := toMap(splitList(strings.ToLower(optionalStringVar(envDebugKey, ""))))
c := &Config{
Templates: []TemplateConfig{
{
Source: optionalStringVar(envTemplateSourceKey, envTemplateSourceDefault),
Destination: optionalStringVar(envTemplateDestinationKey, envTemplateDestinationDefault),
},
},
Signals: createSignalConfig(),
DefaultCertDestination: optionalStringVar(envCertDestinationKey, envCertDestinationDefault),
CertGid: optionalIntVar(envCertGroupIdKey, envCertGroupIdDefault),
CertUid: optionalIntVar(envCertUserIdKey, envCertUserIdDefault),
CertMode: optionalFilemodeVar(envCertModeKey, envCertModeDefault),
WildCardDomains: splitList(optionalStringVar(envWildcardDomainsKey, envWildcardDomainsDefault)),
Users: readUsers(),
ProxyTag: optionalStringVar(envProxyTagKey, envProxyTagDefault),
CertificateDeployment: optionalStringVar(envCertificateDeploymentKey, envCertificateDeploymentDefault),
DebugContainers: debug[envDebugContainersValue],
DebugHeaders: debug[envDebugHeadersValue],
DebugHostnames: debug[envDebugHostnamesValue],
}
if c.CertificateDeployment != CertificateDeploymentDisabled {
c.Acme = AcmeConfig{
DnsProvider: requiredStringVar(envDnsProviderKey),
Email: requiredStringVar(envAcmeEmailKey),
Endpoint: optionalStringVar(envAcmeEndpointKey, lego.LEDirectoryProduction),
KeyType: certcrypto.KeyType(optionalStringVar(envAcmeKeyTypeKey, envAcmeKeyTypeDefault)),
CacheLocation: optionalStringVar(envAcmeCacheLocationKey, envAcmeCacheLocationDefault),
}
}
return c
}
func readUsers() []User {
var users []User
err := yaml.Unmarshal([]byte(optionalStringVar(envUsersKey, envUsersDefault)), &users)
if err != nil {
panic(fmt.Errorf("unable to parse users struct: %s", err))
}
return users
}
func splitList(input string) (result []string) {
result = []string{}
for _, part := range strings.Split(strings.ReplaceAll(input, " ", ","), ",") {
if len(part) > 0 {
result = append(result, part)
}
}
return
}
func toMap(input []string) map[string]bool {
res := make(map[string]bool)
for k := range input {
res[input[k]] = true
}
return res
}