From 708d7c5a1c5c67d49eaef9b0f3e000e427940d23 Mon Sep 17 00:00:00 2001 From: killianmuldoon Date: Mon, 6 Nov 2023 10:28:56 +0000 Subject: [PATCH] Drop Cluster API fuzzing targets Signed-off-by: killianmuldoon --- .../cluster-api/bootstrap_kubeadm_fuzzer.go | 127 ---- projects/cluster-api/build.sh | 94 --- .../cluster-api/cluster_controller_fuzzer.go | 97 --- .../clusterclass_controller_fuzzer.go | 97 --- projects/cluster-api/conditions_fuzzer.go | 53 -- projects/cluster-api/conversion_fuzzer.go | 646 ------------------ projects/cluster-api/conversion_fuzzer2.go | 189 ----- projects/cluster-api/crd-creation/main.go | 111 --- .../internal_kubeadm_controller_fuzzer.go | 141 ---- .../cluster-api/kubeadm_internal_fuzzer.go | 85 --- .../cluster-api/machine_controller_fuzzer.go | 97 --- .../machinedeployment_controller_fuzzer.go | 97 --- .../machinehealthcheck_controller_fuzzer.go | 96 --- .../machineset_controller_fuzzer.go | 97 --- projects/cluster-api/non-oss-fuzz/Dockerfile | 13 - projects/cluster-api/non-oss-fuzz/build.sh | 24 - .../non-oss-fuzz/cluster_controller_fuzzer.go | 183 ----- .../cluster-api/non-oss-fuzz/run_fuzzer.sh | 2 - projects/cluster-api/patch_fuzzer.go | 85 --- .../topology_cluster_reconciler_fuzzer.go | 132 ---- projects/cluster-api/util_container_fuzzer.go | 35 - .../v1beta1_machine_webhook_fuzzer.go | 119 ---- projects/cluster-api/yaml_fuzzer.go | 35 - 23 files changed, 2655 deletions(-) delete mode 100644 projects/cluster-api/bootstrap_kubeadm_fuzzer.go delete mode 100755 projects/cluster-api/build.sh delete mode 100644 projects/cluster-api/cluster_controller_fuzzer.go delete mode 100644 projects/cluster-api/clusterclass_controller_fuzzer.go delete mode 100644 projects/cluster-api/conditions_fuzzer.go delete mode 100644 projects/cluster-api/conversion_fuzzer.go delete mode 100644 projects/cluster-api/conversion_fuzzer2.go delete mode 100644 projects/cluster-api/crd-creation/main.go delete mode 100644 projects/cluster-api/internal_kubeadm_controller_fuzzer.go delete mode 100644 projects/cluster-api/kubeadm_internal_fuzzer.go delete mode 100644 projects/cluster-api/machine_controller_fuzzer.go delete mode 100644 projects/cluster-api/machinedeployment_controller_fuzzer.go delete mode 100644 projects/cluster-api/machinehealthcheck_controller_fuzzer.go delete mode 100644 projects/cluster-api/machineset_controller_fuzzer.go delete mode 100644 projects/cluster-api/non-oss-fuzz/Dockerfile delete mode 100755 projects/cluster-api/non-oss-fuzz/build.sh delete mode 100644 projects/cluster-api/non-oss-fuzz/cluster_controller_fuzzer.go delete mode 100755 projects/cluster-api/non-oss-fuzz/run_fuzzer.sh delete mode 100644 projects/cluster-api/patch_fuzzer.go delete mode 100644 projects/cluster-api/topology_cluster_reconciler_fuzzer.go delete mode 100644 projects/cluster-api/util_container_fuzzer.go delete mode 100644 projects/cluster-api/v1beta1_machine_webhook_fuzzer.go delete mode 100644 projects/cluster-api/yaml_fuzzer.go diff --git a/projects/cluster-api/bootstrap_kubeadm_fuzzer.go b/projects/cluster-api/bootstrap_kubeadm_fuzzer.go deleted file mode 100644 index cd1dc252..00000000 --- a/projects/cluster-api/bootstrap_kubeadm_fuzzer.go +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright 2022 ADA Logics Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package utils - -import ( - "github.com/blang/semver" - bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" - fuzz "github.com/AdaLogics/go-fuzz-headers" -) - - -func FuzzKubeadmTypesMarshalling(data []byte) int { - if len(data)<10 { - return 0 - } - intType := int(data[0]) - switch intType%4 { - case 0: - return fuzzMarshalClusterConfigurationForVersion(data[1:]) - case 1: - return fuzzMarshalClusterStatusForVersion(data[1:]) - case 2: - return fuzzMarshalInitConfigurationForVersion(data[1:]) - case 3: - return fuzzMarshalJoinConfigurationForVersion(data[1:]) - } - return 1 -} - -func fuzzMarshalClusterConfigurationForVersion(data []byte) int { - f := fuzz.NewConsumer(data) - obj := &bootstrapv1.ClusterConfiguration{} - err := f.GenerateStruct(obj) - if err != nil { - return 0 - } - versionStr, err := f.GetString() - if err != nil { - return 0 - } - version, err := semver.Parse(versionStr) - if err != nil { - return 0 - } - _, _ = MarshalClusterConfigurationForVersion(obj, version) - return 1 -} - -func fuzzMarshalClusterStatusForVersion(data []byte) int { - f := fuzz.NewConsumer(data) - obj := &bootstrapv1.ClusterStatus{} - err := f.GenerateStruct(obj) - if err != nil { - return 0 - } - versionStr, err := f.GetString() - if err != nil { - return 0 - } - version, err := semver.Parse(versionStr) - if err != nil { - return 0 - } - _, _ = MarshalClusterStatusForVersion(obj, version) - return 1 -} - -func fuzzMarshalInitConfigurationForVersion(data []byte) int { - f := fuzz.NewConsumer(data) - obj := &bootstrapv1.InitConfiguration{} - err := f.GenerateStruct(obj) - if err != nil { - return 0 - } - versionStr, err := f.GetString() - if err != nil { - return 0 - } - version, err := semver.Parse(versionStr) - if err != nil { - return 0 - } - _, _ = MarshalInitConfigurationForVersion(obj, version) - return 1 -} - -func fuzzMarshalJoinConfigurationForVersion(data []byte) int { - f := fuzz.NewConsumer(data) - obj := &bootstrapv1.JoinConfiguration{} - err := f.GenerateStruct(obj) - if err != nil { - return 0 - } - versionStr, err := f.GetString() - if err != nil { - return 0 - } - version, err := semver.Parse(versionStr) - if err != nil { - return 0 - } - _, _ = MarshalJoinConfigurationForVersion(obj, version) - return 1 -} - -func FuzzUnmarshalClusterConfiguration(data []byte) int { - _, _ = UnmarshalClusterConfiguration(string(data)) - return 1 -} - -func FuzzUnmarshalClusterStatus(data []byte) int { - _, _ = UnmarshalClusterStatus(string(data)) - return 1 -} \ No newline at end of file diff --git a/projects/cluster-api/build.sh b/projects/cluster-api/build.sh deleted file mode 100755 index 8f823c4a..00000000 --- a/projects/cluster-api/build.sh +++ /dev/null @@ -1,94 +0,0 @@ -set -o nounset -set -o pipefail -set -o errexit -set -x - -go get github.com/AdaLogics/go-fuzz-headers@latest - -# Controllers -########################################################## -cp $SRC/cncf-fuzzing/projects/cluster-api/cluster_controller_fuzzer.go \ - $SRC/cluster-api/internal/controllers/cluster/ -compile_go_fuzzer sigs.k8s.io/cluster-api/internal/controllers/cluster FuzzClusterReconcile fuzz_cluster_controller - -cp $SRC/cncf-fuzzing/projects/cluster-api/clusterclass_controller_fuzzer.go \ - $SRC/cluster-api/internal/controllers/clusterclass/ -compile_go_fuzzer sigs.k8s.io/cluster-api/internal/controllers/clusterclass FuzzClusterClassReconcile fuzz_clusterclass_controller - -cp $SRC/cncf-fuzzing/projects/cluster-api/machine_controller_fuzzer.go \ - $SRC/cluster-api/internal/controllers/machine/ -compile_go_fuzzer sigs.k8s.io/cluster-api/internal/controllers/machine FuzzMachineReconcile fuzz_machine_controller - -cp $SRC/cncf-fuzzing/projects/cluster-api/machinedeployment_controller_fuzzer.go \ - $SRC/cluster-api/internal/controllers/machinedeployment/ -compile_go_fuzzer sigs.k8s.io/cluster-api/internal/controllers/machinedeployment FuzzMachineDeploymentReconcile fuzz_machinedepoyment_controller - -cp $SRC/cncf-fuzzing/projects/cluster-api/machinehealthcheck_controller_fuzzer.go \ - $SRC/cluster-api/internal/controllers/machinehealthcheck/ -compile_go_fuzzer sigs.k8s.io/cluster-api/internal/controllers/machinehealthcheck FuzzMachineHealthCheckReconcile fuzz_machinehealthcheck_controller - -cp $SRC/cncf-fuzzing/projects/cluster-api/machineset_controller_fuzzer.go \ - $SRC/cluster-api/internal/controllers/machineset/ -compile_go_fuzzer sigs.k8s.io/cluster-api/internal/controllers/machineset FuzzMachinesetReconcile fuzz_machineset_controller -######################################################## - - -cp $SRC/cncf-fuzzing/projects/cluster-api/util_container_fuzzer.go \ - $SRC/cluster-api/util/container/ -compile_go_fuzzer sigs.k8s.io/cluster-api/util/container FuzzModifyImageRepository fuzz_modify_image_repository -compile_go_fuzzer sigs.k8s.io/cluster-api/util/container FuzzModifyImageTag fuzz_modify_image_tag - -cp $SRC/cncf-fuzzing/projects/cluster-api/kubeadm_internal_fuzzer.go \ - $SRC/cluster-api/controlplane/kubeadm/internal/ -cd $SRC/cluster-api/controlplane/kubeadm/internal -compile_go_fuzzer . FuzzMatchesMachineSpec fuzz_matches_machine_spec -cd $SRC/cluster-api - -cp $SRC/cncf-fuzzing/projects/cluster-api/internal_kubeadm_controller_fuzzer.go \ - $SRC/cluster-api/controlplane/kubeadm/internal/controllers/ -mv $SRC/cluster-api/controlplane/kubeadm/internal/controllers/fakes_test.go \ - $SRC/cluster-api/controlplane/kubeadm/internal/controllers/fakes_test_fuzz.go -mv $SRC/cluster-api/controlplane/kubeadm/internal/controllers/controller_test.go \ - $SRC/cluster-api/controlplane/kubeadm/internal/controllers/controller_test_fuzz.go -rm $SRC/cluster-api/controlplane/kubeadm/internal/controllers/suite_test.go -cd $SRC/cluster-api/controlplane/kubeadm/internal/controllers -compile_go_fuzzer . FuzzKubeadmControlPlaneReconciler fuzz_kubeadm_controlplane_reconciler -cd $SRC/cluster-api - -cp $SRC/cncf-fuzzing/projects/cluster-api/patch_fuzzer.go \ - $SRC/cluster-api/util/patch/ -compile_go_fuzzer sigs.k8s.io/cluster-api/util/patch FuzzPatch fuzz_patch - -cp $SRC/cncf-fuzzing/projects/cluster-api/conditions_fuzzer.go \ - $SRC/cluster-api/util/conditions/ -compile_go_fuzzer sigs.k8s.io/cluster-api/util/conditions FuzzPatchApply fuzz_patch_apply - -cp $SRC/cncf-fuzzing/projects/cluster-api/topology_cluster_reconciler_fuzzer.go \ - $SRC/cluster-api/internal/controllers/topology/cluster/ -compile_go_fuzzer sigs.k8s.io/cluster-api/internal/controllers/topology/cluster FuzzClusterReconcile fuzz_cluster_reconcile - -mkdir $SRC/cluster-api/fuzz -cp $SRC/cncf-fuzzing/projects/cluster-api/conversion_fuzzer2.go \ - $SRC/cluster-api/fuzz/ -compile_go_fuzzer sigs.k8s.io/cluster-api/fuzz FuzzConversionOfAllTypes fuzz_conversion_of_all_types - - -cp $SRC/cncf-fuzzing/projects/cluster-api/yaml_fuzzer.go \ - $SRC/cluster-api/util/yaml/ - -compile_go_fuzzer sigs.k8s.io/cluster-api/util/yaml FuzzYamlParse fuzz_yaml_parser - -cp $SRC/cncf-fuzzing/projects/cluster-api/bootstrap_kubeadm_fuzzer.go \ - $SRC/cluster-api/bootstrap/kubeadm/types/ - -compile_go_fuzzer sigs.k8s.io/cluster-api/bootstrap/kubeadm/types FuzzKubeadmTypesMarshalling fuzz_kubeadm_types_marshalling -compile_go_fuzzer sigs.k8s.io/cluster-api/bootstrap/kubeadm/types FuzzUnmarshalClusterConfiguration fuzz_unmarshal_cluster_configuration -compile_go_fuzzer sigs.k8s.io/cluster-api/bootstrap/kubeadm/types FuzzUnmarshalClusterStatus fuzz_unmarshal_cluster_status - -cp $SRC/cncf-fuzzing/projects/cluster-api/conversion_fuzzer.go \ - $SRC/cluster-api/api/v1alpha3/ -compile_go_fuzzer sigs.k8s.io/cluster-api/api/v1alpha3 FuzzV1alpha3Conversion fuzz_v1alpha3_conversion - -cp $SRC/cncf-fuzzing/projects/cluster-api/v1beta1_machine_webhook_fuzzer.go \ - $SRC/cluster-api/api/v1beta1/ -compile_go_fuzzer sigs.k8s.io/cluster-api/api/v1beta1 FuzzWebhookValidation fuzz_webhook_validation diff --git a/projects/cluster-api/cluster_controller_fuzzer.go b/projects/cluster-api/cluster_controller_fuzzer.go deleted file mode 100644 index 242b9c4f..00000000 --- a/projects/cluster-api/cluster_controller_fuzzer.go +++ /dev/null @@ -1,97 +0,0 @@ -package cluster - -import ( - "context" - "fmt" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "sigs.k8s.io/cluster-api/internal/test/builder" - corev1 "k8s.io/api/core/v1" - "sigs.k8s.io/cluster-api/util" - "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - "github.com/ghodss/yaml" - fuzz "github.com/AdaLogics/go-fuzz-headers" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/apimachinery/pkg/runtime" - clientgoscheme "k8s.io/client-go/kubernetes/scheme" -) - -var ( - fuzzCtx = context.Background() - fakeSchemeForFuzzing = runtime.NewScheme() -) - -func init() { - _ = clientgoscheme.AddToScheme(fakeSchemeForFuzzing) - _ = clusterv1.AddToScheme(fakeSchemeForFuzzing) - _ = apiextensionsv1.AddToScheme(fakeSchemeForFuzzing) - _ = corev1.AddToScheme(fakeSchemeForFuzzing) -} - -// helper function to crate an unstructured object. -func GetUnstructured(f *fuzz.ConsumeFuzzer) (*unstructured.Unstructured, error) { - yamlStr, err := f.GetString() - if err != nil { - return nil, err - } - obj := make(map[string]interface{}) - err = yaml.Unmarshal([]byte(yamlStr), &obj) - if err != nil { - return nil, err - } - return &unstructured.Unstructured{Object: obj}, nil -} - - -func validateUnstructured(unstr *unstructured.Unstructured) error { - if _, ok := unstr.Object["kind"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["apiVersion"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["spec"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["status"]; !ok { - return fmt.Errorf("invalid unstr") - } - return nil -} - -func FuzzClusterReconcile(data []byte) int { - f := fuzz.NewConsumer(data) - unstr, err := GetUnstructured(f) - if err != nil { - return 0 - } - err = validateUnstructured(unstr) - if err != nil { - return 0 - } - cluster := &clusterv1.Cluster{} - err = f.GenerateStruct(cluster) - if err != nil { - return 0 - } - node := &corev1.Node{} - err = f.GenerateStruct(node) - if err != nil { - return 0 - } - clientFake := fake.NewClientBuilder().WithScheme(fakeSchemeForFuzzing).WithObjects( - cluster, - node, - unstr, - builder.GenericInfrastructureMachineCRD.DeepCopy(), - ).Build() - r := &Reconciler{ - Client: clientFake, - APIReader: clientFake, - } - - _, _ = r.Reconcile(fuzzCtx, reconcile.Request{NamespacedName: util.ObjectKey(cluster)}) - return 1 -} diff --git a/projects/cluster-api/clusterclass_controller_fuzzer.go b/projects/cluster-api/clusterclass_controller_fuzzer.go deleted file mode 100644 index 566b6228..00000000 --- a/projects/cluster-api/clusterclass_controller_fuzzer.go +++ /dev/null @@ -1,97 +0,0 @@ -package clusterclass - -import ( - "context" - "fmt" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "sigs.k8s.io/cluster-api/internal/test/builder" - corev1 "k8s.io/api/core/v1" - "sigs.k8s.io/cluster-api/util" - "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - "github.com/ghodss/yaml" - fuzz "github.com/AdaLogics/go-fuzz-headers" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/apimachinery/pkg/runtime" - clientgoscheme "k8s.io/client-go/kubernetes/scheme" -) - -var ( - fuzzCtx = context.Background() - fakeSchemeForFuzzing = runtime.NewScheme() -) - -func init() { - _ = clientgoscheme.AddToScheme(fakeSchemeForFuzzing) - _ = clusterv1.AddToScheme(fakeSchemeForFuzzing) - _ = apiextensionsv1.AddToScheme(fakeSchemeForFuzzing) - _ = corev1.AddToScheme(fakeSchemeForFuzzing) -} - -// helper function to crate an unstructured object. -func GetUnstructured(f *fuzz.ConsumeFuzzer) (*unstructured.Unstructured, error) { - yamlStr, err := f.GetString() - if err != nil { - return nil, err - } - obj := make(map[string]interface{}) - err = yaml.Unmarshal([]byte(yamlStr), &obj) - if err != nil { - return nil, err - } - return &unstructured.Unstructured{Object: obj}, nil -} - - -func validateUnstructured(unstr *unstructured.Unstructured) error { - if _, ok := unstr.Object["kind"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["apiVersion"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["spec"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["status"]; !ok { - return fmt.Errorf("invalid unstr") - } - return nil -} - -func FuzzClusterClassReconcile(data []byte) int { - f := fuzz.NewConsumer(data) - unstr, err := GetUnstructured(f) - if err != nil { - return 0 - } - err = validateUnstructured(unstr) - if err != nil { - return 0 - } - cluster := &clusterv1.ClusterClass{} - err = f.GenerateStruct(cluster) - if err != nil { - return 0 - } - node := &corev1.Node{} - err = f.GenerateStruct(node) - if err != nil { - return 0 - } - clientFake := fake.NewClientBuilder().WithScheme(fakeSchemeForFuzzing).WithObjects( - cluster, - node, - unstr, - builder.GenericInfrastructureMachineCRD.DeepCopy(), - ).Build() - r := &Reconciler{ - Client: clientFake, - APIReader: clientFake, - } - - _, _ = r.Reconcile(fuzzCtx, reconcile.Request{NamespacedName: util.ObjectKey(cluster)}) - return 1 -} \ No newline at end of file diff --git a/projects/cluster-api/conditions_fuzzer.go b/projects/cluster-api/conditions_fuzzer.go deleted file mode 100644 index 60d04be1..00000000 --- a/projects/cluster-api/conditions_fuzzer.go +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2022 ADA Logics Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package conditions - -import ( - fuzz "github.com/AdaLogics/go-fuzz-headers" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" -) - -func FuzzPatchApply(data []byte) int { - f := fuzz.NewConsumer(data) - getterBefore := &clusterv1.Cluster{} - err := f.GenerateStruct(getterBefore) - if err != nil { - return 0 - } - getterAfter := &clusterv1.Cluster{} - err = f.GenerateStruct(getterAfter) - if err != nil { - return 0 - } - - setter := &clusterv1.Cluster{} - err = f.GenerateStruct(setter) - if err != nil { - return 0 - } - - var options []ApplyOption - err = f.CreateSlice(&options) - if err != nil { - return 0 - } - patch, err := NewPatch(getterBefore, getterAfter) - if err != nil { - return 0 - } - _ = patch.Apply(setter, options...) - return 1 -} diff --git a/projects/cluster-api/conversion_fuzzer.go b/projects/cluster-api/conversion_fuzzer.go deleted file mode 100644 index 6eeb36ff..00000000 --- a/projects/cluster-api/conversion_fuzzer.go +++ /dev/null @@ -1,646 +0,0 @@ -// Copyright 2022 ADA Logics Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package v1alpha3 - -import ( - "fmt" - "time" - - "github.com/google/go-cmp/cmp" - corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/apitesting/fuzzer" - apiequality "k8s.io/apimachinery/pkg/api/equality" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes/scheme" - "sigs.k8s.io/controller-runtime/pkg/conversion" - "k8s.io/apimachinery/pkg/util/intstr" - - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - "sigs.k8s.io/cluster-api/api/v1alpha4" - - fuzz "github.com/AdaLogics/go-fuzz-headers" - - utilconversion "sigs.k8s.io/cluster-api/util/conversion" -) - -func createConditions() clusterv1.Conditions { - return clusterv1.Conditions{} -} - -func customMachineHealth() []interface{} { - return []interface{}{ - func(mds *clusterv1.MachineDeploymentStatus, c fuzz.Continue) error { - phase, err := c.F.GetString() - if err != nil { - return err - } - observedGeneration, err := c.F.GetInt() - if err != nil { - return err - } - selector, err := c.F.GetString() - if err != nil { - return err - } - replicas, err := c.F.GetInt() - if err != nil { - return err - } - updatedReplicas, err := c.F.GetInt() - if err != nil { - return err - } - mds.Phase = phase - mds.Conditions = createConditions() - mds.ObservedGeneration = int64(observedGeneration) - mds.Selector = selector - mds.Replicas = int32(replicas) - mds.UpdatedReplicas = int32(updatedReplicas) - //fmt.Printf("%+v\n", mds) - return nil - }, - func(mss *clusterv1.MachineSetStatus, c fuzz.Continue) error { - err := c.GenerateStruct(mss) - if err != nil { - return err - } - mss.Conditions = createConditions() - return nil - }, - func(ms *clusterv1.MachineStatus, c fuzz.Continue) error { - err := c.GenerateStruct(ms) - if err != nil { - return err - } - nsi := &corev1.NodeSystemInfo{} - err = c.GenerateStruct(nsi) - if err != nil { - return err - } - nsi.BootID = "44a832f3-8cfb-4de5-b7d2-d66030b6cd95" - nsi.OperatingSystem = "linux" - nsi.KubeletVersion = "fuzzVersion" - nsi.SystemUUID = "SystemUUID" - nsi.KubeProxyVersion = "KubeProxyVersion" - nsi.Architecture = "fuzz" - nsi.MachineID = "fuzz" - nsi.ContainerRuntimeVersion = "fuzz" - /*nsi.KernelVersion = cadvisortest.FakeKernelVersion - nsi.OSImage = cadvisortest.FakeContainerOSVersion*/ - nsi.KernelVersion = "fuzz" - nsi.OSImage = "fuzz" - - ms.NodeInfo = nsi - ms.Conditions = createConditions() - return nil - }, - func(ms *v1alpha4.MachineStatus, c fuzz.Continue) error { - err := c.GenerateStruct(ms) - if err != nil { - return err - } - ms.Version = nil - return nil - }, - func(ccs *clusterv1.ClusterClassSpec, c fuzz.Continue) error { - err := c.GenerateStruct(ccs) - if err != nil { - return err - } - if len(ccs.Variables) == 0 { - ccs.Variables = nil - } - if len(ccs.Patches) == 0 { - ccs.Patches = nil - } - return nil - }, - func(ccs *intstr.IntOrString, c fuzz.Continue) error { - if ccs == nil { - return fmt.Errorf("intstr was nil") - } - makeStr, err := c.F.GetBool() - if err != nil { - return err - } - if makeStr { - ccs.Type = 1 - newStr, err := c.F.GetString() - if err != nil { - return err - } - ccs.StrVal = newStr - ccs.IntVal = 0 - } else { - ccs.Type = 2 - ccs.StrVal = "" - newInt, err := c.F.GetInt() - if err != nil { - return err - } - ccs.IntVal = int32(newInt) - } - - return nil - }, - func(conds *clusterv1.Conditions, c fuzz.Continue) error { - //Conditions need some modificatons, but setting them here - // won't work, so we check in the fuzzer wheterh they are set properly. - return nil - }, - func(mrds *clusterv1.MachineRollingUpdateDeployment, c fuzz.Continue) error { - err := c.GenerateStruct(mrds) - if err != nil { - return err - } - newString := "DeletePolicy" - mrds.DeletePolicy = &newString - return nil - }, - func(met *metav1.TypeMeta, c fuzz.Continue) error { - met.APIVersion = "" - met.Kind = "" - return nil - }, - func(nsi *corev1.NodeSystemInfo, c fuzz.Continue) error { - err := c.GenerateStruct(nsi) - if err != nil { - return err - } - validChars := "-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - bootid, err := c.F.GetStringFrom(validChars, 40) - if err != nil { - return err - } - operatingsystem, err := c.F.GetStringFrom(validChars, 40) - if err != nil { - return err - } - kubeletversion, err := c.F.GetStringFrom(validChars, 40) - if err != nil { - return err - } - systemuuid, err := c.F.GetStringFrom(validChars, 40) - if err != nil { - return err - } - kubeproxyversion, err := c.F.GetStringFrom(validChars, 40) - if err != nil { - return err - } - architecture, err := c.F.GetStringFrom(validChars, 40) - if err != nil { - return err - } - machineid, err := c.F.GetStringFrom(validChars, 40) - if err != nil { - return err - } - containerruntimeversion, err := c.F.GetStringFrom(validChars, 40) - if err != nil { - return err - } - kernelversion, err := c.F.GetStringFrom(validChars, 40) - if err != nil { - return err - } - osimage, err := c.F.GetStringFrom(validChars, 40) - if err != nil { - return err - } - nsi.BootID = bootid - nsi.OperatingSystem = operatingsystem - nsi.KubeletVersion = kubeletversion - nsi.SystemUUID = systemuuid - nsi.KubeProxyVersion = kubeproxyversion - nsi.Architecture = architecture - nsi.MachineID = machineid - nsi.ContainerRuntimeVersion = containerruntimeversion - nsi.KernelVersion = kernelversion - nsi.OSImage = osimage - return nil - }, - func(top *clusterv1.Topology, c fuzz.Continue) error { - layout := "Mon, 01/02/06, 03:04PM" - timeStr := "Thu, 02/03/07, 04:05PM" - rolloutAfter, err := time.Parse(layout, timeStr) - if err != nil { - return err - } - newTime := metav1.NewTime(rolloutAfter) - top.RolloutAfter = &newTime - return nil - }, - func(mhs *clusterv1.MachineHealthCheckSpec, c fuzz.Continue) error { - err := c.GenerateStruct(mhs) - if err != nil { - return err - } - unhealthyRange := "[1-3]" - mhs.UnhealthyRange = &unhealthyRange - return nil - }, - } -} - -// FuzzV1alpha3Conversion implements a fuzzer that is similar to the -// roundtrip test in cluster-api/util/conversion.FuzzTestFunc -func FuzzV1alpha3Conversion(data []byte) int { - if len(data) < 100 { - return 0 - } - targetType := int(data[0]) - targetType = targetType % 11 - - var hubName string - var spokeName string - var input utilconversion.FuzzTestFuncInput - switch targetType { - case 0: - input = utilconversion.FuzzTestFuncInput{ - Hub: &clusterv1.Cluster{}, - Spoke: &Cluster{}, - SpokeAfterMutation: clusterSpokeAfterMutationFuzz, - FuzzerFuncs: []fuzzer.FuzzerFuncs{}, - } - hubName = "sigs.k8s.io/cluster-api/api/v1beta1.Cluster" - spokeName = "sigs.k8s.io/cluster-api/api/v1alpha3.Cluster" - case 1: - input = utilconversion.FuzzTestFuncInput{ - Hub: &clusterv1.Machine{}, - Spoke: &Machine{}, - FuzzerFuncs: []fuzzer.FuzzerFuncs{}, - } - hubName = "sigs.k8s.io/cluster-api/api/v1beta1.Machine" - spokeName = "sigs.k8s.io/cluster-api/api/v1alpha3.Machine" - case 2: - input = utilconversion.FuzzTestFuncInput{ - Hub: &clusterv1.MachineSet{}, - Spoke: &MachineSet{}, - FuzzerFuncs: []fuzzer.FuzzerFuncs{}, - } - hubName = "sigs.k8s.io/cluster-api/api/v1beta1.MachineSet" - spokeName = "sigs.k8s.io/cluster-api/api/v1alpha3.MachineSet" - case 3: - input = utilconversion.FuzzTestFuncInput{ - Hub: &clusterv1.MachineDeployment{}, - Spoke: &MachineDeployment{}, - FuzzerFuncs: []fuzzer.FuzzerFuncs{}, - } - hubName = "sigs.k8s.io/cluster-api/api/v1beta1.MachineDeployment" - spokeName = "sigs.k8s.io/cluster-api/api/v1alpha3.MachineDeployment" - case 4: - input = utilconversion.FuzzTestFuncInput{ - Hub: &clusterv1.MachineHealthCheck{}, - Spoke: &MachineHealthCheck{}, - } - hubName = "sigs.k8s.io/cluster-api/api/v1beta1.MachineHealthCheck" - spokeName = "sigs.k8s.io/cluster-api/api/v1alpha3.MachineHealthCheck" - case 5: - input = utilconversion.FuzzTestFuncInput{ - Hub: &clusterv1.Cluster{}, - Spoke: &v1alpha4.Cluster{}, - } - hubName = "sigs.k8s.io/cluster-api/api/v1beta1.Cluster" - spokeName = "sigs.k8s.io/cluster-api/api/v1alpha4.Cluster" - case 6: - input = utilconversion.FuzzTestFuncInput{ - Hub: &clusterv1.ClusterClass{}, - Spoke: &v1alpha4.ClusterClass{}, - } - hubName = "sigs.k8s.io/cluster-api/api/v1beta1.ClusterClass" - spokeName = "sigs.k8s.io/cluster-api/api/v1alpha4.ClusterClass" - case 7: - input = utilconversion.FuzzTestFuncInput{ - Hub: &clusterv1.Machine{}, - Spoke: &v1alpha4.Machine{}, - } - hubName = "sigs.k8s.io/cluster-api/api/v1beta1.Machine" - spokeName = "sigs.k8s.io/cluster-api/api/v1alpha4.Machine" - case 8: - input = utilconversion.FuzzTestFuncInput{ - Hub: &clusterv1.MachineSet{}, - Spoke: &v1alpha4.MachineSet{}, - } - hubName = "sigs.k8s.io/cluster-api/api/v1beta1.MachineSet" - spokeName = "sigs.k8s.io/cluster-api/api/v1alpha4.MachineSet" - case 9: - input = utilconversion.FuzzTestFuncInput{ - Hub: &clusterv1.MachineDeployment{}, - Spoke: &v1alpha4.MachineDeployment{}, - } - hubName = "sigs.k8s.io/cluster-api/api/v1beta1.MachineDeployment" - spokeName = "sigs.k8s.io/cluster-api/api/v1alpha4.MachineDeployment" - case 10: - input = utilconversion.FuzzTestFuncInput{ - Hub: &clusterv1.MachineHealthCheck{}, - Spoke: &v1alpha4.MachineHealthCheck{}, - } - hubName = "sigs.k8s.io/cluster-api/api/v1beta1.MachineHealthCheck" - spokeName = "sigs.k8s.io/cluster-api/api/v1alpha4.MachineHealthCheck" - default: - return -1 - } - doConversionRoundtrip(input, data[1:], hubName, spokeName) - return 1 -} - -// performs the conversion roundtrip test -func doConversionRoundtrip(input utilconversion.FuzzTestFuncInput, data []byte, hubName, spokeName string) error { - - f := fuzz.NewConsumer(data) - mhsFunc := customMachineHealth() - f.AddFuncs(mhsFunc) - - if input.Scheme == nil { - input.Scheme = scheme.Scheme - } - - // Create the spoke and fuzz it - spokeBefore := input.Spoke.DeepCopyObject().(conversion.Convertible) - err := f.GenerateWithCustom(spokeBefore) - if err != nil { - return err - } - - // Set some fields to avoid false positives - setCustomFieldsSpoke(spokeBefore) - - // First convert spoke to hub - hubCopy := input.Hub.DeepCopyObject().(conversion.Hub) - err = spokeBefore.ConvertTo(hubCopy) - if err != nil { - return err - } - - // Convert hub back to spoke and check if the resulting spoke is equal to the spoke before the round trip - spokeAfter := input.Spoke.DeepCopyObject().(conversion.Convertible) - err = spokeAfter.ConvertFrom(hubCopy) - if err != nil { - return err - } - - // Remove data annotation eventually added by ConvertFrom for avoiding data loss in hub-spoke-hub round trips - // NOTE: There are use case when we want to skip this operation, e.g. if the spoke object does not have ObjectMeta (e.g. kubeadm types). - if !input.SkipSpokeAnnotationCleanup { - metaAfter := spokeAfter.(metav1.Object) - delete(metaAfter.GetAnnotations(), utilconversion.DataAnnotation) - } - - if input.SpokeAfterMutation != nil { - input.SpokeAfterMutation(spokeAfter) - } - - if !apiequality.Semantic.DeepEqual(spokeBefore, spokeAfter) { - panic(fmt.Sprintf(cmp.Diff(spokeBefore, spokeAfter))) - } - - hubBefore := input.Hub.DeepCopyObject().(conversion.Hub) - err = f.GenerateWithCustom(hubBefore) - if err != nil { - return err - } - - // Validate machine deployment - if _, ok := hubBefore.(*clusterv1.MachineDeployment); ok { - err = validateMachineDeployment(hubBefore) - if err != nil { - return err - } - } - // Validate ClusterClass - if _, ok := hubBefore.(*clusterv1.ClusterClass); ok { - err = validateClusterClass(hubBefore) - if err != nil { - return err - } - } - - // Set some fields to avoid false positives - //setCustomFieldsHub(hubBefore) - - // First convert hub to spoke - dstCopy := input.Spoke.DeepCopyObject().(conversion.Convertible) - err = dstCopy.ConvertFrom(hubBefore) - if err != nil { - return err - } - - // Convert spoke back to hub and check if the resulting hub is equal to the hub before the round trip - hubAfter := input.Hub.DeepCopyObject().(conversion.Hub) - err = dstCopy.ConvertTo(hubAfter) - if err != nil { - return err - } - - if input.HubAfterMutation != nil { - input.HubAfterMutation(hubAfter) - } - - if !apiequality.Semantic.DeepEqual(hubBefore, hubAfter) { - panic(fmt.Sprintf("Failed roundtrip for %s:\n\n%+v\n", hubName, cmp.Diff(hubBefore, hubAfter))) - } - return nil -} - -func isValidUtf8(input []byte) bool { - for _, b := range input { - if int(b) > 127 { - return false - } - } - return true -} - -// validates MachineDeployment -func validateMachineDeployment(hubBefore conversion.Hub) error { - if len(hubBefore.(*clusterv1.MachineDeployment).Status.Conditions) == 0 { - return fmt.Errorf("No conditions were created") - } - if _, ok := hubBefore.(*clusterv1.MachineDeployment); ok { - for i, _ := range hubBefore.(*clusterv1.MachineDeployment).Status.Conditions { - cond := hubBefore.(*clusterv1.MachineDeployment).Status.Conditions[i] - if !isValidUtf8([]byte(cond.Message)) { - return fmt.Errorf("Invalid utf8") - } - if !isValidUtf8([]byte(cond.Type)) { - return fmt.Errorf("Invalid utf8") - } - if !isValidUtf8([]byte(cond.Reason)) { - return fmt.Errorf("Invalid utf8") - } - if !isValidUtf8([]byte(cond.Severity)) { - return fmt.Errorf("Invalid utf8") - } - if !isValidUtf8([]byte(cond.Status)) { - return fmt.Errorf("Invalid utf8") - } - } - } - return nil -} - -func validateClusterClass(hubBefore conversion.Hub) error { - // TODO: Randomize this - mu := hubBefore.(*clusterv1.ClusterClass).Spec.ControlPlane.MachineHealthCheck.MaxUnhealthy - mu.StrVal = "" - mu.Type = 2 - mu.IntVal = 1 - return nil -} - -// validates a cluster to prevent false positives. -func validateCluster(hubBefore conversion.Hub, f *fuzz.ConsumeFuzzer) error { - layout := "Mon, 01/02/06, 03:04PM" - timeStr := "Thu, 02/03/07, 04:05PM" - rolloutAfter, err := time.Parse(layout, timeStr) - if err != nil { - return err - } - newTime := metav1.NewTime(rolloutAfter) - - clusterTopology := hubBefore.(*clusterv1.Cluster).Spec.Topology - if clusterTopology == nil { - return fmt.Errorf("cp is nil") - } - clusterTopology.RolloutAfter = &newTime - - for _, md := range clusterTopology.Workers.MachineDeployments { - if len(md.Metadata.Labels) == 0 { - return fmt.Errorf("No labels created") - } - if len(md.Variables.Overrides) == 0 { - return fmt.Errorf("No overrides created") - } - } - - for _, v := range clusterTopology.Variables { - if len(v.Value.Raw) == 0 { - return fmt.Errorf("empty value") - } - } - - if len(clusterTopology.Variables) == 0 { - return fmt.Errorf("No value created") - } - - for _, md := range clusterTopology.Workers.MachineDeployments { - for _, o := range md.Variables.Overrides { - if len(o.Value.Raw) == 0 { - return fmt.Errorf("empty value") - } - } - } - - if len(clusterTopology.ControlPlane.Metadata.Labels) == 0 { - return fmt.Errorf("No controlplane labels created") - } - - if len(clusterTopology.ControlPlane.Metadata.Annotations) == 0 { - return fmt.Errorf("No controlplane annotations created") - } - return nil -} - -// sets a few fields of each type to prevent false positives. -func setCustomFieldsSpoke(spokeBefore conversion.Convertible) { - if _, ok := spokeBefore.(*Cluster); ok { - spokeBefore.(*Cluster).TypeMeta.Kind = "" - spokeBefore.(*Cluster).TypeMeta.APIVersion = "" - return - } - if _, ok := spokeBefore.(*MachineHealthCheck); ok { - spokeBefore.(*MachineHealthCheck).TypeMeta.Kind = "" - spokeBefore.(*MachineHealthCheck).TypeMeta.APIVersion = "" - return - } - if _, ok := spokeBefore.(*MachineSet); ok { - spokeBefore.(*MachineSet).TypeMeta.Kind = "" - spokeBefore.(*MachineSet).TypeMeta.APIVersion = "" - spokeBefore.(*MachineSet).Spec.Template.ObjectMeta.Name = "" - spokeBefore.(*MachineSet).Spec.Template.ObjectMeta.GenerateName = "" - spokeBefore.(*MachineSet).Spec.Template.ObjectMeta.Namespace = "" - spokeBefore.(*MachineSet).Spec.Template.ObjectMeta.OwnerReferences = nil - spokeBefore.(*MachineSet).Spec.Template.Spec.Bootstrap.Data = nil - return - } - if _, ok := spokeBefore.(*MachineDeployment); ok { - spokeBefore.(*MachineDeployment).TypeMeta.Kind = "" - spokeBefore.(*MachineDeployment).TypeMeta.APIVersion = "" - spokeBefore.(*MachineDeployment).Spec.Template.ObjectMeta.Name = "" - spokeBefore.(*MachineDeployment).Spec.Template.ObjectMeta.GenerateName = "" - spokeBefore.(*MachineDeployment).Spec.Template.ObjectMeta.Namespace = "" - spokeBefore.(*MachineDeployment).Spec.Template.ObjectMeta.OwnerReferences = nil - spokeBefore.(*MachineDeployment).Spec.Template.Spec.Bootstrap.Data = nil - return - } - if _, ok := spokeBefore.(*Machine); ok { - spokeBefore.(*Machine).TypeMeta.Kind = "" - spokeBefore.(*Machine).TypeMeta.APIVersion = "" - spokeBefore.(*Machine).Spec.Bootstrap.Data = nil - spokeBefore.(*Machine).Status.Version = nil - return - } -} - -// sets a few fields of each type to prevent false positives. -func setCustomFieldsHub(hubBefore conversion.Hub) { - if _, ok := hubBefore.(*clusterv1.Cluster); ok { - hubBefore.(*clusterv1.Cluster).TypeMeta.Kind = "" - hubBefore.(*clusterv1.Cluster).TypeMeta.APIVersion = "" - return - } - if _, ok := hubBefore.(*clusterv1.MachineHealthCheck); ok { - hubBefore.(*clusterv1.MachineHealthCheck).TypeMeta.Kind = "" - hubBefore.(*clusterv1.MachineHealthCheck).TypeMeta.APIVersion = "" - return - } - if _, ok := hubBefore.(*clusterv1.Machine); ok { - hubBefore.(*clusterv1.Machine).TypeMeta.Kind = "" - hubBefore.(*clusterv1.Machine).TypeMeta.APIVersion = "" - return - } - if _, ok := hubBefore.(*clusterv1.MachineSet); ok { - hubBefore.(*clusterv1.MachineSet).TypeMeta.Kind = "" - hubBefore.(*clusterv1.MachineSet).TypeMeta.APIVersion = "" - return - } - if _, ok := hubBefore.(*clusterv1.MachineDeployment); ok { - hubBefore.(*clusterv1.MachineDeployment).TypeMeta.Kind = "" - hubBefore.(*clusterv1.MachineDeployment).TypeMeta.APIVersion = "" - return - } -} - -func clusterSpokeAfterMutationFuzz(c conversion.Convertible) { - cluster := c.(*Cluster) - - // Create a temporary 0-length slice using the same underlying array as cluster.Status.Conditions to avoid - // allocations. - tmp := cluster.Status.Conditions[:0] - - for i := range cluster.Status.Conditions { - condition := cluster.Status.Conditions[i] - - // Keep everything that is not ControlPlaneInitializedCondition - if condition.Type != ConditionType(clusterv1.ControlPlaneInitializedCondition) { - tmp = append(tmp, condition) - } - } - - // Point cluster.Status.Conditions and our slice that does not have ControlPlaneInitializedCondition - cluster.Status.Conditions = tmp -} diff --git a/projects/cluster-api/conversion_fuzzer2.go b/projects/cluster-api/conversion_fuzzer2.go deleted file mode 100644 index e75ac900..00000000 --- a/projects/cluster-api/conversion_fuzzer2.go +++ /dev/null @@ -1,189 +0,0 @@ -// Copyright 2022 ADA Logics Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package fuzz - -import ( - fuzz "github.com/AdaLogics/go-fuzz-headers" - "sigs.k8s.io/controller-runtime/pkg/conversion" - - "sigs.k8s.io/cluster-api/api/v1alpha3" - "sigs.k8s.io/cluster-api/api/v1alpha4" - "sigs.k8s.io/cluster-api/api/v1beta1" - bootstrapv1alpha3 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3" - bootstrapv1alpha4 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4" - bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" - "sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/upstreamv1beta1" - "sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/upstreamv1beta2" - "sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/upstreamv1beta3" - controlplanev1alpha3 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha3" - controlplanev1alpha4 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha4" - controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" -) - -type FuzzTestFuncInput struct { - Hub conversion.Hub - Spoke conversion.Convertible -} - -func createInput(objType1, objType2 int) FuzzTestFuncInput { - input := &FuzzTestFuncInput{} - switch objType1 { - case 0: - input.Hub = &v1beta1.Cluster{} - case 1: - input.Hub = &v1beta1.ClusterList{} - case 2: - input.Hub = &v1beta1.Machine{} - case 3: - input.Hub = &v1beta1.MachineList{} - case 4: - input.Hub = &v1beta1.MachineSet{} - case 5: - input.Hub = &v1beta1.MachineSetList{} - case 6: - input.Hub = &v1beta1.MachineDeployment{} - case 7: - input.Hub = &v1beta1.MachineDeploymentList{} - case 8: - input.Hub = &v1beta1.MachineHealthCheck{} - case 9: - input.Hub = &v1beta1.MachineHealthCheckList{} - case 10: - input.Hub = &bootstrapv1.ClusterConfiguration{} - case 11: - input.Hub = &bootstrapv1.ClusterStatus{} - case 12: - input.Hub = &bootstrapv1.InitConfiguration{} - case 13: - input.Hub = &bootstrapv1.JoinConfiguration{} - case 14: - input.Hub = &controlplanev1.KubeadmControlPlane{} - } - - switch objType2 { - case 0: - input.Spoke = &v1alpha3.Cluster{} - case 1: - input.Spoke = &v1alpha3.ClusterList{} - case 2: - input.Spoke = &v1alpha3.Cluster{} - case 3: - input.Spoke = &v1alpha3.ClusterList{} - case 4: - input.Spoke = &v1alpha3.Machine{} - case 5: - input.Spoke = &v1alpha3.MachineList{} - case 6: - input.Spoke = &v1alpha3.MachineSet{} - case 7: - input.Spoke = &v1alpha3.MachineSetList{} - case 8: - input.Spoke = &v1alpha3.MachineDeployment{} - case 9: - input.Spoke = &v1alpha3.MachineDeploymentList{} - case 10: - input.Spoke = &v1alpha3.MachineHealthCheck{} - case 11: - input.Spoke = &v1alpha3.MachineHealthCheckList{} - case 12: - input.Spoke = &v1alpha4.Cluster{} - case 13: - input.Spoke = &v1alpha4.ClusterList{} - case 14: - input.Spoke = &v1alpha4.ClusterClass{} - case 15: - input.Spoke = &v1alpha4.ClusterClassList{} - case 16: - input.Spoke = &v1alpha4.Machine{} - case 17: - input.Spoke = &v1alpha4.MachineList{} - case 18: - input.Spoke = &v1alpha4.MachineSetList{} - case 19: - input.Spoke = &v1alpha4.MachineDeployment{} - case 20: - input.Spoke = &v1alpha4.MachineDeploymentList{} - case 21: - input.Spoke = &v1alpha4.MachineHealthCheck{} - case 22: - input.Spoke = &v1alpha4.MachineHealthCheckList{} - case 23: - input.Spoke = &bootstrapv1alpha3.KubeadmConfig{} - case 24: - input.Spoke = &bootstrapv1alpha3.KubeadmConfigTemplate{} - case 25: - input.Spoke = &bootstrapv1alpha4.KubeadmConfig{} - case 26: - input.Spoke = &upstreamv1beta2.ClusterConfiguration{} - case 27: - input.Spoke = &upstreamv1beta2.ClusterStatus{} - case 28: - input.Spoke = &upstreamv1beta2.InitConfiguration{} - case 29: - input.Spoke = &upstreamv1beta2.JoinConfiguration{} - case 30: - input.Spoke = &upstreamv1beta1.ClusterConfiguration{} - case 31: - input.Spoke = &upstreamv1beta1.ClusterStatus{} - case 32: - input.Spoke = &upstreamv1beta1.InitConfiguration{} - case 33: - input.Spoke = &upstreamv1beta1.JoinConfiguration{} - case 34: - input.Spoke = &upstreamv1beta3.ClusterConfiguration{} - case 35: - input.Spoke = &upstreamv1beta3.InitConfiguration{} - case 36: - input.Spoke = &upstreamv1beta3.JoinConfiguration{} - case 37: - input.Spoke = &controlplanev1alpha3.KubeadmControlPlane{} - case 38: - input.Spoke = &controlplanev1alpha4.KubeadmControlPlane{} - case 39: - input.Spoke = &controlplanev1alpha4.KubeadmControlPlaneTemplate{} - } - return *input -} - -// Calls ConvertFrom() and ConvertTo() with valid -// conversion.Convertible and conversion.Hub -func FuzzConversionOfAllTypes(data []byte) int { - f := fuzz.NewConsumer(data) - objType1, err := f.GetInt() - if err != nil { - return 0 - } - - objType2, err := f.GetInt() - if err != nil { - return 0 - } - input := createInput(objType1%15, objType2%40) - - err = f.GenerateStruct(input.Hub) - if err != nil { - return 0 - } - - err = f.GenerateStruct(input.Spoke) - if err != nil { - return 0 - } - - input.Spoke.ConvertTo(input.Hub) - input.Spoke.ConvertFrom(input.Hub) - return 1 -} diff --git a/projects/cluster-api/crd-creation/main.go b/projects/cluster-api/crd-creation/main.go deleted file mode 100644 index 636b57f0..00000000 --- a/projects/cluster-api/crd-creation/main.go +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright 2022 ADA Logics Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package main - -import ( - "fmt" - "os" - "path/filepath" - "strings" -) - -func walkDir(dirPath string, fileMap map[string]string) error { - err := filepath.Walk(dirPath, - func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if !info.IsDir() { - dat, err := os.ReadFile(path) - if err != nil { - panic(err) - } - escapedString := strings.Replace(string(dat), "`", "", -1) - fileMap[path] = escapedString - } - return nil - }) - return err -} - -// Checks whether the directory of a filename exists. -// If it doesn't exists, then it is created. -func checkOrCreateDir(fileNamePath string) error { - path := filepath.Dir(fileNamePath) - if _, err := os.Stat(path); os.IsNotExist(err) { - err := os.MkdirAll(path, 0755) - if err != nil { - return err - } - } - return nil -} - -func createFile(fileMap map[string]string) error { - var builder strings.Builder - builder.WriteString("package envtest\n\n") - builder.WriteString("var CrdMap = map[string]string{") - - for k, v := range fileMap { - k2 := strings.Replace(k, "/src/cluster-api", ".", -1) - builder.WriteString("\"" + k2 + "\":`" + v + "`,\n") - //fmt.Println(filepath.Dir(k)) - } - builder.WriteString("}\n") - f, err := os.Create("/src/cluster-api/internal/test/envtest/static_crds.go") - if err != nil { - panic(err) - } - - defer f.Close() - _, err = f.WriteString(builder.String()) - if err != nil { - panic(err) - } - return nil -} - -func compareFileMaps(fileMap map[string]string) { - /*for k, v := range fileMap { - if crdMap[k] != fileMap[k] { - panic("they should be similar") - } - }*/ -} - -func main() { - dirPaths := []string{"/src/cluster-api/config/crd/bases", - "/src/cluster-api/controlplane/kubeadm/config/crd/bases", - "/src/cluster-api/bootstrap/kubeadm/config/crd/bases", - "/src/cluster-api/config/webhook", - "/src/cluster-api/bootstrap/kubeadm/config/webhook", - "/src/cluster-api/controlplane/kubeadm/config/webhook", - } - - fileMap := make(map[string]string) - for _, dirPath := range dirPaths { - err := walkDir(dirPath, fileMap) - if err != nil { - panic(err) - } - } - err := createFile(fileMap) - if err != nil { - panic(err) - } - fmt.Println("Created static crds") - -} diff --git a/projects/cluster-api/internal_kubeadm_controller_fuzzer.go b/projects/cluster-api/internal_kubeadm_controller_fuzzer.go deleted file mode 100644 index 02c263f9..00000000 --- a/projects/cluster-api/internal_kubeadm_controller_fuzzer.go +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright 2022 ADA Logics Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package controllers - -import ( - "context" - "encoding/json" - "fmt" - - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" - controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" - "sigs.k8s.io/cluster-api/internal/test/envtest" - "sigs.k8s.io/cluster-api/util/collections" - //ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/yaml" - "strings" - - fuzz "github.com/AdaLogics/go-fuzz-headers" -) - -var ( - env *envtest.Environment - //ctx = ctrl.SetupSignalHandler() - ctx, _ = context.WithCancel(context.Background()) - fakeGenericMachineTemplateCRD = &apiextensionsv1.CustomResourceDefinition{ - TypeMeta: metav1.TypeMeta{ - APIVersion: apiextensionsv1.SchemeGroupVersion.String(), - Kind: "CustomResourceDefinition", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "genericmachinetemplate.generic.io", - Labels: map[string]string{ - "cluster.x-k8s.io/v1beta1": "v1", - }, - }, - Spec: apiextensionsv1.CustomResourceDefinitionSpec{ - Group: "generic.io", - Names: apiextensionsv1.CustomResourceDefinitionNames{ - Kind: "GenericMachineTemplate", - }, - }, - } -) - -func FuzzKubeadmControlPlaneReconciler(data []byte) int { - f := fuzz.NewConsumer(data) - cluster, kcp, tmpl, err := createClusterWithControlPlaneFuzz(f) - if err != nil { - return 0 - } - if tmpl == nil { - return 0 - } - objs := []client.Object{fakeGenericMachineTemplateCRD, cluster.DeepCopy(), kcp.DeepCopy(), tmpl.DeepCopy()} - - m := &clusterv1.Machine{} - err = f.GenerateStruct(m) - if err != nil { - return 0 - } - cfg := &bootstrapv1.KubeadmConfig{} - err = f.GenerateStruct(cfg) - if err != nil { - return 0 - } - objs = append(objs, m, cfg) - fmc := &fakeManagementCluster{ - Machines: collections.Machines{}, - Workload: fakeWorkloadCluster{}, - } - fmc.Machines.Insert(m) - fakeClient := newFakeClient(objs...) - fmc.Reader = fakeClient - - r := &KubeadmControlPlaneReconciler{ - Client: fakeClient, - APIReader: fakeClient, - managementCluster: fmc, - managementClusterUncached: fmc, - } - fmt.Println("calling reconcile") - _, err = r.reconcile(ctx, cluster, kcp) - return 1 -} - -func createClusterWithControlPlaneFuzz(f *fuzz.ConsumeFuzzer) (*clusterv1.Cluster, *controlplanev1.KubeadmControlPlane, *unstructured.Unstructured, error) { - cluster := &clusterv1.Cluster{} - err := f.GenerateStruct(cluster) - if err != nil { - return nil, nil, nil, err - } - - kcp := &controlplanev1.KubeadmControlPlane{} - err = f.GenerateStruct(kcp) - if err != nil { - return nil, nil, nil, err - } - - unstructuredStr, err := f.GetString() - if err != nil { - return nil, nil, nil, err - } - unstr, err := UnstructuredForFuzzing(unstructuredStr) - if err != nil { - return nil, nil, nil, err - } - - return cluster, kcp, unstr, nil -} - -func UnstructuredForFuzzing(text string) (*unstructured.Unstructured, error) { - un := &unstructured.Unstructured{} - var err error - if strings.HasPrefix(text, "{") { - err = json.Unmarshal([]byte(text), &un) - } else { - err = yaml.Unmarshal([]byte(text), &un) - } - if err != nil { - return nil, err - } - return un, nil -} diff --git a/projects/cluster-api/kubeadm_internal_fuzzer.go b/projects/cluster-api/kubeadm_internal_fuzzer.go deleted file mode 100644 index d33d6a48..00000000 --- a/projects/cluster-api/kubeadm_internal_fuzzer.go +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright 2022 ADA Logics Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package internal - -import ( - "encoding/json" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" - controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" - "sigs.k8s.io/yaml" - "strings" - - fuzz "github.com/AdaLogics/go-fuzz-headers" -) - -func FuzzMatchesMachineSpec(data []byte) int { - f := fuzz.NewConsumer(data) - machineConfigs := make(map[string]*bootstrapv1.KubeadmConfig) - err := f.FuzzMap(&machineConfigs) - if err != nil { - return 0 - } - kcp := &controlplanev1.KubeadmControlPlane{} - err = f.GenerateStruct(kcp) - if err != nil { - return 0 - } - infraConfigs, err := createInfraConfigs(f) - if err != nil { - return 0 - } - _ = MatchesMachineSpec(infraConfigs, machineConfigs, kcp) - return 1 -} - -func createInfraConfigs(f *fuzz.ConsumeFuzzer) (map[string]*unstructured.Unstructured, error) { - numberOfKeys, err := f.GetInt() - if err != nil { - return nil, err - } - infraConfigs := make(map[string]*unstructured.Unstructured) - for i := 0; i < numberOfKeys%10; i++ { - key, err := f.GetString() - if err != nil { - return nil, err - } - valStr, err := f.GetString() - if err != nil { - return nil, err - } - val, err := UnstructuredForFuzzing(valStr) - if err != nil { - return nil, err - } - infraConfigs[key] = val - } - return infraConfigs, nil -} - -func UnstructuredForFuzzing(text string) (*unstructured.Unstructured, error) { - un := &unstructured.Unstructured{} - var err error - if strings.HasPrefix(text, "{") { - err = json.Unmarshal([]byte(text), &un) - } else { - err = yaml.Unmarshal([]byte(text), &un) - } - if err != nil { - return nil, err - } - return un, nil -} diff --git a/projects/cluster-api/machine_controller_fuzzer.go b/projects/cluster-api/machine_controller_fuzzer.go deleted file mode 100644 index 07c38477..00000000 --- a/projects/cluster-api/machine_controller_fuzzer.go +++ /dev/null @@ -1,97 +0,0 @@ -package machine - -import ( - "context" - "fmt" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "sigs.k8s.io/cluster-api/internal/test/builder" - corev1 "k8s.io/api/core/v1" - "sigs.k8s.io/cluster-api/util" - "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - "github.com/ghodss/yaml" - fuzz "github.com/AdaLogics/go-fuzz-headers" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/apimachinery/pkg/runtime" - clientgoscheme "k8s.io/client-go/kubernetes/scheme" -) - -var ( - fuzzCtx = context.Background() - fakeSchemeForFuzzing = runtime.NewScheme() -) - -func init() { - _ = clientgoscheme.AddToScheme(fakeSchemeForFuzzing) - _ = clusterv1.AddToScheme(fakeSchemeForFuzzing) - _ = apiextensionsv1.AddToScheme(fakeSchemeForFuzzing) - _ = corev1.AddToScheme(fakeSchemeForFuzzing) -} - -// helper function to crate an unstructured object. -func GetUnstructured(f *fuzz.ConsumeFuzzer) (*unstructured.Unstructured, error) { - yamlStr, err := f.GetString() - if err != nil { - return nil, err - } - obj := make(map[string]interface{}) - err = yaml.Unmarshal([]byte(yamlStr), &obj) - if err != nil { - return nil, err - } - return &unstructured.Unstructured{Object: obj}, nil -} - - -func validateUnstructured(unstr *unstructured.Unstructured) error { - if _, ok := unstr.Object["kind"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["apiVersion"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["spec"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["status"]; !ok { - return fmt.Errorf("invalid unstr") - } - return nil -} - -func FuzzMachineReconcile(data []byte) int { - f := fuzz.NewConsumer(data) - unstr, err := GetUnstructured(f) - if err != nil { - return 0 - } - err = validateUnstructured(unstr) - if err != nil { - return 0 - } - cluster := &clusterv1.Machine{} - err = f.GenerateStruct(cluster) - if err != nil { - return 0 - } - node := &corev1.Node{} - err = f.GenerateStruct(node) - if err != nil { - return 0 - } - clientFake := fake.NewClientBuilder().WithScheme(fakeSchemeForFuzzing).WithObjects( - cluster, - node, - unstr, - builder.GenericInfrastructureMachineCRD.DeepCopy(), - ).Build() - r := &Reconciler{ - Client: clientFake, - APIReader: clientFake, - } - - _, _ = r.Reconcile(fuzzCtx, reconcile.Request{NamespacedName: util.ObjectKey(cluster)}) - return 1 -} diff --git a/projects/cluster-api/machinedeployment_controller_fuzzer.go b/projects/cluster-api/machinedeployment_controller_fuzzer.go deleted file mode 100644 index ed02b7cd..00000000 --- a/projects/cluster-api/machinedeployment_controller_fuzzer.go +++ /dev/null @@ -1,97 +0,0 @@ -package machinedeployment - -import ( - "context" - "fmt" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "sigs.k8s.io/cluster-api/internal/test/builder" - corev1 "k8s.io/api/core/v1" - "sigs.k8s.io/cluster-api/util" - "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - "github.com/ghodss/yaml" - fuzz "github.com/AdaLogics/go-fuzz-headers" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/apimachinery/pkg/runtime" - clientgoscheme "k8s.io/client-go/kubernetes/scheme" -) - -var ( - fuzzCtx = context.Background() - fakeSchemeForFuzzing = runtime.NewScheme() -) - -func init() { - _ = clientgoscheme.AddToScheme(fakeSchemeForFuzzing) - _ = clusterv1.AddToScheme(fakeSchemeForFuzzing) - _ = apiextensionsv1.AddToScheme(fakeSchemeForFuzzing) - _ = corev1.AddToScheme(fakeSchemeForFuzzing) -} - -// helper function to crate an unstructured object. -func GetUnstructured(f *fuzz.ConsumeFuzzer) (*unstructured.Unstructured, error) { - yamlStr, err := f.GetString() - if err != nil { - return nil, err - } - obj := make(map[string]interface{}) - err = yaml.Unmarshal([]byte(yamlStr), &obj) - if err != nil { - return nil, err - } - return &unstructured.Unstructured{Object: obj}, nil -} - - -func validateUnstructured(unstr *unstructured.Unstructured) error { - if _, ok := unstr.Object["kind"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["apiVersion"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["spec"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["status"]; !ok { - return fmt.Errorf("invalid unstr") - } - return nil -} - -func FuzzMachineDeploymentReconcile(data []byte) int { - f := fuzz.NewConsumer(data) - unstr, err := GetUnstructured(f) - if err != nil { - return 0 - } - err = validateUnstructured(unstr) - if err != nil { - return 0 - } - cluster := &clusterv1.MachineDeployment{} - err = f.GenerateStruct(cluster) - if err != nil { - return 0 - } - node := &corev1.Node{} - err = f.GenerateStruct(node) - if err != nil { - return 0 - } - clientFake := fake.NewClientBuilder().WithScheme(fakeSchemeForFuzzing).WithObjects( - cluster, - node, - node, - builder.GenericInfrastructureMachineCRD.DeepCopy(), - ).Build() - r := &Reconciler{ - Client: clientFake, - APIReader: clientFake, - } - - _, _ = r.Reconcile(fuzzCtx, reconcile.Request{NamespacedName: util.ObjectKey(cluster)}) - return 1 -} \ No newline at end of file diff --git a/projects/cluster-api/machinehealthcheck_controller_fuzzer.go b/projects/cluster-api/machinehealthcheck_controller_fuzzer.go deleted file mode 100644 index 24e47c5d..00000000 --- a/projects/cluster-api/machinehealthcheck_controller_fuzzer.go +++ /dev/null @@ -1,96 +0,0 @@ -package machinehealthcheck - -import ( - "context" - "fmt" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "sigs.k8s.io/cluster-api/internal/test/builder" - corev1 "k8s.io/api/core/v1" - "sigs.k8s.io/cluster-api/util" - "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - "github.com/ghodss/yaml" - fuzz "github.com/AdaLogics/go-fuzz-headers" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/apimachinery/pkg/runtime" - clientgoscheme "k8s.io/client-go/kubernetes/scheme" -) - -var ( - fuzzCtx = context.Background() - fakeSchemeForFuzzing = runtime.NewScheme() -) - -func init() { - _ = clientgoscheme.AddToScheme(fakeSchemeForFuzzing) - _ = clusterv1.AddToScheme(fakeSchemeForFuzzing) - _ = apiextensionsv1.AddToScheme(fakeSchemeForFuzzing) - _ = corev1.AddToScheme(fakeSchemeForFuzzing) -} - -// helper function to crate an unstructured object. -func GetUnstructured(f *fuzz.ConsumeFuzzer) (*unstructured.Unstructured, error) { - yamlStr, err := f.GetString() - if err != nil { - return nil, err - } - obj := make(map[string]interface{}) - err = yaml.Unmarshal([]byte(yamlStr), &obj) - if err != nil { - return nil, err - } - return &unstructured.Unstructured{Object: obj}, nil -} - - -func validateUnstructured(unstr *unstructured.Unstructured) error { - if _, ok := unstr.Object["kind"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["apiVersion"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["spec"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["status"]; !ok { - return fmt.Errorf("invalid unstr") - } - return nil -} - -func FuzzMachineHealthCheckReconcile(data []byte) int { - f := fuzz.NewConsumer(data) - unstr, err := GetUnstructured(f) - if err != nil { - return 0 - } - err = validateUnstructured(unstr) - if err != nil { - return 0 - } - mhc := &clusterv1.MachineHealthCheck{} - err = f.GenerateStruct(mhc) - if err != nil { - return 0 - } - node := &corev1.Node{} - err = f.GenerateStruct(node) - if err != nil { - return 0 - } - clientFake := fake.NewClientBuilder().WithScheme(fakeSchemeForFuzzing).WithObjects( - mhc, - node, - unstr, - builder.GenericInfrastructureMachineCRD.DeepCopy(), - ).Build() - r := &Reconciler{ - Client: clientFake, - } - - _, _ = r.Reconcile(fuzzCtx, reconcile.Request{NamespacedName: util.ObjectKey(mhc)}) - return 1 -} diff --git a/projects/cluster-api/machineset_controller_fuzzer.go b/projects/cluster-api/machineset_controller_fuzzer.go deleted file mode 100644 index 29ec5028..00000000 --- a/projects/cluster-api/machineset_controller_fuzzer.go +++ /dev/null @@ -1,97 +0,0 @@ -package machineset - -import ( - "context" - "fmt" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "sigs.k8s.io/cluster-api/internal/test/builder" - corev1 "k8s.io/api/core/v1" - "sigs.k8s.io/cluster-api/util" - "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - "github.com/ghodss/yaml" - fuzz "github.com/AdaLogics/go-fuzz-headers" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/apimachinery/pkg/runtime" - clientgoscheme "k8s.io/client-go/kubernetes/scheme" -) - -var ( - fuzzCtx = context.Background() - fakeSchemeForFuzzing = runtime.NewScheme() -) - -func init() { - _ = clientgoscheme.AddToScheme(fakeSchemeForFuzzing) - _ = clusterv1.AddToScheme(fakeSchemeForFuzzing) - _ = apiextensionsv1.AddToScheme(fakeSchemeForFuzzing) - _ = corev1.AddToScheme(fakeSchemeForFuzzing) -} - -// helper function to crate an unstructured object. -func GetUnstructured(f *fuzz.ConsumeFuzzer) (*unstructured.Unstructured, error) { - yamlStr, err := f.GetString() - if err != nil { - return nil, err - } - obj := make(map[string]interface{}) - err = yaml.Unmarshal([]byte(yamlStr), &obj) - if err != nil { - return nil, err - } - return &unstructured.Unstructured{Object: obj}, nil -} - - -func validateUnstructured(unstr *unstructured.Unstructured) error { - if _, ok := unstr.Object["kind"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["apiVersion"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["spec"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["status"]; !ok { - return fmt.Errorf("invalid unstr") - } - return nil -} - -func FuzzMachinesetReconcile(data []byte) int { - f := fuzz.NewConsumer(data) - unstr, err := GetUnstructured(f) - if err != nil { - return 0 - } - err = validateUnstructured(unstr) - if err != nil { - return 0 - } - machineset := &clusterv1.MachineSet{} - err = f.GenerateStruct(machineset) - if err != nil { - return 0 - } - node := &corev1.Node{} - err = f.GenerateStruct(node) - if err != nil { - return 0 - } - clientFake := fake.NewClientBuilder().WithScheme(fakeSchemeForFuzzing).WithObjects( - machineset, - node, - unstr, - builder.GenericInfrastructureMachineCRD.DeepCopy(), - ).Build() - r := &Reconciler{ - Client: clientFake, - APIReader: clientFake, - } - - _, _ = r.Reconcile(fuzzCtx, reconcile.Request{NamespacedName: util.ObjectKey(machineset)}) - return 1 -} \ No newline at end of file diff --git a/projects/cluster-api/non-oss-fuzz/Dockerfile b/projects/cluster-api/non-oss-fuzz/Dockerfile deleted file mode 100644 index 4d83f7f9..00000000 --- a/projects/cluster-api/non-oss-fuzz/Dockerfile +++ /dev/null @@ -1,13 +0,0 @@ -FROM gcr.io/oss-fuzz-base/base-builder-go -RUN git clone --depth 1 https://github.com/kubernetes-sigs/cluster-api -RUN cd $SRC && go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest -RUN git clone --depth 1 --branch cluster-api https://github.com/AdamKorcz/cncf-fuzzing $SRC/cncf-fuzzing -RUN mv $SRC/cncf-fuzzing/projects/cluster-api/non-oss-fuzz/build.sh $SRC/ -WORKDIR $SRC/cluster-api -ENV FUZZING_LANGUAGE go -ENV FUZZER fuzz_cluster_controller -ENV FUZZ_TIME 600 -RUN compile -RUN mkdir -p /usr/local/kubebuilder/bin -CMD ["sh", "-c", "export KUBEBUILDER_BINARIES=$(setup-envtest use -p path) && mv ${KUBEBUILDER_BINARIES}/* /usr/local/kubebuilder/bin/ && /out/${FUZZER} -max_total_time=${FUZZ_TIME}"] - diff --git a/projects/cluster-api/non-oss-fuzz/build.sh b/projects/cluster-api/non-oss-fuzz/build.sh deleted file mode 100755 index ccd06bc8..00000000 --- a/projects/cluster-api/non-oss-fuzz/build.sh +++ /dev/null @@ -1,24 +0,0 @@ -# Setup CRDs for the controller fuzzers -cd $SRC/cncf-fuzzing/projects/cluster-api/crd-creation -go mod init create-fuzz-env -go mod tidy -go run main.go - -cd $SRC/cluster-api -sed -i 's/root := path.Join(path.Dir(filename), "..", "..", "..")/root := "."/g' $SRC/cluster-api/internal/test/envtest/environment.go -sed -i '120 a code := 1' $SRC/cluster-api/internal/test/envtest/environment.go -sed -i 's/code := input.M.Run()/return 1/g' $SRC/cluster-api/internal/test/envtest/environment.go -sed -i 's/"path"/\/\/"path"/g' $SRC/cluster-api/internal/test/envtest/environment.go -sed -i 's/_, filename, _, _/\/\/_, filename, _, _/g' $SRC/cluster-api/internal/test/envtest/environment.go -#sed -i 's/err = kerrors.NewAggregate([]error{err, env.Stop()})/\/\/err = kerrors.NewAggregate([]error{err, env.Stop()})/g' $SRC/cluster-api/internal/test/envtest/environment.go - -sed -i 's/root := path.Join(path.Dir(filename), "..", "..", "..")/root := "."/g' $SRC/cluster-api/internal/test/envtest/webhooks.go -sed -i 's/"path"/\/\/"path"/g' $SRC/cluster-api/internal/test/envtest/webhooks.go -sed -i 's/_, filename, _, _/\/\/_, filename, _, _/g' $SRC/cluster-api/internal/test/envtest/webhooks.go -sed -i 's/goruntime "runtime"/\/\/goruntime "runtime"/g' $SRC/cluster-api/internal/test/envtest/webhooks.go - - -cp $SRC/cncf-fuzzing/projects/cluster-api/cluster_controller_fuzzer.go \ - $SRC/cluster-api/internal/controllers/cluster/ -compile_go_fuzzer sigs.k8s.io/cluster-api/internal/controllers/cluster FuzzClusterController fuzz_cluster_controller -exit 0 \ No newline at end of file diff --git a/projects/cluster-api/non-oss-fuzz/cluster_controller_fuzzer.go b/projects/cluster-api/non-oss-fuzz/cluster_controller_fuzzer.go deleted file mode 100644 index de5625e5..00000000 --- a/projects/cluster-api/non-oss-fuzz/cluster_controller_fuzzer.go +++ /dev/null @@ -1,183 +0,0 @@ -// Copyright 2022 ADA Logics Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package cluster - -import ( - "context" - "fmt" - "os" - "path/filepath" - "sync" - "testing" - "time" - - // +kubebuilder:scaffold:imports - . "github.com/onsi/gomega" - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/apimachinery/pkg/runtime" - clientgoscheme "k8s.io/client-go/kubernetes/scheme" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/controller" - - fuzz "github.com/AdaLogics/go-fuzz-headers" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - "sigs.k8s.io/cluster-api/api/v1beta1/index" - "sigs.k8s.io/cluster-api/controllers/remote" - machinecontroller "sigs.k8s.io/cluster-api/internal/controllers/machine" - "sigs.k8s.io/cluster-api/internal/test/envtest" -) - -const ( - timeout = time.Second * 30 - clusterReconcileNamespaceFuzz = "test-cluster-reconcile" -) - -var ( - env *envtest.Environment - ctx = ctrl.SetupSignalHandler() - fakeScheme = runtime.NewScheme() - fuzzInitter sync.Once -) - -// Checks whether the directory of a filename exists. -// If it doesn't exists, then it is created. -func createFile(fileNamePath, fileContents string) error { - path := filepath.Dir(fileNamePath) - if _, err := os.Stat(path); os.IsNotExist(err) { - err := os.MkdirAll(path, 0755) - if err != nil { - return err - } - } - f, err := os.Create(fileNamePath) - if err != nil { - return err - } - defer f.Close() - _, err = f.WriteString(fileContents) - if err != nil { - return err - } - return nil -} - -func createCrdFiles() { - crds := envtest.CrdMap - - for k, v := range crds { - err := createFile(k, v) - if err != nil { - panic(err) - } - } -} - -func fuzzInit() { - createCrdFiles() - _ = clientgoscheme.AddToScheme(fakeScheme) - _ = clusterv1.AddToScheme(fakeScheme) - _ = apiextensionsv1.AddToScheme(fakeScheme) - TestMain() -} - -func TestMain() { - m := &testing.M{} - setupIndexes := func(ctx context.Context, mgr ctrl.Manager) { - if err := index.AddDefaultIndexes(ctx, mgr); err != nil { - panic(fmt.Sprintf("unable to setup index: %v", err)) - } - } - - setupReconcilers := func(ctx context.Context, mgr ctrl.Manager) { - // Set up a ClusterCacheTracker and ClusterCacheReconciler to provide to controllers - // requiring a connection to a remote cluster - log := ctrl.Log.WithName("remote").WithName("ClusterCacheTracker") - tracker, err := remote.NewClusterCacheTracker( - mgr, - remote.ClusterCacheTrackerOptions{ - Log: &log, - Indexes: remote.DefaultIndexes, - }, - ) - if err != nil { - panic(fmt.Sprintf("unable to create cluster cache tracker: %v", err)) - } - if err := (&remote.ClusterCacheReconciler{ - Client: mgr.GetClient(), - Log: ctrl.Log.WithName("remote").WithName("ClusterCacheReconciler"), - Tracker: tracker, - }).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil { - panic(fmt.Sprintf("Failed to start ClusterCacheReconciler: %v", err)) - } - if err := (&Reconciler{ - Client: mgr.GetClient(), - APIReader: mgr.GetClient(), - }).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil { - panic(fmt.Sprintf("Failed to start ClusterReconciler: %v", err)) - } - if err := (&machinecontroller.Reconciler{ - Client: mgr.GetClient(), - APIReader: mgr.GetAPIReader(), - Tracker: tracker, - }).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil { - panic(fmt.Sprintf("Failed to start MachineReconciler: %v", err)) - } - } - - SetDefaultEventuallyPollingInterval(100 * time.Millisecond) - SetDefaultEventuallyTimeout(timeout) - - envtest.Run(ctx, envtest.RunInput{ - M: m, - SetupEnv: func(e *envtest.Environment) { env = e }, - SetupIndexes: setupIndexes, - SetupReconcilers: setupReconcilers, - }) -} - -// This fuzzer does not run on OSS-fuzz because the kubebuilder -// binaries are not available in the OSS-fuzz runtime environement. -func FuzzClusterController(data []byte) int { - fuzzInitter.Do(fuzzInit) - f := fuzz.NewConsumer(data) - instance := &clusterv1.Cluster{} - err := f.GenerateStruct(instance) - if err != nil { - return 0 - } - - ns, err := env.CreateNamespace(ctx, clusterReconcileNamespaceFuzz) - if err != nil { - return 0 - } - defer func() { - if err := env.Delete(ctx, ns); err != nil { - panic(err) - } - }() - - err = env.Create(ctx, instance) - if err != nil { - return 0 - } - defer func() { - err := env.Delete(ctx, instance) - if err != nil { - panic(err) - } - }() - return 1 -} diff --git a/projects/cluster-api/non-oss-fuzz/run_fuzzer.sh b/projects/cluster-api/non-oss-fuzz/run_fuzzer.sh deleted file mode 100755 index 8a31861d..00000000 --- a/projects/cluster-api/non-oss-fuzz/run_fuzzer.sh +++ /dev/null @@ -1,2 +0,0 @@ -sudo docker build -t cluster_controller_fuzzer . -sudo docker run cluster_controller_fuzzer \ No newline at end of file diff --git a/projects/cluster-api/patch_fuzzer.go b/projects/cluster-api/patch_fuzzer.go deleted file mode 100644 index d9ac1838..00000000 --- a/projects/cluster-api/patch_fuzzer.go +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright 2022 ADA Logics Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package patch - -import ( - fuzz "github.com/AdaLogics/go-fuzz-headers" - appsv1 "k8s.io/api/apps/v1" - corev1 "k8s.io/api/core/v1" - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/kubernetes/scheme" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/fake" -) - -var ( - fuzzCtx = ctrl.SetupSignalHandler() - fakeScheme = runtime.NewScheme() - objTypes = map[int]string{ - 0: "Node", - 1: "Machine", - 2: "Deployment", - } - objects = []client.Object{&appsv1.Deployment{}, - &clusterv1.Machine{}, - &corev1.Node{}, - &addonsv1.ClusterResourceSet{}, - &addonsv1.ClusterResourceSetBinding{}, - &clusterv1.Cluster{}, - &clusterv1.ClusterClass{}, - &clusterv1.MachineSet{}} -) - -func init() { - _ = scheme.AddToScheme(fakeScheme) - _ = clusterv1.AddToScheme(fakeScheme) - _ = apiextensionsv1.AddToScheme(fakeScheme) - _ = addonsv1.AddToScheme(fakeScheme) -} - -func FuzzPatch(data []byte) int { - //var obj client.Object - f := fuzz.NewConsumer(data) - objType, err := f.GetInt() - if err != nil { - return 0 - } - obj := objects[objType%len(objects)] - err = f.GenerateStruct(obj) - if err != nil { - return 0 - } - patcher, err := NewHelper(obj, fake.NewClientBuilder(). - WithScheme(fakeScheme). - WithObjects(obj). - Build()) - if err != nil { - return 0 - } - err = f.GenerateStruct(obj) - if err != nil { - return 0 - } - _ = patcher.Patch(fuzzCtx, obj) - if err != nil { - return 0 - } - return 1 -} diff --git a/projects/cluster-api/topology_cluster_reconciler_fuzzer.go b/projects/cluster-api/topology_cluster_reconciler_fuzzer.go deleted file mode 100644 index 5b9fb7b5..00000000 --- a/projects/cluster-api/topology_cluster_reconciler_fuzzer.go +++ /dev/null @@ -1,132 +0,0 @@ -// Copyright 2022 ADA Logics Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package cluster - -import ( - "context" - "fmt" - - "github.com/ghodss/yaml" - corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - //ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - "sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/scope" - "sigs.k8s.io/cluster-api/internal/test/builder" - //"sigs.k8s.io/cluster-api/internal/test/envtest" - "sigs.k8s.io/cluster-api/util" - "sigs.k8s.io/controller-runtime/pkg/reconcile" - - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/apimachinery/pkg/runtime" - clientgoscheme "k8s.io/client-go/kubernetes/scheme" - - "sync" - - fuzz "github.com/AdaLogics/go-fuzz-headers" -) - -var ( - fakeSchemeForFuzzing = runtime.NewScheme() - //env *envtest.Environment - //ctx = ctrl.SetupSignalHandler() - fuzzCtx = context.Background() - initter sync.Once -) - -func initFunc() { - _ = clientgoscheme.AddToScheme(fakeSchemeForFuzzing) - _ = clusterv1.AddToScheme(fakeSchemeForFuzzing) - _ = apiextensionsv1.AddToScheme(fakeSchemeForFuzzing) - _ = corev1.AddToScheme(fakeSchemeForFuzzing) -} - -// helper function to crate an unstructured object. -func GetUnstructured(f *fuzz.ConsumeFuzzer) (*unstructured.Unstructured, error) { - yamlStr, err := f.GetString() - if err != nil { - return nil, err - } - obj := make(map[string]interface{}) - err = yaml.Unmarshal([]byte(yamlStr), &obj) - if err != nil { - return nil, err - } - return &unstructured.Unstructured{Object: obj}, nil -} - -func validateBlueprint(b *scope.ClusterBlueprint) error { - if b.ClusterClass == nil { - return fmt.Errorf("ClusterClass is nil") - } - if b.ClusterClass.Spec.ControlPlane.MachineInfrastructure == nil { - return fmt.Errorf("ClusterClass.Spec.ControlPlane.MachineInfrastructure is nil") - } - return nil -} - -func validateUnstructured(unstr *unstructured.Unstructured) error { - if _, ok := unstr.Object["kind"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["apiVersion"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["spec"]; !ok { - return fmt.Errorf("invalid unstr") - } - if _, ok := unstr.Object["status"]; !ok { - return fmt.Errorf("invalid unstr") - } - return nil -} - -func FuzzClusterReconcile(data []byte) int { - f := fuzz.NewConsumer(data) - unstr, err := GetUnstructured(f) - if err != nil { - return 0 - } - err = validateUnstructured(unstr) - if err != nil { - return 0 - } - cluster := &clusterv1.Cluster{} - err = f.GenerateStruct(cluster) - if err != nil { - return 0 - } - node := &corev1.Node{} - err = f.GenerateStruct(node) - if err != nil { - return 0 - } - clientFake := fake.NewClientBuilder().WithObjects( - node, - cluster, - builder.GenericInfrastructureMachineCRD.DeepCopy(), - unstr, - ).Build() - r := &Reconciler{ - Client: clientFake, - APIReader: clientFake, - } - - _, _ = r.Reconcile(fuzzCtx, reconcile.Request{NamespacedName: util.ObjectKey(cluster)}) - return 1 -} diff --git a/projects/cluster-api/util_container_fuzzer.go b/projects/cluster-api/util_container_fuzzer.go deleted file mode 100644 index 0e43445f..00000000 --- a/projects/cluster-api/util_container_fuzzer.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2022 ADA Logics Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package container - -func FuzzModifyImageRepository(data []byte) int { - if len(data)%2 != 0 { - return 0 - } - imageName := string(data[:len(data)/2]) - repositoryName := string(data[len(data)/2:]) - _, _ = ModifyImageRepository(imageName, repositoryName) - return 1 -} -func FuzzModifyImageTag(data []byte) int { - if len(data)%2 != 0 { - return 0 - } - imageName := string(data[:len(data)/2]) - repositoryName := string(data[len(data)/2:]) - _, _ = ModifyImageTag(imageName, repositoryName) - return 1 -} diff --git a/projects/cluster-api/v1beta1_machine_webhook_fuzzer.go b/projects/cluster-api/v1beta1_machine_webhook_fuzzer.go deleted file mode 100644 index f310e2eb..00000000 --- a/projects/cluster-api/v1beta1_machine_webhook_fuzzer.go +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright 2022 ADA Logics Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package v1beta1 - -import ( - fuzz "github.com/AdaLogics/go-fuzz-headers" -) - -func FuzzWebhookValidation(data []byte) int { - if len(data) < 10 { - return -1 - } - targetType := int(data[0]) - restOfData := data[1:] - switch targetType % 4 { - case 0: - return fuzzMachineValidate(restOfData) - case 1: - return fuzzMachineDeploymentSpecValidate(restOfData) - case 2: - return fuzzMachineHealthCheck(restOfData) - case 3: - return fuzzMachineSet(restOfData) - } - return 1 -} - -func fuzzMachineValidate(data []byte) int { - f := fuzz.NewConsumer(data) - m1 := &Machine{} - err := f.GenerateStruct(m1) - if err != nil { - return 0 - } - err = m1.ValidateCreate() - if err != nil { - return 0 - } - m2 := &Machine{} - err = f.GenerateStruct(m2) - if err != nil { - return 0 - } - m1.ValidateUpdate(m2) - return 1 -} - -func fuzzMachineDeploymentSpecValidate(data []byte) int { - f := fuzz.NewConsumer(data) - md1 := &MachineDeployment{} - err := f.GenerateStruct(md1) - if err != nil { - return 0 - } - err = md1.ValidateCreate() - if err != nil { - return 0 - } - md2 := &MachineDeployment{} - err = f.GenerateStruct(md2) - if err != nil { - return 0 - } - md1.ValidateUpdate(md2) - return 1 -} - -func fuzzMachineHealthCheck(data []byte) int { - f := fuzz.NewConsumer(data) - mhc1 := &MachineHealthCheck{} - err := f.GenerateStruct(mhc1) - if err != nil { - return 0 - } - err = mhc1.ValidateCreate() - if err != nil { - return 0 - } - mhc2 := &MachineHealthCheck{} - err = f.GenerateStruct(mhc2) - if err != nil { - return 0 - } - mhc1.ValidateUpdate(mhc2) - return 1 -} - -func fuzzMachineSet(data []byte) int { - f := fuzz.NewConsumer(data) - ms1 := &MachineSet{} - err := f.GenerateStruct(ms1) - if err != nil { - return 0 - } - err = ms1.ValidateCreate() - if err != nil { - return 0 - } - ms2 := &MachineSet{} - err = f.GenerateStruct(ms2) - if err != nil { - return 0 - } - ms1.ValidateUpdate(ms2) - return 1 -} diff --git a/projects/cluster-api/yaml_fuzzer.go b/projects/cluster-api/yaml_fuzzer.go deleted file mode 100644 index d02c33be..00000000 --- a/projects/cluster-api/yaml_fuzzer.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2022 ADA Logics Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package yaml - -import ( - "os" -) - -func FuzzYamlParse(data []byte) int { - file, err := os.Create("fuzz") - if err != nil { - return 0 - } - defer file.Close() - defer os.Remove("fuzz") - _, err = file.Write(data) - if err != nil { - return 0 - } - _, _ = Parse(ParseInput{File: "fuzz"}) - return 1 -}