|
1 | 1 | #!/usr/bin/python3
|
2 | 2 | import logging, argparse, utils, asyncio
|
3 | 3 | from cxone_api import CxOneClient
|
| 4 | +from cxone_api.scanning import ScanInvoker |
| 5 | +from cxone_api.projects import ProjectRepoConfig |
| 6 | +from cxone_api.util import json_on_ok |
4 | 7 | from posix_ipc import Semaphore, BusyError, O_CREAT
|
5 | 8 |
|
6 | 9 | utils.configure_normal_logging()
|
|
15 | 18 | parser.add_argument('--branch', '-b', action='store', type=str, required=True, dest="branch", help="The code repository URL.")
|
16 | 19 | parser.add_argument('--schedule', '-s', action='store', type=str, required=False, default='unknown', dest="schedule", help="The schedule string assigned to the 'scheduled' scan tag.")
|
17 | 20 |
|
| 21 | + |
| 22 | +async def should_scan(client : CxOneClient, project_repo : ProjectRepoConfig, branch : str) -> bool: |
| 23 | + if not await project_repo.is_scm_imported: |
| 24 | + running_scans = json_on_ok(await client.get_scans(tags_keys="scheduled", branch=branch, project_id=project_repo.project_id, statuses=['Queued', 'Running'])) |
| 25 | + if int(running_scans['filteredTotalCount']) == 0: |
| 26 | + return True |
| 27 | + else: |
| 28 | + # It currently isn't possible to tag a scan created in a project that was import from SCM, so just look |
| 29 | + # at the last scan in status Queued or Running. |
| 30 | + potential_running_scan = json_on_ok(await client.get_projects_last_scan(branch=branch, limit=1, project_ids=[project_repo.project_id], scan_status="Running")) |
| 31 | + potential_queued_scan = json_on_ok(await client.get_projects_last_scan(branch=branch, limit=1, project_ids=[project_repo.project_id], scan_status="Queued")) |
| 32 | + |
| 33 | + if project_repo.project_id in potential_running_scan.keys() or project_repo.project_id in potential_queued_scan.keys(): |
| 34 | + return True |
| 35 | + |
| 36 | + return False |
| 37 | + |
| 38 | + |
18 | 39 | async def main():
|
19 | 40 | try:
|
20 | 41 | args = parser.parse_args()
|
@@ -51,31 +72,18 @@ async def main():
|
51 | 72 |
|
52 | 73 | try:
|
53 | 74 | __log.debug(f"Semaphore acquired for {utils.make_safe_name(args.projectid, args.branch)}")
|
| 75 | + |
| 76 | + project_repo = await ProjectRepoConfig.from_project_id(client, args.projectid) |
54 | 77 |
|
55 | 78 | # Do not submit a scheduled scan if a scheduled scan is already running.
|
56 |
| - scans = (await client.get_scans(tags_keys="scheduled", branch=args.branch, project_id=args.projectid, statuses=['Queued', 'Running'])).json() |
57 |
| - |
58 |
| - if scans['filteredTotalCount'] == 0: |
59 |
| - scan_spec = { |
60 |
| - "type" : "git", |
61 |
| - "handler" : { |
62 |
| - "branch" : args.branch, |
63 |
| - "repoUrl" : args.repo |
64 |
| - }, |
65 |
| - "project" : { |
66 |
| - "id" : args.projectid, |
67 |
| - }, |
68 |
| - "config" : [{ "type" : x, "value" : {} } for x in args.engines], |
69 |
| - "tags" : tag |
70 |
| - } |
71 |
| - |
72 |
| - |
73 |
| - response = await client.execute_scan(scan_spec) |
74 |
| - if response.ok: |
| 79 | + if await should_scan(client, project_repo, args.branch): |
| 80 | + |
| 81 | + scan_response = await ScanInvoker.scan_get_response(client, project_repo, args.branch, args.engines, tag) |
| 82 | + |
| 83 | + if scan_response.ok: |
75 | 84 | __log.info(f"Scanning project {args.projectid} branch {args.branch}")
|
76 | 85 | else:
|
77 |
| - __log.error(f"Failed to start scan for project {args.projectid} branch {args.branch}: {response.status_code}:{response.reason}") |
78 |
| - |
| 86 | + __log.error(f"Failed to start scan for project {args.projectid} branch {args.branch}: {scan_response.status_code}:{scan_response.json()}") |
79 | 87 |
|
80 | 88 | else:
|
81 | 89 | __log.warning(f"Scheduled scan for project {args.projectid} branch {args.branch} is already running, skipping.")
|
|
0 commit comments