@@ -36,25 +36,87 @@ import (
36
36
"gopkg.in/yaml.v2"
37
37
)
38
38
39
- // DefaultHTTPClientConfig is the default HTTP client configuration.
40
- var DefaultHTTPClientConfig = HTTPClientConfig {
41
- FollowRedirects : true ,
42
- EnableHTTP2 : true ,
43
- }
39
+ var (
40
+ // DefaultHTTPClientConfig is the default HTTP client configuration.
41
+ DefaultHTTPClientConfig = HTTPClientConfig {
42
+ FollowRedirects : true ,
43
+ EnableHTTP2 : true ,
44
+ }
44
45
45
- // defaultHTTPClientOptions holds the default HTTP client options.
46
- var defaultHTTPClientOptions = httpClientOptions {
47
- keepAlivesEnabled : true ,
48
- http2Enabled : true ,
49
- // 5 minutes is typically above the maximum sane scrape interval. So we can
50
- // use keepalive for all configurations.
51
- idleConnTimeout : 5 * time .Minute ,
52
- }
46
+ // defaultHTTPClientOptions holds the default HTTP client options.
47
+ defaultHTTPClientOptions = httpClientOptions {
48
+ keepAlivesEnabled : true ,
49
+ http2Enabled : true ,
50
+ // 5 minutes is typically above the maximum sane scrape interval. So we can
51
+ // use keepalive for all configurations.
52
+ idleConnTimeout : 5 * time .Minute ,
53
+ }
54
+ )
53
55
54
56
type closeIdler interface {
55
57
CloseIdleConnections ()
56
58
}
57
59
60
+ type TLSVersion uint16
61
+
62
+ var TLSVersions = map [string ]TLSVersion {
63
+ "TLS13" : (TLSVersion )(tls .VersionTLS13 ),
64
+ "TLS12" : (TLSVersion )(tls .VersionTLS12 ),
65
+ "TLS11" : (TLSVersion )(tls .VersionTLS11 ),
66
+ "TLS10" : (TLSVersion )(tls .VersionTLS10 ),
67
+ }
68
+
69
+ func (tv * TLSVersion ) UnmarshalYAML (unmarshal func (interface {}) error ) error {
70
+ var s string
71
+ err := unmarshal ((* string )(& s ))
72
+ if err != nil {
73
+ return err
74
+ }
75
+ if v , ok := TLSVersions [s ]; ok {
76
+ * tv = v
77
+ return nil
78
+ }
79
+ return fmt .Errorf ("unknown TLS version: %s" , s )
80
+ }
81
+
82
+ func (tv * TLSVersion ) MarshalYAML () (interface {}, error ) {
83
+ if tv != nil || * tv == 0 {
84
+ return []byte ("null" ), nil
85
+ }
86
+ for s , v := range TLSVersions {
87
+ if * tv == v {
88
+ return s , nil
89
+ }
90
+ }
91
+ return nil , fmt .Errorf ("unknown TLS version: %d" , tv )
92
+ }
93
+
94
+ // MarshalJSON implements the json.Unmarshaler interface for TLSVersion.
95
+ func (tv * TLSVersion ) UnmarshalJSON (data []byte ) error {
96
+ var s string
97
+ if err := json .Unmarshal (data , & s ); err != nil {
98
+ return err
99
+ }
100
+ if v , ok := TLSVersions [s ]; ok {
101
+ * tv = v
102
+ return nil
103
+ }
104
+ return fmt .Errorf ("unknown TLS version: %s" , s )
105
+ }
106
+
107
+ // MarshalJSON implements the json.Marshaler interface for TLSVersion.
108
+ func (tv * TLSVersion ) MarshalJSON () ([]byte , error ) {
109
+ if tv != nil || * tv == 0 {
110
+ return []byte ("null" ), nil
111
+ }
112
+ for s , v := range TLSVersions {
113
+ if * tv == v {
114
+ return []byte (s ), nil
115
+ }
116
+ }
117
+ return nil , fmt .Errorf ("unknown TLS version: %d" , tv )
118
+ }
119
+
58
120
// BasicAuth contains basic HTTP authentication credentials.
59
121
type BasicAuth struct {
60
122
Username string `yaml:"username" json:"username"`
@@ -669,7 +731,10 @@ func cloneRequest(r *http.Request) *http.Request {
669
731
670
732
// NewTLSConfig creates a new tls.Config from the given TLSConfig.
671
733
func NewTLSConfig (cfg * TLSConfig ) (* tls.Config , error ) {
672
- tlsConfig := & tls.Config {InsecureSkipVerify : cfg .InsecureSkipVerify }
734
+ tlsConfig := & tls.Config {
735
+ InsecureSkipVerify : cfg .InsecureSkipVerify ,
736
+ MinVersion : uint16 (cfg .MinVersion ),
737
+ }
673
738
674
739
// If a CA cert is provided then let's read it in so we can validate the
675
740
// scrape target's certificate properly.
@@ -714,6 +779,8 @@ type TLSConfig struct {
714
779
ServerName string `yaml:"server_name,omitempty" json:"server_name,omitempty"`
715
780
// Disable target certificate validation.
716
781
InsecureSkipVerify bool `yaml:"insecure_skip_verify" json:"insecure_skip_verify"`
782
+ // Minimum TLS version.
783
+ MinVersion TLSVersion `yaml:"min_version,omitempty" json:"min_version,omitempty"`
717
784
}
718
785
719
786
// SetDirectory joins any relative file paths with dir.
@@ -726,12 +793,6 @@ func (c *TLSConfig) SetDirectory(dir string) {
726
793
c .KeyFile = JoinDir (dir , c .KeyFile )
727
794
}
728
795
729
- // UnmarshalYAML implements the yaml.Unmarshaler interface.
730
- func (c * TLSConfig ) UnmarshalYAML (unmarshal func (interface {}) error ) error {
731
- type plain TLSConfig
732
- return unmarshal ((* plain )(c ))
733
- }
734
-
735
796
// getClientCertificate reads the pair of client cert and key from disk and returns a tls.Certificate.
736
797
func (c * TLSConfig ) getClientCertificate (* tls.CertificateRequestInfo ) (* tls.Certificate , error ) {
737
798
cert , err := tls .LoadX509KeyPair (c .CertFile , c .KeyFile )
0 commit comments