Skip to content

Commit e55e4e8

Browse files
arielkrurotemavni
andauthored
feat(sast): Remove semgrep and turn CDK UTs into integration tests (bridgecrewio#81)
* remove parser tests * move cdk test files * move cdk test files 2 * remove parsers * update registries (cdk + sast) * remove sast consts * remove semgrep dependency * update prisma engine * cdk tests v1 -> v2 * remove semgrep engine * update sast report check * remove old tests * update runner + tests + report * new integration tests * initial cdk tests * remove pipfile script * cdk integration tests step * cdk integration tests step fix * cdk integration tests step fix * cdk update checks * cdk update checks * cdk update checks * remove old cdk tests * fix tests * remove unused mypy comment * remove old functions from base_registry.py * improve integration tests bash * typing * lint * lint * remove semgrep leftovers * update prepare_data.sh * bql v1 -> v2 * skip tests * move engine to self * update bash script * support remove_default_policies flag * test cdk runner through integration tests, using cdk as framework * make cdk run all langs * 0.1 to 0.2 * lint * lint * import fix * import fix * lint --------- Co-authored-by: Rotem Avni <[email protected]>
1 parent be2146f commit e55e4e8

File tree

96 files changed

+276
-2365
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+276
-2365
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ max-line-length = 120
55
# E203,E501 don't work with black together
66
ignore = E203,E501,E731,W503,W504,DUO107,DUO104,DUO130,DUO109,DUO116,B028,B950,TC001,TC003,TC006,B907
77
select = C,E,F,W,B,B9,A,TC
8-
extend-exclude = .github, .pytest_cache, docs/*, venv/*, tests/*, flake8_plugins/*
8+
extend-exclude = .github, .pytest_cache, docs/*, venv/*, tests/*, flake8_plugins/*, cdk_integration_tests/src/python/*
99

1010
[flake8:local-plugins]
1111
extension =

.github/workflows/pr-test.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,44 @@ jobs:
161161
run: |
162162
pipenv run pytest sast_integration_tests
163163
164+
cdk-integration-tests:
165+
strategy:
166+
fail-fast: true
167+
matrix:
168+
python: [ "3.8" ]
169+
os: [ ubuntu-latest, macos-latest]
170+
runs-on: ${{ matrix.os }}
171+
steps:
172+
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3
173+
- uses: actions/setup-python@bd6b4b6205c4dbad673328db7b31b7fab9e241c0 # v4
174+
with:
175+
python-version: ${{ matrix.python }}
176+
cache: "pipenv"
177+
cache-dependency-path: "Pipfile.lock"
178+
- uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3
179+
- name: Install pipenv
180+
run: |
181+
python -m pip install --no-cache-dir --upgrade pipenv
182+
- name: Build & install checkov package
183+
run: |
184+
# remove venv, if exists
185+
pipenv --rm || true
186+
pipenv --python ${{ matrix.python }}
187+
pipenv run pip install pytest pytest-xdist
188+
pipenv run python setup.py sdist bdist_wheel
189+
bash -c 'pipenv run pip install dist/checkov3-*.whl'
190+
- name: Create checkov reports
191+
env:
192+
LOG_LEVEL: INFO
193+
BC_API_KEY: ${{ secrets.BC_API_KEY }}
194+
run: bash -c './cdk_integration_tests/prepare_data.sh'
195+
- name: Run integration tests
196+
env:
197+
LOG_LEVEL: INFO
198+
BC_API_KEY: ${{ secrets.BC_API_KEY }}
199+
run: |
200+
pipenv run pytest cdk_integration_tests
201+
164202
performance-tests:
165203
if:
166204
true == false

Pipfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ yarl = "*"
8989
openai = "*"
9090
spdx-tools = "<0.8.0"
9191
license-expression = "==30.1.0"
92-
semgrep = "==1.10.0"
9392
pydantic = "==1.10.7"
9493

9594
[requires]
File renamed without changes.

cdk_integration_tests/prepare_data.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
# iterate over all the cdk python checks
4+
for file in "checkov/cdk/checks/python"/*; do
5+
# Ensure it's a yaml file
6+
if [[ -f "$file" && "$file" == *.yaml ]]; then
7+
basename=$(basename -- "$file")
8+
filename="${basename%.*}"
9+
# create a report for this check
10+
echo "creating report for check: $filename"
11+
pipenv run checkov -s --framework cdk -o json \
12+
-d "cdk_integration_tests/src/python/$filename" \
13+
--external-checks-dir "checkov/cdk/checks/python/$filename.yaml" > "checkov_report_cdk_python_$filename.json"
14+
fi
15+
done
16+
17+
#todo: iterate over all the cdk typescript checks - when ts supported in sast
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/bash
2+
3+
# In order to run this script set the following environment variables:
4+
# BC_API_URL - your API url.
5+
# BC_KEY - generate API key via Platform.
6+
# You can also add the local SAST_ARTIFACT_PATH and LOG_LEVEL.
7+
8+
# You can also set those vars in the set_env_vars() function, and uncomment the call to it.
9+
10+
# The working dir should be the checkov project dir.
11+
# For example: on /Users/ajbara/dev2/checkov dir run BC_API_URL=https://ws342vj2ze.execute-api.us-west-2.amazonaws.com/v1 BC_KEY=xyz LOG_LEVEL=Info /Users/ajbara/dev2/checkov/sast_integration_tests/run_integration_tests.sh
12+
13+
set_env_vars() {
14+
export SAST_ARTIFACT_PATH=""
15+
export BC_API_KEY=""
16+
export LOG_LEVEL=DEBUG
17+
export BC_API_URL=""
18+
}
19+
20+
prepare_data () {
21+
for file in "checkov/cdk/checks/python"/*; do
22+
# Ensure it's a regular file (not a directory or symlink, etc.)
23+
if [ -f "$file" ]; then
24+
basename=$(basename -- "$file")
25+
filename="${basename%.*}"
26+
# create a report for this check
27+
echo "creating report for check: $filename"
28+
python checkov/main.py -s --framework cdk -o json \
29+
-d "cdk_integration_tests/src/python/$filename" \
30+
--external-checks-dir "checkov/cdk/checks/python/$filename.yaml" > "checkov_report_cdk_python_$filename.json"
31+
fi
32+
done
33+
34+
}
35+
36+
delete_reports () {
37+
rm -r checkov_report*
38+
rm results.sarif
39+
rm checkov_checks_list.txt
40+
}
41+
42+
echo "calling set_env_vars"
43+
set_env_vars
44+
45+
if [[ -z "BC_API_KEY" ]]; then
46+
echo "BC_API_KEY is missing."
47+
exit 1
48+
fi
49+
50+
echo $BC_API_URL
51+
if [[ -z "$BC_API_URL" ]]; then
52+
echo "BC_API_URL is missing."
53+
exit 1
54+
fi
55+
56+
cd ..
57+
58+
echo $VIRTUAL_ENV
59+
if [ ! -z "$VIRTUAL_ENV" ]; then
60+
deactivate
61+
fi
62+
63+
#activate virtual env
64+
ENV_PATH=$(pipenv --venv)
65+
echo $ENV_PATH
66+
source $ENV_PATH/bin/activate
67+
68+
echo $(pwd)
69+
working_dir=$(pwd) # should be the path of local checkov project
70+
export PYTHONPATH="$working_dir/checkov:$PYTHONPATH"
71+
72+
prepare_data
73+
74+
#Run integration tests.
75+
echo "running integration tests"
76+
pytest cdk_integration_tests
77+
78+
deactivate
79+
80+
echo "Deleting reports"
81+
delete_reports
82+

0 commit comments

Comments
 (0)