-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.go
148 lines (138 loc) · 3.98 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
package acme
import (
"crypto/tls"
"net"
"strconv"
"strings"
"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"
)
const pluginName = "acme"
func init() {
plugin.Register(pluginName, setup)
}
func setup(c *caddy.Controller) error {
acmeTemplate, zoneName, err := parseACME(c)
provider := Provider{
recordMap: make(map[string]*RecordStore),
}
if err != nil {
return plugin.Error(pluginName, err)
}
config := dnsserver.GetConfig(c)
acmeConfig := AcmeConfig{
Zone: zoneName,
}
acmeHandler := &AcmeHandler{
provider: &provider,
AcmeConfig: &acmeConfig,
}
config.AddPlugin(func(next plugin.Handler) plugin.Handler {
acmeHandler.Next = next
return acmeHandler
})
c.OnFirstStartup(func() error {
go func() error {
authoritativeNameservers, err := getAuthoritativeNameServers(zoneName)
if err != nil {
return err
}
authoritativeNameserver := authoritativeNameservers[len(authoritativeNameservers)-1]
ipAddr, err := getExternalIpAddress(authoritativeNameserver)
if err != nil {
log.Error(err)
return err
}
acmeHandler.Ipv4Addr = net.ParseIP(ipAddr).To4()
acmeHandler.Ipv6Addr = net.ParseIP(ipAddr).To16()
acmeHandler.AuthoritativeNameserver = authoritativeNameserver
acmeTemplate.DNS01Solver = &certmagic.DNS01Solver{
DNSProvider: &provider,
Resolvers: []string{ipAddr},
}
A := NewACME(acmeTemplate, zoneName)
err = A.IssueCert([]string{zoneName})
if err != nil {
log.Error(err)
return err
}
log.Info("Certificate Issued")
err = configureTLS(A, zoneName, config)
if err != nil {
log.Error(err)
return err
}
log.Info("TLS Configured")
return nil
}()
return nil
})
return nil
}
func setTLSDefaults(tlsConfig *tls.Config) {
tlsConfig.MinVersion = tls.VersionTLS12
tlsConfig.MaxVersion = tls.VersionTLS13
tlsConfig.CipherSuites = []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
}
tlsConfig.PreferServerCipherSuites = true
}
func parseACME(c *caddy.Controller) (certmagic.ACMEManager, string, error) {
acmeTemplate := certmagic.ACMEManager{
Agreed: true,
DisableHTTPChallenge: true,
DisableTLSALPNChallenge: true,
}
var zoneName string
for c.Next() {
for c.NextBlock() {
term := strings.ToLower(c.Val())
switch term {
case DOMAIN:
args := c.RemainingArgs()
if len(args) > 1 {
return acmeTemplate, zoneName, c.Errf("unexpected number of arguments: %#v", args)
}
zoneName = args[0]
case CHALLENGE:
args := c.RemainingArgs()
challenge := args[0]
if !(len(args) == 3 && args[1] == PORT) {
return acmeTemplate, zoneName, c.Errf("unexpected number of arguments: %#v", args)
}
port, err := strconv.Atoi(args[2])
if err != nil {
return acmeTemplate, zoneName, c.Errf("%s port is not an int: %#v", challenge, args)
}
switch challenge {
case HTTPChallenge:
acmeTemplate.AltHTTPPort = port
acmeTemplate.DisableHTTPChallenge = false
case TLPSALPNChallenge:
acmeTemplate.AltTLSALPNPort = port
acmeTemplate.DisableTLSALPNChallenge = false
default:
return acmeTemplate, zoneName, c.Errf("unexpected challenge %s: challenge should only be tlsalpn or http", challenge)
}
default:
return acmeTemplate, zoneName, c.Errf("unexpected term: %s: term should only be challenge or domain", term)
}
}
}
if zoneName == "" {
return acmeTemplate, zoneName, c.Errf("Domain not provided")
}
acmeTemplate.CA = certmagic.LetsEncryptProductionCA
return acmeTemplate, zoneName, nil
}