From 0591c82aa435c0569245b9e1590af215cf73a0de Mon Sep 17 00:00:00 2001 From: Michael Gruener Date: Thu, 21 Jan 2021 14:18:34 +0100 Subject: [PATCH] Improve mimetype handling and mapstructure downgrade mitchellh/mapstructure added https://github.com/mitchellh/mapstructure/issues/187 with 1.3.2 which somehow breaks the decode() function of kusible. Added TODOs where applicable to check later --- go.mod | 2 +- go.sum | 5 +++-- pkg/inventory/config/types.go | 2 ++ pkg/loader/file.go | 9 ++++----- pkg/loader/s3.go | 9 ++++----- pkg/loader/util.go | 2 +- pkg/playbook/config/config.go | 2 ++ 7 files changed, 17 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 2f7f022..a25a9dd 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/imdario/mergo v0.3.11 github.com/kjk/lzmadec v0.0.0-20200118223809-980b947af806 github.com/kr/pretty v0.2.1 // indirect - github.com/mitchellh/mapstructure v1.4.1 + github.com/mitchellh/mapstructure v1.3.1 github.com/pborman/ansi v1.0.0 github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.7.0 diff --git a/go.sum b/go.sum index 7c074d3..ef2cb11 100644 --- a/go.sum +++ b/go.sum @@ -534,9 +534,10 @@ github.com/mitchellh/gox v1.0.1/go.mod h1:ED6BioOGXMswlXa2zxfh/xdd5QhwYliBFn9V18 github.com/mitchellh/hashstructure v1.0.0/go.mod h1:QjSHrPWS+BGUVBYkbTZWEnOh3G1DutKwClXU/ABz6AQ= github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag= -github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mitchellh/mapstructure v1.3.1 h1:cCBH2gTD2K0OtLlv/Y5H01VQCqmlDxz30kS5Y5bqfLA= +github.com/mitchellh/mapstructure v1.3.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f h1:2+myh5ml7lgEU/51gbeLHfKGNfgEQQIWrlbdaOsidbQ= github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A= github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= diff --git a/pkg/inventory/config/types.go b/pkg/inventory/config/types.go index 5e4ce57..257a514 100644 --- a/pkg/inventory/config/types.go +++ b/pkg/inventory/config/types.go @@ -56,6 +56,8 @@ type Params map[string]interface{} // decode the given data with the default decoder settings func decode(data *map[string]interface{}, result interface{}) error { + // TODO: check https://github.com/mitchellh/mapstructure/issues/187 to + // support mitchellh/mapstructure > 1.3.1 decoderConfig := &mapstructure.DecoderConfig{ ZeroFields: true, ErrorUnused: false, diff --git a/pkg/loader/file.go b/pkg/loader/file.go index 22d96c1..498d1ce 100644 --- a/pkg/loader/file.go +++ b/pkg/loader/file.go @@ -69,23 +69,22 @@ func (b *FileBackend) Load() ([]byte, error) { } var raw []byte - switch mime.String() { - case "text/plain": + if mime.Is("text/plain") { raw, err = ioutil.ReadFile(b.config.Path) if err != nil { return nil, err } - case "application/x-7z-compressed": + } else if mime.Is("application/x-7z-compressed") { raw, err = extractSingleTar7ZipFile(b.config.Path, b.config.DecryptKey) if err != nil { return nil, err } - case "application/octet-stream": + } else if mime.Is("application/octet-stream") { raw, err = decryptOpensslSymmetricFile(b.config.Path, b.config.DecryptKey) if err != nil { return nil, err } - default: + } else { return nil, errors.New("Unknown source file type: " + mime.String()) } diff --git a/pkg/loader/s3.go b/pkg/loader/s3.go index 17c757b..3154f47 100644 --- a/pkg/loader/s3.go +++ b/pkg/loader/s3.go @@ -138,20 +138,19 @@ func (b *S3Backend) Load() ([]byte, error) { var rawKubeconfig []byte - switch mime.String() { - case "text/plain": + if mime.Is("text/plain") { rawKubeconfig = data - case "application/x-7z-compressed": + } else if mime.Is("application/x-7z-compressed") { rawKubeconfig, err = extractSingleTar7Zip(data, b.config.DecryptKey) if err != nil { return nil, err } - case "application/octet-stream": + } else if mime.Is("application/octet-stream") { rawKubeconfig, err = decryptOpensslSymmetric(data, b.config.DecryptKey) if err != nil { return nil, err } - default: + } else { return nil, errors.New("Unknown kubeconfig source file type: " + mime.String()) } diff --git a/pkg/loader/util.go b/pkg/loader/util.go index d1e43b7..2e9d545 100644 --- a/pkg/loader/util.go +++ b/pkg/loader/util.go @@ -58,7 +58,7 @@ func extractSingleTar7ZipFile(path string, password string) ([]byte, error) { if err != nil { return nil, err } - if mime.String() != "application/x-7z-compressed" { + if !mime.Is("application/x-7z-compressed") { return nil, errors.New("expected MIME type application/x-7z-compressed but got " + mime.String()) } diff --git a/pkg/playbook/config/config.go b/pkg/playbook/config/config.go index 70c4336..d274ee7 100644 --- a/pkg/playbook/config/config.go +++ b/pkg/playbook/config/config.go @@ -28,6 +28,8 @@ import ( // decode the given data with the default decoder settings func decode(data *map[string]interface{}, result interface{}) error { + // TODO: check https://github.com/mitchellh/mapstructure/issues/187 to + // support mitchellh/mapstructure > 1.3.1 decoderConfig := &mapstructure.DecoderConfig{ ZeroFields: true, ErrorUnused: false,