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 <[email protected]>
  • Loading branch information
mikelolasagasti committed Sep 10, 2024
1 parent ad41e17 commit d3e814e
Show file tree
Hide file tree
Showing 3 changed files with 11 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.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 (
Expand All @@ -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
)
10 changes: 7 additions & 3 deletions web/tls_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
package web

import (
"bytes"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"log/slog"
"net"
"net/http"
Expand All @@ -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 (
Expand Down Expand Up @@ -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))
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 @@ -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`),
Expand Down Expand Up @@ -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)`,
Expand Down

0 comments on commit d3e814e

Please sign in to comment.