-
Notifications
You must be signed in to change notification settings - Fork 484
/
validation.go
150 lines (141 loc) · 4.83 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cluster
import (
"fmt"
"github.com/grafana/agent/pkg/metrics/instance"
"github.com/grafana/loki/clients/pkg/promtail/discovery/consulagent"
"github.com/prometheus/common/config"
"github.com/prometheus/prometheus/discovery"
"github.com/prometheus/prometheus/discovery/aws"
"github.com/prometheus/prometheus/discovery/azure"
"github.com/prometheus/prometheus/discovery/consul"
"github.com/prometheus/prometheus/discovery/digitalocean"
"github.com/prometheus/prometheus/discovery/dns"
"github.com/prometheus/prometheus/discovery/eureka"
"github.com/prometheus/prometheus/discovery/file"
"github.com/prometheus/prometheus/discovery/gce"
"github.com/prometheus/prometheus/discovery/hetzner"
"github.com/prometheus/prometheus/discovery/http"
"github.com/prometheus/prometheus/discovery/kubernetes"
"github.com/prometheus/prometheus/discovery/linode"
"github.com/prometheus/prometheus/discovery/marathon"
"github.com/prometheus/prometheus/discovery/moby"
"github.com/prometheus/prometheus/discovery/openstack"
"github.com/prometheus/prometheus/discovery/scaleway"
"github.com/prometheus/prometheus/discovery/triton"
"github.com/prometheus/prometheus/discovery/zookeeper"
)
func validateNofiles(c *instance.Config) error {
for i, rw := range c.RemoteWrite {
if err := validateHTTPNoFiles(&rw.HTTPClientConfig); err != nil {
return fmt.Errorf("failed to validate remote_write at index %d: %w", i, err)
}
}
for i, sc := range c.ScrapeConfigs {
if err := validateHTTPNoFiles(&sc.HTTPClientConfig); err != nil {
return fmt.Errorf("failed to validate scrape_config at index %d: %w", i, err)
}
for j, disc := range sc.ServiceDiscoveryConfigs {
if err := validateDiscoveryNoFiles(disc); err != nil {
return fmt.Errorf("failed to validate service discovery at index %d within scrape_config at index %d: %w", j, i, err)
}
}
}
return nil
}
func validateHTTPNoFiles(cfg *config.HTTPClientConfig) error {
checks := []struct {
name string
check func() bool
}{
{"bearer_token_file", func() bool { return cfg.BearerTokenFile != "" }},
{"password_file", func() bool { return cfg.BasicAuth != nil && cfg.BasicAuth.PasswordFile != "" }},
{"credentials_file", func() bool { return cfg.Authorization != nil && cfg.Authorization.CredentialsFile != "" }},
{"ca_file", func() bool { return cfg.TLSConfig.CAFile != "" }},
{"cert_file", func() bool { return cfg.TLSConfig.CertFile != "" }},
{"key_file", func() bool { return cfg.TLSConfig.KeyFile != "" }},
}
for _, check := range checks {
if check.check() {
return fmt.Errorf("%s must be empty unless dangerous_allow_reading_files is set", check.name)
}
}
return nil
}
func validateDiscoveryNoFiles(disc discovery.Config) error {
switch d := disc.(type) {
case discovery.StaticConfig:
// no-op
case *azure.SDConfig:
// no-op
case *consul.SDConfig:
if err := validateHTTPNoFiles(&config.HTTPClientConfig{TLSConfig: d.HTTPClientConfig.TLSConfig}); err != nil {
return err
}
case *consulagent.SDConfig:
if err := validateHTTPNoFiles(&config.HTTPClientConfig{TLSConfig: d.TLSConfig}); err != nil {
return err
}
case *digitalocean.SDConfig:
if err := validateHTTPNoFiles(&d.HTTPClientConfig); err != nil {
return err
}
case *dns.SDConfig:
// no-op
case *moby.DockerSwarmSDConfig:
if err := validateHTTPNoFiles(&d.HTTPClientConfig); err != nil {
return err
}
case *aws.EC2SDConfig:
// no-op
case *eureka.SDConfig:
if err := validateHTTPNoFiles(&d.HTTPClientConfig); err != nil {
return err
}
case *file.SDConfig:
// no-op
case *gce.SDConfig:
// no-op
case *hetzner.SDConfig:
if err := validateHTTPNoFiles(&d.HTTPClientConfig); err != nil {
return err
}
case *kubernetes.SDConfig:
if err := validateHTTPNoFiles(&d.HTTPClientConfig); err != nil {
return err
}
case *marathon.SDConfig:
if err := validateHTTPNoFiles(&d.HTTPClientConfig); err != nil {
return err
}
if d.AuthTokenFile != "" {
return fmt.Errorf("auth_token_file must be empty unless dangerous_allow_reading_files is set")
}
case *openstack.SDConfig:
if err := validateHTTPNoFiles(&config.HTTPClientConfig{TLSConfig: d.TLSConfig}); err != nil {
return err
}
case *scaleway.SDConfig:
if err := validateHTTPNoFiles(&d.HTTPClientConfig); err != nil {
return err
}
case *triton.SDConfig:
if err := validateHTTPNoFiles(&config.HTTPClientConfig{TLSConfig: d.TLSConfig}); err != nil {
return err
}
case *http.SDConfig:
if err := validateHTTPNoFiles(&d.HTTPClientConfig); err != nil {
return err
}
case *linode.SDConfig:
if err := validateHTTPNoFiles(&d.HTTPClientConfig); err != nil {
return err
}
case *zookeeper.NerveSDConfig:
// no-op
case *zookeeper.ServersetSDConfig:
// no-op
default:
return fmt.Errorf("unknown service discovery %s; rejecting config for safety. set dangerous_allow_reading_files to ignore", d.Name())
}
return nil
}