|
| 1 | +# This file is part of CycloneDX Python Library |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# SPDX-License-Identifier: Apache-2.0 |
| 16 | +# Copyright (c) OWASP Foundation. All Rights Reserved. |
| 17 | + |
| 18 | +"""Bom related utilities""" |
| 19 | + |
| 20 | +__all__ = ['BomRefDiscriminator'] |
| 21 | + |
| 22 | +from collections.abc import Iterable |
| 23 | +from itertools import chain |
| 24 | +from random import random |
| 25 | +from typing import TYPE_CHECKING, Any |
| 26 | + |
| 27 | +if TYPE_CHECKING: # pragma: no cover |
| 28 | + from ...model.bom import Bom |
| 29 | + from ...model.bom_ref import BomRef |
| 30 | + |
| 31 | + |
| 32 | +class BomRefDiscriminator: |
| 33 | + """ |
| 34 | + Ensure that a collection of BomRef objects has unique, non‑empty values. |
| 35 | +
|
| 36 | + The discriminator inspects the provided BomRef instances and assigns new, |
| 37 | + automatically generated identifiers to any BomRef whose value is missing |
| 38 | + or duplicates another. Original values are preserved so they can be |
| 39 | + restored later via `reset()` or by using this class as a context manager. |
| 40 | + """ |
| 41 | + |
| 42 | + def __init__(self, bomrefs: Iterable['BomRef'], prefix: str = 'BomRef') -> None: |
| 43 | + # do not use dict/set here, different BomRefs with same value have same hash and would shadow each other |
| 44 | + self._bomrefs = tuple((bomref, bomref.value) for bomref in bomrefs) |
| 45 | + self._prefix = prefix |
| 46 | + |
| 47 | + def __enter__(self) -> None: |
| 48 | + self.discriminate() |
| 49 | + |
| 50 | + def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: |
| 51 | + self.reset() |
| 52 | + |
| 53 | + def discriminate(self) -> None: |
| 54 | + """ |
| 55 | + Enforce uniqueness across all BomRef values. |
| 56 | +
|
| 57 | + Any BomRef whose value is `None` or duplicates a previously encountered |
| 58 | + value is assigned a newly generated unique identifier. |
| 59 | + """ |
| 60 | + known_values = [] |
| 61 | + for bomref, _ in self._bomrefs: |
| 62 | + value = bomref.value |
| 63 | + if value is None or value in known_values: |
| 64 | + value = self._make_unique() |
| 65 | + bomref.value = value |
| 66 | + known_values.append(value) |
| 67 | + |
| 68 | + def reset(self) -> None: |
| 69 | + """ |
| 70 | + Restore all BomRef values to their original state. |
| 71 | + """ |
| 72 | + for bomref, original_value in self._bomrefs: |
| 73 | + bomref.value = original_value |
| 74 | + |
| 75 | + def _make_unique(self) -> str: |
| 76 | + return f'{self._prefix}{str(random())[1:]}{str(random())[1:]}' # nosec B311 |
| 77 | + |
| 78 | + @classmethod |
| 79 | + def from_bom(cls, bom: 'Bom', prefix: str = 'BomRef') -> 'BomRefDiscriminator': |
| 80 | + """ |
| 81 | + Create a discriminator for all BomRefs contained within a BOM. |
| 82 | +
|
| 83 | + This includes BomRefs from |
| 84 | + - components |
| 85 | + - services |
| 86 | + - vulnerabilities |
| 87 | + """ |
| 88 | + return cls(chain( |
| 89 | + map(lambda c: c.bom_ref, bom._get_all_components()), |
| 90 | + map(lambda s: s.bom_ref, bom.services), |
| 91 | + map(lambda v: v.bom_ref, bom.vulnerabilities) |
| 92 | + ), prefix) |
0 commit comments