Skip to content

Commit

Permalink
fix: Renaming test project manifest files to avoid
Browse files Browse the repository at this point in the history
security scanning

Why: To prevent deprecated/vulnerable library dependencies in
sample projects from being flagged during security scanning,
as these sample projects are not intended for production use
but needed to run integration tests.

How: Renaming all manifest files
(e.g., pom.xml, package.json, requirements.txt, Gemfile)
to a placeholder name (_PLACEHOLDER) and renaming them back with
the correct name during the integration test setup in a temp directory.
  • Loading branch information
as14692 committed May 9, 2024
1 parent 710b081 commit 28612bb
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 16 deletions.
23 changes: 20 additions & 3 deletions Integration-Test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,27 @@ $Lines_To_Find = @(
"using dependency library github.com/golang/snappy version 0.0.1. upgrade to at least version 0.0.2"
)

Write-Host "Setting up sample-projects"
# Copy all sample projects to temp directory and rename project manifests to correct format.
# This is done to avoid security scanning them for deprecated/vulnerable library dependancies
# These sample projects are not intended to be used in production, but are used for testing
$sample_projects_dir = Get-Random
Copy-Item -Path "./sample-projects" -Destination "./temp/$sample_projects_dir" -Recurse
Rename-Item -Path "./temp/$sample_projects_dir/dotnet-samples/sample_csproj_PLACEHOLDER" -NewName "sample.csproj"
Rename-Item -Path "./temp/$sample_projects_dir/go-samples/compatible/go_mod_PLACEHOLDER" -NewName "go.mod"
Rename-Item -Path "./temp/$sample_projects_dir/go-samples/incompatible/go_mod_PLACEHOLDER" -NewName "go.mod"
Rename-Item -Path "./temp/$sample_projects_dir/java-samples/pom_xml_PLACEHOLDER" -NewName "pom.xml"
Rename-Item -Path "./temp/$sample_projects_dir/node-samples/package_json_PLACEHOLDER" -NewName "package.json"
Rename-Item -Path "./temp/$sample_projects_dir/python-samples/compatible/requirements_txt_PLACEHOLDER" -NewName "requirements.txt"
Rename-Item -Path "./temp/$sample_projects_dir/python-samples/incompatible/requirements_txt_PLACEHOLDER" -NewName "requirements.txt"
Rename-Item -Path "./temp/$sample_projects_dir/ruby-samples/Gemfile_PLACEHOLDER" -NewName "Gemfile"

Write-Host "Running samples to console"
$ResultConsole = Invoke-Expression ".\dist\$Filename\$Filename.exe .\sample-projects"
$ResultConsole = Invoke-Expression ".\dist\$Filename\$Filename.exe .\temp\$sample_projects_dir"
Test-Report "Console" $ResultConsole $Lines_To_Find

Write-Host "Running samples to HTML report"
Invoke-Expression ".\dist\$Filename\$Filename.exe .\sample-projects --output test.html"
Invoke-Expression ".\dist\$Filename\$Filename.exe .\temp\$sample_projects_dir --output test.html"
$ResultHtml = Get-Content -Path test.html
Test-Report "HTML" $ResultHtml $Lines_To_Find
Remove-Item -Path test.html
Expand Down Expand Up @@ -86,7 +101,7 @@ $Dependencies = @(
"<si><t>httpclient</t></si>"
"<si><t>jruby-openssl</t></si>"
)
Invoke-Expression ".\dist\$Filename\$Filename.exe .\sample-projects --output test.xlsx --output-format dependencies"
Invoke-Expression ".\dist\$Filename\$Filename.exe .\temp\$sample_projects_dir --output test.xlsx --output-format dependencies"
# xlsx files are compressed files, so we need to unzip them and then compare them
Expand-Archive test.xlsx -DestinationPath temp
$ResultXlsx = Get-Content ".\temp\xl\sharedStrings.xml"
Expand Down Expand Up @@ -120,4 +135,6 @@ else
Write-Host "**PASSED** directory not found test"
}

Remove-Item -Recurse -Force $sample_project_directory

exit 0
7 changes: 5 additions & 2 deletions container-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ source ./test-helpers.sh
# This script tests container image build and runs a couple of
# tests against the generated image.

echo "Setting up sample-projects"
sample_project_directory=$(get_sample_projects_relative_path)

docker build -t porting-advisor .
if [ $? -ne 0 ]; then
echo "**ERROR**: building container image" && exit 1
fi

echo "Running container on samples to console"
docker run --rm -v $(pwd)/sample-projects:/source porting-advisor /source > console_test.txt
docker run --rm -v $(pwd)/$sample_project_directory:/source porting-advisor /source > console_test.txt
test_report 'console' 'console_test.txt' "${lines_to_find[@]}"
if [ $? -ne 0 ]; then
echo "**ERROR**: running container to console" && exit 1
fi
rm console_test.txt

