|
| 1 | +//go:build gofuzz |
| 2 | +// +build gofuzz |
| 3 | + |
| 4 | +/* |
| 5 | +Copyright 2020 The Flux authors |
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package controllers |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + v2 "github.com/fluxcd/helm-controller/api/v2beta1" |
| 21 | + "github.com/go-logr/logr" |
| 22 | + corev1 "k8s.io/api/core/v1" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/apimachinery/pkg/runtime" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 27 | + "sync" |
| 28 | + |
| 29 | + fuzz "github.com/AdaLogics/go-fuzz-headers" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + initter sync.Once |
| 34 | + scheme *runtime.Scheme |
| 35 | +) |
| 36 | + |
| 37 | +// An init function that is invoked by way of sync.Do |
| 38 | +func initFunc() { |
| 39 | + scheme = runtime.NewScheme() |
| 40 | + err := corev1.AddToScheme(scheme) |
| 41 | + if err != nil { |
| 42 | + panic(err) |
| 43 | + } |
| 44 | + err = v2.AddToScheme(scheme) |
| 45 | + if err != nil { |
| 46 | + panic(err) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// FuzzHelmreleaseComposeValues implements a fuzzer |
| 51 | +// that targets HelmReleaseReconciler.composeValues() |
| 52 | +func FuzzHelmreleaseComposeValues(data []byte) int { |
| 53 | + initter.Do(initFunc) |
| 54 | + |
| 55 | + f := fuzz.NewConsumer(data) |
| 56 | + |
| 57 | + hr := v2.HelmRelease{} |
| 58 | + err := f.GenerateStruct(&hr) |
| 59 | + if err != nil { |
| 60 | + return 0 |
| 61 | + } |
| 62 | + |
| 63 | + r, err := createReconciler(f) |
| 64 | + if err != nil { |
| 65 | + return 0 |
| 66 | + } |
| 67 | + |
| 68 | + _, _ = r.composeValues(logr.NewContext(context.TODO(), log.NullLogger{}), hr) |
| 69 | + return 1 |
| 70 | +} |
| 71 | + |
| 72 | +// FuzzHelmreleasereconcile implements a fuzzer |
| 73 | +// that targets HelmReleaseReconciler.reconcile() |
| 74 | +func FuzzHelmreleasereconcile(data []byte) int { |
| 75 | + initter.Do(initFunc) |
| 76 | + |
| 77 | + f := fuzz.NewConsumer(data) |
| 78 | + |
| 79 | + hr := v2.HelmRelease{} |
| 80 | + err := f.GenerateStruct(&hr) |
| 81 | + if err != nil { |
| 82 | + return 0 |
| 83 | + } |
| 84 | + |
| 85 | + r, err := createReconciler(f) |
| 86 | + if err != nil { |
| 87 | + return 0 |
| 88 | + } |
| 89 | + |
| 90 | + _, _, _ = r.reconcile(logr.NewContext(context.TODO(), log.NullLogger{}), hr) |
| 91 | + return 1 |
| 92 | +} |
| 93 | + |
| 94 | +// Allows the fuzzer to create a reconciler |
| 95 | +func createReconciler(f *fuzz.ConsumeFuzzer) (*HelmReleaseReconciler, error) { |
| 96 | + // Get the type of object: |
| 97 | + var resources []runtime.Object |
| 98 | + r := &HelmReleaseReconciler{} |
| 99 | + getSecret, err := f.GetBool() |
| 100 | + if err != nil { |
| 101 | + return r, err |
| 102 | + } |
| 103 | + name, err := f.GetString() |
| 104 | + if err != nil { |
| 105 | + return r, err |
| 106 | + } |
| 107 | + if getSecret { |
| 108 | + inputMap := make(map[string][]byte) |
| 109 | + err = f.FuzzMap(&inputMap) |
| 110 | + if err != nil { |
| 111 | + return r, err |
| 112 | + } |
| 113 | + resources = []runtime.Object{ |
| 114 | + valuesSecret(name, inputMap), |
| 115 | + } |
| 116 | + } else { |
| 117 | + inputMap := make(map[string]string) |
| 118 | + err = f.FuzzMap(&inputMap) |
| 119 | + if err != nil { |
| 120 | + return r, err |
| 121 | + } |
| 122 | + resources = []runtime.Object{ |
| 123 | + valuesConfigMap(name, inputMap), |
| 124 | + } |
| 125 | + } |
| 126 | + c := fake.NewFakeClientWithScheme(scheme, resources...) |
| 127 | + r.Client = c |
| 128 | + return r, nil |
| 129 | +} |
| 130 | + |
| 131 | +// Taken from |
| 132 | +// https://github.com/fluxcd/helm-controller/blob/main/controllers/helmrelease_controller_test.go#L282 |
| 133 | +func valuesSecret(name string, data map[string][]byte) *corev1.Secret { |
| 134 | + return &corev1.Secret{ |
| 135 | + ObjectMeta: metav1.ObjectMeta{Name: name}, |
| 136 | + Data: data, |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// Taken from |
| 141 | +// https://github.com/fluxcd/helm-controller/blob/main/controllers/helmrelease_controller_test.go#L289 |
| 142 | +func valuesConfigMap(name string, data map[string]string) *corev1.ConfigMap { |
| 143 | + return &corev1.ConfigMap{ |
| 144 | + ObjectMeta: metav1.ObjectMeta{Name: name}, |
| 145 | + Data: data, |
| 146 | + } |
| 147 | +} |
0 commit comments