2017-05-26 13:32:03 +00:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-26 16:18:04 +00:00
|
|
|
"io"
|
2017-05-26 13:32:03 +00:00
|
|
|
"os"
|
2019-06-26 16:18:04 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2017-05-26 13:32:03 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-01-29 15:46:09 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/safe"
|
2017-05-26 13:32:03 +00:00
|
|
|
)
|
|
|
|
|
2018-05-22 10:02:03 +00:00
|
|
|
type ProvideTestCase struct {
|
2019-07-18 14:26:05 +00:00
|
|
|
desc string
|
|
|
|
directoryPaths []string
|
|
|
|
filePath string
|
|
|
|
expectedNumRouter int
|
|
|
|
expectedNumService int
|
|
|
|
expectedNumTLSConf int
|
|
|
|
expectedNumTLSOptions int
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 12:14:35 +00:00
|
|
|
func TestTLSCertificateContent(t *testing.T) {
|
2021-03-04 19:08:03 +00:00
|
|
|
tempDir := t.TempDir()
|
2019-06-26 16:18:04 +00:00
|
|
|
|
|
|
|
fileTLS, err := createTempFile("./fixtures/toml/tls_file.cert", tempDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-13 12:14:35 +00:00
|
|
|
fileTLSKey, err := createTempFile("./fixtures/toml/tls_file_key.cert", tempDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-04 19:08:03 +00:00
|
|
|
fileConfig, err := os.CreateTemp(tempDir, "temp*.toml")
|
2019-06-26 16:18:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
content := `
|
2019-06-27 21:58:03 +00:00
|
|
|
[[tls.certificates]]
|
|
|
|
certFile = "` + fileTLS.Name() + `"
|
2021-07-13 12:14:35 +00:00
|
|
|
keyFile = "` + fileTLSKey.Name() + `"
|
|
|
|
|
|
|
|
[tls.options.default.clientAuth]
|
|
|
|
caFiles = ["` + fileTLS.Name() + `"]
|
|
|
|
|
|
|
|
[tls.stores.default.defaultCertificate]
|
|
|
|
certFile = "` + fileTLS.Name() + `"
|
|
|
|
keyFile = "` + fileTLSKey.Name() + `"
|
|
|
|
|
|
|
|
[http.serversTransports.default]
|
|
|
|
rootCAs = ["` + fileTLS.Name() + `"]
|
|
|
|
[[http.serversTransports.default.certificates]]
|
|
|
|
certFile = "` + fileTLS.Name() + `"
|
|
|
|
keyFile = "` + fileTLSKey.Name() + `"
|
2022-12-09 08:58:05 +00:00
|
|
|
|
|
|
|
[tcp.serversTransports.default]
|
|
|
|
[tcp.serversTransports.default.tls]
|
|
|
|
rootCAs = ["` + fileTLS.Name() + `"]
|
|
|
|
[[tcp.serversTransports.default.tls.certificates]]
|
|
|
|
certFile = "` + fileTLS.Name() + `"
|
|
|
|
keyFile = "` + fileTLSKey.Name() + `"
|
2019-06-26 16:18:04 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
_, err = fileConfig.Write([]byte(content))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
provider := &Provider{}
|
2019-09-13 17:28:04 +00:00
|
|
|
configuration, err := provider.loadFileConfig(context.Background(), fileConfig.Name(), true)
|
2019-06-26 16:18:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-27 21:58:03 +00:00
|
|
|
require.Equal(t, "CONTENT", configuration.TLS.Certificates[0].Certificate.CertFile.String())
|
2021-07-13 12:14:35 +00:00
|
|
|
require.Equal(t, "CONTENTKEY", configuration.TLS.Certificates[0].Certificate.KeyFile.String())
|
|
|
|
|
|
|
|
require.Equal(t, "CONTENT", configuration.TLS.Options["default"].ClientAuth.CAFiles[0].String())
|
|
|
|
|
|
|
|
require.Equal(t, "CONTENT", configuration.TLS.Stores["default"].DefaultCertificate.CertFile.String())
|
|
|
|
require.Equal(t, "CONTENTKEY", configuration.TLS.Stores["default"].DefaultCertificate.KeyFile.String())
|
|
|
|
|
|
|
|
require.Equal(t, "CONTENT", configuration.HTTP.ServersTransports["default"].Certificates[0].CertFile.String())
|
|
|
|
require.Equal(t, "CONTENTKEY", configuration.HTTP.ServersTransports["default"].Certificates[0].KeyFile.String())
|
|
|
|
require.Equal(t, "CONTENT", configuration.HTTP.ServersTransports["default"].RootCAs[0].String())
|
2022-12-09 08:58:05 +00:00
|
|
|
|
|
|
|
require.Equal(t, "CONTENT", configuration.TCP.ServersTransports["default"].TLS.Certificates[0].CertFile.String())
|
|
|
|
require.Equal(t, "CONTENTKEY", configuration.TCP.ServersTransports["default"].TLS.Certificates[0].KeyFile.String())
|
|
|
|
require.Equal(t, "CONTENT", configuration.TCP.ServersTransports["default"].TLS.RootCAs[0].String())
|
2019-06-26 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorWhenEmptyConfig(t *testing.T) {
|
|
|
|
provider := &Provider{}
|
2019-07-10 07:26:04 +00:00
|
|
|
configChan := make(chan dynamic.Message)
|
2019-06-26 16:18:04 +00:00
|
|
|
errorChan := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
err := provider.Provide(configChan, safe.NewPool(context.Background()))
|
|
|
|
assert.Error(t, err)
|
|
|
|
close(errorChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
timeout := time.After(time.Second)
|
|
|
|
select {
|
|
|
|
case <-configChan:
|
|
|
|
t.Fatal("We should not receive config message")
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatal("timeout while waiting for config")
|
|
|
|
case <-errorChan:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 10:02:03 +00:00
|
|
|
func TestProvideWithoutWatch(t *testing.T) {
|
|
|
|
for _, test := range getTestCases() {
|
|
|
|
t.Run(test.desc+" without watch", func(t *testing.T) {
|
2020-12-29 09:54:03 +00:00
|
|
|
provider := createProvider(t, test, false)
|
2019-07-10 07:26:04 +00:00
|
|
|
configChan := make(chan dynamic.Message)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
provider.DebugLogGeneratedTemplate = true
|
2018-05-22 10:02:03 +00:00
|
|
|
|
|
|
|
go func() {
|
2018-07-11 07:08:03 +00:00
|
|
|
err := provider.Provide(configChan, safe.NewPool(context.Background()))
|
2018-05-22 10:02:03 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
timeout := time.After(time.Second)
|
|
|
|
select {
|
2018-11-14 09:18:03 +00:00
|
|
|
case conf := <-configChan:
|
2020-06-17 14:48:04 +00:00
|
|
|
|
2019-06-27 21:58:03 +00:00
|
|
|
require.NotNil(t, conf.Configuration.HTTP)
|
2020-02-11 00:26:04 +00:00
|
|
|
numServices := len(conf.Configuration.HTTP.Services) + len(conf.Configuration.TCP.Services) + len(conf.Configuration.UDP.Services)
|
|
|
|
numRouters := len(conf.Configuration.HTTP.Routers) + len(conf.Configuration.TCP.Routers) + len(conf.Configuration.UDP.Routers)
|
2020-06-17 14:48:04 +00:00
|
|
|
assert.Equal(t, test.expectedNumService, numServices)
|
|
|
|
assert.Equal(t, test.expectedNumRouter, numRouters)
|
2019-06-27 21:58:03 +00:00
|
|
|
require.NotNil(t, conf.Configuration.TLS)
|
|
|
|
assert.Len(t, conf.Configuration.TLS.Certificates, test.expectedNumTLSConf)
|
2019-07-18 14:26:05 +00:00
|
|
|
assert.Len(t, conf.Configuration.TLS.Options, test.expectedNumTLSOptions)
|
2018-05-22 10:02:03 +00:00
|
|
|
case <-timeout:
|
|
|
|
t.Errorf("timeout while waiting for config")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvideWithWatch(t *testing.T) {
|
|
|
|
for _, test := range getTestCases() {
|
|
|
|
t.Run(test.desc+" with watch", func(t *testing.T) {
|
2020-12-29 09:54:03 +00:00
|
|
|
provider := createProvider(t, test, true)
|
2019-07-10 07:26:04 +00:00
|
|
|
configChan := make(chan dynamic.Message)
|
2018-05-22 10:02:03 +00:00
|
|
|
|
|
|
|
go func() {
|
2018-07-11 07:08:03 +00:00
|
|
|
err := provider.Provide(configChan, safe.NewPool(context.Background()))
|
2018-05-22 10:02:03 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
timeout := time.After(time.Second)
|
|
|
|
select {
|
2018-11-14 09:18:03 +00:00
|
|
|
case conf := <-configChan:
|
2019-06-27 21:58:03 +00:00
|
|
|
require.NotNil(t, conf.Configuration.HTTP)
|
2020-02-11 00:26:04 +00:00
|
|
|
numServices := len(conf.Configuration.HTTP.Services) + len(conf.Configuration.TCP.Services) + len(conf.Configuration.UDP.Services)
|
|
|
|
numRouters := len(conf.Configuration.HTTP.Routers) + len(conf.Configuration.TCP.Routers) + len(conf.Configuration.UDP.Routers)
|
|
|
|
assert.Equal(t, numServices, 0)
|
|
|
|
assert.Equal(t, numRouters, 0)
|
2019-06-27 21:58:03 +00:00
|
|
|
require.NotNil(t, conf.Configuration.TLS)
|
|
|
|
assert.Len(t, conf.Configuration.TLS.Certificates, 0)
|
2018-05-22 10:02:03 +00:00
|
|
|
case <-timeout:
|
|
|
|
t.Errorf("timeout while waiting for config")
|
|
|
|
}
|
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
if len(test.filePath) > 0 {
|
|
|
|
err := copyFile(test.filePath, provider.Filename)
|
|
|
|
require.NoError(t, err)
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
if len(test.directoryPaths) > 0 {
|
|
|
|
for i, filePath := range test.directoryPaths {
|
|
|
|
err := copyFile(filePath, filepath.Join(provider.Directory, strconv.Itoa(i)+filepath.Ext(filePath)))
|
|
|
|
require.NoError(t, err)
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 20:38:03 +00:00
|
|
|
timeout = time.After(1 * time.Second)
|
2018-11-14 09:18:03 +00:00
|
|
|
var numUpdates, numServices, numRouters, numTLSConfs int
|
2018-10-04 07:26:03 +00:00
|
|
|
for {
|
2018-05-22 10:02:03 +00:00
|
|
|
select {
|
2018-11-14 09:18:03 +00:00
|
|
|
case conf := <-configChan:
|
2018-10-04 07:26:03 +00:00
|
|
|
numUpdates++
|
2020-02-11 00:26:04 +00:00
|
|
|
numServices = len(conf.Configuration.HTTP.Services) + len(conf.Configuration.TCP.Services) + len(conf.Configuration.UDP.Services)
|
|
|
|
numRouters = len(conf.Configuration.HTTP.Routers) + len(conf.Configuration.TCP.Routers) + len(conf.Configuration.UDP.Routers)
|
2019-06-27 21:58:03 +00:00
|
|
|
numTLSConfs = len(conf.Configuration.TLS.Certificates)
|
2018-11-14 09:18:03 +00:00
|
|
|
t.Logf("received update #%d: services %d/%d, routers %d/%d, TLS configs %d/%d", numUpdates, numServices, test.expectedNumService, numRouters, test.expectedNumRouter, numTLSConfs, test.expectedNumTLSConf)
|
2018-10-04 07:26:03 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
if numServices == test.expectedNumService && numRouters == test.expectedNumRouter && numTLSConfs == test.expectedNumTLSConf {
|
2018-10-04 07:26:03 +00:00
|
|
|
return
|
|
|
|
}
|
2018-05-22 10:02:03 +00:00
|
|
|
case <-timeout:
|
2018-10-04 07:26:03 +00:00
|
|
|
t.Fatal("timeout while waiting for config")
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func getTestCases() []ProvideTestCase {
|
|
|
|
return []ProvideTestCase{
|
|
|
|
{
|
|
|
|
desc: "simple file",
|
2019-06-26 16:18:04 +00:00
|
|
|
filePath: "./fixtures/toml/simple_file_01.toml",
|
|
|
|
expectedNumRouter: 3,
|
|
|
|
expectedNumService: 6,
|
|
|
|
expectedNumTLSConf: 5,
|
|
|
|
},
|
2020-02-11 00:26:04 +00:00
|
|
|
{
|
|
|
|
desc: "simple file with tcp and udp",
|
|
|
|
filePath: "./fixtures/toml/simple_file_02.toml",
|
|
|
|
expectedNumRouter: 5,
|
|
|
|
expectedNumService: 8,
|
|
|
|
expectedNumTLSConf: 5,
|
|
|
|
},
|
2019-06-26 16:18:04 +00:00
|
|
|
{
|
|
|
|
desc: "simple file yaml",
|
|
|
|
filePath: "./fixtures/yaml/simple_file_01.yml",
|
2018-11-14 09:18:03 +00:00
|
|
|
expectedNumRouter: 3,
|
|
|
|
expectedNumService: 6,
|
|
|
|
expectedNumTLSConf: 5,
|
|
|
|
},
|
|
|
|
{
|
2019-06-26 16:18:04 +00:00
|
|
|
desc: "template file",
|
|
|
|
filePath: "./fixtures/toml/template_file.toml",
|
|
|
|
expectedNumRouter: 20,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "template file yaml",
|
|
|
|
filePath: "./fixtures/yaml/template_file.yml",
|
|
|
|
expectedNumRouter: 20,
|
|
|
|
},
|
2018-11-14 09:18:03 +00:00
|
|
|
{
|
2019-06-26 16:18:04 +00:00
|
|
|
desc: "simple directory",
|
|
|
|
directoryPaths: []string{
|
|
|
|
"./fixtures/toml/dir01_file01.toml",
|
|
|
|
"./fixtures/toml/dir01_file02.toml",
|
|
|
|
"./fixtures/toml/dir01_file03.toml",
|
|
|
|
},
|
2019-07-18 14:26:05 +00:00
|
|
|
expectedNumRouter: 2,
|
|
|
|
expectedNumService: 3,
|
|
|
|
expectedNumTLSConf: 4,
|
|
|
|
expectedNumTLSOptions: 1,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
2019-06-26 16:18:04 +00:00
|
|
|
{
|
|
|
|
desc: "simple directory yaml",
|
|
|
|
directoryPaths: []string{
|
|
|
|
"./fixtures/yaml/dir01_file01.yml",
|
|
|
|
"./fixtures/yaml/dir01_file02.yml",
|
|
|
|
"./fixtures/yaml/dir01_file03.yml",
|
|
|
|
},
|
2019-07-18 14:26:05 +00:00
|
|
|
expectedNumRouter: 2,
|
|
|
|
expectedNumService: 3,
|
|
|
|
expectedNumTLSConf: 4,
|
|
|
|
expectedNumTLSOptions: 1,
|
2019-06-26 16:18:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "template in directory",
|
|
|
|
directoryPaths: []string{
|
|
|
|
"./fixtures/toml/template_in_directory_file01.toml",
|
|
|
|
"./fixtures/toml/template_in_directory_file02.toml",
|
|
|
|
},
|
|
|
|
expectedNumRouter: 20,
|
|
|
|
expectedNumService: 20,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "template in directory yaml",
|
|
|
|
directoryPaths: []string{
|
|
|
|
"./fixtures/yaml/template_in_directory_file01.yml",
|
|
|
|
"./fixtures/yaml/template_in_directory_file02.yml",
|
|
|
|
},
|
|
|
|
expectedNumRouter: 20,
|
|
|
|
expectedNumService: 20,
|
|
|
|
},
|
2021-07-19 07:58:14 +00:00
|
|
|
{
|
|
|
|
desc: "simple file with empty store yaml",
|
|
|
|
filePath: "./fixtures/yaml/simple_empty_store.yml",
|
|
|
|
expectedNumRouter: 0,
|
|
|
|
expectedNumService: 0,
|
|
|
|
expectedNumTLSConf: 0,
|
|
|
|
},
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 09:54:03 +00:00
|
|
|
func createProvider(t *testing.T, test ProvideTestCase, watch bool) *Provider {
|
|
|
|
t.Helper()
|
|
|
|
|
2021-03-04 19:08:03 +00:00
|
|
|
tempDir := t.TempDir()
|
2018-05-22 10:02:03 +00:00
|
|
|
|
|
|
|
provider := &Provider{}
|
2019-06-17 09:48:05 +00:00
|
|
|
provider.Watch = true
|
2018-05-22 10:02:03 +00:00
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
if len(test.directoryPaths) > 0 {
|
2018-05-22 10:02:03 +00:00
|
|
|
if !watch {
|
2019-06-26 16:18:04 +00:00
|
|
|
for _, filePath := range test.directoryPaths {
|
|
|
|
var err error
|
|
|
|
_, err = createTempFile(filePath, tempDir)
|
|
|
|
require.NoError(t, err)
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
provider.Directory = tempDir
|
|
|
|
}
|
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
if len(test.filePath) > 0 {
|
|
|
|
var file *os.File
|
2018-05-22 10:02:03 +00:00
|
|
|
if watch {
|
2019-06-26 16:18:04 +00:00
|
|
|
var err error
|
2021-03-04 19:08:03 +00:00
|
|
|
file, err = os.CreateTemp(tempDir, "temp*"+filepath.Ext(test.filePath))
|
2019-06-26 16:18:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
file, err = createTempFile(test.filePath, tempDir)
|
|
|
|
require.NoError(t, err)
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
provider.Filename = file.Name()
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 09:54:03 +00:00
|
|
|
t.Cleanup(func() {
|
2019-03-18 08:34:03 +00:00
|
|
|
os.RemoveAll(tempDir)
|
2020-12-29 09:54:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return provider
|
2018-05-22 10:02:03 +00:00
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
func copyFile(srcPath, dstPath string) error {
|
2020-07-07 12:42:03 +00:00
|
|
|
dst, err := os.OpenFile(dstPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666)
|
2019-06-26 16:18:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
2019-06-26 16:18:04 +00:00
|
|
|
defer dst.Close()
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
src, err := os.Open(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
2019-06-26 16:18:04 +00:00
|
|
|
defer src.Close()
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
_, err = io.Copy(dst, src)
|
|
|
|
return err
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
2019-01-29 15:46:09 +00:00
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
func createTempFile(srcPath, tempDir string) (*os.File, error) {
|
2021-03-04 19:08:03 +00:00
|
|
|
file, err := os.CreateTemp(tempDir, "temp*"+filepath.Ext(srcPath))
|
2019-06-26 16:18:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2019-01-29 15:46:09 +00:00
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
src, err := os.Open(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer src.Close()
|
2019-01-29 15:46:09 +00:00
|
|
|
|
2019-06-26 16:18:04 +00:00
|
|
|
_, err = io.Copy(file, src)
|
|
|
|
return file, err
|
2019-01-29 15:46:09 +00:00
|
|
|
}
|