2017-07-06 14:28:13 +00:00
|
|
|
package integration
|
2016-02-25 23:31:35 +00:00
|
|
|
|
|
|
|
import (
|
2017-05-17 13:22:44 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"io/ioutil"
|
2016-02-25 23:31:35 +00:00
|
|
|
"net/http"
|
2017-05-17 13:22:44 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2016-02-25 23:31:35 +00:00
|
|
|
"time"
|
|
|
|
|
2018-01-24 16:52:03 +00:00
|
|
|
"github.com/abronan/valkeyrie"
|
|
|
|
"github.com/abronan/valkeyrie/store"
|
|
|
|
"github.com/abronan/valkeyrie/store/etcd/v2"
|
2017-05-17 13:22:44 +00:00
|
|
|
"github.com/containous/traefik/integration/try"
|
|
|
|
"github.com/go-check/check"
|
|
|
|
|
|
|
|
checker "github.com/vdemeester/shakers"
|
2016-02-25 23:31:35 +00:00
|
|
|
)
|
|
|
|
|
2016-03-27 14:27:56 +00:00
|
|
|
// Etcd test suites (using libcompose)
|
2016-04-27 23:26:18 +00:00
|
|
|
type EtcdSuite struct {
|
|
|
|
BaseSuite
|
|
|
|
kv store.Store
|
|
|
|
}
|
2016-03-27 14:27:56 +00:00
|
|
|
|
2016-06-24 07:58:42 +00:00
|
|
|
func (s *EtcdSuite) SetUpTest(c *check.C) {
|
2016-03-27 14:27:56 +00:00
|
|
|
s.createComposeProject(c, "etcd")
|
2016-04-27 23:26:18 +00:00
|
|
|
s.composeProject.Start(c)
|
|
|
|
|
|
|
|
etcd.Register()
|
2016-07-11 11:36:35 +00:00
|
|
|
url := s.composeProject.Container(c, "etcd").NetworkSettings.IPAddress + ":2379"
|
2018-01-24 16:52:03 +00:00
|
|
|
kv, err := valkeyrie.NewStore(
|
2016-04-27 23:26:18 +00:00
|
|
|
store.ETCD,
|
2016-04-27 23:43:43 +00:00
|
|
|
[]string{url},
|
2016-04-27 23:26:18 +00:00
|
|
|
&store.Config{
|
|
|
|
ConnectionTimeout: 10 * time.Second,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal("Cannot create store etcd")
|
|
|
|
}
|
2016-04-27 23:43:43 +00:00
|
|
|
s.kv = kv
|
2016-04-27 23:26:18 +00:00
|
|
|
|
2016-04-27 23:43:43 +00:00
|
|
|
// wait for etcd
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Do(60*time.Second, func() error {
|
2017-11-17 16:22:03 +00:00
|
|
|
_, err := kv.Exists("test", nil)
|
2017-05-17 13:22:44 +00:00
|
|
|
return err
|
2016-04-27 23:43:43 +00:00
|
|
|
})
|
2016-04-27 23:26:18 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2016-03-27 14:27:56 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 07:58:42 +00:00
|
|
|
func (s *EtcdSuite) TearDownTest(c *check.C) {
|
|
|
|
// shutdown and delete compose project
|
|
|
|
if s.composeProject != nil {
|
|
|
|
s.composeProject.Stop(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdSuite) TearDownSuite(c *check.C) {}
|
|
|
|
|
2016-02-25 23:31:35 +00:00
|
|
|
func (s *EtcdSuite) TestSimpleConfiguration(c *check.C) {
|
2016-04-27 23:43:43 +00:00
|
|
|
etcdHost := s.composeProject.Container(c, "etcd").NetworkSettings.IPAddress
|
2017-07-10 12:58:31 +00:00
|
|
|
|
2017-11-17 16:22:03 +00:00
|
|
|
file := s.adaptFile(c, "fixtures/etcd/simple.toml", struct {
|
|
|
|
EtcdHost string
|
|
|
|
UseAPIV3 bool
|
|
|
|
}{
|
|
|
|
etcdHost,
|
|
|
|
false,
|
|
|
|
})
|
2016-04-27 23:43:43 +00:00
|
|
|
defer os.Remove(file)
|
2017-07-10 12:58:31 +00:00
|
|
|
|
2017-09-13 08:34:04 +00:00
|
|
|
cmd, display := s.traefikCmd(withConfigFile(file))
|
|
|
|
defer display(c)
|
2016-02-25 23:31:35 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
// TODO validate : run on 80
|
2016-03-15 17:57:56 +00:00
|
|
|
// Expected a 404 as we did not configure anything
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8000/", 1000*time.Millisecond, try.StatusCodeIs(http.StatusNotFound))
|
2016-03-15 17:57:56 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2016-02-25 23:31:35 +00:00
|
|
|
}
|
2016-04-27 23:26:18 +00:00
|
|
|
|
|
|
|
func (s *EtcdSuite) TestNominalConfiguration(c *check.C) {
|
2016-04-27 23:43:43 +00:00
|
|
|
etcdHost := s.composeProject.Container(c, "etcd").NetworkSettings.IPAddress
|
2017-07-10 12:58:31 +00:00
|
|
|
|
2017-11-17 16:22:03 +00:00
|
|
|
file := s.adaptFile(c, "fixtures/etcd/simple.toml", struct {
|
|
|
|
EtcdHost string
|
|
|
|
UseAPIV3 bool
|
|
|
|
}{
|
|
|
|
etcdHost,
|
|
|
|
false,
|
|
|
|
})
|
2016-04-27 23:43:43 +00:00
|
|
|
defer os.Remove(file)
|
2017-07-10 12:58:31 +00:00
|
|
|
|
2017-09-13 08:34:04 +00:00
|
|
|
cmd, display := s.traefikCmd(withConfigFile(file))
|
|
|
|
defer display(c)
|
2016-04-27 23:26:18 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
whoami1IP := s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress
|
|
|
|
whoami2IP := s.composeProject.Container(c, "whoami2").NetworkSettings.IPAddress
|
|
|
|
whoami3IP := s.composeProject.Container(c, "whoami3").NetworkSettings.IPAddress
|
|
|
|
whoami4IP := s.composeProject.Container(c, "whoami4").NetworkSettings.IPAddress
|
2016-04-27 23:26:18 +00:00
|
|
|
|
|
|
|
backend1 := map[string]string{
|
|
|
|
"/traefik/backends/backend1/circuitbreaker/expression": "NetworkErrorRatio() > 0.5",
|
2017-05-17 13:22:44 +00:00
|
|
|
"/traefik/backends/backend1/servers/server1/url": "http://" + whoami1IP + ":80",
|
2016-04-27 23:26:18 +00:00
|
|
|
"/traefik/backends/backend1/servers/server1/weight": "10",
|
2017-05-17 13:22:44 +00:00
|
|
|
"/traefik/backends/backend1/servers/server2/url": "http://" + whoami2IP + ":80",
|
2016-04-27 23:26:18 +00:00
|
|
|
"/traefik/backends/backend1/servers/server2/weight": "1",
|
|
|
|
}
|
|
|
|
backend2 := map[string]string{
|
|
|
|
"/traefik/backends/backend2/loadbalancer/method": "drr",
|
2017-05-17 13:22:44 +00:00
|
|
|
"/traefik/backends/backend2/servers/server1/url": "http://" + whoami3IP + ":80",
|
2016-04-27 23:26:18 +00:00
|
|
|
"/traefik/backends/backend2/servers/server1/weight": "1",
|
2017-05-17 13:22:44 +00:00
|
|
|
"/traefik/backends/backend2/servers/server2/url": "http://" + whoami4IP + ":80",
|
2016-04-27 23:26:18 +00:00
|
|
|
"/traefik/backends/backend2/servers/server2/weight": "2",
|
|
|
|
}
|
|
|
|
frontend1 := map[string]string{
|
|
|
|
"/traefik/frontends/frontend1/backend": "backend2",
|
|
|
|
"/traefik/frontends/frontend1/entrypoints": "http",
|
2016-06-06 20:33:29 +00:00
|
|
|
"/traefik/frontends/frontend1/priority": "1",
|
2016-04-27 23:26:18 +00:00
|
|
|
"/traefik/frontends/frontend1/routes/test_1/rule": "Host:test.localhost",
|
|
|
|
}
|
|
|
|
frontend2 := map[string]string{
|
|
|
|
"/traefik/frontends/frontend2/backend": "backend1",
|
|
|
|
"/traefik/frontends/frontend2/entrypoints": "http",
|
2016-06-06 20:33:29 +00:00
|
|
|
"/traefik/frontends/frontend2/priority": "10",
|
2016-04-27 23:26:18 +00:00
|
|
|
"/traefik/frontends/frontend2/routes/test_2/rule": "Path:/test",
|
|
|
|
}
|
|
|
|
for key, value := range backend1 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range backend2 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range frontend1 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range frontend2 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:43:43 +00:00
|
|
|
// wait for etcd
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Do(60*time.Second, func() error {
|
2017-11-17 16:22:03 +00:00
|
|
|
_, err := s.kv.Exists("/traefik/frontends/frontend2/routes/test_2/rule", nil)
|
2017-05-17 13:22:44 +00:00
|
|
|
return err
|
2016-04-27 23:43:43 +00:00
|
|
|
})
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2017-11-23 10:50:03 +00:00
|
|
|
// wait for Træfik
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8081/api/providers", 60*time.Second, try.BodyContains("Path:/test"))
|
2016-04-27 23:43:43 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2016-04-27 23:26:18 +00:00
|
|
|
|
|
|
|
client := &http.Client{}
|
2017-05-17 13:22:44 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
|
2016-04-27 23:26:18 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Host = "test.localhost"
|
|
|
|
response, err := client.Do(req)
|
|
|
|
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
c.Assert(response.StatusCode, checker.Equals, http.StatusOK)
|
2016-04-27 23:26:18 +00:00
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
if !strings.Contains(string(body), whoami3IP) &&
|
|
|
|
!strings.Contains(string(body), whoami4IP) {
|
2016-04-27 23:26:18 +00:00
|
|
|
c.Fail()
|
|
|
|
}
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
req, err = http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/test", nil)
|
2016-04-27 23:26:18 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
response, err = client.Do(req)
|
|
|
|
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
c.Assert(response.StatusCode, checker.Equals, http.StatusOK)
|
2016-04-27 23:26:18 +00:00
|
|
|
|
|
|
|
body, err = ioutil.ReadAll(response.Body)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
if !strings.Contains(string(body), whoami1IP) &&
|
|
|
|
!strings.Contains(string(body), whoami2IP) {
|
2016-04-27 23:26:18 +00:00
|
|
|
c.Fail()
|
|
|
|
}
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
req, err = http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/test2", nil)
|
2017-08-18 00:18:02 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2016-04-27 23:26:18 +00:00
|
|
|
req.Host = "test2.localhost"
|
2016-04-27 23:43:43 +00:00
|
|
|
resp, err := client.Do(req)
|
2016-04-27 23:26:18 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound)
|
2016-04-27 23:26:18 +00:00
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
resp, err = http.Get("http://127.0.0.1:8000/")
|
2016-04-27 23:26:18 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound)
|
2016-04-27 23:26:18 +00:00
|
|
|
}
|
2016-06-24 07:58:42 +00:00
|
|
|
|
|
|
|
func (s *EtcdSuite) TestGlobalConfiguration(c *check.C) {
|
|
|
|
etcdHost := s.composeProject.Container(c, "etcd").NetworkSettings.IPAddress
|
|
|
|
err := s.kv.Put("/traefik/entrypoints/http/address", []byte(":8001"), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// wait for etcd
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Do(60*time.Second, func() error {
|
2017-11-17 16:22:03 +00:00
|
|
|
_, err := s.kv.Exists("/traefik/entrypoints/http/address", nil)
|
2017-05-17 13:22:44 +00:00
|
|
|
return err
|
2016-06-24 07:58:42 +00:00
|
|
|
})
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2017-11-23 10:50:03 +00:00
|
|
|
// start Træfik
|
2017-09-13 08:34:04 +00:00
|
|
|
cmd, display := s.traefikCmd(
|
2017-07-10 12:58:31 +00:00
|
|
|
withConfigFile("fixtures/simple_web.toml"),
|
|
|
|
"--etcd",
|
|
|
|
"--etcd.endpoint="+etcdHost+":4001")
|
2017-09-13 08:34:04 +00:00
|
|
|
defer display(c)
|
2016-06-24 07:58:42 +00:00
|
|
|
err = cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
whoami1IP := s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress
|
|
|
|
whoami2IP := s.composeProject.Container(c, "whoami2").NetworkSettings.IPAddress
|
|
|
|
whoami3IP := s.composeProject.Container(c, "whoami3").NetworkSettings.IPAddress
|
|
|
|
whoami4IP := s.composeProject.Container(c, "whoami4").NetworkSettings.IPAddress
|
2016-06-24 07:58:42 +00:00
|
|
|
|
|
|
|
backend1 := map[string]string{
|
|
|
|
"/traefik/backends/backend1/circuitbreaker/expression": "NetworkErrorRatio() > 0.5",
|
2017-05-17 13:22:44 +00:00
|
|
|
"/traefik/backends/backend1/servers/server1/url": "http://" + whoami1IP + ":80",
|
2016-06-24 07:58:42 +00:00
|
|
|
"/traefik/backends/backend1/servers/server1/weight": "10",
|
2017-05-17 13:22:44 +00:00
|
|
|
"/traefik/backends/backend1/servers/server2/url": "http://" + whoami2IP + ":80",
|
2016-06-24 07:58:42 +00:00
|
|
|
"/traefik/backends/backend1/servers/server2/weight": "1",
|
|
|
|
}
|
|
|
|
backend2 := map[string]string{
|
|
|
|
"/traefik/backends/backend2/loadbalancer/method": "drr",
|
2017-05-17 13:22:44 +00:00
|
|
|
"/traefik/backends/backend2/servers/server1/url": "http://" + whoami3IP + ":80",
|
2016-06-24 07:58:42 +00:00
|
|
|
"/traefik/backends/backend2/servers/server1/weight": "1",
|
2017-05-17 13:22:44 +00:00
|
|
|
"/traefik/backends/backend2/servers/server2/url": "http://" + whoami4IP + ":80",
|
2016-06-24 07:58:42 +00:00
|
|
|
"/traefik/backends/backend2/servers/server2/weight": "2",
|
|
|
|
}
|
|
|
|
frontend1 := map[string]string{
|
|
|
|
"/traefik/frontends/frontend1/backend": "backend2",
|
|
|
|
"/traefik/frontends/frontend1/entrypoints": "http",
|
|
|
|
"/traefik/frontends/frontend1/priority": "1",
|
|
|
|
"/traefik/frontends/frontend1/routes/test_1/rule": "Host:test.localhost",
|
|
|
|
}
|
|
|
|
frontend2 := map[string]string{
|
|
|
|
"/traefik/frontends/frontend2/backend": "backend1",
|
|
|
|
"/traefik/frontends/frontend2/entrypoints": "http",
|
|
|
|
"/traefik/frontends/frontend2/priority": "10",
|
|
|
|
"/traefik/frontends/frontend2/routes/test_2/rule": "Path:/test",
|
|
|
|
}
|
|
|
|
for key, value := range backend1 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range backend2 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range frontend1 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range frontend2 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for etcd
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Do(60*time.Second, func() error {
|
2017-11-17 16:22:03 +00:00
|
|
|
_, err := s.kv.Exists("/traefik/frontends/frontend2/routes/test_2/rule", nil)
|
2017-05-17 13:22:44 +00:00
|
|
|
return err
|
2016-06-24 07:58:42 +00:00
|
|
|
})
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// wait for traefik
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8080/api/providers", 60*time.Second, try.BodyContains("Path:/test"))
|
2016-06-24 07:58:42 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2017-11-23 10:50:03 +00:00
|
|
|
// check
|
2017-05-17 13:22:44 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8001/", nil)
|
2016-06-24 07:58:42 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Host = "test.localhost"
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Request(req, 500*time.Millisecond, try.StatusCodeIs(http.StatusOK))
|
2016-06-24 07:58:42 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
2016-06-27 10:19:14 +00:00
|
|
|
|
2017-12-23 16:45:25 +00:00
|
|
|
func (s *EtcdSuite) TestCertificatesContentWithSNIConfigHandshake(c *check.C) {
|
2016-06-27 10:19:14 +00:00
|
|
|
etcdHost := s.composeProject.Container(c, "etcd").NetworkSettings.IPAddress
|
2017-11-23 10:50:03 +00:00
|
|
|
// start Træfik
|
2017-09-13 08:34:04 +00:00
|
|
|
cmd, display := s.traefikCmd(
|
2017-07-10 12:58:31 +00:00
|
|
|
withConfigFile("fixtures/simple_web.toml"),
|
|
|
|
"--etcd",
|
|
|
|
"--etcd.endpoint="+etcdHost+":4001")
|
2017-09-13 08:34:04 +00:00
|
|
|
defer display(c)
|
2016-06-27 10:19:14 +00:00
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
whoami1IP := s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress
|
|
|
|
whoami2IP := s.composeProject.Container(c, "whoami2").NetworkSettings.IPAddress
|
|
|
|
whoami3IP := s.composeProject.Container(c, "whoami3").NetworkSettings.IPAddress
|
|
|
|
whoami4IP := s.composeProject.Container(c, "whoami4").NetworkSettings.IPAddress
|
2016-06-27 10:19:14 +00:00
|
|
|
|
2017-11-23 10:50:03 +00:00
|
|
|
// Copy the contents of the certificate files into ETCD
|
2016-06-27 10:19:14 +00:00
|
|
|
snitestComCert, err := ioutil.ReadFile("fixtures/https/snitest.com.cert")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
snitestComKey, err := ioutil.ReadFile("fixtures/https/snitest.com.key")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
snitestOrgCert, err := ioutil.ReadFile("fixtures/https/snitest.org.cert")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
snitestOrgKey, err := ioutil.ReadFile("fixtures/https/snitest.org.key")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
globalConfig := map[string]string{
|
|
|
|
"/traefik/entrypoints/https/address": ":4443",
|
|
|
|
"/traefik/entrypoints/https/tls/certificates/0/certfile": string(snitestComCert),
|
|
|
|
"/traefik/entrypoints/https/tls/certificates/0/keyfile": string(snitestComKey),
|
|
|
|
"/traefik/entrypoints/https/tls/certificates/1/certfile": string(snitestOrgCert),
|
|
|
|
"/traefik/entrypoints/https/tls/certificates/1/keyfile": string(snitestOrgKey),
|
|
|
|
"/traefik/defaultentrypoints/0": "https",
|
|
|
|
}
|
|
|
|
|
|
|
|
backend1 := map[string]string{
|
|
|
|
"/traefik/backends/backend1/circuitbreaker/expression": "NetworkErrorRatio() > 0.5",
|
2017-05-17 13:22:44 +00:00
|
|
|
"/traefik/backends/backend1/servers/server1/url": "http://" + whoami1IP + ":80",
|
2016-06-27 10:19:14 +00:00
|
|
|
"/traefik/backends/backend1/servers/server1/weight": "10",
|
2017-05-17 13:22:44 +00:00
|
|
|
"/traefik/backends/backend1/servers/server2/url": "http://" + whoami2IP + ":80",
|
2016-06-27 10:19:14 +00:00
|
|
|
"/traefik/backends/backend1/servers/server2/weight": "1",
|
|
|
|
}
|
|
|
|
backend2 := map[string]string{
|
|
|
|
"/traefik/backends/backend2/loadbalancer/method": "drr",
|
2017-05-17 13:22:44 +00:00
|
|
|
"/traefik/backends/backend2/servers/server1/url": "http://" + whoami3IP + ":80",
|
2016-06-27 10:19:14 +00:00
|
|
|
"/traefik/backends/backend2/servers/server1/weight": "1",
|
2017-05-17 13:22:44 +00:00
|
|
|
"/traefik/backends/backend2/servers/server2/url": "http://" + whoami4IP + ":80",
|
2016-06-27 10:19:14 +00:00
|
|
|
"/traefik/backends/backend2/servers/server2/weight": "2",
|
|
|
|
}
|
|
|
|
frontend1 := map[string]string{
|
|
|
|
"/traefik/frontends/frontend1/backend": "backend2",
|
|
|
|
"/traefik/frontends/frontend1/entrypoints": "http",
|
|
|
|
"/traefik/frontends/frontend1/priority": "1",
|
|
|
|
"/traefik/frontends/frontend1/routes/test_1/rule": "Host:snitest.com",
|
|
|
|
}
|
|
|
|
frontend2 := map[string]string{
|
|
|
|
"/traefik/frontends/frontend2/backend": "backend1",
|
|
|
|
"/traefik/frontends/frontend2/entrypoints": "http",
|
|
|
|
"/traefik/frontends/frontend2/priority": "10",
|
|
|
|
"/traefik/frontends/frontend2/routes/test_2/rule": "Host:snitest.org",
|
|
|
|
}
|
|
|
|
for key, value := range globalConfig {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range backend1 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range backend2 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range frontend1 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range frontend2 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for etcd
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Do(60*time.Second, try.KVExists(s.kv, "/traefik/frontends/frontend2/routes/test_2/rule"))
|
2016-06-27 10:19:14 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
err = cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
// wait for traefik
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8080/api/providers", 60*time.Second, try.BodyContains("Host:snitest.org"))
|
2016-06-27 10:19:14 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2017-11-23 10:50:03 +00:00
|
|
|
// check
|
2016-06-27 10:19:14 +00:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
ServerName: "snitest.com",
|
|
|
|
}
|
|
|
|
conn, err := tls.Dial("tcp", "127.0.0.1:4443", tlsConfig)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("failed to connect to server"))
|
|
|
|
|
|
|
|
defer conn.Close()
|
|
|
|
err = conn.Handshake()
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("TLS handshake error"))
|
|
|
|
|
|
|
|
cs := conn.ConnectionState()
|
|
|
|
err = cs.PeerCertificates[0].VerifyHostname("snitest.com")
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("certificate did not match SNI servername"))
|
|
|
|
}
|
2016-07-20 09:48:39 +00:00
|
|
|
|
|
|
|
func (s *EtcdSuite) TestCommandStoreConfig(c *check.C) {
|
|
|
|
etcdHost := s.composeProject.Container(c, "etcd").NetworkSettings.IPAddress
|
|
|
|
|
2017-09-13 08:34:04 +00:00
|
|
|
cmd, display := s.traefikCmd(
|
2017-07-10 12:58:31 +00:00
|
|
|
"storeconfig",
|
|
|
|
withConfigFile("fixtures/simple_web.toml"),
|
|
|
|
"--etcd.endpoint="+etcdHost+":4001")
|
2017-09-13 08:34:04 +00:00
|
|
|
defer display(c)
|
2016-07-20 09:48:39 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2017-11-23 10:50:03 +00:00
|
|
|
// wait for Træfik finish without error
|
2016-07-20 09:48:39 +00:00
|
|
|
cmd.Wait()
|
|
|
|
|
2017-11-23 10:50:03 +00:00
|
|
|
// CHECK
|
2016-07-20 09:48:39 +00:00
|
|
|
checkmap := map[string]string{
|
|
|
|
"/traefik/loglevel": "DEBUG",
|
|
|
|
"/traefik/defaultentrypoints/0": "http",
|
|
|
|
"/traefik/entrypoints/http/address": ":8000",
|
2018-01-11 08:46:03 +00:00
|
|
|
"/traefik/api/entrypoint": "traefik",
|
2017-05-17 13:22:44 +00:00
|
|
|
"/traefik/etcd/endpoint": etcdHost + ":4001",
|
2016-07-20 09:48:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range checkmap {
|
|
|
|
var p *store.KVPair
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Do(60*time.Second, func() error {
|
2017-11-17 16:22:03 +00:00
|
|
|
p, err = s.kv.Get(key, nil)
|
2017-05-17 13:22:44 +00:00
|
|
|
return err
|
2016-07-20 09:48:39 +00:00
|
|
|
})
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
c.Assert(string(p.Value), checker.Equals, value)
|
|
|
|
}
|
|
|
|
}
|
2017-11-23 10:50:03 +00:00
|
|
|
|
|
|
|
func (s *EtcdSuite) TestSNIDynamicTlsConfig(c *check.C) {
|
|
|
|
etcdHost := s.composeProject.Container(c, "etcd").NetworkSettings.IPAddress
|
|
|
|
// start Træfik
|
|
|
|
cmd, display := s.traefikCmd(
|
|
|
|
withConfigFile("fixtures/etcd/simple_https.toml"),
|
|
|
|
"--etcd",
|
|
|
|
"--etcd.endpoint="+etcdHost+":4001",
|
|
|
|
"--etcd.watch=true",
|
|
|
|
)
|
|
|
|
defer display(c)
|
|
|
|
|
|
|
|
// prepare to config
|
|
|
|
whoami1IP := s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress
|
|
|
|
whoami2IP := s.composeProject.Container(c, "whoami2").NetworkSettings.IPAddress
|
|
|
|
whoami3IP := s.composeProject.Container(c, "whoami3").NetworkSettings.IPAddress
|
|
|
|
whoami4IP := s.composeProject.Container(c, "whoami4").NetworkSettings.IPAddress
|
|
|
|
|
|
|
|
snitestComCert, err := ioutil.ReadFile("fixtures/https/snitest.com.cert")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
snitestComKey, err := ioutil.ReadFile("fixtures/https/snitest.com.key")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
snitestOrgCert, err := ioutil.ReadFile("fixtures/https/snitest.org.cert")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
snitestOrgKey, err := ioutil.ReadFile("fixtures/https/snitest.org.key")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
backend1 := map[string]string{
|
|
|
|
"/traefik/backends/backend1/circuitbreaker/expression": "NetworkErrorRatio() > 0.5",
|
|
|
|
"/traefik/backends/backend1/servers/server1/url": "http://" + whoami1IP + ":80",
|
|
|
|
"/traefik/backends/backend1/servers/server1/weight": "1",
|
|
|
|
"/traefik/backends/backend1/servers/server2/url": "http://" + whoami2IP + ":80",
|
|
|
|
"/traefik/backends/backend1/servers/server2/weight": "1",
|
|
|
|
}
|
|
|
|
backend2 := map[string]string{
|
|
|
|
"/traefik/backends/backend2/loadbalancer/method": "drr",
|
|
|
|
"/traefik/backends/backend2/servers/server1/url": "http://" + whoami3IP + ":80",
|
|
|
|
"/traefik/backends/backend2/servers/server1/weight": "1",
|
|
|
|
"/traefik/backends/backend2/servers/server2/url": "http://" + whoami4IP + ":80",
|
|
|
|
"/traefik/backends/backend2/servers/server2/weight": "1",
|
|
|
|
}
|
|
|
|
frontend1 := map[string]string{
|
|
|
|
"/traefik/frontends/frontend1/backend": "backend2",
|
|
|
|
"/traefik/frontends/frontend1/entrypoints": "https",
|
|
|
|
"/traefik/frontends/frontend1/priority": "1",
|
|
|
|
"/traefik/frontends/frontend1/routes/test_1/rule": "Host:snitest.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
frontend2 := map[string]string{
|
|
|
|
"/traefik/frontends/frontend2/backend": "backend1",
|
|
|
|
"/traefik/frontends/frontend2/entrypoints": "https",
|
|
|
|
"/traefik/frontends/frontend2/priority": "10",
|
|
|
|
"/traefik/frontends/frontend2/routes/test_2/rule": "Host:snitest.org",
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsconfigure1 := map[string]string{
|
2018-01-23 15:30:07 +00:00
|
|
|
"/traefik/tls/snitestcom/entrypoints": "https",
|
|
|
|
"/traefik/tls/snitestcom/certificate/keyfile": string(snitestComKey),
|
|
|
|
"/traefik/tls/snitestcom/certificate/certfile": string(snitestComCert),
|
2017-11-23 10:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tlsconfigure2 := map[string]string{
|
2018-01-23 15:30:07 +00:00
|
|
|
"/traefik/tls/snitestorg/entrypoints": "https",
|
|
|
|
"/traefik/tls/snitestorg/certificate/keyfile": string(snitestOrgKey),
|
|
|
|
"/traefik/tls/snitestorg/certificate/certfile": string(snitestOrgCert),
|
2017-11-23 10:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// config backends,frontends and first tls keypair
|
|
|
|
for key, value := range backend1 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range backend2 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range frontend1 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range frontend2 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
for key, value := range tlsconfigure1 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
tr1 := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
ServerName: "snitest.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tr2 := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
ServerName: "snitest.org",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for etcd
|
|
|
|
err = try.Do(60*time.Second, func() error {
|
2018-01-23 15:30:07 +00:00
|
|
|
_, err := s.kv.Get("/traefik/tls/snitestcom/certificate/keyfile", nil)
|
2017-11-23 10:50:03 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
err = cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
// wait for Træfik
|
|
|
|
err = try.GetRequest("http://127.0.0.1:8081/api/providers", 60*time.Second, try.BodyContains(string("MIIEpQIBAAKCAQEA1RducBK6EiFDv3TYB8ZcrfKWRVaSfHzWicO3J5WdST9oS7h")))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "https://127.0.0.1:4443/", nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
client := &http.Client{Transport: tr1}
|
|
|
|
req.Host = tr1.TLSClientConfig.ServerName
|
|
|
|
req.Header.Set("Host", tr1.TLSClientConfig.ServerName)
|
|
|
|
req.Header.Set("Accept", "*/*")
|
|
|
|
var resp *http.Response
|
|
|
|
resp, err = client.Do(req)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
cn := resp.TLS.PeerCertificates[0].Subject.CommonName
|
|
|
|
c.Assert(cn, checker.Equals, "snitest.com")
|
|
|
|
|
|
|
|
// now we configure the second keypair in etcd and the request for host "snitest.org" will use the second keypair
|
|
|
|
|
|
|
|
for key, value := range tlsconfigure2 {
|
|
|
|
err := s.kv.Put(key, []byte(value), nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for etcd
|
|
|
|
err = try.Do(60*time.Second, func() error {
|
2018-01-23 15:30:07 +00:00
|
|
|
_, err := s.kv.Get("/traefik/tls/snitestorg/certificate/keyfile", nil)
|
2017-11-23 10:50:03 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// waiting for Træfik to pull configuration
|
|
|
|
err = try.GetRequest("http://127.0.0.1:8081/api/providers", 30*time.Second, try.BodyContains("MIIEogIBAAKCAQEAvG9kL+vF57+MICehzbqcQAUlAOSl5r"))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, "https://127.0.0.1:4443/", nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
client = &http.Client{Transport: tr2}
|
|
|
|
req.Host = tr2.TLSClientConfig.ServerName
|
|
|
|
req.Header.Set("Host", tr2.TLSClientConfig.ServerName)
|
|
|
|
req.Header.Set("Accept", "*/*")
|
|
|
|
resp, err = client.Do(req)
|
2017-12-04 19:04:08 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2017-11-23 10:50:03 +00:00
|
|
|
cn = resp.TLS.PeerCertificates[0].Subject.CommonName
|
|
|
|
c.Assert(cn, checker.Equals, "snitest.org")
|
|
|
|
}
|