traefik/integration/acme_test.go

166 lines
4.4 KiB
Go
Raw Normal View History

package main
import (
"crypto/tls"
2017-06-27 12:42:12 +00:00
"fmt"
"net/http"
"time"
"github.com/containous/traefik/integration/try"
2017-06-27 12:42:12 +00:00
"github.com/containous/traefik/testhelpers"
"github.com/go-check/check"
checker "github.com/vdemeester/shakers"
)
// ACME test suites (using libcompose)
type AcmeSuite struct {
BaseSuite
boulderIP string
}
// Acme tests configuration
type AcmeTestCase struct {
onDemand bool
traefikConfFilePath string
domainToCheck string
}
2017-06-27 12:42:12 +00:00
const (
// Domain to check
acmeDomain = "traefik.acme.wtf"
2017-06-27 12:42:12 +00:00
// Wildcard domain to check
wildcardDomain = "*.acme.wtf"
)
func (s *AcmeSuite) SetUpSuite(c *check.C) {
s.createComposeProject(c, "boulder")
s.composeProject.Start(c)
s.boulderIP = s.composeProject.Container(c, "boulder").NetworkSettings.IPAddress
// wait for boulder
err := try.GetRequest("http://"+s.boulderIP+":4000/directory", 120*time.Second, try.StatusCodeIs(http.StatusOK))
c.Assert(err, checker.IsNil)
}
func (s *AcmeSuite) TearDownSuite(c *check.C) {
// shutdown and delete compose project
if s.composeProject != nil {
s.composeProject.Stop(c)
}
}
// Test OnDemand option with none provided certificate
func (s *AcmeSuite) TestOnDemandRetrieveAcmeCertificate(c *check.C) {
2017-06-27 12:42:12 +00:00
testCase := AcmeTestCase{
traefikConfFilePath: "fixtures/acme/acme.toml",
onDemand: true,
domainToCheck: acmeDomain}
2017-06-27 12:42:12 +00:00
s.retrieveAcmeCertificate(c, testCase)
}
// Test OnHostRule option with none provided certificate
func (s *AcmeSuite) TestOnHostRuleRetrieveAcmeCertificate(c *check.C) {
2017-06-27 12:42:12 +00:00
testCase := AcmeTestCase{
traefikConfFilePath: "fixtures/acme/acme.toml",
onDemand: false,
domainToCheck: acmeDomain}
2017-06-27 12:42:12 +00:00
s.retrieveAcmeCertificate(c, testCase)
}
// Test OnDemand option with a wildcard provided certificate
func (s *AcmeSuite) TestOnDemandRetrieveAcmeCertificateWithWildcard(c *check.C) {
2017-06-27 12:42:12 +00:00
testCase := AcmeTestCase{
traefikConfFilePath: "fixtures/acme/acme_provided.toml",
onDemand: true,
domainToCheck: wildcardDomain}
2017-06-27 12:42:12 +00:00
s.retrieveAcmeCertificate(c, testCase)
}
// Test onHostRule option with a wildcard provided certificate
func (s *AcmeSuite) TestOnHostRuleRetrieveAcmeCertificateWithWildcard(c *check.C) {
2017-06-27 12:42:12 +00:00
testCase := AcmeTestCase{
traefikConfFilePath: "fixtures/acme/acme_provided.toml",
onDemand: false,
domainToCheck: wildcardDomain}
2017-06-27 12:42:12 +00:00
s.retrieveAcmeCertificate(c, testCase)
}
// Doing an HTTPS request and test the response certificate
2017-06-27 12:42:12 +00:00
func (s *AcmeSuite) retrieveAcmeCertificate(c *check.C, testCase AcmeTestCase) {
file := s.adaptFile(c, testCase.traefikConfFilePath, struct {
BoulderHost string
OnDemand, OnHostRule bool
2017-06-27 12:42:12 +00:00
}{
BoulderHost: s.boulderIP,
OnDemand: testCase.onDemand,
OnHostRule: !testCase.onDemand,
})
cmd, output := s.cmdTraefikWithConfigFile(file)
err := cmd.Start()
c.Assert(err, checker.IsNil)
defer cmd.Process.Kill()
backend := startTestServer("9010", http.StatusOK)
defer backend.Close()
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
// wait for traefik (generating acme account take some seconds)
err = try.Do(90*time.Second, func() error {
_, err := client.Get("https://127.0.0.1:5001")
return err
})
// TODO: waiting a refactor of integration tests
s.displayTraefikLog(c, output)
c.Assert(err, checker.IsNil)
tr = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
ServerName: acmeDomain,
},
}
client = &http.Client{Transport: tr}
2017-06-27 12:42:12 +00:00
req := testhelpers.MustNewRequest(http.MethodGet, "https://127.0.0.1:5001/", nil)
req.Host = acmeDomain
req.Header.Set("Host", acmeDomain)
req.Header.Set("Accept", "*/*")
var resp *http.Response
2017-06-27 12:42:12 +00:00
// Retry to send a Request which uses the LE generated certificate
2017-06-27 12:42:12 +00:00
err = try.Do(60*time.Second, func() error {
resp, err = client.Do(req)
2017-06-27 12:42:12 +00:00
// /!\ If connection is not closed, SSLHandshake will only be done during the first trial /!\
req.Close = true
2017-06-27 12:42:12 +00:00
if err != nil {
return err
}
2017-06-27 12:42:12 +00:00
cn := resp.TLS.PeerCertificates[0].Subject.CommonName
if cn != testCase.domainToCheck {
return fmt.Errorf("domain %s found in place of %s", cn, testCase.domainToCheck)
}
return nil
})
2017-06-27 12:42:12 +00:00
c.Assert(err, checker.IsNil)
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK)
// Check Domain into response certificate
2017-06-27 12:42:12 +00:00
c.Assert(resp.TLS.PeerCertificates[0].Subject.CommonName, checker.Equals, testCase.domainToCheck)
}