From 5ec469c67aeb700a5f50c78bb49de34be7ea0182 Mon Sep 17 00:00:00 2001 From: Ilya Dmitrichenko Date: Thu, 4 Apr 2019 15:43:24 +0100 Subject: [PATCH] Enable strict parsing --- examples/05-advanced-nodegroups.yaml | 2 +- pkg/eks/api.go | 10 ++++ pkg/eks/api_test.go | 74 ++++++++++++++++++++++++++++ pkg/eks/eks_test.go | 2 +- pkg/eks/testdata/bad-field-1.json | 11 +++++ pkg/eks/testdata/bad-field-1.yaml | 7 +++ pkg/eks/testdata/bad-field-2.yaml | 17 +++++++ pkg/eks/testdata/bad-type-1.yaml | 8 +++ pkg/eks/testdata/example.json | 11 +++++ pkg/eks/testdata/old-version.json | 11 +++++ 10 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 pkg/eks/api_test.go create mode 100644 pkg/eks/testdata/bad-field-1.json create mode 100644 pkg/eks/testdata/bad-field-1.yaml create mode 100644 pkg/eks/testdata/bad-field-2.yaml create mode 100644 pkg/eks/testdata/bad-type-1.yaml create mode 100644 pkg/eks/testdata/example.json create mode 100644 pkg/eks/testdata/old-version.json diff --git a/examples/05-advanced-nodegroups.yaml b/examples/05-advanced-nodegroups.yaml index c2ccaab167..f7ea7019b8 100644 --- a/examples/05-advanced-nodegroups.yaml +++ b/examples/05-advanced-nodegroups.yaml @@ -24,7 +24,7 @@ nodeGroups: labels: nodegroup-type: backend-cluster-addons privateNetworking: true - preBootsrapCommand: + preBootstrapCommand: # allow docker registries to be deployed as cluster service - 'echo {\"insecure-registries\": [\"172.20.0.0/16\",\"10.100.0.0/16\"]} > /etc/docker/daemon.json' - "systemctl restart docker" diff --git a/pkg/eks/api.go b/pkg/eks/api.go index 6ee4400720..2f00c0adff 100644 --- a/pkg/eks/api.go +++ b/pkg/eks/api.go @@ -17,6 +17,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/scheme" + "sigs.k8s.io/yaml" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" @@ -158,6 +159,15 @@ func LoadConfigFromFile(configFile string, cfg *api.ClusterConfig) error { return errors.Wrapf(err, "reading config file %q", configFile) } + // strict mode is not available in runtime.Decode, so we use the parser + // directly; we don't store the resulting object, this is just the means + // of detecting any unknown keys + // NOTE: we must use sigs.k8s.io/yaml, as it behaves differently from + // github.com/ghodss/yaml, which didn't handle nested structs well + if err := yaml.UnmarshalStrict(data, &api.ClusterConfig{}); err != nil { + return errors.Wrapf(err, "loading config file %q", configFile) + } + obj, err := runtime.Decode(scheme.Codecs.UniversalDeserializer(), data) if err != nil { return errors.Wrapf(err, "loading config file %q", configFile) diff --git a/pkg/eks/api_test.go b/pkg/eks/api_test.go new file mode 100644 index 0000000000..ca3db2ea87 --- /dev/null +++ b/pkg/eks/api_test.go @@ -0,0 +1,74 @@ +package eks_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha4" + + . "github.com/weaveworks/eksctl/pkg/eks" +) + +var _ = Describe("eksctl API", func() { + + Context("loading config files", func() { + var ( + cfg *api.ClusterConfig + ) + BeforeEach(func() { + err := api.Register() + Expect(err).ToNot(HaveOccurred()) + cfg = &api.ClusterConfig{} + }) + + It("should load a valid YAML config without error", func() { + err := LoadConfigFromFile("../../examples/01-simple-cluster.yaml", cfg) + Expect(err).ToNot(HaveOccurred()) + Expect(cfg.Metadata.Name).To(Equal("cluster-1")) + Expect(cfg.NodeGroups).To(HaveLen(1)) + }) + + It("should load a valid JSON config without error", func() { + err := LoadConfigFromFile("testdata/example.json", cfg) + Expect(err).ToNot(HaveOccurred()) + Expect(cfg.Metadata.Name).To(Equal("cluster-1")) + Expect(cfg.NodeGroups).To(HaveLen(1)) + }) + + It("should error when version is a float, not a string", func() { + err := LoadConfigFromFile("testdata/bad-type-1.yaml", cfg) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(HavePrefix(`loading config file "testdata/bad-type-1.yaml": v1alpha4.ClusterConfig.Metadata: v1alpha4.ClusterMeta.Version: ReadString: expects " or n, but found 1`)) + }) + + It("should reject unknown field in a YAML config", func() { + err := LoadConfigFromFile("testdata/bad-field-1.yaml", cfg) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(HavePrefix(`loading config file "testdata/bad-field-1.yaml": error unmarshaling JSON: while decoding JSON: json: unknown field "zone"`)) + }) + + It("should reject unknown field in a YAML config", func() { + err := LoadConfigFromFile("testdata/bad-field-2.yaml", cfg) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(HavePrefix(`loading config file "testdata/bad-field-2.yaml": error unmarshaling JSON: while decoding JSON: json: unknown field "bar"`)) + }) + + It("should reject unknown field in a JSON config", func() { + err := LoadConfigFromFile("testdata/bad-field-1.json", cfg) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(HavePrefix(`loading config file "testdata/bad-field-1.json": error unmarshaling JSON: while decoding JSON: json: unknown field "nodes"`)) + }) + + It("should reject old API version", func() { + err := LoadConfigFromFile("testdata/old-version.json", cfg) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(HavePrefix(`loading config file "testdata/old-version.json": no kind "ClusterConfig" is registered for version "eksctl.io/v1alpha3" in scheme "k8s.io/client-go/kubernetes/scheme/register.go:60"`)) + }) + + It("should error when cannot read a file", func() { + err := LoadConfigFromFile("../../examples/nothing.xml", cfg) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(Equal(`reading config file "../../examples/nothing.xml": open ../../examples/nothing.xml: no such file or directory`)) + }) + }) +}) diff --git a/pkg/eks/eks_test.go b/pkg/eks/eks_test.go index 3064c4d481..e5f3683911 100644 --- a/pkg/eks/eks_test.go +++ b/pkg/eks/eks_test.go @@ -17,7 +17,7 @@ import ( "github.com/weaveworks/eksctl/pkg/testutils/mockprovider" ) -var _ = Describe("Eks", func() { +var _ = Describe("EKS API wrapper", func() { var ( c *ClusterProvider p *mockprovider.MockProvider diff --git a/pkg/eks/testdata/bad-field-1.json b/pkg/eks/testdata/bad-field-1.json new file mode 100644 index 0000000000..d717554e6b --- /dev/null +++ b/pkg/eks/testdata/bad-field-1.json @@ -0,0 +1,11 @@ +{ + "apiVersion": "eksctl.io/v1alpha3", + "kind": "ClusterConfig", + "metadata": { + "name": "cluster-1", + "region": "eu-north-1" + }, + "nodeGroups": [ + { "name": "ng-1", "instanceType": "m5.large", "nodes": 10 } + ] +} diff --git a/pkg/eks/testdata/bad-field-1.yaml b/pkg/eks/testdata/bad-field-1.yaml new file mode 100644 index 0000000000..9c4e9df9b0 --- /dev/null +++ b/pkg/eks/testdata/bad-field-1.yaml @@ -0,0 +1,7 @@ +--- +apiVersion: eksctl.io/v1alpha4 +kind: ClusterConfig + +metadata: + name: cluster-1 + zone: eu-north-1a diff --git a/pkg/eks/testdata/bad-field-2.yaml b/pkg/eks/testdata/bad-field-2.yaml new file mode 100644 index 0000000000..6a61473fbb --- /dev/null +++ b/pkg/eks/testdata/bad-field-2.yaml @@ -0,0 +1,17 @@ +--- +apiVersion: eksctl.io/v1alpha4 +kind: ClusterConfig + +metadata: + name: cluster-1 + region: eu-north-1 + +nodeGroups: + - name: ng-1 + instanceType: m5.large + desiredCapacity: 10 + ssh: true + iam: + withAddonPolicies: + foo: 1 + bar: 2 diff --git a/pkg/eks/testdata/bad-type-1.yaml b/pkg/eks/testdata/bad-type-1.yaml new file mode 100644 index 0000000000..e70639b165 --- /dev/null +++ b/pkg/eks/testdata/bad-type-1.yaml @@ -0,0 +1,8 @@ +--- +apiVersion: eksctl.io/v1alpha4 +kind: ClusterConfig + +metadata: + name: cluster-1 + region: eu-north-1 + version: 1.10 diff --git a/pkg/eks/testdata/example.json b/pkg/eks/testdata/example.json new file mode 100644 index 0000000000..f73d637a3e --- /dev/null +++ b/pkg/eks/testdata/example.json @@ -0,0 +1,11 @@ +{ + "apiVersion": "eksctl.io/v1alpha4", + "kind": "ClusterConfig", + "metadata": { + "name": "cluster-1", + "region": "eu-north-1" + }, + "nodeGroups": [ + { "name": "ng-1", "instanceType": "m5.large", "desiredCapacity": 10 } + ] +} diff --git a/pkg/eks/testdata/old-version.json b/pkg/eks/testdata/old-version.json new file mode 100644 index 0000000000..847ed45242 --- /dev/null +++ b/pkg/eks/testdata/old-version.json @@ -0,0 +1,11 @@ +{ + "apiVersion": "eksctl.io/v1alpha3", + "kind": "ClusterConfig", + "metadata": { + "name": "cluster-1", + "region": "eu-north-1" + }, + "nodeGroups": [ + { "name": "ng-1", "instanceType": "m5.large", "desiredCapacity": 10 } + ] +}