1
// Copyright Istio Authors
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
20
fuzz "github.com/AdaLogics/go-fuzz-headers"
22
"istio.io/istio/pilot/pkg/model"
23
"istio.io/istio/pilot/pkg/serviceregistry/memory"
24
"istio.io/istio/pkg/config/host"
25
"istio.io/istio/pkg/config/mesh"
26
"istio.io/istio/pkg/config/protocol"
27
"istio.io/istio/pkg/slices"
30
var protocols = []protocol.Instance{
45
// Creates a new fuzzed ServiceInstance
46
func NewSI(f *fuzz.ConsumeFuzzer) (*model.ServiceInstance, error) {
47
si := &model.ServiceInstance{}
48
err := f.GenerateStruct(si)
56
p, err := createPort(f)
60
s.Ports = append(s.Ports, p)
70
// Gets a protocol from global var protocols
71
func getProtocolInstance(f *fuzz.ConsumeFuzzer) (protocol.Instance, error) {
72
pIndex, err := f.GetInt()
74
return protocol.Unsupported, errors.New("could not create protocolInstance")
76
i := protocols[pIndex%len(protocols)]
80
// Creates a new fuzzed Port
81
func createPort(f *fuzz.ConsumeFuzzer) (*model.Port, error) {
83
name, err := f.GetString()
87
port, err := f.GetInt()
91
protocolinstance, err := getProtocolInstance(f)
97
p.Protocol = protocolinstance
101
// Creates a new fuzzed Port slice
102
func createPorts(f *fuzz.ConsumeFuzzer) ([]*model.Port, error) {
103
ports := make([]*model.Port, 0, 20)
104
numberOfPorts, err := f.GetInt()
109
maxPorts := numberOfPorts % 20
113
for i := 0; i < maxPorts; i++ {
114
port, err := createPort(f)
118
ports = append(ports, port)
123
// Creates a new fuzzed Service
124
func NewS(f *fuzz.ConsumeFuzzer) (*model.Service, error) {
125
s := &model.Service{}
126
err := f.GenerateStruct(s)
130
ports, err := createPorts(f)
135
hostname, err := f.GetString()
139
s.Hostname = host.Name(hostname)
147
// Creates an Environment with fuzzed values
148
// and passes that to InitContext
149
func FuzzInitContext(data []byte) int {
150
f := fuzz.NewConsumer(data)
152
// Create service instances
153
serviceInstances := make([]*model.ServiceInstance, 0, 20)
154
number, err := f.GetInt()
158
// We allow a maximum of 20 service instances
159
numberOfS := number % 20
160
for i := 0; i < numberOfS; i++ {
165
serviceInstances = append(serviceInstances, si)
169
services := make([]*model.Service, 0, 20)
170
number, err = f.GetInt()
174
// We allow a maximum of 20 services
175
numberOfS = number % 20
176
for i := 0; i < numberOfS; i++ {
181
services = append(services, s)
184
configString, err := f.GetString()
188
m, err := mesh.ApplyMeshConfigDefaults(configString)
193
env := &model.Environment{}
194
store := model.NewFakeStore()
196
env.ConfigStore = store
197
sd := memory.NewServiceDiscovery(services...)
198
sd.WantGetProxyServiceTargets = slices.Map(serviceInstances, model.ServiceInstanceToTarget)
199
env.ServiceDiscovery = sd
201
env.Watcher = mesh.NewFixedWatcher(m)
202
env.EndpointIndex = model.NewEndpointIndex(model.DisabledCache{})
204
pc := model.NewPushContext()
205
_ = pc.InitContext(env, nil, nil)
209
func FuzzBNMUnmarshalJSON(data []byte) int {
210
var bnm model.BootstrapNodeMetadata
211
_ = bnm.UnmarshalJSON(data)