Skip to content

Commit f15a07d

Browse files
author
Harry Harpham
committed
Expand and refactor helpers/testsuite, add bundler and scan tests
- Add TestServer to helpers/testsuite for tests requiring a configurable, local TLS server - Remove CreateSelfSignedCert from helpers/testsuite due to overlap with initca.New - Complete re-do of SignCertificate in helpers/testsuite using internal packages (rather than parsing CLI) - Re-factor helpers/testsuite into logical distinct files for maintainability - Add tests to helpers/testsuite for TestServer - Add tests to bundler to test BundleFromRemote against local testsuite.TestServer (TLS) - Add tests to scan to test against local testsuite.TestServer (both TLS and TCP)
2 parents 57a3be2 + d71896b commit f15a07d

File tree

32 files changed

+451
-63
lines changed

32 files changed

+451
-63
lines changed

.travis.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ before_script:
1010
- go get github.com/mattn/goveralls
1111
- go get -v github.com/GeertJohan/fgt
1212
script:
13-
- go get github.com/cloudflare/cfssl/...
14-
- go test github.com/cloudflare/cfssl/...
13+
- go get github.com/cloudflare/cfssl/...
14+
- go test github.com/cloudflare/cfssl/...
1515
- go vet github.com/cloudflare/cfssl/...
1616
- fgt golint github.com/cloudflare/cfssl/...
1717
- go list -f '{{if len .TestGoFiles}}"go test -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"{{end}}' ./... | xargs -i sh -c {}
18-
- gover
19-
- goveralls -coverprofile=gover.coverprofile -service=travis-ci -repotoken $COVERALLS_TOKEN
18+
- gover
19+
- if [ $COVERALLS_TOKEN != "" ]; then
20+
goveralls -coverprofile=gover.coverprofile -service=travis-ci -repotoken $COVERALLS_TOKEN;
21+
fi
2022
notifications:
2123
email:
2224
recipients:

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
[![Build Status](https://travis-ci.org/cloudflare/cfssl.png?branch=master)](https://travis-ci.org/cloudflare/cfssl)
44
[![Coverage Status](https://coveralls.io/repos/cloudflare/cfssl/badge.svg?branch=master)](https://coveralls.io/r/cloudflare/cfssl?branch=master)
55
[![GoDoc](https://godoc.org/github.com/cloudflare/cfssl?status.png)](https://godoc.org/github.com/cloudflare/cfssl)
6-
## CloudFlare's SSL tool
6+
## CloudFlare's PKI/TLS toolkit
77

8-
CFSSL is CloudFlare's SSL swiss army knife. It is both a command line
9-
tool and an HTTP API server for signing, verifying, and bundling SSL
8+
CFSSL is CloudFlare's PKI/TLS swiss army knife. It is both a command line
9+
tool and an HTTP API server for signing, verifying, and bundling TLS
1010
certificates. It requires Go 1.4 to build.
1111

1212
Note that certain linux distributions have certain algorithms removed
13-
(RHEL-based distributions in particular), so the golang from the
13+
(RHEL-based distributions in particular), so the golang from the
1414
official repositories will not work. Users of these distributions should
1515
[install go manually](golang.org) to install CFSSL.
1616

api/api.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ type Handler interface {
1616
}
1717

1818
// HTTPHandler is a wrapper that encapsulates Handler interface as http.Handler.
19-
// HttpHandler also enforces that the Handler only responds to requests with registered HTTP method.
19+
// HTTPHandler also enforces that the Handler only responds to requests with registered HTTP methods.
2020
type HTTPHandler struct {
21-
Handler // CFSSL handler
22-
Method string // The assoicated HTTP method
21+
Handler // CFSSL handler
22+
Methods []string // The associated HTTP methods
2323
}
2424

2525
// HandlerFunc is similar to the http.HandlerFunc type; it serves as
@@ -69,11 +69,17 @@ func handleError(w http.ResponseWriter, err error) (code int) {
6969
// and return the response with proper HTTP status code
7070
func (h HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7171
var err error
72+
var match bool
7273
// Throw 405 when requested with an unsupported verb.
73-
if r.Method != h.Method {
74-
err = errors.NewMethodNotAllowed(r.Method)
75-
} else {
74+
for _, m := range h.Methods {
75+
if m == r.Method {
76+
match = true
77+
}
78+
}
79+
if match {
7680
err = h.Handle(w, r)
81+
} else {
82+
err = errors.NewMethodNotAllowed(r.Method)
7783
}
7884
status := handleError(w, err)
7985
log.Infof("%s - \"%s %s\" %d", r.RemoteAddr, r.Method, r.URL, status)

api/api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func get(t *testing.T, ts *httptest.Server) (resp *http.Response, body []byte) {
7373
}
7474

7575
func TestRigidHandle(t *testing.T) {
76-
ts := httptest.NewServer(HTTPHandler{Handler: HandlerFunc(simpleHandle), Method: "POST"})
76+
ts := httptest.NewServer(HTTPHandler{Handler: HandlerFunc(simpleHandle), Methods: []string{"POST"}})
7777
defer ts.Close()
7878

7979
// Response to compliment
@@ -143,7 +143,7 @@ func TestRigidHandle(t *testing.T) {
143143
}
144144

145145
func TestCleverHandle(t *testing.T) {
146-
ts := httptest.NewServer(HTTPHandler{Handler: HandlerFunc(cleverHandle), Method: "POST"})
146+
ts := httptest.NewServer(HTTPHandler{Handler: HandlerFunc(cleverHandle), Methods: []string{"POST"}})
147147
defer ts.Close()
148148

149149
// Response ty to compliment

api/bundle/bundle.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package bundle implements the HTTP handler for the bundle command.
12
package bundle
23

34
import (
@@ -27,7 +28,7 @@ func NewHandler(caBundleFile, intBundleFile string) (http.Handler, error) {
2728
}
2829

2930
log.Info("bundler API ready")
30-
return api.HTTPHandler{Handler: b, Method: "POST"}, nil
31+
return api.HTTPHandler{Handler: b, Methods: []string{"POST"}}, nil
3132
}
3233

3334
// Handle implements an http.Handler interface for the bundle handler.

api/client/client.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package client implements the a Go client for CFSSL API commands.
12
package client
23

34
import (
@@ -153,20 +154,25 @@ func (srv *Server) Info(jsonData []byte) (*InfoResp, error) {
153154
return nil, err
154155
}
155156

156-
cert := res["certificate"]
157-
usages := res["usages"].([]interface{})
158-
exp := res["expiry"]
157+
info := new(InfoResp)
158+
159+
if val, ok := res["certificate"]; ok {
160+
info.Certificate = val.(string)
161+
}
162+
var usages []interface{}
163+
if val, ok := res["usages"]; ok {
164+
usages = val.([]interface{})
165+
}
166+
if val, ok := res["expiry"]; ok {
167+
info.ExpiryString = val.(string)
168+
}
159169

160170
usageStrings := make([]string, len(usages))
161171
for i, s := range usages {
162172
usageStrings[i] = s.(string)
163173
}
164174

165-
return &InfoResp{
166-
Certificate: cert.(string),
167-
Usage: usageStrings,
168-
ExpiryString: exp.(string),
169-
}, nil
175+
return info, nil
170176
}
171177

172178
func (srv *Server) getResultMap(jsonData []byte, target string) (result map[string]interface{}, err error) {

api/generator/generator.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package generator implements the HTTP handlers for certificate generation.
12
package generator
23

34
import (
@@ -51,7 +52,7 @@ func NewHandler(validator Validator) (http.Handler, error) {
5152
Handler: &Handler{
5253
generator: &csr.Generator{Validator: validator},
5354
},
54-
Method: "POST",
55+
Methods: []string{"POST"},
5556
}, nil
5657
}
5758

@@ -176,7 +177,7 @@ func NewCertGeneratorHandler(validator Validator, caFile, caKeyFile string, poli
176177

177178
cg.generator = &csr.Generator{Validator: validator}
178179

179-
return api.HTTPHandler{Handler: cg, Method: "POST"}, nil
180+
return api.HTTPHandler{Handler: cg, Methods: []string{"POST"}}, nil
180181
}
181182

182183
// NewCertGeneratorHandlerFromSigner returns a handler directly from
@@ -187,7 +188,7 @@ func NewCertGeneratorHandlerFromSigner(validator Validator, signer signer.Signer
187188
generator: &csr.Generator{Validator: validator},
188189
signer: signer,
189190
},
190-
Method: "POST",
191+
Methods: []string{"POST"},
191192
}
192193
}
193194

api/info/info.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package info implements the HTTP handler for the info command.
12
package info
23

34
import (
@@ -30,7 +31,7 @@ func NewHandler(s signer.Signer) (http.Handler, error) {
3031
Handler: &Handler{
3132
sign: s,
3233
},
33-
Method: "POST",
34+
Methods: []string{"POST"},
3435
}, nil
3536
}
3637

@@ -98,7 +99,7 @@ func NewMultiHandler(signers map[string]signer.Signer, defaultLabel string) (htt
9899
signers: signers,
99100
defaultLabel: defaultLabel,
100101
},
101-
Method: "POST",
102+
Methods: []string{"POST"},
102103
}, nil
103104
}
104105

api/initca/initca.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package initca implements the HTTP handler for the CA initialization command
12
package initca
23

34
import (
@@ -54,5 +55,5 @@ func initialCAHandler(w http.ResponseWriter, r *http.Request) error {
5455
// NewHandler returns a new http.Handler that handles request to
5556
// initialize a CA.
5657
func NewHandler() http.Handler {
57-
return api.HTTPHandler{Handler: api.HandlerFunc(initialCAHandler), Method: "POST"}
58+
return api.HTTPHandler{Handler: api.HandlerFunc(initialCAHandler), Methods: []string{"POST"}}
5859
}

api/scan/scan.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ func scanHandler(w http.ResponseWriter, r *http.Request) error {
5151

5252
// NewHandler returns a new http.Handler that handles a scan request.
5353
func NewHandler() http.Handler {
54-
return api.HTTPHandler{Handler: api.HandlerFunc(scanHandler), Method: "GET"}
54+
return api.HTTPHandler{
55+
Handler: api.HandlerFunc(scanHandler),
56+
Methods: []string{"GET"},
57+
}
5558
}
5659

5760
// scanInfoHandler is an HTTP handler that returns a JSON blob result describing
@@ -66,5 +69,5 @@ func scanInfoHandler(w http.ResponseWriter, r *http.Request) error {
6669

6770
// NewInfoHandler returns a new http.Handler that handles a request for scan info.
6871
func NewInfoHandler() http.Handler {
69-
return api.HTTPHandler{Handler: api.HandlerFunc(scanInfoHandler), Method: "GET"}
72+
return api.HTTPHandler{Handler: api.HandlerFunc(scanInfoHandler), Methods: []string{"GET"}}
7073
}

0 commit comments

Comments
 (0)