-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.py
44 lines (37 loc) · 1.3 KB
/
scan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import subprocess
import json
import logging
from typing import Tuple, Optional, Dict, Any
import re
def make_filename_compatible(input_string):
# Define a replacement dictionary for characters that are not allowed in filenames
replacements = {
'/': '_',
':': '-',
'@': '_at_',
'#': '_hash_',
'%': '_percent_',
'&': '_and_',
'*': '_star_',
'?': '_question_',
' ': '_',
'.': '_',
'-': '_'
}
# Replace each forbidden character with its replacement
for forbidden_char, replacement in replacements.items():
input_string = input_string.replace(forbidden_char, replacement)
# Remove any other characters that are not alphanumeric or underscores
input_string = re.sub(r'[^A-Za-z0-9_\-\.]', '', input_string)
return input_string
def scan_image(image: str) -> Tuple[Optional[Dict[str, Any]], Optional[str]]:
result = subprocess.run(
["trivy", "--scanners", "vuln", "image", "-f", "json", "--severity", "HIGH,CRITICAL", image],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
if result.returncode != 0:
logging.error(f"Error scanning image {image}: {result.stderr}")
return None, result.stderr
return json.loads(result.stdout), None