config: resolve more file paths (#5284)

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2019-03-12 11:24:15 +01:00 committed by Brian Brazil
parent 683fbc59ec
commit 027d2ece14
4 changed files with 72 additions and 23 deletions

View File

@ -153,11 +153,17 @@ func resolveFilepaths(baseDir string, cfg *Config) {
cfg.RuleFiles[i] = join(rf) cfg.RuleFiles[i] = join(rf)
} }
tlsPaths := func(cfg *config_util.TLSConfig) {
cfg.CAFile = join(cfg.CAFile)
cfg.CertFile = join(cfg.CertFile)
cfg.KeyFile = join(cfg.KeyFile)
}
clientPaths := func(scfg *config_util.HTTPClientConfig) { clientPaths := func(scfg *config_util.HTTPClientConfig) {
if scfg.BasicAuth != nil {
scfg.BasicAuth.PasswordFile = join(scfg.BasicAuth.PasswordFile)
}
scfg.BearerTokenFile = join(scfg.BearerTokenFile) scfg.BearerTokenFile = join(scfg.BearerTokenFile)
scfg.TLSConfig.CAFile = join(scfg.TLSConfig.CAFile) tlsPaths(&scfg.TLSConfig)
scfg.TLSConfig.CertFile = join(scfg.TLSConfig.CertFile)
scfg.TLSConfig.KeyFile = join(scfg.TLSConfig.KeyFile)
} }
sdPaths := func(cfg *sd_config.ServiceDiscoveryConfig) { sdPaths := func(cfg *sd_config.ServiceDiscoveryConfig) {
for _, kcfg := range cfg.KubernetesSDConfigs { for _, kcfg := range cfg.KubernetesSDConfigs {
@ -165,15 +171,16 @@ func resolveFilepaths(baseDir string, cfg *Config) {
} }
for _, mcfg := range cfg.MarathonSDConfigs { for _, mcfg := range cfg.MarathonSDConfigs {
mcfg.AuthTokenFile = join(mcfg.AuthTokenFile) mcfg.AuthTokenFile = join(mcfg.AuthTokenFile)
mcfg.HTTPClientConfig.BearerTokenFile = join(mcfg.HTTPClientConfig.BearerTokenFile) clientPaths(&mcfg.HTTPClientConfig)
mcfg.HTTPClientConfig.TLSConfig.CAFile = join(mcfg.HTTPClientConfig.TLSConfig.CAFile)
mcfg.HTTPClientConfig.TLSConfig.CertFile = join(mcfg.HTTPClientConfig.TLSConfig.CertFile)
mcfg.HTTPClientConfig.TLSConfig.KeyFile = join(mcfg.HTTPClientConfig.TLSConfig.KeyFile)
} }
for _, consulcfg := range cfg.ConsulSDConfigs { for _, consulcfg := range cfg.ConsulSDConfigs {
consulcfg.TLSConfig.CAFile = join(consulcfg.TLSConfig.CAFile) tlsPaths(&consulcfg.TLSConfig)
consulcfg.TLSConfig.CertFile = join(consulcfg.TLSConfig.CertFile) }
consulcfg.TLSConfig.KeyFile = join(consulcfg.TLSConfig.KeyFile) for _, cfg := range cfg.OpenstackSDConfigs {
tlsPaths(&cfg.TLSConfig)
}
for _, cfg := range cfg.TritonSDConfigs {
tlsPaths(&cfg.TLSConfig)
} }
for _, filecfg := range cfg.FileSDConfigs { for _, filecfg := range cfg.FileSDConfigs {
for i, fn := range filecfg.Files { for i, fn := range filecfg.Files {
@ -190,6 +197,12 @@ func resolveFilepaths(baseDir string, cfg *Config) {
clientPaths(&cfg.HTTPClientConfig) clientPaths(&cfg.HTTPClientConfig)
sdPaths(&cfg.ServiceDiscoveryConfig) sdPaths(&cfg.ServiceDiscoveryConfig)
} }
for _, cfg := range cfg.RemoteReadConfigs {
clientPaths(&cfg.HTTPClientConfig)
}
for _, cfg := range cfg.RemoteWriteConfigs {
clientPaths(&cfg.HTTPClientConfig)
}
} }
func (c Config) String() string { func (c Config) String() string {

View File

@ -23,9 +23,13 @@ import (
"testing" "testing"
"time" "time"
"github.com/prometheus/prometheus/pkg/relabel" config_util "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
"github.com/prometheus/prometheus/discovery/azure" "github.com/prometheus/prometheus/discovery/azure"
sd_config "github.com/prometheus/prometheus/discovery/config"
"github.com/prometheus/prometheus/discovery/consul" "github.com/prometheus/prometheus/discovery/consul"
"github.com/prometheus/prometheus/discovery/dns" "github.com/prometheus/prometheus/discovery/dns"
"github.com/prometheus/prometheus/discovery/ec2" "github.com/prometheus/prometheus/discovery/ec2"
@ -36,12 +40,8 @@ import (
"github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/discovery/triton" "github.com/prometheus/prometheus/discovery/triton"
"github.com/prometheus/prometheus/discovery/zookeeper" "github.com/prometheus/prometheus/discovery/zookeeper"
"github.com/prometheus/prometheus/pkg/relabel"
config_util "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
sd_config "github.com/prometheus/prometheus/discovery/config"
"github.com/prometheus/prometheus/util/testutil" "github.com/prometheus/prometheus/util/testutil"
"gopkg.in/yaml.v2"
) )
func mustParseURL(u string) *config_util.URL { func mustParseURL(u string) *config_util.URL {
@ -88,6 +88,12 @@ var expectedConf = &Config{
URL: mustParseURL("http://remote2/push"), URL: mustParseURL("http://remote2/push"),
RemoteTimeout: model.Duration(30 * time.Second), RemoteTimeout: model.Duration(30 * time.Second),
QueueConfig: DefaultQueueConfig, QueueConfig: DefaultQueueConfig,
HTTPClientConfig: config_util.HTTPClientConfig{
TLSConfig: config_util.TLSConfig{
CertFile: filepath.FromSlash("testdata/valid_cert_file"),
KeyFile: filepath.FromSlash("testdata/valid_key_file"),
},
},
}, },
}, },
@ -102,6 +108,12 @@ var expectedConf = &Config{
RemoteTimeout: model.Duration(1 * time.Minute), RemoteTimeout: model.Duration(1 * time.Minute),
ReadRecent: false, ReadRecent: false,
RequiredMatchers: model.LabelSet{"job": "special"}, RequiredMatchers: model.LabelSet{"job": "special"},
HTTPClientConfig: config_util.HTTPClientConfig{
TLSConfig: config_util.TLSConfig{
CertFile: filepath.FromSlash("testdata/valid_cert_file"),
KeyFile: filepath.FromSlash("testdata/valid_key_file"),
},
},
}, },
}, },
@ -346,6 +358,10 @@ var expectedConf = &Config{
Username: "myusername", Username: "myusername",
Password: "mysecret", Password: "mysecret",
}, },
TLSConfig: config_util.TLSConfig{
CertFile: filepath.FromSlash("testdata/valid_cert_file"),
KeyFile: filepath.FromSlash("testdata/valid_key_file"),
},
}, },
NamespaceDiscovery: kubernetes.NamespaceDiscovery{}, NamespaceDiscovery: kubernetes.NamespaceDiscovery{},
}, },
@ -360,6 +376,12 @@ var expectedConf = &Config{
MetricsPath: DefaultScrapeConfig.MetricsPath, MetricsPath: DefaultScrapeConfig.MetricsPath,
Scheme: DefaultScrapeConfig.Scheme, Scheme: DefaultScrapeConfig.Scheme,
HTTPClientConfig: config_util.HTTPClientConfig{
BasicAuth: &config_util.BasicAuth{
Username: "myusername",
PasswordFile: filepath.FromSlash("testdata/valid_password_file"),
},
},
ServiceDiscoveryConfig: sd_config.ServiceDiscoveryConfig{ ServiceDiscoveryConfig: sd_config.ServiceDiscoveryConfig{
KubernetesSDConfigs: []*kubernetes.SDConfig{ KubernetesSDConfigs: []*kubernetes.SDConfig{
@ -561,9 +583,9 @@ var expectedConf = &Config{
Port: 80, Port: 80,
RefreshInterval: model.Duration(60 * time.Second), RefreshInterval: model.Duration(60 * time.Second),
TLSConfig: config_util.TLSConfig{ TLSConfig: config_util.TLSConfig{
CAFile: "valid_ca_file", CAFile: "testdata/valid_ca_file",
CertFile: "valid_cert_file", CertFile: "testdata/valid_cert_file",
KeyFile: "valid_key_file", KeyFile: "testdata/valid_key_file",
}, },
}, },
}, },
@ -603,7 +625,7 @@ func TestLoadConfig(t *testing.T) {
testutil.Ok(t, err) testutil.Ok(t, err)
expectedConf.original = c.original expectedConf.original = c.original
testutil.Equals(t, expectedConf, c) assert.Equal(t, expectedConf, c)
} }
// YAML marshaling must not reveal authentication credentials. // YAML marshaling must not reveal authentication credentials.

View File

@ -19,6 +19,9 @@ remote_write:
regex: expensive.* regex: expensive.*
action: drop action: drop
- url: http://remote2/push - url: http://remote2/push
tls_config:
cert_file: valid_cert_file
key_file: valid_key_file
remote_read: remote_read:
- url: http://remote1/read - url: http://remote1/read
@ -27,6 +30,9 @@ remote_read:
read_recent: false read_recent: false
required_matchers: required_matchers:
job: special job: special
tls_config:
cert_file: valid_cert_file
key_file: valid_key_file
scrape_configs: scrape_configs:
- job_name: prometheus - job_name: prometheus
@ -153,6 +159,9 @@ scrape_configs:
kubernetes_sd_configs: kubernetes_sd_configs:
- role: endpoints - role: endpoints
api_server: 'https://localhost:1234' api_server: 'https://localhost:1234'
tls_config:
cert_file: valid_cert_file
key_file: valid_key_file
basic_auth: basic_auth:
username: 'myusername' username: 'myusername'
@ -167,6 +176,11 @@ scrape_configs:
names: names:
- default - default
basic_auth:
username: 'myusername'
password_file: valid_password_file
- job_name: service-marathon - job_name: service-marathon
marathon_sd_configs: marathon_sd_configs:
- servers: - servers:
@ -231,8 +245,8 @@ scrape_configs:
refresh_interval: 1m refresh_interval: 1m
version: 1 version: 1
tls_config: tls_config:
cert_file: testdata/valid_cert_file cert_file: valid_cert_file
key_file: testdata/valid_key_file key_file: valid_key_file
- job_name: service-openstack - job_name: service-openstack
openstack_sd_configs: openstack_sd_configs:

2
vendor/modules.txt vendored
View File

@ -262,8 +262,8 @@ github.com/shurcooL/httpfs/vfsutil
# github.com/shurcooL/vfsgen v0.0.0-20180711163814-62bca832be04 # github.com/shurcooL/vfsgen v0.0.0-20180711163814-62bca832be04
github.com/shurcooL/vfsgen github.com/shurcooL/vfsgen
# github.com/stretchr/testify v1.2.2 # github.com/stretchr/testify v1.2.2
github.com/stretchr/testify/require
github.com/stretchr/testify/assert github.com/stretchr/testify/assert
github.com/stretchr/testify/require
# go.opencensus.io v0.18.1-0.20181204023538-aab39bd6a98b # go.opencensus.io v0.18.1-0.20181204023538-aab39bd6a98b
go.opencensus.io/plugin/ochttp go.opencensus.io/plugin/ochttp
go.opencensus.io/plugin/ochttp/propagation/tracecontext go.opencensus.io/plugin/ochttp/propagation/tracecontext