Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dockerfile and script to perform fuzzing test on all swagger files and individual #4569

Merged
merged 6 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ stage/*
snap/.snapcraft/*
squashfs-root/

# result files
fuzz_results/*
41 changes: 41 additions & 0 deletions Dockerfile.fuzz
jim-wang-intel marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# ----------------------------------------------------------------------------------
# Copyright 2023 Intel Corporation
#
# 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.
#
# ----------------------------------------------------------------------------------

FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine

RUN apk add --no-cache python3 py3-pip bash coreutils

WORKDIR /restler-fuzzer
RUN wget -q -O - https://github.com/microsoft/restler-fuzzer/archive/refs/tags/v9.2.2.tar.gz | \
tar xz --strip-components 1 && \
mkdir -p restler_bin

RUN python3 ./build-restler.py --dest_dir ./restler_bin/

COPY fuzzing_docker.sh /restler-fuzzer/fuzzing.sh
COPY /openapi/v3/* /restler-fuzzer/openapi/

ENTRYPOINT ["/restler-fuzzer/fuzzing.sh"]
# CMD core-data below only do fuzz-lean for core-data
lenny-goodell marked this conversation as resolved.
Show resolved Hide resolved
# This can take about 2 minutes to finish
# You may swap out core-data to other server to perform fuzz-lean
CMD ["core-data", "/restler-fuzzer/openapi/core-data.yaml"]
lenny-goodell marked this conversation as resolved.
Show resolved Hide resolved
# CMD of "all" will do fuzz-lean test for core-commmand, core-data, core-metadata, support-notifications, and support-scheduler,
lenny-goodell marked this conversation as resolved.
Show resolved Hide resolved
# basically all services under openapi/v3/ directory
# This can take more than 20 minutes to finish
# Comment out above CMD and uncomment below CMD of "all" to fuzz all services
# CMD ["all"]
lenny-goodell marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# SPDX-License-Identifier: Apache-2.0
#

.PHONY: build clean unittest hadolint lint test docker run sbom
.PHONY: build clean unittest hadolint lint test docker run sbom docker-fuzz fuzz-test
lenny-goodell marked this conversation as resolved.
Show resolved Hide resolved

# change the following boolean flag to include or exclude the delayed start libs for builds for most of core services except support services
INCLUDE_DELAYED_START_BUILD_CORE:="false"
Expand Down Expand Up @@ -367,3 +367,10 @@ sbom:
docker run -it --rm \
-v "$$PWD:/edgex-go" -v "$$PWD/sbom:/sbom" \
spdx/spdx-sbom-generator -p /edgex-go/ -o /sbom/ --include-license-text true

docker-fuzz:
docker build -f Dockerfile.fuzz -t fuzz-edgex-go:latest .

fuzz-test:
lenny-goodell marked this conversation as resolved.
Show resolved Hide resolved
# not joining the edgex-network due to swagger file url pointing to localhost for fuzz testing in the container
docker run --net host --rm -v "$$PWD/fuzz_results:/fuzz_results" fuzz-edgex-go:latest
lenny-goodell marked this conversation as resolved.
Show resolved Hide resolved
73 changes: 73 additions & 0 deletions fuzzing_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env bash
lenny-goodell marked this conversation as resolved.
Show resolved Hide resolved
# /*******************************************************************************
# * Copyright 2023 Intel Corporation.
# *
# * 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.
# *******************************************************************************/


EDGEX_PROJECT_NAME=${1}
echo $EDGEX_PROJECT_NAME
SWAGGER_FILE_NAME_PATH=${2}
echo $SWAGGER_FILE_NAME_PATH

SWAGGER_FILE_PATH="/restler-fuzzer/openapi"

usage()
{
echo "Usage:"
echo "./fuzzing_docker.sh <EDGEX_PROJECT_NAME> <SWAGGER_FILE_NAME_PATH>"
echo
echo "<EDGEX_PROJECT_NAME> is required, options: all|core-data|core-command|core-metadata|support-notifications|support-scheduler"
echo "<SWAGGER_FILE_NAME_PATH> is required for NOT \"all\" EDGEX_PROJECT_NAME, it is the path and filename of a project swagger file"
exit 1
jim-wang-intel marked this conversation as resolved.
Show resolved Hide resolved
}

runFuzzLeanPerSwagger() {
echo "--compile from swagger file: $2"
./restler_bin/restler/Restler compile --api_spec "$2"

echo "--test the grammar"
./restler_bin/restler/Restler test --grammar_file ./Compile/grammar.py --dictionary_file ./Compile/dict.json --settings ./Compile/engine_settings.json --no_ssl

# assuming edgex service is already running on host
echo "--run fuzz-lean"
./restler_bin/restler/Restler fuzz-lean --grammar_file ./Compile/grammar.py --dictionary_file ./Compile/dict.json --settings ./Compile/engine_settings.json --no_ssl

echo "--copy result logs into $1"
mkdir -p /fuzz_results/"$1"
cp -r ./Test/ /fuzz_results/"$1"/
}

if [ "$EDGEX_PROJECT_NAME" == "" ]
then
echo "Please provide a valid project name."
usage
fi
if [ "$EDGEX_PROJECT_NAME" == "all" ]
then
echo "fuzz-lean for all swagger files"

for swagger in "$SWAGGER_FILE_PATH"/*
do
projectname=$(basename "$swagger" .yaml)
echo "$projectname"
echo "$swagger"
if [[ "$projectname" != *"."* ]]
then
runFuzzLeanPerSwagger $projectname $swagger
fi
done
else
vli11 marked this conversation as resolved.
Show resolved Hide resolved
echo "fuzz-lean a specific swagger file only"
runFuzzLeanPerSwagger $EDGEX_PROJECT_NAME $SWAGGER_FILE_NAME_PATH
fi