Skip to content

Commit 1491765

Browse files
authored
Merge pull request #62 from onekey-sec/sbom-upload
SBOM upload
2 parents e7c664e + 2afd1e3 commit 1491765

3 files changed

Lines changed: 30 additions & 11 deletions

File tree

onekey_client/cli/firmware_upload.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@
3232
)
3333
@click.option("--version", help="Firmware version")
3434
@click.option("--name", help="Firmware name")
35-
@click.argument("filename", type=click.Path(exists=True, path_type=Path))
35+
@click.option(
36+
"--sbom", help="Firmware SBOM", type=click.Path(exists=True, path_type=Path)
37+
)
38+
@click.argument(
39+
"filename", type=click.Path(exists=True, path_type=Path), required=False
40+
)
3641
@click.pass_obj
3742
def upload_firmware(
3843
client: Client,
@@ -42,17 +47,23 @@ def upload_firmware(
4247
analysis_configuration_name: str,
4348
version: str | None,
4449
name: str | None,
45-
filename: Path,
50+
sbom: Path | None,
51+
filename: Path | None,
4652
):
47-
"""Upload a firmware to the ONEKEY platform."""
53+
"""Upload a firmware / SBOM to the ONEKEY platform."""
54+
if filename is None and sbom is None:
55+
error = "Either `--sbom` or `FILENAME` or both must be provided"
56+
raise click.BadParameter(error)
57+
4858
product_group_id = _get_product_group_id_by_name(client, product_group_name)
4959
analysis_configuration_id = _get_analysis_configuration_id_by_name(
5060
client, analysis_configuration_name
5161
)
5262

5363
if name is None:
64+
postfix = filename.name if filename is not None else sbom.stem
5465
name = (
55-
f"{vendor_name}-{product_name}-{filename.name}"
66+
f"{vendor_name}-{product_name}-{postfix}"
5667
if version is None
5768
else f"{vendor_name}-{product_name}-{version}"
5869
)
@@ -67,7 +78,9 @@ def upload_firmware(
6778
)
6879

6980
try:
70-
res = client.upload_firmware(metadata, filename, enable_monitoring=False)
81+
res = client.upload_firmware(
82+
metadata, filename, sbom_path=sbom, enable_monitoring=False
83+
)
7184
click.echo(res["id"])
7285
except QueryError as e:
7386
click.echo("Error during firmware upload:")

onekey_client/client.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,14 @@ def query(self, query: str, variables: dict | None = None, timeout=60):
188188
def upload_firmware(
189189
self,
190190
metadata: m.FirmwareMetadata,
191-
path: Path,
191+
path: Path | None,
192192
*,
193+
sbom_path: Path | None = None,
193194
enable_monitoring: bool,
194195
timeout=60,
195196
):
197+
assert path is not None or sbom_path is not None
198+
196199
variables = {
197200
"firmware": {
198201
"name": metadata.name,
@@ -215,9 +218,12 @@ def upload_firmware(
215218
raise errors.QueryError(res["createFirmwareUpload"]["errors"])
216219

217220
upload_url = res["createFirmwareUpload"]["uploadUrl"]
218-
return self._post_with_token(
219-
upload_url, files={"firmware": path.open("rb")}, timeout=timeout
220-
)
221+
222+
files = {}
223+
files["firmware"] = path.open("rb") if path is not None else b""
224+
if sbom_path is not None:
225+
files["sbom"] = sbom_path.open("rb")
226+
return self._post_with_token(upload_url, files=files, timeout=timeout)
221227

222228
@_tenant_required
223229
def get_product_groups(self):

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[project]
22
name = "onekey_client"
3-
version = "2.4.0"
3+
version = "2.5.0"
44
description = "ONEKEY API client"
55
authors = [{ name = "ONEKEY", email = "support@onekey.com" }]
6-
requires-python = ">=3.9"
6+
requires-python = ">=3.10"
77
readme = "README.md"
88
license = "MIT"
99
keywords = ["iot", "security", "firmware", "analysis"]

0 commit comments

Comments
 (0)