refactor: fix some code.
This commit is contained in:
parent
07524f5c99
commit
0472d19bd4
15 changed files with 71 additions and 15 deletions
|
@ -107,7 +107,7 @@ func (a *ACME) init() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
a.defaultCertificate = cert
|
a.defaultCertificate = cert
|
||||||
// TODO: to remove in the futurs
|
// TODO: to remove in the future
|
||||||
if len(a.StorageFile) > 0 && len(a.Storage) == 0 {
|
if len(a.StorageFile) > 0 && len(a.Storage) == 0 {
|
||||||
log.Warn("ACME.StorageFile is deprecated, use ACME.Storage instead")
|
log.Warn("ACME.StorageFile is deprecated, use ACME.Storage instead")
|
||||||
a.Storage = a.StorageFile
|
a.Storage = a.StorageFile
|
||||||
|
|
|
@ -40,8 +40,8 @@ func (s *AccessLogSuite) TestAccessLog(c *check.C) {
|
||||||
defer os.Remove(traefikTestLogFile)
|
defer os.Remove(traefikTestLogFile)
|
||||||
|
|
||||||
err = try.Do(1*time.Second, func() error {
|
err = try.Do(1*time.Second, func() error {
|
||||||
if _, err := os.Stat(traefikTestLogFile); err != nil {
|
if _, errStat := os.Stat(traefikTestLogFile); errStat != nil {
|
||||||
return fmt.Errorf("could not get stats for log file: %s", err)
|
return fmt.Errorf("could not get stats for log file: %s", errStat)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
|
@ -140,8 +140,8 @@ func (s *AcmeSuite) retrieveAcmeCertificate(c *check.C, testCase AcmeTestCase) {
|
||||||
|
|
||||||
// wait for traefik (generating acme account take some seconds)
|
// wait for traefik (generating acme account take some seconds)
|
||||||
err = try.Do(90*time.Second, func() error {
|
err = try.Do(90*time.Second, func() error {
|
||||||
_, err := client.Get("https://127.0.0.1:5001")
|
_, errGet := client.Get("https://127.0.0.1:5001")
|
||||||
return err
|
return errGet
|
||||||
})
|
})
|
||||||
c.Assert(err, checker.IsNil)
|
c.Assert(err, checker.IsNil)
|
||||||
|
|
||||||
|
|
|
@ -36,10 +36,10 @@ func (s *ConstraintSuite) SetUpSuite(c *check.C) {
|
||||||
|
|
||||||
// Wait for consul to elect itself leader
|
// Wait for consul to elect itself leader
|
||||||
err = try.Do(3*time.Second, func() error {
|
err = try.Do(3*time.Second, func() error {
|
||||||
leader, err := consulClient.Status().Leader()
|
leader, errLeader := consulClient.Status().Leader()
|
||||||
|
|
||||||
if err != nil || len(leader) == 0 {
|
if errLeader != nil || len(leader) == 0 {
|
||||||
return fmt.Errorf("Leader not found. %v", err)
|
return fmt.Errorf("leader not found. %v", errLeader)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -655,6 +655,7 @@ func (s *ConsulSuite) TestSNIDynamicTlsConfig(c *check.C) {
|
||||||
req.Header.Set("Host", tr2.TLSClientConfig.ServerName)
|
req.Header.Set("Host", tr2.TLSClientConfig.ServerName)
|
||||||
req.Header.Set("Accept", "*/*")
|
req.Header.Set("Accept", "*/*")
|
||||||
resp, err = client.Do(req)
|
resp, err = client.Do(req)
|
||||||
|
c.Assert(err, checker.IsNil)
|
||||||
cn = resp.TLS.PeerCertificates[0].Subject.CommonName
|
cn = resp.TLS.PeerCertificates[0].Subject.CommonName
|
||||||
c.Assert(cn, checker.Equals, "snitest.org")
|
c.Assert(cn, checker.Equals, "snitest.org")
|
||||||
}
|
}
|
||||||
|
|
|
@ -573,6 +573,7 @@ func (s *Etcd3Suite) TestSNIDynamicTlsConfig(c *check.C) {
|
||||||
req.Header.Set("Host", tr2.TLSClientConfig.ServerName)
|
req.Header.Set("Host", tr2.TLSClientConfig.ServerName)
|
||||||
req.Header.Set("Accept", "*/*")
|
req.Header.Set("Accept", "*/*")
|
||||||
resp, err = client.Do(req)
|
resp, err = client.Do(req)
|
||||||
|
c.Assert(err, checker.IsNil)
|
||||||
cn = resp.TLS.PeerCertificates[0].Subject.CommonName
|
cn = resp.TLS.PeerCertificates[0].Subject.CommonName
|
||||||
c.Assert(cn, checker.Equals, "snitest.org")
|
c.Assert(cn, checker.Equals, "snitest.org")
|
||||||
}
|
}
|
||||||
|
|
|
@ -589,6 +589,7 @@ func (s *EtcdSuite) TestSNIDynamicTlsConfig(c *check.C) {
|
||||||
req.Header.Set("Host", tr2.TLSClientConfig.ServerName)
|
req.Header.Set("Host", tr2.TLSClientConfig.ServerName)
|
||||||
req.Header.Set("Accept", "*/*")
|
req.Header.Set("Accept", "*/*")
|
||||||
resp, err = client.Do(req)
|
resp, err = client.Do(req)
|
||||||
|
c.Assert(err, checker.IsNil)
|
||||||
cn = resp.TLS.PeerCertificates[0].Subject.CommonName
|
cn = resp.TLS.PeerCertificates[0].Subject.CommonName
|
||||||
c.Assert(cn, checker.Equals, "snitest.org")
|
c.Assert(cn, checker.Equals, "snitest.org")
|
||||||
}
|
}
|
||||||
|
|
|
@ -189,6 +189,7 @@ func (s *GRPCSuite) TestGRPCBuffer(c *check.C) {
|
||||||
stopStreamExample := make(chan bool)
|
stopStreamExample := make(chan bool)
|
||||||
defer func() { stopStreamExample <- true }()
|
defer func() { stopStreamExample <- true }()
|
||||||
lis, err := net.Listen("tcp", ":0")
|
lis, err := net.Listen("tcp", ":0")
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
_, port, err := net.SplitHostPort(lis.Addr().String())
|
_, port, err := net.SplitHostPort(lis.Addr().String())
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
|
|
@ -282,6 +282,7 @@ func (s *WebsocketSuite) TestSSLTermination(c *check.C) {
|
||||||
//Add client self-signed cert
|
//Add client self-signed cert
|
||||||
roots := x509.NewCertPool()
|
roots := x509.NewCertPool()
|
||||||
certContent, err := ioutil.ReadFile("./resources/tls/local.cert")
|
certContent, err := ioutil.ReadFile("./resources/tls/local.cert")
|
||||||
|
c.Assert(err, checker.IsNil)
|
||||||
roots.AppendCertsFromPEM(certContent)
|
roots.AppendCertsFromPEM(certContent)
|
||||||
gorillawebsocket.DefaultDialer.TLSClientConfig = &tls.Config{
|
gorillawebsocket.DefaultDialer.TLSClientConfig = &tls.Config{
|
||||||
RootCAs: roots,
|
RootCAs: roots,
|
||||||
|
@ -489,6 +490,7 @@ func (s *WebsocketSuite) TestSSLhttp2(c *check.C) {
|
||||||
//Add client self-signed cert
|
//Add client self-signed cert
|
||||||
roots := x509.NewCertPool()
|
roots := x509.NewCertPool()
|
||||||
certContent, err := ioutil.ReadFile("./resources/tls/local.cert")
|
certContent, err := ioutil.ReadFile("./resources/tls/local.cert")
|
||||||
|
c.Assert(err, checker.IsNil)
|
||||||
roots.AppendCertsFromPEM(certContent)
|
roots.AppendCertsFromPEM(certContent)
|
||||||
gorillawebsocket.DefaultDialer.TLSClientConfig = &tls.Config{
|
gorillawebsocket.DefaultDialer.TLSClientConfig = &tls.Config{
|
||||||
RootCAs: roots,
|
RootCAs: roots,
|
||||||
|
|
|
@ -46,11 +46,6 @@ func RegisterInfluxDB(config *types.InfluxDB) Registry {
|
||||||
|
|
||||||
// initInfluxDBTicker initializes metrics pusher and creates a influxDBClient if not created already
|
// initInfluxDBTicker initializes metrics pusher and creates a influxDBClient if not created already
|
||||||
func initInfluxDBTicker(config *types.InfluxDB) *time.Ticker {
|
func initInfluxDBTicker(config *types.InfluxDB) *time.Ticker {
|
||||||
address := config.Address
|
|
||||||
if len(address) == 0 {
|
|
||||||
address = "localhost:8089"
|
|
||||||
}
|
|
||||||
|
|
||||||
pushInterval, err := time.ParseDuration(config.PushInterval)
|
pushInterval, err := time.ParseDuration(config.PushInterval)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("Unable to parse %s into pushInterval, using 10s as default value", config.PushInterval)
|
log.Warnf("Unable to parse %s into pushInterval, using 10s as default value", config.PushInterval)
|
||||||
|
|
|
@ -134,7 +134,7 @@ func (p *Provider) loadConfig() *types.Configuration {
|
||||||
templateObjects := struct {
|
templateObjects := struct {
|
||||||
Prefix string
|
Prefix string
|
||||||
}{
|
}{
|
||||||
// Allow `/traefik/alias` to superesede `p.Prefix`
|
// Allow `/traefik/alias` to supersede `p.Prefix`
|
||||||
strings.TrimSuffix(p.get(p.Prefix, p.Prefix+"/alias"), "/"),
|
strings.TrimSuffix(p.get(p.Prefix, p.Prefix+"/alias"), "/"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ type BaseProvider struct {
|
||||||
DebugLogGeneratedTemplate bool `description:"Enable debug logging of generated configuration template." export:"true"`
|
DebugLogGeneratedTemplate bool `description:"Enable debug logging of generated configuration template." export:"true"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MatchConstraints must match with EVERY single contraint
|
// MatchConstraints must match with EVERY single constraint
|
||||||
// returns first constraint that do not match or nil
|
// returns first constraint that do not match or nil
|
||||||
func (p *BaseProvider) MatchConstraints(tags []string) (bool, *types.Constraint) {
|
func (p *BaseProvider) MatchConstraints(tags []string) (bool, *types.Constraint) {
|
||||||
// if there is no tags and no constraints, filtering is disabled
|
// if there is no tags and no constraints, filtering is disabled
|
||||||
|
@ -116,6 +116,7 @@ func split(sep, s string) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normalize transform a string that work with the rest of traefik
|
// Normalize transform a string that work with the rest of traefik
|
||||||
|
// Replace '.' with '-' in quoted keys because of this issue https://github.com/BurntSushi/toml/issues/78
|
||||||
func Normalize(name string) string {
|
func Normalize(name string) string {
|
||||||
fargs := func(c rune) bool {
|
fargs := func(c rune) bool {
|
||||||
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
|
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
|
||||||
|
|
|
@ -464,6 +464,45 @@ func TestBaseProvider_GetConfiguration(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNormalize(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
desc string
|
||||||
|
name string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "without special chars",
|
||||||
|
name: "foobar",
|
||||||
|
expected: "foobar",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "with special chars",
|
||||||
|
name: "foo.foo.foo;foo:foo!foo/foo\\foo)foo_123-ç_àéè",
|
||||||
|
expected: "foo-foo-foo-foo-foo-foo-foo-foo-foo-123-ç-àéè",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "starts with special chars",
|
||||||
|
name: ".foo.foo",
|
||||||
|
expected: "foo-foo",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "ends with special chars",
|
||||||
|
name: "foo.foo.",
|
||||||
|
expected: "foo-foo",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range testCases {
|
||||||
|
test := test
|
||||||
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
actual := Normalize(test.name)
|
||||||
|
assert.Equal(t, test.expected, actual)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func readTemplateFile(t *testing.T, path string) string {
|
func readTemplateFile(t *testing.T, path string) string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
expectedContent, err := ioutil.ReadFile(path)
|
expectedContent, err := ioutil.ReadFile(path)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/containous/traefik/log"
|
||||||
"github.com/containous/traefik/whitelist"
|
"github.com/containous/traefik/whitelist"
|
||||||
"github.com/vulcand/oxy/forward"
|
"github.com/vulcand/oxy/forward"
|
||||||
)
|
)
|
||||||
|
@ -39,10 +40,18 @@ type headerRewriter struct {
|
||||||
func (h *headerRewriter) Rewrite(req *http.Request) {
|
func (h *headerRewriter) Rewrite(req *http.Request) {
|
||||||
clientIP, _, err := net.SplitHostPort(req.RemoteAddr)
|
clientIP, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
h.secureRewriter.Rewrite(req)
|
h.secureRewriter.Rewrite(req)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
authorized, _, err := h.ips.Contains(clientIP)
|
authorized, _, err := h.ips.Contains(clientIP)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
h.secureRewriter.Rewrite(req)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if h.insecure || authorized {
|
if h.insecure || authorized {
|
||||||
h.secureRewriter.Rewrite(req)
|
h.secureRewriter.Rewrite(req)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1133,6 +1133,9 @@ func (s *Server) loadConfig(configurations types.Configurations, globalConfigura
|
||||||
}
|
}
|
||||||
|
|
||||||
regex, replacement, err := s.buildRedirect(proto, entryPoint)
|
regex, replacement, err := s.buildRedirect(proto, entryPoint)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error creating Frontend Redirect: %v", err)
|
||||||
|
}
|
||||||
rewrite, err := middlewares.NewRewrite(regex, replacement, true)
|
rewrite, err := middlewares.NewRewrite(regex, replacement, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error creating Frontend Redirect: %v", err)
|
log.Fatalf("Error creating Frontend Redirect: %v", err)
|
||||||
|
@ -1298,6 +1301,9 @@ func (s *Server) loadEntryPointConfig(entryPointName string, entryPoint *configu
|
||||||
protocol = "https"
|
protocol = "https"
|
||||||
}
|
}
|
||||||
regex, replacement, err = s.buildRedirect(protocol, entryPoint)
|
regex, replacement, err = s.buildRedirect(protocol, entryPoint)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
rewrite, err := middlewares.NewRewrite(regex, replacement, true)
|
rewrite, err := middlewares.NewRewrite(regex, replacement, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue