Skip to content

Commit f8432a3

Browse files
committed
added proxy and cert options
1 parent ec4f33e commit f8432a3

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/scanoss/scanner.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def __init__(self, wfp: str = None, scan_output: str = None, output_format: str
9393
post_size: int = 64, timeout: int = 120, no_wfp_file: bool = False,
9494
all_extensions: bool = False, all_folders: bool = False, hidden_files_folders: bool = False,
9595
scan_options: int = 7, sc_timeout: int = 600, sc_command: str = None, grpc_url: str = None,
96-
obfuscate: bool = False, ignore_cert_errors: bool = False
96+
obfuscate: bool = False, ignore_cert_errors: bool = False, proxy: str = None, ca_cert: str = None
9797
):
9898
"""
9999
Initialise scanning class, including Winnowing, ScanossApi and ThreadedScanning
@@ -116,11 +116,12 @@ def __init__(self, wfp: str = None, scan_output: str = None, output_format: str
116116
)
117117
self.scanoss_api = ScanossApi(debug=debug, trace=trace, quiet=quiet, api_key=api_key, url=url,
118118
sbom_path=sbom_path, scan_type=scan_type, flags=flags, timeout=timeout,
119-
ver_details=ver_details, ignore_cert_errors=ignore_cert_errors
119+
ver_details=ver_details, ignore_cert_errors=ignore_cert_errors,
120+
proxy=proxy, ca_cert=ca_cert
120121
)
121122
sc_deps = ScancodeDeps(debug=debug, quiet=quiet, trace=trace, timeout=sc_timeout, sc_command=sc_command)
122123
grpc_api = ScanossGrpc(url=grpc_url, debug=debug, quiet=quiet, trace=trace, api_key=api_key,
123-
ver_details=ver_details
124+
ver_details=ver_details, ca_cert=ca_cert
124125
)
125126
self.threaded_deps = ThreadedDependencies(sc_deps, grpc_api, debug=debug, quiet=quiet, trace=trace)
126127
self.nb_threads = nb_threads

src/scanoss/scanossapi.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
import requests
3030
import uuid
3131
import http.client as http_client
32+
import urllib3
3233

34+
from urllib3.exceptions import InsecureRequestWarning
3335
from .scanossbase import ScanossBase
34-
from requests.packages.urllib3.exceptions import InsecureRequestWarning
3536

36-
DEFAULT_URL = "https://osskb.org/api/scan/direct"
37+
DEFAULT_URL = "https://osskb.org/api/scan/direct" # default free service URL
38+
DEFAULT_URL2 = "https://scanoss.com/api/scan/direct" # default premium service URL
3739
SCANOSS_SCAN_URL = os.environ.get("SCANOSS_SCAN_URL") if os.environ.get("SCANOSS_SCAN_URL") else DEFAULT_URL
3840
SCANOSS_API_KEY = os.environ.get("SCANOSS_API_KEY") if os.environ.get("SCANOSS_API_KEY") else ''
3941

@@ -46,7 +48,8 @@ class ScanossApi(ScanossBase):
4648

4749
def __init__(self, scan_type: str = None, sbom_path: str = None, scan_format: str = None, flags: str = None,
4850
url: str = None, api_key: str = None, debug: bool = False, trace: bool = False, quiet: bool = False,
49-
timeout: int = 120, ver_details: str = None, ignore_cert_errors: bool = False):
51+
timeout: int = 120, ver_details: str = None, ignore_cert_errors: bool = False,
52+
proxy: str = None, ca_cert: str = None):
5053
"""
5154
Initialise the SCANOSS API
5255
:param scan_type: Scan type (default identify)
@@ -61,11 +64,15 @@ def __init__(self, scan_type: str = None, sbom_path: str = None, scan_format: st
6164
6265
To set a custom certificate use:
6366
REQUESTS_CA_BUNDLE=/path/to/cert.pem
64-
SSL_CERT_FILE=/path/to/cert.pem
67+
To enable a Proxy use:
68+
HTTP_PROXY='http://<ip>:<port>'
69+
HTTPS_PROXY='http://<ip>:<port>'
6570
"""
6671
super().__init__(debug, trace, quiet)
6772
self.url = url if url else SCANOSS_SCAN_URL
6873
self.api_key = api_key if api_key else SCANOSS_API_KEY
74+
if self.api_key and not url and not os.environ.get("SCANOSS_SCAN_URL"):
75+
self.url = DEFAULT_URL2 # API key specific and no alternative URL, so use the default premium
6976
self.scan_type = scan_type
7077
self.scan_format = scan_format if scan_format else 'plain'
7178
self.sbom_path = sbom_path
@@ -83,9 +90,14 @@ def __init__(self, scan_type: str = None, sbom_path: str = None, scan_format: st
8390
if self.trace:
8491
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
8592
http_client.HTTPConnection.debuglevel = 1
93+
self.verify = None
8694
if self.ignore_cert_errors:
8795
self.print_debug(f'Ignoring cert errors...')
88-
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
96+
urllib3.disable_warnings(InsecureRequestWarning)
97+
self.verify = False
98+
elif ca_cert:
99+
self.verify = ca_cert
100+
self.proxies = {'https': proxy, 'http': proxy} if proxy else None
89101

90102
def load_sbom(self):
91103
"""
@@ -124,10 +136,9 @@ def scan(self, wfp: str, context: str = None, scan_id: int = None):
124136
try:
125137
r = None
126138
r = requests.post(self.url, files=scan_files, data=form_data, headers=self.headers,
127-
timeout=self.timeout,
128-
verify=False if self.ignore_cert_errors else None
139+
timeout=self.timeout, verify=self.verify, proxies=self.proxies
129140
)
130-
except requests.exceptions.SSLError as e:
141+
except (requests.exceptions.SSLError, requests.exceptions.ProxyError) as e:
131142
self.print_stderr(f'ERROR: Exception ({e.__class__.__name__}) POSTing data - {e}.')
132143
raise Exception(f"ERROR: The SCANOSS API request failed for {self.url}") from e
133144
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e:

0 commit comments

Comments
 (0)