Generating an SBOM from requirements.txt without including CycloneDX's components? #487
-
Currently, I am trying to generate a SBOM for a Python project and following the direct instructions in the documentation (installing CycloneDX via pip, running pip freeze, and then running cyclonedx_py on my requirements.txt leads to my SBOM being filled with unwanted CycloneDX dependencies. What is the recommended way of generating an SBOM from requirements.txt without including CycloneDX's components? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 4 replies
-
see also: #427 |
Beta Was this translation helpful? Give feedback.
-
Could you run |
Beta Was this translation helpful? Give feedback.
-
I typically use pipx to install Python based CLI tools. It creates a dedicated venv for each tool, so you don't taint it in any way and you have no package version collision and other weird issues. So, basically install pipx and then install pipx install cyclonedx-bom
cyclonedx-py -r |
Beta Was this translation helpful? Give feedback.
-
If you want to take this one step further and only include the package dependencies with no extras (i.e., dev packages), after installing mkdir -p build/
python -m pip freeze --local --disable-pip-version-check --exclude-editable > build/prune-requirements.txt
python -m pip wheel --wheel-dir build/wheelhouse/ --requirement build/prune-requirements.txt
python -m pip wheel --wheel-dir build/wheelhouse/ .
python -m pip uninstall --yes --requirement build/prune-requirements.txt
python -m pip install --no-index --find-links=build/wheelhouse/ --editable .
rm -fr build/ and then generate the make prune requirements sbom |
Beta Was this translation helpful? Give feedback.
-
Hey, FWIW, I maintain packageurl and pip_requirements_parser used in this tool! There are two cases: A. is going to be always be difficult because you cannot wholesale subtract this library requirements as they may also satisfy requirements of the project being analyzed. But if this the approach you want subtracting is the only way ... and even with some prudent checks it is hard to get right. You're hit by the observer effect https://en.wikipedia.org/wiki/Observer_effect B. is a better approach IMHO... meaning that you use the tool installed elsewhere and point it to a project and you DO NOT modify this project installed packages. This would mean one or more of these:
For B., you could include as a library https://github.com/nexB/python-inspector that would do quite a bit of B1., B2. and B3. and it can also avoid biases from the current Python/OS/arch of the tool runtime vs. the project runtime. |
Beta Was this translation helpful? Give feedback.
-
The question is, are you generating an sbom from 3rd party source code or are you generating an sbom for a project that is being developed on the local system or in a python virtual environment on the local system? Either way you need to be aware that when you do a pip freeze it will include your environment packages on the system, or you can try the -l flag to tell pip to ignore globally install packages. Most other tools like pigar or pipreqs will have a similar option. edit So if you are creating an sbom from 3rd party source code you would NOT do a pip freeze because it will include packages installed on your environment, instead try testing pigar and or pipreqs to generate a decent requirements.txt file. Also with the cyclonedx-py by default, or with adding the -e flag, it will include packages in the local pip environment. If you don't want the localy install packages you would use flag -r and -i together to point to a requirements.txt and omit the -e. |
Beta Was this translation helpful? Give feedback.
-
I found a work around for me.
With a bit more work in this it would also be possible to generate the missing dependencies per component (#518) |
Beta Was this translation helpful? Give feedback.
-
similar case: CycloneDX/cyclonedx-python-lib#568 (comment) |
Beta Was this translation helpful? Give feedback.
Could you run
pip freeze
one step before installing this toolcyclonedx_py
and all other non-essential dependencies?this way, they would not appear in the requirements list you provided.