socketdev 3.6.1__py3-none-any.whl → 3.6.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- socketdev/__init__.py +5 -9
- socketdev/core/api.py +7 -1
- socketdev/core/retired_token.py +34 -0
- socketdev/core/token_selection.py +19 -0
- socketdev/fullscans/__init__.py +61 -4
- socketdev/version.py +1 -1
- {socketdev-3.6.1.dist-info → socketdev-3.6.2.dist-info}/METADATA +12 -2
- {socketdev-3.6.1.dist-info → socketdev-3.6.2.dist-info}/RECORD +10 -8
- {socketdev-3.6.1.dist-info → socketdev-3.6.2.dist-info}/WHEEL +0 -0
- {socketdev-3.6.1.dist-info → socketdev-3.6.2.dist-info}/licenses/LICENSE +0 -0
socketdev/__init__.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import os
|
|
2
2
|
from socketdev.core.api import API
|
|
3
|
+
from socketdev.core.retired_token import reject_retired_token
|
|
4
|
+
from socketdev.core.token_selection import select_token
|
|
3
5
|
from socketdev.dependencies import Dependencies
|
|
4
6
|
from socketdev.diffscans import DiffScans
|
|
5
7
|
from socketdev.export import Export
|
|
@@ -53,21 +55,15 @@ request_timeout = 1200
|
|
|
53
55
|
|
|
54
56
|
class socketdev:
|
|
55
57
|
def __init__(self, token: Optional[str] = None, timeout: int = 1200, allow_unverified: bool = False, user_agent: Optional[str] = None):
|
|
56
|
-
|
|
57
|
-
if token is None:
|
|
58
|
-
token = (
|
|
59
|
-
os.getenv("SOCKET_SECURITY_API_TOKEN") or
|
|
60
|
-
os.getenv("SOCKET_SECURITY_API_KEY") or
|
|
61
|
-
os.getenv("SOCKET_API_KEY") or
|
|
62
|
-
os.getenv("SOCKET_API_TOKEN")
|
|
63
|
-
)
|
|
64
|
-
|
|
58
|
+
token = select_token(token, os.environ)
|
|
65
59
|
if token is None:
|
|
66
60
|
raise ValueError(
|
|
67
61
|
"API token is required. Provide it as a parameter or set one of these environment variables: "
|
|
68
62
|
"SOCKET_SECURITY_API_TOKEN, SOCKET_SECURITY_API_KEY, SOCKET_API_KEY, SOCKET_API_TOKEN"
|
|
69
63
|
)
|
|
70
64
|
|
|
65
|
+
reject_retired_token(token)
|
|
66
|
+
|
|
71
67
|
self.api = API()
|
|
72
68
|
self.token = token + ":"
|
|
73
69
|
self.api.encode_key(self.token)
|
socketdev/core/api.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import base64
|
|
2
|
+
from typing import Optional
|
|
2
3
|
from socketdev.log import log
|
|
4
|
+
from socketdev.core.retired_token import reject_retired_basic_auth_token
|
|
3
5
|
|
|
4
6
|
import requests
|
|
5
7
|
from socketdev.core.classes import Response
|
|
@@ -29,6 +31,7 @@ class API:
|
|
|
29
31
|
self.user_agent = None
|
|
30
32
|
|
|
31
33
|
def encode_key(self, token: str):
|
|
34
|
+
reject_retired_basic_auth_token(token)
|
|
32
35
|
self.encoded_key = base64.b64encode(token.encode()).decode("ascii")
|
|
33
36
|
|
|
34
37
|
def set_timeout(self, timeout: int):
|
|
@@ -47,6 +50,8 @@ class API:
|
|
|
47
50
|
payload: [dict, str] = None,
|
|
48
51
|
files: list = None,
|
|
49
52
|
method: str = "GET",
|
|
53
|
+
*,
|
|
54
|
+
request_timeout: Optional[float] = None,
|
|
50
55
|
) -> Response:
|
|
51
56
|
if self.encoded_key is None or self.encoded_key == "":
|
|
52
57
|
raise APIKeyMissing
|
|
@@ -68,7 +73,8 @@ class API:
|
|
|
68
73
|
|
|
69
74
|
response = requests.request(
|
|
70
75
|
method.upper(), url, headers=headers, data=payload, files=files,
|
|
71
|
-
timeout=self.request_timeout
|
|
76
|
+
timeout=self.request_timeout if request_timeout is None else request_timeout,
|
|
77
|
+
verify=not self.allow_unverified
|
|
72
78
|
)
|
|
73
79
|
request_duration = time.time() - start_time
|
|
74
80
|
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import hmac
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
RETIRED_TOKEN_SHA256 = "9cab58b24140ee12c08e265729d246f9200f8ef9b1d884f90c06c4e6b69adf18"
|
|
6
|
+
RETIRED_TOKEN_ERROR = (
|
|
7
|
+
"This API token has been retired. Create a new API token in Socket "
|
|
8
|
+
"and update your configuration."
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# Fingerprint identifies a retired credential; it is not a password verifier.
|
|
13
|
+
def _matches_retired_digest(token: Optional[str], digest: str) -> bool:
|
|
14
|
+
if token is None:
|
|
15
|
+
return False
|
|
16
|
+
token_digest = hashlib.sha256(token.strip().encode("utf-8")).hexdigest()
|
|
17
|
+
return hmac.compare_digest(token_digest, digest)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _reject_retired_token(token: Optional[str], digest: str) -> None:
|
|
21
|
+
if _matches_retired_digest(token, digest):
|
|
22
|
+
raise ValueError(RETIRED_TOKEN_ERROR)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _reject_basic_auth_token(token: str, digest: str) -> None:
|
|
26
|
+
_reject_retired_token(token.partition(":")[0], digest)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def reject_retired_token(token: Optional[str]) -> None:
|
|
30
|
+
_reject_retired_token(token, RETIRED_TOKEN_SHA256)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def reject_retired_basic_auth_token(token: str) -> None:
|
|
34
|
+
_reject_basic_auth_token(token, RETIRED_TOKEN_SHA256)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from typing import Mapping, Optional
|
|
2
|
+
|
|
3
|
+
TOKEN_ENVIRONMENT_VARIABLES = (
|
|
4
|
+
"SOCKET_SECURITY_API_TOKEN",
|
|
5
|
+
"SOCKET_SECURITY_API_KEY",
|
|
6
|
+
"SOCKET_API_KEY",
|
|
7
|
+
"SOCKET_API_TOKEN",
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def select_token(
|
|
12
|
+
explicit_token: Optional[str], environment: Mapping[str, str]
|
|
13
|
+
) -> Optional[str]:
|
|
14
|
+
if explicit_token is not None:
|
|
15
|
+
return explicit_token
|
|
16
|
+
return next(
|
|
17
|
+
(environment[name] for name in TOKEN_ENVIRONMENT_VARIABLES if environment.get(name)),
|
|
18
|
+
None,
|
|
19
|
+
)
|
socketdev/fullscans/__init__.py
CHANGED
|
@@ -4,9 +4,12 @@ from enum import Enum
|
|
|
4
4
|
from typing import Any, Dict, List, Optional, Union
|
|
5
5
|
from dataclasses import dataclass, asdict, field
|
|
6
6
|
import urllib.parse
|
|
7
|
+
import math
|
|
8
|
+
import time
|
|
7
9
|
from ..core.dedupe import Dedupe
|
|
8
10
|
from ..core.enums import unknown_enum_value
|
|
9
11
|
from ..utils import IntegrationType, Utils
|
|
12
|
+
from ..exceptions import APIFailure, APITimeout
|
|
10
13
|
|
|
11
14
|
log = logging.getLogger("socketdev")
|
|
12
15
|
|
|
@@ -960,15 +963,65 @@ class FullScans:
|
|
|
960
963
|
)
|
|
961
964
|
return {}
|
|
962
965
|
|
|
963
|
-
def stream(
|
|
964
|
-
|
|
965
|
-
|
|
966
|
+
def stream(
|
|
967
|
+
self,
|
|
968
|
+
org_slug: str,
|
|
969
|
+
full_scan_id: str,
|
|
970
|
+
use_types: bool = False,
|
|
971
|
+
*,
|
|
972
|
+
poll_timeout_seconds: Optional[float] = None,
|
|
973
|
+
poll_initial_interval_seconds: float = 5.0,
|
|
974
|
+
poll_max_interval_seconds: float = 10.0,
|
|
975
|
+
) -> Union[dict, FullScanStreamResponse]:
|
|
976
|
+
request_timeout = self.api.request_timeout
|
|
977
|
+
timeout = request_timeout if poll_timeout_seconds is None else poll_timeout_seconds
|
|
978
|
+
values = (timeout, poll_initial_interval_seconds, poll_max_interval_seconds, request_timeout)
|
|
979
|
+
if any(
|
|
980
|
+
isinstance(value, bool)
|
|
981
|
+
or not isinstance(value, (int, float))
|
|
982
|
+
or not math.isfinite(value)
|
|
983
|
+
or value <= 0
|
|
984
|
+
for value in values
|
|
985
|
+
):
|
|
986
|
+
raise ValueError("Full scan polling timeout and intervals must be positive finite numbers")
|
|
987
|
+
if poll_initial_interval_seconds > poll_max_interval_seconds:
|
|
988
|
+
raise ValueError("Initial polling interval must not exceed maximum polling interval")
|
|
989
|
+
|
|
990
|
+
path = (
|
|
991
|
+
"orgs/" + urllib.parse.quote(org_slug, safe="") + "/full-scans/"
|
|
992
|
+
+ urllib.parse.quote(full_scan_id, safe="") + "?cached=true"
|
|
993
|
+
)
|
|
994
|
+
deadline = time.monotonic() + timeout
|
|
995
|
+
interval = poll_initial_interval_seconds
|
|
996
|
+
while True:
|
|
997
|
+
retry_error = None
|
|
998
|
+
try:
|
|
999
|
+
response = self.api.do_request(
|
|
1000
|
+
path=path,
|
|
1001
|
+
method="GET",
|
|
1002
|
+
request_timeout=min(float(request_timeout), max(deadline - time.monotonic(), 0.001)),
|
|
1003
|
+
)
|
|
1004
|
+
except APIFailure as error:
|
|
1005
|
+
if not error.is_transient_error():
|
|
1006
|
+
raise
|
|
1007
|
+
retry_error = error
|
|
1008
|
+
else:
|
|
1009
|
+
if response.status_code != 202:
|
|
1010
|
+
break
|
|
1011
|
+
response.close()
|
|
1012
|
+
# Give up now if the next attempt could not start before the deadline,
|
|
1013
|
+
# rather than sleeping out the rest of the budget and never polling again.
|
|
1014
|
+
if deadline - time.monotonic() <= interval:
|
|
1015
|
+
raise APITimeout("Timed out waiting for full scan results") from retry_error
|
|
1016
|
+
time.sleep(interval)
|
|
1017
|
+
interval = min(poll_max_interval_seconds, interval * 1.5)
|
|
966
1018
|
|
|
967
1019
|
if response.status_code == 200:
|
|
968
1020
|
try:
|
|
969
1021
|
stream_str = []
|
|
970
1022
|
artifacts = {}
|
|
971
1023
|
result = response.text
|
|
1024
|
+
response.close()
|
|
972
1025
|
result = result.strip('"').strip()
|
|
973
1026
|
for line in result.split("\n"):
|
|
974
1027
|
if line != '"' and line != "" and line is not None:
|
|
@@ -993,6 +1046,7 @@ class FullScans:
|
|
|
993
1046
|
return artifacts
|
|
994
1047
|
|
|
995
1048
|
except Exception as e:
|
|
1049
|
+
response.close()
|
|
996
1050
|
error_message = f"Error parsing stream response: {str(e)}"
|
|
997
1051
|
log.error(error_message)
|
|
998
1052
|
if use_types:
|
|
@@ -1001,7 +1055,10 @@ class FullScans:
|
|
|
1001
1055
|
)
|
|
1002
1056
|
return {}
|
|
1003
1057
|
|
|
1004
|
-
|
|
1058
|
+
try:
|
|
1059
|
+
error_message = response.json().get("error", {}).get("message", "Unknown error")
|
|
1060
|
+
finally:
|
|
1061
|
+
response.close()
|
|
1005
1062
|
log.error(f"Error streaming full scan: {response.status_code}, message: {error_message}")
|
|
1006
1063
|
if use_types:
|
|
1007
1064
|
return FullScanStreamResponse.from_dict(
|
socketdev/version.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
__version__ = "3.6.
|
|
1
|
+
__version__ = "3.6.2"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: socketdev
|
|
3
|
-
Version: 3.6.
|
|
3
|
+
Version: 3.6.2
|
|
4
4
|
Summary: Socket Security Python SDK
|
|
5
5
|
Project-URL: Homepage, https://github.com/SocketDev/socket-sdk-python
|
|
6
6
|
Author-email: Douglas Coburn <douglas@socket.dev>
|
|
@@ -342,7 +342,11 @@ Stream a diff between two full scans. Returns a scan diff.
|
|
|
342
342
|
|
|
343
343
|
fullscans.stream(org_slug, full_scan_id, use_types=False)
|
|
344
344
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|
345
|
-
Stream all SBOM artifacts for a full scan.
|
|
345
|
+
Stream all SBOM artifacts for a full scan. If the scan is still being analyzed, the SDK polls
|
|
346
|
+
cached results until they are ready, backing off from 5 to 10 seconds between attempts, and
|
|
347
|
+
raises ``APITimeout`` if they are not ready within the polling budget. Transient failures
|
|
348
|
+
(HTTP 408/502/503/504, timeouts, dropped connections) are retried within the budget; other
|
|
349
|
+
errors raise immediately.
|
|
346
350
|
|
|
347
351
|
**Usage:**
|
|
348
352
|
|
|
@@ -355,11 +359,17 @@ Stream all SBOM artifacts for a full scan.
|
|
|
355
359
|
# With typed response
|
|
356
360
|
print(socket.fullscans.stream("org_slug", "full_scan_id", use_types=True))
|
|
357
361
|
|
|
362
|
+
# Wait up to 10 minutes for analysis to finish
|
|
363
|
+
print(socket.fullscans.stream("org_slug", "full_scan_id", poll_timeout_seconds=600))
|
|
364
|
+
|
|
358
365
|
**PARAMETERS:**
|
|
359
366
|
|
|
360
367
|
- **org_slug (str)** - The organization name
|
|
361
368
|
- **full_scan_id (str)** - The ID of the full scan
|
|
362
369
|
- **use_types (bool)** - Whether to return typed response objects (default: False)
|
|
370
|
+
- **poll_timeout_seconds (float)** - Keyword-only. Total time to wait for results (default: the SDK's configured ``timeout``)
|
|
371
|
+
- **poll_initial_interval_seconds (float)** - Keyword-only. Wait before the first retry (default: 5.0)
|
|
372
|
+
- **poll_max_interval_seconds (float)** - Keyword-only. Longest wait between retries (default: 10.0)
|
|
363
373
|
|
|
364
374
|
fullscans.metadata(org_slug, full_scan_id, use_types=False)
|
|
365
375
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
socketdev/__init__.py,sha256=
|
|
1
|
+
socketdev/__init__.py,sha256=4S3ApgU21s_1Wd4bEfife4ifAzmrdzLMS2gvft1a6Vo,4224
|
|
2
2
|
socketdev/exceptions.py,sha256=yr1O1wkJUi-pvXQuqdAj4phNggHmT2N57I6DOdGdsgI,4225
|
|
3
3
|
socketdev/log.py,sha256=AgEcsPN3y9U-fze8pTFvLigVvNob2vmNC-TkOOQkkeQ,90
|
|
4
|
-
socketdev/version.py,sha256=
|
|
4
|
+
socketdev/version.py,sha256=JuDKlvRQpMKwL3bc24_MxmGe7_wOb6HCm8Kz4Ba4dac,22
|
|
5
5
|
socketdev/alertfullscansearch/__init__.py,sha256=YwXrgnrI9z9rAexXFFmoXQcaQdGW47HRhQ58CQBCG3U,884
|
|
6
6
|
socketdev/alerts/__init__.py,sha256=kGLCNXtXTRz3vl2s_bPQ7xuds_QGwNQnah1TfHzp2MU,846
|
|
7
7
|
socketdev/alerttypes/__init__.py,sha256=W-m2D9VxbepSAUELJPmWmX40zksGZbQcSzwQ1kjlB1k,1169
|
|
@@ -9,16 +9,18 @@ socketdev/analytics/__init__.py,sha256=kQGeO67TraqvfmYjfpYCvcNRtxW2a6x5EefEl6Jnc
|
|
|
9
9
|
socketdev/apitokens/__init__.py,sha256=Z6W9Jkh4gdDeJBivfcONZuz7jRKZtxWLTi1n9gkbYVE,4222
|
|
10
10
|
socketdev/auditlog/__init__.py,sha256=vjcVtOK6oujqC6-RW5Hrvmth5BqFkLdinbeyqtycOpA,881
|
|
11
11
|
socketdev/basics/__init__.py,sha256=9jTEwC4dMUV-9pKpU4twK-hM1cY0xeXAxjDrb37sxoQ,4067
|
|
12
|
-
socketdev/core/api.py,sha256=
|
|
12
|
+
socketdev/core/api.py,sha256=JROfXLRZ4IZApCpKw5MJFX21NIAdeBwIzU5oWxAC-3g,6622
|
|
13
13
|
socketdev/core/classes.py,sha256=jY1oyokpSVDA2XckG1pdVOP5PtxDS8CWta8PX6eZFJc,3608
|
|
14
14
|
socketdev/core/dedupe.py,sha256=Fy_HGU77jxt7Dz8TF7AYkEklwtu_xFA40ripaYyBkqs,5774
|
|
15
15
|
socketdev/core/enums.py,sha256=daY3vdm4veuYKCWenFOIsGsga85xzkiLfcygb5F1LSk,1416
|
|
16
16
|
socketdev/core/issues.py,sha256=SKhJz7SrglozeuEk72CbbuCM7X0hgzNMk55uM4wX-TI,68081
|
|
17
|
+
socketdev/core/retired_token.py,sha256=uwPS_1p1e3JzC8yPD3bBVPhhLsqWsTbChorHEdapLMo,1126
|
|
18
|
+
socketdev/core/token_selection.py,sha256=baV-NkykdNeY6_w2CtUNBLfnHeEeAo8B04OzvaMSJ2c,484
|
|
17
19
|
socketdev/dependencies/__init__.py,sha256=eKxcGjRm8IcW_PGnOyr4QQYOq6TnB4xPjooncdVr03g,2117
|
|
18
20
|
socketdev/diffscans/__init__.py,sha256=NCvjirhFGWD93miL87UObQ7SeInBEkNt5R28uLIKYi0,7004
|
|
19
21
|
socketdev/export/__init__.py,sha256=5DqVqHnaK5Aq-ueSTIAplOZJpF21W_pXysIzmMNLGPg,3667
|
|
20
22
|
socketdev/fixes/__init__.py,sha256=ouRSQ5FcqQcjAmrU8aAgd0ZJeo431wBOEVKLR36etik,856
|
|
21
|
-
socketdev/fullscans/__init__.py,sha256=
|
|
23
|
+
socketdev/fullscans/__init__.py,sha256=YYE-deKBbzaLlBOx3ibUlmxS6lOIB9hWw63lueNq1gw,43059
|
|
22
24
|
socketdev/historical/__init__.py,sha256=DXFmRrHLWHKbZrB8eermM4YG6j4C1S4Lz6-V63_p2gs,3472
|
|
23
25
|
socketdev/labels/__init__.py,sha256=Ep19ZraKqi_a-3-eAOWGhPNQsgwGRY9N28Oyxlxrz-g,5128
|
|
24
26
|
socketdev/licensemetadata/__init__.py,sha256=pDcvpkc08QtMqhsnKP1Cfjvs3m-MRPYUK-uLh8aRkrU,815
|
|
@@ -40,7 +42,7 @@ socketdev/triage/__init__.py,sha256=-h599vThVetfRSOAs1e4IUoMjcXS8xOsUfub41XPiX4,
|
|
|
40
42
|
socketdev/uploadmanifests/__init__.py,sha256=-sFHbOW-aw7U0wRfEJT_Edrzx8aCbzD9cTGmVTNJKpw,6043
|
|
41
43
|
socketdev/utils/__init__.py,sha256=E11UJTsWsN-ZurOwtKvPGOTc9UMP3ND79nPbmNRRQ6E,17666
|
|
42
44
|
socketdev/webhooks/__init__.py,sha256=fme6K7ma_Gvk_TFOLz9jVcR5QJbDpMHXqRaU3x_RShY,3507
|
|
43
|
-
socketdev-3.6.
|
|
44
|
-
socketdev-3.6.
|
|
45
|
-
socketdev-3.6.
|
|
46
|
-
socketdev-3.6.
|
|
45
|
+
socketdev-3.6.2.dist-info/METADATA,sha256=jmF51sPasfq9EbFnHbaI93gT5l661j8nscSXp0e8qNs,46523
|
|
46
|
+
socketdev-3.6.2.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
47
|
+
socketdev-3.6.2.dist-info/licenses/LICENSE,sha256=mC0II_UuE-_gCG9XqOLNyrV3crnROzMpdAetGnMrhpw,1068
|
|
48
|
+
socketdev-3.6.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|