From 45ab958c46cef54437b5905f620144fe037fb75e Mon Sep 17 00:00:00 2001 From: Evan Louie Date: Wed, 3 Jul 2019 11:08:35 -0700 Subject: [PATCH] Fix race condition for YAML unmashalling (#211) - Fixes a race condition for yaml unmarshalling by moving the modification of go-yaml DefaultMapType to very start of program; no longer changing it every time an unmarshal occurs. --- cmd/set.go | 2 -- cmd/set_test.go | 2 -- core/component.go | 3 --- main.go | 6 ++++++ 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/cmd/set.go b/cmd/set.go index 6514797..c48a3d4 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io/ioutil" - "reflect" "strings" "github.com/microsoft/fabrikate/core" @@ -91,7 +90,6 @@ func Set(environment string, subcomponent string, pathValuePairStrings []string, return err } yamlContent := map[string]interface{}{} - *yaml.DefaultMapType = reflect.TypeOf(map[string]interface{}{}) err = yaml.Unmarshal(bytes, &yamlContent) if err != nil { return err diff --git a/cmd/set_test.go b/cmd/set_test.go index 4aa17cb..1119af3 100644 --- a/cmd/set_test.go +++ b/cmd/set_test.go @@ -3,7 +3,6 @@ package cmd import ( "io/ioutil" "os" - "reflect" "testing" "github.com/stretchr/testify/assert" @@ -81,7 +80,6 @@ func TestSetValue(t *testing.T) { // Parse yaml fromFile := map[string]interface{}{} - *yaml.DefaultMapType = reflect.TypeOf(map[string]interface{}{}) err = yaml.Unmarshal(bytes, &fromFile) assert.Nil(t, err) diff --git a/core/component.go b/core/component.go index bec4767..8454b5f 100644 --- a/core/component.go +++ b/core/component.go @@ -9,7 +9,6 @@ import ( "os/exec" "path" "path/filepath" - "reflect" "sort" "strings" "sync" @@ -85,9 +84,7 @@ func (c *Component) applyDefaultsAndMigrations() { } func (c *Component) LoadComponent() (loadedComponent Component, err error) { - *yaml.DefaultMapType = reflect.TypeOf(map[string]interface{}{}) err = c.UnmarshalComponent("yaml", yaml.Unmarshal, &loadedComponent) - if err != nil { err = c.UnmarshalComponent("json", json.Unmarshal, &loadedComponent) if err != nil { diff --git a/main.go b/main.go index 869cf9c..3d165a2 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,17 @@ package main import ( + "reflect" + "github.com/microsoft/fabrikate/cmd" log "github.com/sirupsen/logrus" + "github.com/timfpark/yaml" ) func main() { + // modify the DefaultMapType of yaml to map[string]interface{} instead of map[interface]interface{} + *yaml.DefaultMapType = reflect.TypeOf(map[string]interface{}{}) + formatter := new(log.TextFormatter) formatter.TimestampFormat = "02-01-2006 15:04:05" formatter.FullTimestamp = true