This repository has been archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gravel_test.go
226 lines (174 loc) · 7.26 KB
/
gravel_test.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
216
217
218
219
220
221
222
223
224
225
226
package gravel
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"fmt"
"net"
"testing"
"time"
"github.com/go-acme/lego/v3/certcrypto"
"github.com/go-acme/lego/v3/certificate"
"github.com/go-acme/lego/v3/challenge/dns01"
"github.com/go-acme/lego/v3/lego"
"github.com/go-acme/lego/v3/registration"
"github.com/stretchr/testify/assert"
)
func TestNewDefaultGravelOpts(t *testing.T) {
at := assert.New(t)
gopts := NewDefaultGravelOpts()
at.NotEmpty(gopts, "Core defaults should not be empty")
at.NotEmpty(gopts.Logger, "Logger should be instantiated for downstream consumption")
at.NotEmpty(gopts.DnsOpts, "DnsOpts are needed to build the integration DNS server")
at.NotEmpty(gopts.WfeOpts, "WfeOpts are needed to ensure the WFE starts properly")
at.NotEmpty(gopts.DatabaseOpts, "DatabaseOpts are needed to configure the memory store")
at.NotEmpty(gopts.CAOpts, "CAOpts are needed to configure the core CA")
at.NotEmpty(gopts.VAOpts, "VAOpts are needed to ensure the VA starts properly")
at.Empty(gopts.EnableTestIntegration, "EnableTestIntegration should be false in default opts.")
at.NotEmpty(gopts.ListenAddress, "ListenAddress cannot be nil as it's needed to start the Let's Encrypt server.")
}
func TestNewWithDefaults(t *testing.T) {
at := assert.New(t)
g, err := New(NewDefaultGravelOpts())
at.Nil(err, "there should not be an error on instantiation")
at.NotEmpty(g, "Gravel should not be nil")
at.NotEmpty(g.Client, "Gravel HTTP client should not be nil")
at.NotEmpty(g.Logger, "Gravel logger should not be nil")
at.NotEmpty(g.CertificateAuthority, "Gravel Certificate Authority should not be nil")
at.NotEmpty(g.WebFrontEnd, "Gravel WFE should not be nil")
at.NotEmpty(g.VerificationAuthority, "Gravel VA should not be nil")
at.NotEmpty(g.Database, "Gravel memory store should not be nil")
at.NotEmpty(g.DnsServer, "Gravel DNS server should not be nil")
at.Empty(g.CertificateServer, "Gravel Certificate Server should be nil as it's not started yet")
l, err := net.Dial("tcp", fmt.Sprintf(":%d", g.Opts.DnsOpts.DnsPort))
at.Error(err, "TCP connection to the DNS server should error because it's not started yet")
at.Nil(l, "connection interface should be empty")
}
func TestGravelStartDnsServer(t *testing.T) {
at := assert.New(t)
opts := NewDefaultGravelOpts()
g, err := New(opts)
at.Nil(err, "there should not be an error on instantiation")
go g.StartDnsServer()
_, err = net.Dial("udp", fmt.Sprintf("127.0.0.1:%d", opts.DnsOpts.DnsPort))
at.Nil(err, "there should be not connection errors because the server is started")
if err := g.DnsServer.Server.Shutdown(); err != nil {
at.NoError(err, "there must not be an error shutting down the dns server")
}
}
func TestGravelStartWebServer(t *testing.T) {
at := assert.New(t)
opts := NewDefaultGravelOpts()
g, err := New(opts)
at.Nil(err, "there should not be an error on instantiation")
go g.StartWebServer()
time.Sleep(3 * time.Second)
_, err = net.Dial("tcp", opts.ListenAddress)
at.Nil(err, "there should not be an error when dialing the web server")
err = g.CertificateServer.Shutdown(context.Background())
at.Nil(err, "there should not be an error when stopping the server")
}
type TestUser struct {
Email string
Registration *registration.Resource
key crypto.PrivateKey
}
func (u *TestUser) GetEmail() string {
return u.Email
}
func (u TestUser) GetRegistration() *registration.Resource {
return u.Registration
}
func (u *TestUser) GetPrivateKey() crypto.PrivateKey {
return u.key
}
func TestGravelWithDNSCertificate(t *testing.T) {
at := assert.New(t)
opts := NewDefaultGravelOpts()
opts.VAOpts.CustomResolverAddress = fmt.Sprintf("localhost:%d", opts.DnsOpts.DnsPort)
g, err := New(opts)
at.Nil(err, "there should not be an error on instantiation")
// start the servers.
go g.StartDnsServer()
go g.StartWebServer()
time.Sleep(3 * time.Second)
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
at.Nil(err, "there should be no error when generating a certificate")
tu := TestUser{
Email: "[email protected]",
key: privateKey,
}
config := lego.NewConfig(&tu)
config.HTTPClient = g.Client
config.Certificate.KeyType = certcrypto.RSA2048
config.CADirURL = fmt.Sprintf("https://%s%s", g.Opts.ListenAddress, g.Opts.WfeOpts.DirectoryPath)
client, err := lego.NewClient(config)
at.Nil(err, "there should not be an error when instantiating a new let's encrypt client")
err = client.Challenge.SetDNS01Provider(
g.Opts.DnsOpts.Provider,
dns01.AddRecursiveNameservers([]string{
fmt.Sprintf("127.0.0.1:%d", g.Opts.DnsOpts.DnsPort),
}),
dns01.WrapPreCheck(g.DnsServer.PreCheck))
at.Nil(err, "client dns challenge configuration should be nil")
reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
at.Nil(err, "there should be no registration errors")
tu.Registration = reg
request := certificate.ObtainRequest{
Domains: []string{"test.service"},
Bundle: true,
}
certificates, err := client.Certificate.Obtain(request)
at.Nil(err, "there should be no error obtaining a certificate")
at.NotEmpty(certificates, "certificates should not be empty")
at.EqualValues("test.service", certificates.Domain, "certificate domain should match")
if err := g.DnsServer.Server.Shutdown(); err != nil {
at.NoError(err, "there should not be an error shutting down the dns server")
}
if err := g.CertificateServer.Shutdown(context.TODO()); err != nil {
at.NoError(err, "there should not be an error shutting down the dns server")
}
}
func TestGravelWithMultipleDNSCertificates(t *testing.T) {
at := assert.New(t)
opts := NewDefaultGravelOpts()
opts.VAOpts.CustomResolverAddress = fmt.Sprintf("localhost:%d", opts.DnsOpts.DnsPort)
g, err := New(opts)
at.Nil(err, "there should not be an error on instantiation")
// start the servers.
go g.StartDnsServer()
go g.StartWebServer()
time.Sleep(3 * time.Second)
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
at.Nil(err, "there should be no error when generating a certificate")
tu := TestUser{
Email: "[email protected]",
key: privateKey,
}
config := lego.NewConfig(&tu)
config.HTTPClient = g.Client
config.Certificate.KeyType = certcrypto.RSA2048
config.CADirURL = fmt.Sprintf("https://%s%s", g.Opts.ListenAddress, g.Opts.WfeOpts.DirectoryPath)
client, err := lego.NewClient(config)
at.Nil(err, "there should not be an error when instantiating a new let's encrypt client")
err = client.Challenge.SetDNS01Provider(
g.Opts.DnsOpts.Provider,
dns01.AddRecursiveNameservers([]string{
fmt.Sprintf("127.0.0.1:%d", g.Opts.DnsOpts.DnsPort),
}),
dns01.WrapPreCheck(g.DnsServer.PreCheck))
at.Nil(err, "client dns challenge configuration should be nil")
reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
at.Nil(err, "there should be no registration errors")
tu.Registration = reg
request := certificate.ObtainRequest{
Domains: []string{"test.service", "test2.service", "test3.service"},
Bundle: true,
}
certificates, err := client.Certificate.Obtain(request)
at.Nil(err, "there should be no error obtaining a certificate")
at.NotEmpty(certificates, "certificates should not be empty")
at.EqualValues("test.service", certificates.Domain, "certificate domain should match")
}