Skip to content

Commit be73a05

Browse files
Merge pull request #25 from FiniteStateInc/ASOC-2714
[ASOC-2714] fix multipart code and chun ksize
2 parents ea412e6 + 8237183 commit be73a05

File tree

7 files changed

+4481
-4454
lines changed

7 files changed

+4481
-4454
lines changed

docs/finite_state_sdk.html

Lines changed: 4375 additions & 4366 deletions
Large diffs are not rendered by default.

docs/finite_state_sdk/queries.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ <h2>API Documentation</h2>
5656
</ul>
5757

5858

59-
<footer>finite-state-sdk-python v0.1.9</footer>
59+
<footer>finite-state-sdk-python v0.1.10</footer>
6060

6161
<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
6262
built with <span class="visually-hidden">pdoc</span><img

docs/finite_state_sdk/token_cache.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ <h2>API Documentation</h2>
6565
</ul>
6666

6767

68-
<footer>finite-state-sdk-python v0.1.9</footer>
68+
<footer>finite-state-sdk-python v0.1.10</footer>
6969

7070
<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
7171
built with <span class="visually-hidden">pdoc</span><img

docs/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

finite_state_sdk/__init__.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
AUDIENCE = "https://platform.finitestate.io/api/v1/graphql"
1111
TOKEN_URL = "https://platform.finitestate.io/api/v1/auth/token"
1212

13+
"""
14+
DEFAULT CHUNK SIZE: 1000 MiB
15+
"""
16+
DEFAULT_CHUNK_SIZE = 1024**2 * 1000
17+
"""
18+
MAX CHUNK SIZE: 2 GiB
19+
"""
20+
MAX_CHUNK_SIZE = 1024**2 * 2000
21+
"""
22+
MIN CHUNK SIZE: 5 MiB
23+
"""
24+
MIN_CHUNK_SIZE = 1024**2 * 5
25+
1326

1427
class UploadMethod(Enum):
1528
"""
@@ -1094,15 +1107,15 @@ def download_sbom(token, organization_context, sbom_type="CYCLONEDX", sbom_subty
10941107
raise Exception(f"Failed to download the file. Status code: {response.status_code}")
10951108

10961109

1097-
def file_chunks(file_path, chunk_size=1024 * 1024 * 1024 * 5):
1110+
def file_chunks(file_path, chunk_size=DEFAULT_CHUNK_SIZE):
10981111
"""
10991112
Helper method to read a file in chunks.
11001113
11011114
Args:
11021115
file_path (str):
11031116
Local path to the file to read.
11041117
chunk_size (int, optional):
1105-
The size of the chunks to read. Defaults to 5GB.
1118+
The size of the chunks to read. Defaults to DEFAULT_CHUNK_SIZE.
11061119
11071120
Yields:
11081121
bytes: The next chunk of the file.
@@ -2011,10 +2024,11 @@ def update_finding_statuses(token, organization_context, user_id=None, finding_i
20112024
return send_graphql_query(token, organization_context, mutation, variables)
20122025

20132026

2014-
def upload_file_for_binary_analysis(token, organization_context, test_id=None, file_path=None,
2015-
chunk_size=1024 * 1024 * 1024 * 5, quick_scan=False):
2027+
def upload_file_for_binary_analysis(
2028+
token, organization_context, test_id=None, file_path=None, chunk_size=DEFAULT_CHUNK_SIZE, quick_scan=False
2029+
):
20162030
"""
2017-
Upload a file for Binary Analysis. Will automatically chunk the file into chunks and upload each chunk. Chunk size defaults to 5GB.
2031+
Upload a file for Binary Analysis. Will automatically chunk the file into chunks and upload each chunk.
20182032
NOTE: This is NOT for uploading third party scanner results. Use upload_test_results_file for that.
20192033
20202034
Args:
@@ -2027,7 +2041,7 @@ def upload_file_for_binary_analysis(token, organization_context, test_id=None, f
20272041
file_path (str, required):
20282042
Local path to the file to upload.
20292043
chunk_size (int, optional):
2030-
The size of the chunks to read. Defaults to 5GB.
2044+
The size of the chunks to read. 1000 MiB by default. Min 5MiB and max 2GiB.
20312045
quick_scan (bool, optional):
20322046
If True, will perform a quick scan of the Binary. Defaults to False (Full Scan). For details, please see the API documentation.
20332047
@@ -2039,11 +2053,14 @@ def upload_file_for_binary_analysis(token, organization_context, test_id=None, f
20392053
dict: The response from the GraphQL query, a completeMultipartUpload Object.
20402054
"""
20412055
# To upload a file for Binary Analysis, you must use the generateMultiplePartUploadUrl mutation
2042-
20432056
if not test_id:
20442057
raise ValueError("Test Id is required")
20452058
if not file_path:
20462059
raise ValueError("File Path is required")
2060+
if chunk_size < MIN_CHUNK_SIZE:
2061+
raise ValueError(f"Chunk size must be greater than {MIN_CHUNK_SIZE} bytes")
2062+
if chunk_size >= MAX_CHUNK_SIZE:
2063+
raise ValueError(f"Chunk size must be less than {MAX_CHUNK_SIZE} bytes")
20472064

20482065
# Start Multi-part Upload
20492066
graphql_query = '''
@@ -2067,9 +2084,10 @@ def upload_file_for_binary_analysis(token, organization_context, test_id=None, f
20672084
# if the file is greater than max chunk size (or 5 GB), split the file in chunks,
20682085
# call generateUploadPartUrlV2 for each chunk of the file (even if it is a single part)
20692086
# and upload the file to the returned upload URL
2070-
i = 1
2087+
i = 0
20712088
part_data = []
20722089
for chunk in file_chunks(file_path, chunk_size):
2090+
i = i + 1
20732091
graphql_query = '''
20742092
mutation GenerateUploadPartUrl($partNumber: Int!, $uploadId: ID!, $uploadKey: String!) {
20752093
generateUploadPartUrlV2(partNumber: $partNumber, uploadId: $uploadId, uploadKey: $uploadKey) {

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
44

55
[tool.poetry]
66
name = "finite-state-sdk"
7-
version = "0.1.9"
7+
version = "0.1.10"
88
authors = [
99
"Finite State, Inc. <[email protected]>"
1010
]

0 commit comments

Comments
 (0)