From 3305f1eaecf8a3e1e526ec56050a8308d3f2d354 Mon Sep 17 00:00:00 2001 From: huiwq1990 Date: Sat, 30 Jul 2022 14:26:57 +0800 Subject: [PATCH] openyurt fuzzer feature (#67) Signed-off-by: huiwq1990 --- go.mod | 1 - tests/fuzz/Dockerfile.builder | 6 +++ tests/fuzz/go.mod | 6 +++ tests/fuzz/oss_fuzz_build.sh | 37 +++++++++++++++ tests/fuzz/oss_fuzz_run.sh | 20 ++++++++ tests/fuzz/yurtappdaemon_fuzzer.go | 72 ++++++++++++++++++++++++++++ tests/fuzz/yurtappset_fuzzer.go | 75 ++++++++++++++++++++++++++++++ 7 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 tests/fuzz/Dockerfile.builder create mode 100644 tests/fuzz/go.mod create mode 100644 tests/fuzz/oss_fuzz_build.sh create mode 100644 tests/fuzz/oss_fuzz_run.sh create mode 100644 tests/fuzz/yurtappdaemon_fuzzer.go create mode 100644 tests/fuzz/yurtappset_fuzzer.go diff --git a/go.mod b/go.mod index 9eb0907..9565e4a 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/openyurtio/yurt-app-manager go 1.16 require ( - github.com/onsi/gomega v1.13.0 github.com/spf13/cobra v1.1.3 github.com/spf13/pflag v1.0.5 gopkg.in/yaml.v2 v2.4.0 diff --git a/tests/fuzz/Dockerfile.builder b/tests/fuzz/Dockerfile.builder new file mode 100644 index 0000000..06fd16f --- /dev/null +++ b/tests/fuzz/Dockerfile.builder @@ -0,0 +1,6 @@ +FROM gcr.io/oss-fuzz-base/base-builder-go + +COPY ./ $GOPATH/src/github.com/openyurtio/yurt-app-manager/ +COPY ./tests/fuzz/oss_fuzz_build.sh $SRC/build.sh + +WORKDIR $SRC \ No newline at end of file diff --git a/tests/fuzz/go.mod b/tests/fuzz/go.mod new file mode 100644 index 0000000..6822134 --- /dev/null +++ b/tests/fuzz/go.mod @@ -0,0 +1,6 @@ +module github.com/openyurtio/yurt-app-manager/tests/fuzz + +// This module is used only to avoid polluting the main module +// with fuzz dependencies. + +go 1.16 diff --git a/tests/fuzz/oss_fuzz_build.sh b/tests/fuzz/oss_fuzz_build.sh new file mode 100644 index 0000000..f9bda07 --- /dev/null +++ b/tests/fuzz/oss_fuzz_build.sh @@ -0,0 +1,37 @@ +# Copyright 2022 The OpenYurt Authors. +# +# 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. + +#!/usr/bin/env bash + +set -euxo pipefail + +GOPATH="${GOPATH:-/root/go}" +GO_SRC="${GOPATH}/src" +PROJECT_PATH="github.com/openyurtio/yurt-app-manager" + +cd "${GO_SRC}" + +# Move fuzzer to their respective directories. +# This removes dependency noises from the modules' go.mod and go.sum files. +cp "${PROJECT_PATH}/tests/fuzz/yurtappdaemon_fuzzer.go" "${PROJECT_PATH}/pkg/yurtappmanager/controller/yurtappdaemon/yurtappdaemon_fuzzer.go" +cp "${PROJECT_PATH}/tests/fuzz/yurtappset_fuzzer.go" "${PROJECT_PATH}/pkg/yurtappmanager/controller/yurtappset/yurtappset_fuzzer.go" + +# compile fuzz tests for the runtime module +pushd "${PROJECT_PATH}" + +go get -d github.com/AdaLogics/go-fuzz-headers +compile_go_fuzzer "${PROJECT_PATH}/pkg/yurtappmanager/controller/yurtappdaemon/" FuzzAppDaemonReconcile fuzz_yurtappdaemon_controller +compile_go_fuzzer "${PROJECT_PATH}/pkg/yurtappmanager/controller/yurtappset/" FuzzAppSetReconcile fuzz_yurtappset_controller + +popd diff --git a/tests/fuzz/oss_fuzz_run.sh b/tests/fuzz/oss_fuzz_run.sh new file mode 100644 index 0000000..425f2b6 --- /dev/null +++ b/tests/fuzz/oss_fuzz_run.sh @@ -0,0 +1,20 @@ +# Copyright 2022 The OpenYurt Authors. +# +# 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. + +#!/usr/bin/env bash + +set -euxo pipefail + +# run each fuzzer once to ensure they are working properly +find /out -type f -name "fuzz*" -exec echo {} -runs=1 \; | bash -e diff --git a/tests/fuzz/yurtappdaemon_fuzzer.go b/tests/fuzz/yurtappdaemon_fuzzer.go new file mode 100644 index 0000000..ebc8830 --- /dev/null +++ b/tests/fuzz/yurtappdaemon_fuzzer.go @@ -0,0 +1,72 @@ +//go:build gofuzz +// +build gofuzz + +/* +Copyright 2022 The OpenYurt Authors. + +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 yurtappdaemon + +import ( + "context" + + fuzz "github.com/AdaLogics/go-fuzz-headers" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + clientgoscheme "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/tools/record" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + + appsv1alpha1 "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/apis/apps/v1alpha1" + "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/controller/yurtappdaemon/workloadcontroller" +) + +var ( + fuzzCtx = context.Background() + fakeSchemeForFuzzing = runtime.NewScheme() +) + +func init() { + _ = clientgoscheme.AddToScheme(fakeSchemeForFuzzing) + _ = appsv1alpha1.AddToScheme(fakeSchemeForFuzzing) + _ = corev1.AddToScheme(fakeSchemeForFuzzing) +} + +func FuzzAppDaemonReconcile(data []byte) int { + f := fuzz.NewConsumer(data) + + appDaemon := &appsv1alpha1.YurtAppDaemon{} + if err := f.GenerateStruct(appDaemon); err != nil { + return 0 + } + + clientFake := fake.NewClientBuilder().WithScheme(fakeSchemeForFuzzing).WithObjects( + appDaemon, + ).Build() + + r := &ReconcileYurtAppDaemon{ + Client: clientFake, + scheme: fakeSchemeForFuzzing, + recorder: record.NewFakeRecorder(10000), + controls: map[appsv1alpha1.TemplateType]workloadcontroller.WorkloadControllor{ + appsv1alpha1.DeploymentTemplateType: &workloadcontroller.DeploymentControllor{Client: clientFake, Scheme: fakeSchemeForFuzzing}, + }, + } + + _, _ = r.Reconcile(fuzzCtx, reconcile.Request{NamespacedName: types.NamespacedName{Name: appDaemon.Name, Namespace: appDaemon.Namespace}}) + return 1 +} diff --git a/tests/fuzz/yurtappset_fuzzer.go b/tests/fuzz/yurtappset_fuzzer.go new file mode 100644 index 0000000..e2d3f22 --- /dev/null +++ b/tests/fuzz/yurtappset_fuzzer.go @@ -0,0 +1,75 @@ +//go:build gofuzz +// +build gofuzz + +/* +Copyright 2022 The OpenYurt Authors. + +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 yurtappset + +import ( + "context" + + fuzz "github.com/AdaLogics/go-fuzz-headers" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + clientgoscheme "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/tools/record" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + + appsv1alpha1 "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/apis/apps/v1alpha1" + "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/controller/yurtappset/adapter" +) + +var ( + fuzzCtx = context.Background() + fakeSchemeForFuzzing = runtime.NewScheme() +) + +func init() { + _ = clientgoscheme.AddToScheme(fakeSchemeForFuzzing) + _ = appsv1alpha1.AddToScheme(fakeSchemeForFuzzing) + _ = corev1.AddToScheme(fakeSchemeForFuzzing) +} + +func FuzzAppSetReconcile(data []byte) int { + f := fuzz.NewConsumer(data) + + appDaemon := &appsv1alpha1.YurtAppDaemon{} + if err := f.GenerateStruct(appDaemon); err != nil { + return 0 + } + + clientFake := fake.NewClientBuilder().WithScheme(fakeSchemeForFuzzing).WithObjects( + appDaemon, + ).Build() + + r := &ReconcileYurtAppSet{ + Client: clientFake, + scheme: fakeSchemeForFuzzing, + recorder: record.NewFakeRecorder(10000), + poolControls: map[appsv1alpha1.TemplateType]ControlInterface{ + appsv1alpha1.StatefulSetTemplateType: &PoolControl{Client: clientFake, scheme: fakeSchemeForFuzzing, + adapter: &adapter.StatefulSetAdapter{Client: clientFake, Scheme: fakeSchemeForFuzzing}}, + appsv1alpha1.DeploymentTemplateType: &PoolControl{Client: clientFake, scheme: fakeSchemeForFuzzing, + adapter: &adapter.DeploymentAdapter{Client: clientFake, Scheme: fakeSchemeForFuzzing}}, + }, + } + + _, _ = r.Reconcile(fuzzCtx, reconcile.Request{NamespacedName: types.NamespacedName{Name: appDaemon.Name, Namespace: appDaemon.Namespace}}) + return 1 +}