Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Commit

Permalink
Fix race condition for YAML unmashalling (#211)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
evanlouie authored Jul 3, 2019
1 parent de0a74e commit 45ab958
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 7 deletions.
2 changes: 0 additions & 2 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"reflect"
"strings"

"github.com/microsoft/fabrikate/core"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions cmd/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"io/ioutil"
"os"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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)

Expand Down
3 changes: 0 additions & 3 deletions core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os/exec"
"path"
"path/filepath"
"reflect"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 45ab958

Please sign in to comment.