Skip to content

Commit

Permalink
add yurt controller fuzz (#1903)
Browse files Browse the repository at this point in the history
Signed-off-by: huiwq1990 <huiwq1990@163.com>
  • Loading branch information
huiwq1990 authored Jan 10, 2024
1 parent cbba473 commit d7f64fc
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/fuzz/Dockerfile.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM gcr.io/oss-fuzz-base/base-builder-go

COPY ./ $GOPATH/src/github.com/openyurtio/openyurt/
COPY ./test/fuzz/oss_fuzz_build.sh $SRC/build.sh

WORKDIR $SRC
6 changes: 6 additions & 0 deletions test/fuzz/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module github.com/openyurtio/openyurt/test/fuzz

// This module is used only to avoid polluting the main module
// with fuzz dependencies.

go 1.18
39 changes: 39 additions & 0 deletions test/fuzz/oss_fuzz_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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/openyurt"

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}/test/fuzz/yurtappdaemon_fuzzer.go" "${PROJECT_PATH}/pkg/yurtmanager/controller/yurtappdaemon/yurtappdaemon_fuzzer.go"
cp "${PROJECT_PATH}/test/fuzz/yurtappset_fuzzer.go" "${PROJECT_PATH}/pkg/yurtmanager/controller/yurtappset/yurtappset_fuzzer.go"

# compile fuzz test for the runtime module
pushd "${PROJECT_PATH}"

go get -d github.com/AdaLogics/go-fuzz-headers
go mod vendor
go mod tidy
compile_go_fuzzer "${PROJECT_PATH}/pkg/yurtmanager/controller/yurtappdaemon/" FuzzAppDaemonReconcile fuzz_yurtappdaemon_controller
compile_go_fuzzer "${PROJECT_PATH}/pkg/yurtmanager/controller/yurtappset/" FuzzAppSetReconcile fuzz_yurtappset_controller

popd
20 changes: 20 additions & 0 deletions test/fuzz/oss_fuzz_run.sh
Original file line number Diff line number Diff line change
@@ -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
72 changes: 72 additions & 0 deletions test/fuzz/yurtappdaemon_fuzzer.go
Original file line number Diff line number Diff line change
@@ -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/openyurt/pkg/apis/apps/v1alpha1"
"github.com/openyurtio/openyurt/pkg/yurtmanager/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
}
116 changes: 116 additions & 0 deletions test/fuzz/yurtappset_fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//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"
"fmt"

fuzz "github.com/AdaLogics/go-fuzz-headers"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"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"
"sigs.k8s.io/yaml"

appsv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/apps/v1alpha1"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/yurtappset/adapter"
)

var (
fuzzCtx = context.Background()
fakeSchemeForFuzzing = runtime.NewScheme()
)

func init() {
_ = clientgoscheme.AddToScheme(fakeSchemeForFuzzing)
_ = appsv1alpha1.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 FuzzAppSetReconcile(data []byte) int {
f := fuzz.NewConsumer(data)
unstr, err := GetUnstructured(f)
if err != nil {
return 0
}
err = validateUnstructured(unstr)
if err != nil {
return 0
}

appset := &appsv1alpha1.YurtAppSet{}
if err := f.GenerateStruct(appset); err != nil {
return 0
}

clientFake := fake.NewClientBuilder().WithScheme(fakeSchemeForFuzzing).WithObjects(
appset,
).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: appset.Name, Namespace: appset.Namespace}})
return 1
}

0 comments on commit d7f64fc

Please # to comment.