Skip to content

Commit 7ebf3cd

Browse files
committed
Merge branch 'master' into mta-sts-upgrade
2 parents c2a8d29 + 321f55e commit 7ebf3cd

File tree

22 files changed

+281
-141
lines changed

22 files changed

+281
-141
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
- TEST_DB_NAME=starttls_test
1111

1212
install:
13-
- go get -u github.com/golang/lint/golint
13+
- go get -u golang.org/x/lint/golint
1414
- go get github.com/mattn/goveralls
1515
- go get -t ./...
1616

api.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ func getDomainParams(r *http.Request) (models.Domain, error) {
180180
}
181181
mtasts := r.FormValue("mta-sts")
182182
domain := models.Domain{
183-
Name: name,
184-
MTASTSMode: mtasts,
185-
State: models.StateUnvalidated,
183+
Name: name,
184+
MTASTS: mtasts == "on",
185+
State: models.StateUnconfirmed,
186186
}
187187
email, err := getParam("email", r)
188188
if err == nil {
@@ -233,7 +233,7 @@ func (api API) Queue(r *http.Request) APIResponse {
233233
if err != nil {
234234
return badRequest(err.Error())
235235
}
236-
ok, msg, scan := domain.IsQueueable(api.Database, api.List)
236+
ok, msg, scan := domain.IsQueueable(api.Database, api.Database, api.List)
237237
if !ok {
238238
return badRequest(msg)
239239
}
@@ -257,13 +257,13 @@ func (api API) Queue(r *http.Request) APIResponse {
257257
if err != nil {
258258
return badRequest(err.Error())
259259
}
260-
status, err := api.Database.GetDomain(domainName)
260+
domainObj, err := models.GetDomain(api.Database, domainName)
261261
if err != nil {
262262
return APIResponse{StatusCode: http.StatusNotFound, Message: err.Error()}
263263
}
264264
return APIResponse{
265265
StatusCode: http.StatusOK,
266-
Response: status,
266+
Response: domainObj,
267267
}
268268
}
269269
return APIResponse{StatusCode: http.StatusMethodNotAllowed,

checker/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM golang:1.11-alpine
2+
3+
WORKDIR /go/src/github.com/EFForg/starttls-backend/checker
4+
5+
RUN apk add git
6+
7+
ADD . .
8+
9+
RUN go get github.com/EFForg/starttls-backend/checker/cmd/starttls-check
10+
11+
CMD ["/go/bin/starttls-check"]

checker/checker.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ type Checker struct {
2020
// domain. It is used to mock DNS lookups during testing.
2121
lookupMXOverride func(string) ([]*net.MX, error)
2222

23-
// checkHostnameOverride is used to mock checks for a single hostname.
24-
checkHostnameOverride func(string, string) HostnameResult
23+
// CheckHostname defines the function that should be used to check each hostname.
24+
// If nil, FullCheckHostname (all hostname checks) will be used.
25+
CheckHostname func(string, string, time.Duration) HostnameResult
2526

2627
// checkMTASTSOverride is used to mock MTA-STS checks.
2728
checkMTASTSOverride func(string, map[string]HostnameResult) *MTASTSResult

checker/cmd/starttls-check/cmd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func main() {
8383

8484
domainReader := csv.NewReader(instream)
8585
if *aggregate {
86+
c = checker.Checker{
87+
CheckHostname: checker.NoopCheckHostname,
88+
}
8689
resultHandler = &checker.DomainTotals{
8790
Time: time.Now(),
8891
Source: label,

checker/cmd/starttls-check/cmd_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import (
1717
func TestUpdateStats(t *testing.T) {
1818
out = new(bytes.Buffer)
1919
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20-
fmt.Fprintln(w, `1,foo,example1.com
21-
2,bar,example2.com
22-
3,baz,example3.com`)
20+
fmt.Fprintln(w, `1,foo,localhost
21+
2,bar,localhost
22+
3,baz,localhost`)
2323
}))
2424
defer ts.Close()
2525

checker/domain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (c *Checker) CheckDomain(domain string, expectedHostnames []string) DomainR
120120
}
121121
checkedHostnames := make([]string, 0)
122122
for _, hostname := range hostnames {
123-
hostnameResult := c.CheckHostnameWithCache(domain, hostname)
123+
hostnameResult := c.checkHostname(domain, hostname)
124124
result.HostnameResults[hostname] = hostnameResult
125125
if hostnameResult.couldConnect() {
126126
checkedHostnames = append(checkedHostnames, hostname)

checker/domain_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func mockLookupMX(domain string) ([]*net.MX, error) {
5959
return result, nil
6060
}
6161

62-
func mockCheckHostname(domain string, hostname string) HostnameResult {
62+
func mockCheckHostname(domain string, hostname string, _ time.Duration) HostnameResult {
6363
if result, ok := hostnameResults[hostname]; ok {
6464
return HostnameResult{
6565
Result: &result,
@@ -110,11 +110,11 @@ func performTests(t *testing.T, tests []domainTestCase) {
110110

111111
func performTestsWithCacheTimeout(t *testing.T, tests []domainTestCase, cacheExpiry time.Duration) {
112112
c := Checker{
113-
Timeout: time.Second,
114-
Cache: MakeSimpleCache(cacheExpiry),
115-
lookupMXOverride: mockLookupMX,
116-
checkHostnameOverride: mockCheckHostname,
117-
checkMTASTSOverride: mockCheckMTASTS,
113+
Timeout: time.Second,
114+
Cache: MakeSimpleCache(cacheExpiry),
115+
lookupMXOverride: mockLookupMX,
116+
CheckHostname: mockCheckHostname,
117+
checkMTASTSOverride: mockCheckMTASTS,
118118
}
119119
for _, test := range tests {
120120
if test.expectedHostnames == nil {

checker/hostname.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,29 +210,41 @@ func checkTLSVersion(client *smtp.Client, hostname string, timeout time.Duration
210210
return result.Success()
211211
}
212212

213-
// CheckHostnameWithCache returns the result of CheckHostname, using or
214-
// updating the Checker's cache.
215-
func (c *Checker) CheckHostnameWithCache(domain string, hostname string) HostnameResult {
213+
// checkHostname returns the result of c.CheckHostname or FullCheckHostname,
214+
// using or updating the Checker's cache.
215+
func (c *Checker) checkHostname(domain string, hostname string) HostnameResult {
216+
check := c.CheckHostname
217+
if check == nil {
218+
// If CheckHostname hasn't been set, default to the full set of checks.
219+
check = FullCheckHostname
220+
}
221+
216222
if c.Cache == nil {
217-
return c.CheckHostname(domain, hostname)
223+
return check(domain, hostname, c.timeout())
218224
}
219225
hostnameResult, err := c.Cache.GetHostnameScan(hostname)
220226
if err != nil {
221-
hostnameResult = c.CheckHostname(domain, hostname)
227+
hostnameResult = check(domain, hostname, c.timeout())
222228
c.Cache.PutHostnameScan(hostname, hostnameResult)
223229
}
224230
return hostnameResult
225231
}
226232

227-
// CheckHostname performs a series of checks against a hostname for an email domain.
228-
// `domain` is the mail domain that this server serves email for.
229-
// `hostname` is the hostname for this server.
230-
func (c *Checker) CheckHostname(domain string, hostname string) HostnameResult {
231-
if c.checkHostnameOverride != nil {
232-
// Allow the Checker to mock this function.
233-
return c.checkHostnameOverride(domain, hostname)
233+
// NoopCheckHostname returns a fake error result containing `domain` and `hostname`.
234+
func NoopCheckHostname(domain string, hostname string, _ time.Duration) HostnameResult {
235+
r := HostnameResult{
236+
Domain: domain,
237+
Hostname: hostname,
238+
Result: MakeResult("hostnames"),
234239
}
240+
r.addCheck(MakeResult(Connectivity).Error("Skipping hostname checks"))
241+
return r
242+
}
235243

244+
// FullCheckHostname performs a series of checks against a hostname for an email domain.
245+
// `domain` is the mail domain that this server serves email for.
246+
// `hostname` is the hostname for this server.
247+
func FullCheckHostname(domain string, hostname string, timeout time.Duration) HostnameResult {
236248
result := HostnameResult{
237249
Domain: domain,
238250
Hostname: hostname,
@@ -242,7 +254,7 @@ func (c *Checker) CheckHostname(domain string, hostname string) HostnameResult {
242254

243255
// Connect to the SMTP server and use that connection to perform as many checks as possible.
244256
connectivityResult := MakeResult(Connectivity)
245-
client, err := smtpDialWithTimeout(hostname, c.timeout())
257+
client, err := smtpDialWithTimeout(hostname, timeout)
246258
if err != nil {
247259
result.addCheck(connectivityResult.Error("Could not establish connection: %v", err))
248260
return result
@@ -258,7 +270,7 @@ func (c *Checker) CheckHostname(domain string, hostname string) HostnameResult {
258270
// result.addCheck(checkTLSCipher(hostname))
259271

260272
// Creates a new connection to check for SSLv2/3 support because we can't call starttls twice.
261-
result.addCheck(checkTLSVersion(client, hostname, c.timeout()))
273+
result.addCheck(checkTLSVersion(client, hostname, timeout))
262274

263275
return result
264276
}

checker/hostname_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ func TestMain(m *testing.M) {
2525

2626
const testTimeout = 250 * time.Millisecond
2727

28-
var testChecker = Checker{Timeout: testTimeout}
29-
3028
// Code follows pattern from crypto/tls/generate_cert.go
3129
// to generate a cert from a PEM-encoded RSA private key.
3230
func createCert(keyData string, commonName string) string {
@@ -102,7 +100,7 @@ func TestPolicyMatch(t *testing.T) {
102100
}
103101

104102
func TestNoConnection(t *testing.T) {
105-
result := testChecker.CheckHostname("", "example.com")
103+
result := FullCheckHostname("", "example.com", testTimeout)
106104

107105
expected := Result{
108106
Status: 3,
@@ -117,7 +115,7 @@ func TestNoTLS(t *testing.T) {
117115
ln := smtpListenAndServe(t, &tls.Config{})
118116
defer ln.Close()
119117

120-
result := testChecker.CheckHostname("", ln.Addr().String())
118+
result := FullCheckHostname("", ln.Addr().String(), testTimeout)
121119

122120
expected := Result{
123121
Status: 2,
@@ -137,7 +135,7 @@ func TestSelfSigned(t *testing.T) {
137135
ln := smtpListenAndServe(t, &tls.Config{Certificates: []tls.Certificate{cert}})
138136
defer ln.Close()
139137

140-
result := testChecker.CheckHostname("", ln.Addr().String())
138+
result := FullCheckHostname("", ln.Addr().String(), testTimeout)
141139

142140
expected := Result{
143141
Status: 2,
@@ -163,7 +161,7 @@ func TestNoTLS12(t *testing.T) {
163161
})
164162
defer ln.Close()
165163

166-
result := testChecker.CheckHostname("", ln.Addr().String())
164+
result := FullCheckHostname("", ln.Addr().String(), testTimeout)
167165

168166
expected := Result{
169167
Status: 2,
@@ -196,7 +194,7 @@ func TestSuccessWithFakeCA(t *testing.T) {
196194
// conserving the port number.
197195
addrParts := strings.Split(ln.Addr().String(), ":")
198196
port := addrParts[len(addrParts)-1]
199-
result := testChecker.CheckHostname("", "localhost:"+port)
197+
result := FullCheckHostname("", "localhost:"+port, testTimeout)
200198
expected := Result{
201199
Status: 0,
202200
Checks: map[string]*Result{
@@ -271,7 +269,7 @@ func TestFailureWithBadHostname(t *testing.T) {
271269
// conserving the port number.
272270
addrParts := strings.Split(ln.Addr().String(), ":")
273271
port := addrParts[len(addrParts)-1]
274-
result := testChecker.CheckHostname("", "localhost:"+port)
272+
result := FullCheckHostname("", "localhost:"+port, testTimeout)
275273
expected := Result{
276274
Status: 2,
277275
Checks: map[string]*Result{
@@ -311,7 +309,7 @@ func TestAdvertisedCiphers(t *testing.T) {
311309

312310
ln := smtpListenAndServe(t, tlsConfig)
313311
defer ln.Close()
314-
testChecker.CheckHostname("", ln.Addr().String())
312+
FullCheckHostname("", ln.Addr().String(), testTimeout)
315313

316314
// Partial list of ciphers we want to support
317315
expectedCipherSuites := []struct {

0 commit comments

Comments
 (0)