Skip to content

Commit d3e814e

Browse files
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 go-yaml/yaml#805 issue. Signed-off-by: Mikel Olasagasti Uranga <[email protected]>
1 parent ad41e17 commit d3e814e

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/prometheus/common v0.58.0
1010
golang.org/x/crypto v0.26.0
1111
golang.org/x/sync v0.8.0
12-
gopkg.in/yaml.v2 v2.4.0
12+
gopkg.in/yaml.v3 v3.0.1
1313
)
1414

1515
require (
@@ -30,4 +30,5 @@ require (
3030
golang.org/x/sys v0.23.0 // indirect
3131
golang.org/x/text v0.17.0 // indirect
3232
google.golang.org/protobuf v1.34.2 // indirect
33+
gopkg.in/yaml.v2 v2.4.0 // indirect
3334
)

web/tls_config.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
package web
1515

1616
import (
17+
"bytes"
1718
"crypto/tls"
1819
"crypto/x509"
1920
"errors"
2021
"fmt"
22+
"io"
2123
"log/slog"
2224
"net"
2325
"net/http"
@@ -31,7 +33,7 @@ import (
3133
"github.com/mdlayher/vsock"
3234
config_util "github.com/prometheus/common/config"
3335
"golang.org/x/sync/errgroup"
34-
"gopkg.in/yaml.v2"
36+
"gopkg.in/yaml.v3"
3537
)
3638

3739
var (
@@ -122,8 +124,10 @@ func getConfig(configPath string) (*Config, error) {
122124
},
123125
HTTPConfig: HTTPConfig{HTTP2: true},
124126
}
125-
err = yaml.UnmarshalStrict(content, c)
126-
if err == nil {
127+
decoder := yaml.NewDecoder(bytes.NewReader(content))
128+
decoder.KnownFields(true)
129+
err = decoder.Decode(c)
130+
if err == nil && !errors.Is(err, io.EOF) {
127131
err = validateHeaderConfig(c.HTTPConfig.Header)
128132
}
129133
c.TLSConfig.SetDirectory(filepath.Dir(configPath))

web/tls_config_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var (
4545
testlogger = slog.New(slog.NewTextHandler(os.Stdout, nil))
4646

4747
ErrorMap = map[string]*regexp.Regexp{
48+
"End of file": regexp.MustCompile(`EOF`),
4849
"HTTP Response to HTTPS": regexp.MustCompile(`server gave HTTP response to HTTPS client`),
4950
"No such file": regexp.MustCompile(`no such file`),
5051
"Invalid argument": regexp.MustCompile(`invalid argument`),
@@ -110,7 +111,7 @@ func TestYAMLFiles(t *testing.T) {
110111
{
111112
Name: `empty config yml`,
112113
YAMLConfigPath: "testdata/web_config_empty.yml",
113-
ExpectedError: nil,
114+
ExpectedError: ErrorMap["End of file"],
114115
},
115116
{
116117
Name: `invalid config yml (invalid structure)`,

0 commit comments

Comments
 (0)