Skip to content

Commit

Permalink
Update to yaml.v3
Browse files Browse the repository at this point in the history
Also, modify `empty_config_yml` because with yaml.v3 reading empty file
causes Decode to return EOF error, as described in
go-yaml/yaml#805 issue.

Signed-off-by: Mikel Olasagasti Uranga <mikel@olasagasti.info>
  • Loading branch information
mikelolasagasti committed Jul 23, 2023
1 parent 2f4150c commit f7a824b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/prometheus/common v0.44.0
golang.org/x/crypto v0.10.0
golang.org/x/sync v0.3.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -32,4 +32,5 @@ require (
golang.org/x/text v0.10.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10 changes: 7 additions & 3 deletions web/tls_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package web

import (
"bytes"
"io"
"crypto/tls"
"crypto/x509"
"errors"
Expand All @@ -28,7 +30,7 @@ import (
"github.com/go-kit/log/level"
config_util "github.com/prometheus/common/config"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

var (
Expand Down Expand Up @@ -119,8 +121,10 @@ func getConfig(configPath string) (*Config, error) {
},
HTTPConfig: HTTPConfig{HTTP2: true},
}
err = yaml.UnmarshalStrict(content, c)
if err == nil {
decoder := yaml.NewDecoder(bytes.NewReader(content))
decoder.KnownFields(true)
err = decoder.Decode(c)
if err == nil && !errors.Is(err, io.EOF) {
err = validateHeaderConfig(c.HTTPConfig.Header)
}
c.TLSConfig.SetDirectory(filepath.Dir(configPath))
Expand Down
3 changes: 2 additions & 1 deletion web/tls_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var (
testlogger = &testLogger{}

ErrorMap = map[string]*regexp.Regexp{
"End of file": regexp.MustCompile(`EOF`),
"HTTP Response to HTTPS": regexp.MustCompile(`server gave HTTP response to HTTPS client`),
"No such file": regexp.MustCompile(`no such file`),
"Invalid argument": regexp.MustCompile(`invalid argument`),
Expand Down Expand Up @@ -112,7 +113,7 @@ func TestYAMLFiles(t *testing.T) {
{
Name: `empty config yml`,
YAMLConfigPath: "testdata/web_config_empty.yml",
ExpectedError: nil,
ExpectedError: ErrorMap["End of file"],
},
{
Name: `invalid config yml (invalid structure)`,
Expand Down

0 comments on commit f7a824b

Please sign in to comment.