Skip to content

Commit

Permalink
Replace yaml Unmarshal workaround with UnmarshalStrict. (weaveworks#127)
Browse files Browse the repository at this point in the history
Signed-off-by: manosf <manosf@protonmail.com>
  • Loading branch information
manosf authored and brian-brazil committed Apr 26, 2018
1 parent e5b036c commit d811d2e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 43 deletions.
19 changes: 3 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package config

import (
"fmt"
"strings"
)
// This package no longer handles safe yaml parsing. In order to
// ensure correct yaml unmarshalling, use "yaml.UnmarshalStrict()".

func checkOverflow(m map[string]interface{}, ctx string) error {
if len(m) > 0 {
var keys []string
for k := range m {
keys = append(keys, k)
}
return fmt.Errorf("unknown fields in %s: %s", ctx, strings.Join(keys, ", "))
}
return nil
}
package config

// Secret special type for storing secrets.
type Secret string
Expand Down
29 changes: 4 additions & 25 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ type BasicAuth struct {
Username string `yaml:"username"`
Password Secret `yaml:"password,omitempty"`
PasswordFile string `yaml:"password_file,omitempty"`

// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline"`
}

// URL is a custom URL type that allows validation at configuration load time.
Expand Down Expand Up @@ -77,9 +74,6 @@ type HTTPClientConfig struct {
ProxyURL URL `yaml:"proxy_url,omitempty"`
// TLSConfig to use to connect to the targets.
TLSConfig TLSConfig `yaml:"tls_config,omitempty"`

// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline"`
}

// Validate validates the HTTPClientConfig to check only one of BearerToken,
Expand All @@ -103,25 +97,16 @@ func (c *HTTPClientConfig) Validate() error {
// UnmarshalYAML implements the yaml.Unmarshaler interface
func (c *HTTPClientConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain HTTPClientConfig
err := unmarshal((*plain)(c))
if err != nil {
if err := unmarshal((*plain)(c)); err != nil {
return err
}
err = c.Validate()
if err != nil {
return c.Validate()
}
return checkOverflow(c.XXX, "http_client_config")
return c.Validate()
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (a *BasicAuth) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain BasicAuth
err := unmarshal((*plain)(a))
if err != nil {
return err
}
return checkOverflow(a.XXX, "basic_auth")
return unmarshal((*plain)(a))
}

// NewClient returns a http.Client using the specified http.RoundTripper.
Expand Down Expand Up @@ -318,18 +303,12 @@ type TLSConfig struct {
ServerName string `yaml:"server_name,omitempty"`
// Disable target certificate validation.
InsecureSkipVerify bool `yaml:"insecure_skip_verify"`

// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline"`
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *TLSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain TLSConfig
if err := unmarshal((*plain)(c)); err != nil {
return err
}
return checkOverflow(c.XXX, "TLS config")
return unmarshal((*plain)(c))
}

func (c HTTPClientConfig) String() string {
Expand Down
2 changes: 1 addition & 1 deletion config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ func TestInvalidHTTPConfigs(t *testing.T) {
// LoadHTTPConfig parses the YAML input s into a HTTPClientConfig.
func LoadHTTPConfig(s string) (*HTTPClientConfig, error) {
cfg := &HTTPClientConfig{}
err := yaml.Unmarshal([]byte(s), cfg)
err := yaml.UnmarshalStrict([]byte(s), cfg)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion config/tls_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func LoadTLSConfig(filename string) (*tls.Config, error) {
return nil, err
}
cfg := TLSConfig{}
if err = yaml.Unmarshal(content, &cfg); err != nil {
if err = yaml.UnmarshalStrict(content, &cfg); err != nil {
return nil, err
}
return NewTLSConfig(&cfg)
Expand Down

0 comments on commit d811d2e

Please sign in to comment.