Skip to content

Commit 3f57038

Browse files
committed
Added arguments for tests and some cleanup
1 parent 550b6e4 commit 3f57038

File tree

7 files changed

+72
-59
lines changed

7 files changed

+72
-59
lines changed

hook.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package tls
22

33
import (
4-
"fmt"
5-
64
"github.com/coredns/caddy"
75
)
86

@@ -30,11 +28,11 @@ func hook(event caddy.EventName, info interface{}) error {
3028
}
3129
_, err = instance.Restart(corefile)
3230
if err != nil {
33-
fmt.Printf("Error during Restart: %v, \n", err)
31+
log.Errorf("Error during Restart: %v, \n", err)
3432
}
3533
return
3634
case <-r.quit:
37-
log.Info("Certificate renewal quit")
35+
log.Debug("Received quit signal, stopping certificate renewal")
3836
return
3937
}
4038
}

manager.go

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import (
44
"context"
55
"errors"
66
"io/fs"
7-
"strconv"
87
"time"
98

109
"crypto/tls"
1110
"crypto/x509"
12-
//"encoding/pem"
11+
"encoding/pem"
1312
"fmt"
14-
//"os"
13+
"os"
1514

1615
"github.com/caddyserver/certmagic"
1716
"github.com/coredns/coredns/core/dnsserver"
@@ -24,46 +23,36 @@ type ACMEManager struct {
2423
}
2524

2625
// NewACMEManager create a new ACMEManager
27-
func NewACMEManager(config *dnsserver.Config, zone string, ca string) *ACMEManager {
26+
func NewACMEManager(config *dnsserver.Config, zone string, ca string, caCert string, port int) *ACMEManager {
27+
//TODO: change this
2828
if ca == "" {
2929
ca = "localhost:14001/dir" //pebble default
3030
}
3131

32-
// TODO: this lets our acme client trust the pebble cert
33-
// this is only needed for testing and should not be in production
34-
// figure out how to only do this in test cases
35-
//certbytes, err := os.ReadFile("test/certs/pebble.minica.pem")
36-
//if err != nil {
37-
//fmt.Println(err.Error())
38-
//panic("Failed to load Cert")
39-
//}
40-
//pemcert, _ := pem.Decode(certbytes)
41-
//if pemcert == nil {
42-
//fmt.Println("pemcert not found")
43-
//}
44-
//cert, err := x509.ParseCertificate(pemcert.Bytes)
45-
//if err != nil {
46-
//fmt.Println(err)
47-
//panic("Failed to parse Cert")
48-
//}
4932
pool, err := x509.SystemCertPool()
5033
if err != nil {
51-
fmt.Println(err)
52-
panic("Failed to get system Certpool")
34+
log.Errorf("Failed to get system pool of trusted certificates: %v \n", err)
5335
}
54-
//pool.AddCert(cert)
5536

56-
portNumber, err := strconv.Atoi(config.Port)
57-
if err != nil {
58-
fmt.Println(err)
59-
panic("Failed to convert config.Port to integer")
60-
}
37+
if caCert != "" {
38+
certbytes, err := os.ReadFile(caCert)
39+
if err != nil {
40+
log.Errorf("Failed to read certificate provided by cacert option: %v \n", err)
41+
}
42+
pemcert, _ := pem.Decode(certbytes)
43+
if pemcert == nil {
44+
fmt.Println("pemcert not found")
45+
}
46+
cert, err := x509.ParseCertificate(pemcert.Bytes)
47+
if err != nil {
48+
log.Errorf("Failed to parse certificate provided by cacert option: %v \n", err)
49+
}
50+
pool.AddCert(cert)
51+
}
6152

62-
//TODO: the address cannot be hardcoded
6353
solver := &DNSSolver{
64-
Port: portNumber,
65-
Addr: "127.0.0.1:1053",
66-
}
54+
Port: port,
55+
}
6756

6857
certmagic.DefaultACME.Email = "[email protected]"
6958

setup.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tls
22

33
import (
44
"context"
5+
"strconv"
56
"sync"
67
"time"
78

@@ -37,7 +38,9 @@ var (
3738
const (
3839
argDomain = "domain"
3940
argCa = "ca"
41+
argCaCert = "cacert"
4042
argCertPath = "certpath"
43+
argPort = "port"
4144
)
4245

4346
func parseTLS(c *caddy.Controller) error {
@@ -65,6 +68,8 @@ func parseTLS(c *caddy.Controller) error {
6568

6669
var domainNameACME string
6770
var ca string
71+
var caCert string
72+
var port string
6873
//certPath := "/home/marius/.local/share/certmagic/certificates/example.com/example.com.crt"
6974

7075
for c.NextBlock() {
@@ -82,6 +87,18 @@ func parseTLS(c *caddy.Controller) error {
8287
return plugin.Error("tls", c.Errf("To many arguments to ca"))
8388
}
8489
ca = caArgs[0]
90+
case argCaCert:
91+
caCertArgs := c.RemainingArgs()
92+
if len(caCertArgs) > 1 {
93+
return plugin.Error("tls", c.Errf("To many arguments to cacert"))
94+
}
95+
caCert = caCertArgs[0]
96+
case argPort:
97+
portArgs := c.RemainingArgs()
98+
if len(portArgs) > 1 {
99+
return plugin.Error("tls", c.Errf("To many arguments to port"))
100+
}
101+
port = portArgs[0]
85102
case argCertPath:
86103
certPathArgs := c.RemainingArgs()
87104
if len(certPathArgs) > 1 {
@@ -93,7 +110,15 @@ func parseTLS(c *caddy.Controller) error {
93110
}
94111
}
95112

96-
manager := NewACMEManager(config, domainNameACME, ca)
113+
portNumber := 53
114+
if port != "" {
115+
portNumber, err = strconv.Atoi(port)
116+
if err != nil {
117+
log.Errorf("Failed to convert port argument to integer: %v \n", err)
118+
}
119+
}
120+
121+
manager := NewACMEManager(config, domainNameACME, ca, caCert, portNumber)
97122

98123
var names []string
99124
names = append(names, manager.Zone)

solver.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import (
1414
)
1515

1616
type DNSSolver struct {
17-
Port int
18-
Addr string
17+
Port int
1918
DNS *ACMEServer
2019
}
2120

@@ -96,7 +95,7 @@ func (d *DNSSolver) Present(ctx context.Context, challenge acme.Challenge) error
9695
d.DNS = acmeServer
9796

9897
addr := net.UDPAddr{
99-
Port: 53,
98+
Port: d.Port,
10099
IP: net.ParseIP("0.0.0.0"),
101100
}
102101

solver_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import (
99
"github.com/miekg/dns"
1010
)
1111

12-
func TestCheckDNSChallenge() {
13-
_ = []struct {
14-
name string
15-
question string
16-
}{
17-
{
18-
name: "ACME Challenge",
19-
question: "_acme-challenge.example.com.",
20-
},
21-
}
22-
}
12+
//func TestCheckDNSChallenge() {
13+
//_ = []struct {
14+
//name string
15+
//question string
16+
//}{
17+
//{
18+
//name: "ACME Challenge",
19+
//question: "_acme-challenge.example.com.",
20+
//},
21+
//}
22+
//}
2323

2424
func setupACME(readyChan chan string) {
2525
acmeServer := &ACMEServer{

test/Corefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ tls://.:1053 {
22
tls acme {
33
domain example.com
44
ca localhost:14001/dir
5+
cacert test/certs/pebble.minica.pem
56
}
67
whoami
78
}

test/pebble_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ func TestRenewal(t *testing.T) {
174174
tls acme {
175175
domain example.com
176176
ca localhost:14001/dir
177+
cacert test/certs/pebble.minica.pem
178+
port 1053
177179
}
178180
whoami
179181
}`,
@@ -219,19 +221,19 @@ func TestRenewal(t *testing.T) {
219221
// wait for certificate to expire
220222
time.Sleep(80 * time.Second)
221223

222-
_, _, err = client.Exchange(m, tcp)
224+
r, _, err := client.Exchange(m, tcp)
223225

224226
if err != nil {
225227
if err.Error() == "x509: cannot validate certificate for :: because it doesn't contain any IP SANs" {
226-
fmt.Println("This errror is expected")
228+
fmt.Println("Ignoring certificate error")
227229
} else {
228230
fmt.Println(err)
229231
}
230232
}
231233

232-
//if n := len(r.Answer); n != tc.AnswerLength {
233-
//t.Errorf("Expected %v answers, got %v", tc.AnswerLength, n)
234-
//}
234+
if n := len(r.Answer); n != tc.AnswerLength {
235+
t.Errorf("Expected %v answers, got %v", tc.AnswerLength, n)
236+
}
235237

236238
//if tc.AnswerLength > 0 {
237239
//if r.Answer[0].(*dns.A).A.String() != tc.ExpectedIP {
@@ -243,6 +245,5 @@ func TestRenewal(t *testing.T) {
243245
//t.Errorf("Expected 2 RRs in additional section, but got %d", n)
244246
//}
245247
//}
246-
})
247-
}
248+
}) }
248249
}

0 commit comments

Comments
 (0)