-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
214 lines (195 loc) · 5.82 KB
/
setup.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
package tls
import (
"context"
"os"
"strconv"
"sync"
"time"
ctls "crypto/tls"
"github.com/caddyserver/certmagic"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
//"github.com/coredns/coredns/plugin/pkg/log"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/tls"
)
func init() { plugin.Register("tls", setup) }
func setup(c *caddy.Controller) error {
err := parseTLS(c)
if err != nil {
return plugin.Error("tls", err)
}
return nil
}
var (
log = clog.NewWithPlugin("tls")
r = renewCert{quit: make(chan bool), renew: make(chan bool)}
once, shutOnce sync.Once
)
const (
defaultCA = "https://acme-v02.api.letsencrypt.org/directory"
defaultEmail = "[email protected]"
defaultCheckInterval = 15
defaultPort = 53
defaultCertPath = "./local/share/certmagic"
)
func parseTLS(c *caddy.Controller) error {
config := dnsserver.GetConfig(c)
var tlsconf *ctls.Config
var cert *certmagic.Certificate
var err error
clientAuth := ctls.NoClientCert
if config.TLSConfig != nil {
return plugin.Error("tls", c.Errf("TLS already configured for this server instance"))
}
for c.Next() {
args := c.RemainingArgs()
if args[0] == "acme" {
log.Debug("Starting ACME Setup")
ctx := context.Background()
var domainNameACME string
var caCert string
port := defaultPort
email := defaultEmail
ca := defaultCA
checkInterval := defaultCheckInterval
userHome, homeExists := os.LookupEnv("HOME")
if !homeExists {
log.Error("Environment Variable $HOME needs to be set.")
}
certPath := userHome + defaultCertPath
for c.NextBlock() {
token := c.Val()
switch token {
case "domain":
domainArgs := c.RemainingArgs()
if len(domainArgs) > 1 {
return plugin.Error("tls", c.Errf("Too many arguments to domain"))
}
domainNameACME = domainArgs[0]
case "ca":
caArgs := c.RemainingArgs()
if len(caArgs) > 1 {
return plugin.Error("tls", c.Errf("Too many arguments to ca"))
}
ca = caArgs[0]
case "cacert":
caCertArgs := c.RemainingArgs()
if len(caCertArgs) > 1 {
return plugin.Error("tls", c.Errf("Too many arguments to cacert"))
}
caCert = caCertArgs[0]
case "email":
emailArgs := c.RemainingArgs()
if len(emailArgs) > 1 {
return plugin.Error("tls", c.Errf("Too many arguments to email"))
}
email = emailArgs[0]
case "port":
portArgs := c.RemainingArgs()
if len(portArgs) > 1 {
return plugin.Error("tls", c.Errf("Too many arguments to port"))
}
port, err = strconv.Atoi(portArgs[0])
if err != nil {
log.Errorf("Failed to convert port argument to integer: %v \n", err)
}
case "certpath":
certPathArgs := c.RemainingArgs()
if len(certPathArgs) > 1 {
return plugin.Error("tls", c.Errf("Too many arguments to certpath"))
}
certPath = certPathArgs[0]
case "checkinterval":
checkIntervalArgs := c.RemainingArgs()
if len(checkIntervalArgs) > 1 {
return plugin.Error("tls", c.Errf("Too many arguments to checkinterval"))
}
interval, err := strconv.Atoi(checkIntervalArgs[0])
if err != nil {
return plugin.Error("Failed to convert checkInterval argument to integer: %v \n", err)
}
checkInterval = interval
default:
return c.Errf("unknown argument to acme '%s'", token)
}
}
pool, err := setupCertPool(caCert)
if err != nil {
log.Errorf("Failed to add the custom CA certfiicate to the pool of trusted certificates: %v, \n", err)
}
solver := newDNSSolver(port)
certmagicConfig := NewConfig(certPath)
certmagicIssuer := NewIssuer(certmagicConfig, ca, email, pool, solver)
certManager := NewCertManager(domainNameACME, certmagicConfig, certmagicIssuer)
var names []string
names = append(names, certManager.Zone)
tlsconf, cert, err = certManager.configureTLSwithACME(ctx)
if err != nil {
log.Errorf("Failed to setup TLS automatically: %v \n", err)
}
config.TLSConfig = tlsconf
once.Do(func() {
// start a loop that checks for renewals
go func() {
log.Debug("Starting certificate renewal loop in the background")
for {
time.Sleep(time.Duration(checkInterval) * time.Minute)
if cert.NeedsRenewal(certManager.Config) {
log.Info("Certificate expiring soon, initializing reload")
r.renew <- true
}
}
}()
caddy.RegisterEventHook("updateCert", hook)
})
shutOnce.Do(func() {
c.OnFinalShutdown(func() error {
log.Debug("Quiting renewal checker")
r.quit <- true
return nil
})
})
} else {
//No ACME part - plugin continues to work like the normal tls plugin
if len(args) < 2 || len(args) > 3 {
return plugin.Error("tls", c.ArgErr())
}
for c.NextBlock() {
switch c.Val() {
case "client_auth":
authTypeArgs := c.RemainingArgs()
if len(authTypeArgs) != 1 {
return c.ArgErr()
}
switch authTypeArgs[0] {
case "nocert":
clientAuth = ctls.NoClientCert
case "request":
clientAuth = ctls.RequestClientCert
case "require":
clientAuth = ctls.RequireAnyClientCert
case "verify_if_given":
clientAuth = ctls.VerifyClientCertIfGiven
case "require_and_verify":
clientAuth = ctls.RequireAndVerifyClientCert
default:
return c.Errf("unknown authentication type '%s'", authTypeArgs[0])
}
default:
return c.Errf("unknown option '%s'", c.Val())
}
}
tlsconf, err = tls.NewTLSConfigFromArgs(args...)
if err != nil {
return err
}
tlsconf.ClientAuth = clientAuth
// NewTLSConfigs only sets RootCAs, so we need to let ClientCAs refer to it.
tlsconf.ClientCAs = tlsconf.RootCAs
config.TLSConfig = tlsconf
}
}
return nil
}