Skip to content

Commit 3827a78

Browse files
Merge pull request #113 from abnamro/feature/2229659-improve_scan_type_determination_logic
[#2229659] Improve scan type logic by based on specific conditions.
2 parents 008b37a + e2f9074 commit 3827a78

File tree

8 files changed

+113
-24
lines changed

8 files changed

+113
-24
lines changed

components/resc-vcs-scanner/src/vcs_scanner/output_module.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from resc_backend.resc_web_service.schema.branch import Branch
77
from resc_backend.resc_web_service.schema.finding import FindingCreate
88
from resc_backend.resc_web_service.schema.repository import Repository
9-
from resc_backend.resc_web_service.schema.scan import Scan
9+
from resc_backend.resc_web_service.schema.scan import Scan, ScanRead
1010
from resc_backend.resc_web_service.schema.scan_type import ScanType
1111

1212
# First Party
@@ -39,5 +39,5 @@ def write_scan(
3939
rule_pack: str) -> Scan:
4040
pass
4141

42-
def get_last_scanned_commit(self, branch: Branch):
42+
def get_last_scan_for_branch(self, branch: Branch) -> ScanRead:
4343
pass

components/resc-vcs-scanner/src/vcs_scanner/secret_scanners/rws_api_writer.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,13 @@ def write_scan(
117117

118118
return created_scan
119119

120-
def get_last_scanned_commit(self, branch: BranchRead):
121-
last_scanned_commit = None
120+
def get_last_scan_for_branch(self, branch: BranchRead) -> ScanRead:
122121
response = get_last_scan_for_branch(self.rws_url,
123122
branch.id_)
124123
if response.status_code == 200:
125-
json_body = json.loads(response.text)
126-
last_scanned_commit = json_body['last_scanned_commit'] if json_body else None
127-
else:
128-
logger.warning(f"Retrieving last scan details failed with error: {response.status_code}->{response.text}")
129-
return last_scanned_commit
124+
return ScanRead(**json.loads(response.text))
125+
logger.warning(f"Retrieving last scan details failed with error: {response.status_code}->{response.text}")
126+
return None
130127

131128
@retry(wait=wait_exponential(multiplier=1, min=2, max=10), stop=stop_after_attempt(100))
132129
def write_vcs_instances(self, vcs_instances_dict: Dict[str, VCSInstanceRuntime]) \

components/resc-vcs-scanner/src/vcs_scanner/secret_scanners/secret_scanner.py

+22-9
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,11 @@ def run_repository_scan(self) -> None:
8888
return
8989

9090
# Get last scanned commit for the branch
91-
last_scanned_commit = self._output_module.get_last_scanned_commit(branch=created_branch)
91+
last_scan_for_branch = self._output_module.get_last_scan_for_branch(branch=created_branch)
92+
last_scanned_commit = last_scan_for_branch.last_scanned_commit if last_scan_for_branch else None
93+
scan_type_to_run = self.determine_scan_type(branch, last_scan_for_branch)
9294

93-
# Decide which type of scan to run
94-
if self.force_base_scan:
95-
scan_type_to_run = ScanType.BASE
96-
else:
97-
scan_type_to_run = ScanType.INCREMENTAL if last_scanned_commit else ScanType.BASE
98-
99-
# Only insert in to scan and finding table if its BASE Scan or there is new commit, else skip
100-
if scan_type_to_run == ScanType.BASE or last_scanned_commit != branch.latest_commit:
95+
if scan_type_to_run:
10196
# Insert in to scan table
10297
scan_timestamp_start = datetime.utcnow()
10398
created_scan = self._output_module.write_scan(
@@ -251,3 +246,21 @@ def scan_directory(self, directory_path: str) -> Optional[List[FindingBase]]:
251246
if os.path.exists(report_filepath):
252247
os.remove(report_filepath)
253248
return None
249+
250+
# Decide which type of scan to run
251+
def determine_scan_type(self, branch, last_scan_for_branch):
252+
# Force base scan, or has no previous scan
253+
if self.force_base_scan or last_scan_for_branch is None:
254+
return ScanType.BASE
255+
# Has previous scan
256+
if last_scan_for_branch:
257+
last_used_rule_pack = last_scan_for_branch.rule_pack
258+
# Rule-pack is different from previous scan
259+
if last_used_rule_pack != self.rule_pack_version:
260+
return ScanType.BASE
261+
last_scanned_commit = last_scan_for_branch.last_scanned_commit
262+
# Last commit is different from previous scan
263+
if branch and branch.latest_commit != last_scanned_commit:
264+
return ScanType.INCREMENTAL
265+
# Skip scanning, no conditions match
266+
return None

components/resc-vcs-scanner/src/vcs_scanner/secret_scanners/stdout_writer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,5 @@ def write_scan(self, scan_type_to_run: ScanType, last_scanned_commit: str, scan_
179179
id_=1,
180180
rule_pack=rule_pack)
181181

182-
def get_last_scanned_commit(self, branch: Branch):
182+
def get_last_scan_for_branch(self, branch: Branch) -> ScanRead:
183183
return None

components/resc-vcs-scanner/tests/vcs_scanner/secret_scanners/test_rws_api_writer.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def test_write_scan_unsuccessful(warning, post):
236236

237237

238238
@patch("requests.get")
239-
def test_get_last_scanned_commit(get):
239+
def test_get_last_scan_for_branch(get):
240240
url = "https://nonexistingwebsite.com"
241241
branch = BranchRead(id_=1,
242242
branch_id="branch.branch_id",
@@ -255,8 +255,8 @@ def test_get_last_scanned_commit(get):
255255
get.return_value.status_code = 200
256256
get.return_value.text = expected_json
257257

258-
result = RESTAPIWriter(rws_url=url).get_last_scanned_commit(branch)
259-
assert result == expected_result.last_scanned_commit
258+
result = RESTAPIWriter(rws_url=url).get_last_scan_for_branch(branch)
259+
assert result == expected_result
260260

261261

262262
@patch("requests.get")
@@ -272,7 +272,7 @@ def test_get_last_scanned_commit_invalid_id(warning, get):
272272
get.return_value.status_code = 404
273273
get.return_value.text = error_text
274274

275-
result = RESTAPIWriter(rws_url=url).get_last_scanned_commit(branch)
275+
result = RESTAPIWriter(rws_url=url).get_last_scan_for_branch(branch)
276276
assert result is None
277277
warning.assert_called_once()
278278
warning.assert_called_with(f"Retrieving last scan details failed with error: 404->{error_text}")

components/resc-vcs-scanner/tests/vcs_scanner/secret_scanners/test_secret_scanner.py

+79
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Standard Library
22
import sys
3+
from datetime import datetime
34
from unittest.mock import patch
45

56
# Third Party
67
from _pytest.monkeypatch import MonkeyPatch
78
from resc_backend.resc_web_service.schema.branch import Branch
89
from resc_backend.resc_web_service.schema.repository import Repository
10+
from resc_backend.resc_web_service.schema.scan import ScanRead
911
from resc_backend.resc_web_service.schema.scan_type import ScanType
1012

1113
# First Party
@@ -127,3 +129,80 @@ def test_scan_directory(start_scan):
127129
result = secret_scanner.scan_directory(directory_path=repo_clone_path)
128130
assert result is None
129131
start_scan.assert_called_once()
132+
133+
134+
# not a test class
135+
def initialize_and_get_repo_scanner_and_branch():
136+
repository = Repository(project_key="local",
137+
repository_id=1,
138+
repository_name="local",
139+
repository_url="https://repository.url",
140+
vcs_instance=1,
141+
branches=[])
142+
143+
secret_scanner = SecretScanner(
144+
gitleaks_binary_path="/tmp/gitleaks",
145+
gitleaks_rules_path="/rules.toml",
146+
rule_pack_version="2.0.1",
147+
output_plugin=RESTAPIWriter(rws_url="https://fakeurl.com:8000"),
148+
repository=repository,
149+
username="",
150+
personal_access_token="")
151+
152+
branch = Branch(branch_id=1,
153+
branch_name="branch_name1",
154+
latest_commit="latest_commit_2")
155+
156+
return repository, branch, secret_scanner
157+
158+
159+
def test_scan_type_is_base_when_a_latest_scan_is_not_present():
160+
repository, branch, secret_scanner = initialize_and_get_repo_scanner_and_branch()
161+
162+
scan_type = secret_scanner.determine_scan_type(branch, None)
163+
assert scan_type == ScanType.BASE
164+
165+
166+
def test_scan_type_is_base_when_a_latest_scan_is_present_and_rule_pack_is_latest():
167+
repository, branch, secret_scanner = initialize_and_get_repo_scanner_and_branch()
168+
169+
scan_read = ScanRead(id_=1,
170+
branch_id=1,
171+
scan_type=ScanType.BASE,
172+
last_scanned_commit="latest_commit_1",
173+
timestamp=datetime.utcnow(),
174+
increment_number=0,
175+
rule_pack="2.0.2")
176+
177+
scan_type = secret_scanner.determine_scan_type(branch, scan_read)
178+
assert scan_type == ScanType.BASE
179+
180+
181+
def test_scan_type_is_incremental_when_a_latest_scan_is_present_and_rule_pack_is_same():
182+
repository, branch, secret_scanner = initialize_and_get_repo_scanner_and_branch()
183+
184+
scan_read = ScanRead(id_=1,
185+
branch_id=1,
186+
scan_type=ScanType.BASE,
187+
last_scanned_commit="latest_commit_1",
188+
timestamp=datetime.utcnow(),
189+
increment_number=0,
190+
rule_pack="2.0.1")
191+
192+
scan_type = secret_scanner.determine_scan_type(branch, scan_read)
193+
assert scan_type == ScanType.INCREMENTAL
194+
195+
196+
def test_scan_type_is_incremental_when_a_latest_scan_is_present_and_rule_pack_is_same_and_last_commit_is_newer():
197+
repository, branch, secret_scanner = initialize_and_get_repo_scanner_and_branch()
198+
199+
scan_read = ScanRead(id_=1,
200+
branch_id=1,
201+
scan_type=ScanType.BASE,
202+
last_scanned_commit="latest_commit_1",
203+
timestamp=datetime.utcnow(),
204+
increment_number=0,
205+
rule_pack="2.0.1")
206+
207+
scan_type = secret_scanner.determine_scan_type(branch, scan_read)
208+
assert scan_type == ScanType.INCREMENTAL

components/resc-vcs-scanner/tests/vcs_scanner/secret_scanners/test_stdout_writer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_get_last_scanned_commit():
124124
repository_id=1)
125125

126126
result = STDOUTWriter(toml_rule_file_path="toml_path", exit_code_warn=2, exit_code_block=1) \
127-
.get_last_scanned_commit(branch)
127+
.get_last_scan_for_branch(branch)
128128
assert result is None
129129

130130

images/RESC_Scan_Flow_Diagram.png

9.22 KB
Loading

0 commit comments

Comments
 (0)