echo "Running container on samples to HTML report"
docker run --rm -v $(pwd):/source porting-advisor /source/sample-projects --output /source/test.html
docker run --rm -v $(pwd):/source porting-advisor /source/$sample_project_directory --output /source/test.html
test_report 'html' 'test.html' "${lines_to_find[@]}"
if [ $? -ne 0 ]; then
echo "**ERROR**: running container to html report" && exit 1
Expand Down
11 changes: 8 additions & 3 deletions integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ source ./test-helpers.sh
FILE_NAME=`./getBinaryName.sh`
chmod +x ./dist/$FILE_NAME

echo "Setting up sample-projects"
sample_project_directory=$(get_sample_projects_relative_path)

echo "Running samples to console"
./dist/$FILE_NAME ./sample-projects/ > console_test.txt
./dist/$FILE_NAME $sample_project_directory > console_test.txt
test_report 'console' 'console_test.txt' "${lines_to_find[@]}"
rm console_test.txt


echo "Running samples to HTML report"
./dist/$FILE_NAME ./sample-projects/ --output test.html
./dist/$FILE_NAME $sample_project_directory --output test.html
test_report 'html' 'test.html' "${lines_to_find[@]}"
rm test.html

Expand Down Expand Up @@ -57,7 +60,7 @@ declare -a dependencies=("<si><t>component</t></si><si><t>version</t></si><si><t
"<si><t>httpclient</t></si>"
"<si><t>jruby-openssl</t></si>"
)
./dist/$FILE_NAME ./sample-projects/ --output test.xlsx --output-format dependencies
./dist/$FILE_NAME $sample_project_directory --output test.xlsx --output-format dependencies
# xlsx files are compressed files, so we need to unzip them and then compare them
mkdir ./temp
unzip -q ./test.xlsx -d ./temp
Expand Down Expand Up @@ -86,3 +89,5 @@ else
echo "**FAILED**: directory not found test" && exit 1
fi
rm directory_not_found_test.txt

rm -r $sample_project_directory
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions sample-projects/ruby-samples/sample.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "Hello World"
18 changes: 18 additions & 0 deletions test-helpers.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
#!/bin/bash

function get_sample_projects_relative_path() {
# Copy all sample projects to temp directory and rename project manifests to correct format.
# This is done to avoid security scanning them for deprecated/vulnerable library dependancies
# These sample projects are not intended to be used in production, but are used for testing
sample_projects_dir=$RANDOM
mkdir -p ./temp/$sample_projects_dir && cp -r ./sample-projects/* ./temp/$sample_projects_dir
mv ./temp/$sample_projects_dir/dotnet-samples/sample_csproj_PLACEHOLDER ./temp/$sample_projects_dir/dotnet-samples/sample.csproj
mv ./temp/$sample_projects_dir/go-samples/compatible/go_mod_PLACEHOLDER ./temp/$sample_projects_dir/go-samples/compatible/go.mod
mv ./temp/$sample_projects_dir/go-samples/incompatible/go_mod_PLACEHOLDER ./temp/$sample_projects_dir/go-samples/incompatible/go.mod
mv ./temp/$sample_projects_dir/java-samples/pom_xml_PLACEHOLDER ./temp/$sample_projects_dir/java-samples/pom.xml
mv ./temp/$sample_projects_dir/node-samples/package_json_PLACEHOLDER ./temp/$sample_projects_dir/node-samples/package.json
mv ./temp/$sample_projects_dir/python-samples/compatible/requirements_txt_PLACEHOLDER ./temp/$sample_projects_dir/python-samples/compatible/requirements.txt
mv ./temp/$sample_projects_dir/python-samples/incompatible/requirements_txt_PLACEHOLDER ./temp/$sample_projects_dir/python-samples/incompatible/requirements.txt
mv ./temp/$sample_projects_dir/ruby-samples/Gemfile_PLACEHOLDER ./temp/$sample_projects_dir/ruby-samples/Gemfile

echo ./temp/$sample_projects_dir
}

function test_line() {
reportType=$1
result_filename=$2
Expand Down
9 changes: 1 addition & 8 deletions unittest/test_manifester.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,4 @@ def test_get_dependencies_for_ruby_returns_array_of_dependencies(self):
self.assertEqual('6.1.6.1', dependencies[0].version)
self.assertEqual('Gemfile', dependencies[0].filename)
self.assertEqual('ruby', dependencies[0].tool)

def test_scan_folder_returns_array_of_all_dependencies(self):
manifester = Manifester()
sample_path = path.abspath(path.join(path.dirname(__file__), '..', 'sample-projects'))
dependencies = manifester.scan_folder(sample_path)
self.assertGreater(len(dependencies), 0)
self.assertIsInstance(dependencies, list)
self.assertIsInstance(dependencies[0], Dependency)

0 comments on commit 28612bb

Please sign in to comment.