2015-06-13 17:13:04 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
2015-06-13 19:06:47 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
2015-06-13 17:13:04 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-06-13 19:06:47 +00:00
|
|
|
"net/http/httptest"
|
2015-06-13 17:13:04 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-06-13 19:06:47 +00:00
|
|
|
|
|
|
|
"github.com/square/go-jose"
|
2015-06-13 17:13:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSimpleHTTPCanSolve(t *testing.T) {
|
|
|
|
challenge := &simpleHTTPChallenge{}
|
|
|
|
|
|
|
|
// determine public ip
|
|
|
|
resp, err := http.Get("https://icanhazip.com/")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not get public IP -> %v", err)
|
|
|
|
}
|
2015-10-22 04:16:29 +00:00
|
|
|
defer resp.Body.Close()
|
2015-06-13 17:13:04 +00:00
|
|
|
|
|
|
|
ip, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not get public IP -> %v", err)
|
|
|
|
}
|
|
|
|
ipStr := string(ip)
|
|
|
|
|
|
|
|
if expected, actual := false, challenge.CanSolve("google.com"); expected != actual {
|
|
|
|
t.Errorf("Expected CanSolve to return %t for domain 'google.com' but was %t", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
localResolv := strings.Replace(ipStr, "\n", "", -1) + ".xip.io"
|
|
|
|
if expected, actual := true, challenge.CanSolve(localResolv); expected != actual {
|
|
|
|
t.Errorf("Expected CanSolve to return %t for domain 'localhost' but was %t", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2015-06-13 19:06:47 +00:00
|
|
|
|
|
|
|
func TestSimpleHTTP(t *testing.T) {
|
|
|
|
privKey, err := generatePrivateKey(512)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not generate public key -> %v", err)
|
|
|
|
}
|
|
|
|
jws := &jws{privKey: privKey}
|
|
|
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-09-26 17:45:52 +00:00
|
|
|
w.Header().Add("Replay-Nonce", "12345")
|
2015-06-13 19:06:47 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
solver := &simpleHTTPChallenge{jws: jws}
|
2015-09-26 17:45:52 +00:00
|
|
|
clientChallenge := challenge{Type: "simpleHttp", Status: "pending", URI: ts.URL, Token: "123456789"}
|
2015-06-13 19:06:47 +00:00
|
|
|
|
|
|
|
// validate error on non-root bind to 443
|
|
|
|
if err = solver.Solve(clientChallenge, "test.domain"); err == nil {
|
|
|
|
t.Error("BIND: Expected Solve to return an error but the error was nil.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate error on unexpected state
|
2015-10-18 00:09:19 +00:00
|
|
|
solver.optPort = "23456"
|
2015-06-13 19:06:47 +00:00
|
|
|
if err = solver.Solve(clientChallenge, "test.domain"); err == nil {
|
|
|
|
t.Error("UNEXPECTED: Expected Solve to return an error but the error was nil.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate error on invalid status
|
|
|
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-09-26 17:45:52 +00:00
|
|
|
w.Header().Add("Replay-Nonce", "12345")
|
|
|
|
failed := challenge{Type: "simpleHttp", Status: "invalid", URI: ts.URL, Token: "1234567810"}
|
2015-06-13 19:06:47 +00:00
|
|
|
jsonBytes, _ := json.Marshal(&failed)
|
|
|
|
w.Write(jsonBytes)
|
|
|
|
})
|
2015-09-26 17:45:52 +00:00
|
|
|
clientChallenge.Token = "1234567810"
|
2015-06-13 19:06:47 +00:00
|
|
|
if err = solver.Solve(clientChallenge, "test.domain"); err == nil {
|
|
|
|
t.Error("FAILED: Expected Solve to return an error but the error was nil.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate no error on valid response
|
|
|
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-09-26 17:45:52 +00:00
|
|
|
w.Header().Add("Replay-Nonce", "12345")
|
|
|
|
valid := challenge{Type: "simpleHttp", Status: "valid", URI: ts.URL, Token: "1234567811"}
|
2015-06-13 19:06:47 +00:00
|
|
|
jsonBytes, _ := json.Marshal(&valid)
|
|
|
|
w.Write(jsonBytes)
|
|
|
|
})
|
2015-09-26 17:45:52 +00:00
|
|
|
clientChallenge.Token = "1234567811"
|
2015-06-13 19:06:47 +00:00
|
|
|
if err = solver.Solve(clientChallenge, "test.domain"); err != nil {
|
|
|
|
t.Errorf("VALID: Expected Solve to return no error but the error was -> %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-18 00:09:19 +00:00
|
|
|
// Validate server on port 23456 which responds appropriately
|
2015-09-26 17:45:52 +00:00
|
|
|
clientChallenge.Token = "1234567812"
|
2015-06-13 19:06:47 +00:00
|
|
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var request challenge
|
2015-09-26 17:45:52 +00:00
|
|
|
w.Header().Add("Replay-Nonce", "12345")
|
|
|
|
|
|
|
|
if r.Method == "HEAD" {
|
|
|
|
return
|
|
|
|
}
|
2015-06-13 19:06:47 +00:00
|
|
|
|
|
|
|
clientJws, _ := ioutil.ReadAll(r.Body)
|
|
|
|
j, err := jose.ParseSigned(string(clientJws))
|
|
|
|
if err != nil {
|
2015-09-26 17:45:52 +00:00
|
|
|
t.Errorf("Client sent invalid JWS to the server.\n\t%v", err)
|
|
|
|
return
|
2015-06-13 19:06:47 +00:00
|
|
|
}
|
|
|
|
output, err := j.Verify(&privKey.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to verify client data -> %v", err)
|
|
|
|
}
|
|
|
|
json.Unmarshal(output, &request)
|
|
|
|
|
|
|
|
transport := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
|
|
|
|
client := &http.Client{Transport: transport}
|
|
|
|
|
2015-10-18 00:09:19 +00:00
|
|
|
reqURL := "https://localhost:23456/.well-known/acme-challenge/" + clientChallenge.Token
|
2015-06-13 19:06:47 +00:00
|
|
|
t.Logf("Request URL is: %s", reqURL)
|
|
|
|
req, _ := http.NewRequest("GET", reqURL, nil)
|
|
|
|
req.Host = "test.domain"
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2015-10-18 00:09:19 +00:00
|
|
|
t.Errorf("Expected the solver to listen on port 23456 -> %v", err)
|
2015-06-13 19:06:47 +00:00
|
|
|
}
|
2015-10-22 04:16:29 +00:00
|
|
|
defer resp.Body.Close()
|
2015-06-13 19:06:47 +00:00
|
|
|
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
bodyStr := string(body)
|
2015-09-26 17:45:52 +00:00
|
|
|
clientResponse, err := jose.ParseSigned(bodyStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Client answered with invalid JWS.\n\t%v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = clientResponse.Verify(&privKey.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to verify client data -> %v", err)
|
2015-06-13 19:06:47 +00:00
|
|
|
}
|
|
|
|
|
2015-09-26 17:45:52 +00:00
|
|
|
valid := challenge{Type: "simpleHttp", Status: "valid", URI: ts.URL, Token: "1234567812"}
|
2015-06-13 19:06:47 +00:00
|
|
|
jsonBytes, _ := json.Marshal(&valid)
|
|
|
|
w.Write(jsonBytes)
|
|
|
|
})
|
|
|
|
if err = solver.Solve(clientChallenge, "test.domain"); err != nil {
|
|
|
|
t.Errorf("VALID: Expected Solve to return no error but the error was -> %v", err)
|
|
|
|
}
|
|
|
|
}
|