From d3e814eb4b34ef53bd288ed9019b666c25ca3aa0 Mon Sep 17 00:00:00 2001 From: Mikel Olasagasti Uranga Date: Sun, 23 Jul 2023 16:09:29 +0200 Subject: [PATCH] Update to yaml.v3 Also, modify `empty_config_yml` because with yaml.v3 reading empty file causes Decode to return EOF error, as described in https://github.com/go-yaml/yaml/issues/805 issue. Signed-off-by: Mikel Olasagasti Uranga --- go.mod | 3 ++- web/tls_config.go | 10 +++++++--- web/tls_config_test.go | 3 ++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 7902b3a1..81a587d8 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/prometheus/common v0.58.0 golang.org/x/crypto v0.26.0 golang.org/x/sync v0.8.0 - gopkg.in/yaml.v2 v2.4.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -30,4 +30,5 @@ require ( golang.org/x/sys v0.23.0 // indirect golang.org/x/text v0.17.0 // indirect google.golang.org/protobuf v1.34.2 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/web/tls_config.go b/web/tls_config.go index 0730a938..94bcd131 100644 --- a/web/tls_config.go +++ b/web/tls_config.go @@ -14,10 +14,12 @@ package web import ( + "bytes" "crypto/tls" "crypto/x509" "errors" "fmt" + "io" "log/slog" "net" "net/http" @@ -31,7 +33,7 @@ import ( "github.com/mdlayher/vsock" config_util "github.com/prometheus/common/config" "golang.org/x/sync/errgroup" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" ) var ( @@ -122,8 +124,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)) diff --git a/web/tls_config_test.go b/web/tls_config_test.go index b28c6671..e3b69d7a 100644 --- a/web/tls_config_test.go +++ b/web/tls_config_test.go @@ -45,6 +45,7 @@ var ( testlogger = slog.New(slog.NewTextHandler(os.Stdout, nil)) 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`), @@ -110,7 +111,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)`,