scripts/sbom: add shared SPDX elements

Implement shared SPDX elements used in all three documents.

Assisted-by: Cursor:claude-sonnet-4-5
Assisted-by: OpenCode:GLM-4-7
Co-developed-by: Maximilian Huber <maximilian.huber@tngtech.com>
Signed-off-by: Maximilian Huber <maximilian.huber@tngtech.com>
Signed-off-by: Luis Augenstein <luis.augenstein@tngtech.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Luis Augenstein
2026-05-18 08:20:56 +02:00
committed by Greg Kroah-Hartman
parent a68a29a1cc
commit 11f9c14d0e
3 changed files with 45 additions and 1 deletions

View File

@@ -3,6 +3,7 @@
import argparse
from dataclasses import dataclass
from datetime import datetime, timezone
from enum import Enum
import os
from typing import Any
@@ -52,6 +53,9 @@ class KernelSbomConfig:
write_output_on_error: bool
"""Whether to write output documents even if errors occur."""
created: datetime
"""Datetime to use for the SPDX created property of the CreationInfo element."""
spdxId_prefix: str
"""Prefix to use for all SPDX element IDs."""
@@ -195,6 +199,10 @@ def get_config() -> KernelSbomConfig:
fail_on_unknown_build_command = not args["do_not_fail_on_unknown_build_command"]
write_output_on_error = args["write_output_on_error"]
created = datetime.fromtimestamp(
max([os.path.getmtime(os.path.join(obj_tree, root_path)) for root_path in root_paths]),
tz=timezone.utc,
)
spdxId_prefix = args["spdxId_prefix"]
prettify_json = args["prettify_json"]
@@ -218,6 +226,7 @@ def get_config() -> KernelSbomConfig:
debug=debug,
fail_on_unknown_build_command=fail_on_unknown_build_command,
write_output_on_error=write_output_on_error,
created=created,
spdxId_prefix=spdxId_prefix,
prettify_json=prettify_json,
)

View File

@@ -1,18 +1,20 @@
# SPDX-License-Identifier: GPL-2.0-only OR MIT
# Copyright (C) 2025 TNG Technology Consulting GmbH
from datetime import datetime
from typing import Protocol
from sbom.config import KernelSpdxDocumentKind
from sbom.cmd_graph import CmdGraph
from sbom.path_utils import PathStr
from sbom.spdx_graph.spdx_graph_model import SpdxGraph, SpdxIdGeneratorCollection
from sbom.spdx_graph.shared_spdx_elements import SharedSpdxElements
class SpdxGraphConfig(Protocol):
obj_tree: PathStr
src_tree: PathStr
created: datetime
def build_spdx_graphs(
@@ -33,4 +35,5 @@ def build_spdx_graphs(
Returns:
Dictionary of SPDX graphs
"""
shared_elements = SharedSpdxElements.create(spdx_id_generators.base, config.created)
return {}

View File

@@ -0,0 +1,32 @@
# SPDX-License-Identifier: GPL-2.0-only OR MIT
# Copyright (C) 2025 TNG Technology Consulting GmbH
from dataclasses import dataclass
from datetime import datetime, timezone
from sbom.spdx.core import CreationInfo, SoftwareAgent
from sbom.spdx.spdxId import SpdxIdGenerator
@dataclass(frozen=True)
class SharedSpdxElements:
agent: SoftwareAgent
creation_info: CreationInfo
@classmethod
def create(cls, spdx_id_generator: SpdxIdGenerator, created: datetime) -> "SharedSpdxElements":
"""
Creates shared SPDX elements used across multiple documents.
Args:
spdx_id_generator: Generator for creating SPDX IDs.
created: SPDX 'created' property used for the creation info.
Returns:
SharedSpdxElements with agent and creation info.
"""
agent = SoftwareAgent(
spdxId=spdx_id_generator.generate(),
name="KernelSbom",
)
creation_info = CreationInfo(createdBy=[agent], created=created.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"))
return SharedSpdxElements(agent=agent, creation_info=creation_info)