Compare commits
52 Commits
784b41f762
...
main
Author | SHA1 | Date | |
---|---|---|---|
6004f6aeb4
|
|||
3ffcf262e5
|
|||
550b5ff28a
|
|||
302f4df307
|
|||
b8f4a3c46f
|
|||
1cee3b5dae
|
|||
0d28c61c0f
|
|||
7fefc75d64
|
|||
e0398bd8fb
|
|||
5ff83f3af7
|
|||
e85858d342
|
|||
6811740835 | |||
86ab9808d8 | |||
117a74989e
|
|||
b91a7ae0fc
|
|||
e3043c5646
|
|||
9afa00f61f
|
|||
bd92d8eb87
|
|||
5a1d6903e8
|
|||
67bab6710c
|
|||
f55c3da3ef
|
|||
f50d821aec
|
|||
609f33b181
|
|||
7c8e1156aa
|
|||
226456ccd2
|
|||
227ef294d3
|
|||
a14e0ab2c5
|
|||
471fcb2177
|
|||
0d4ac4022a
|
|||
405d66cdcb
|
|||
a32d9fd643
|
|||
7f394f82ee
|
|||
c8577edf0c
|
|||
02aba34391
|
|||
1fb4c387a7
|
|||
92b61fdae0
|
|||
4ddb6cfd30
|
|||
0eb761db27
|
|||
9cc81373dc
|
|||
b856424640
|
|||
d6a8f5f436
|
|||
4cc4e5cec6
|
|||
aec0e3fb0e
|
|||
05c5c49cd5
|
|||
9156b594e3
|
|||
4fbda91e15
|
|||
50248acefb
|
|||
c9c57445b9
|
|||
18631dc02a
|
|||
ea90b8b8b0
|
|||
15b2e69960
|
|||
9f6f769486
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,4 +4,5 @@ defs/
|
||||
__pycache__/
|
||||
.*.swp
|
||||
tmp/
|
||||
locallibs
|
||||
|
||||
|
117
.gitlab-ci.yml
Normal file
117
.gitlab-ci.yml
Normal file
@ -0,0 +1,117 @@
|
||||
stages:
|
||||
- generate-api-clients
|
||||
- build
|
||||
|
||||
variables:
|
||||
REGISTRY: devnexus.krohne.com:18079/repository/docker-krohne
|
||||
IMAGE_NAME: $REGISTRY/$CI_PROJECT_NAME
|
||||
DTRACK_API_URL: https://dtrack-api-rd.krohne.com
|
||||
DEFECTDOJO_API_URL: https://defectdojo-rd.krohne.com
|
||||
KROHNE_CA_URL: https://devwiki.krohnegroup.com/lib/exe/fetch.php?media=krohne-ca.crt
|
||||
KROHNE_CA_CHECKSUM: a921e440a742f1e67c7714306e2c0d76
|
||||
|
||||
.generate-api:
|
||||
stage: generate-api-clients
|
||||
image: openapitools/openapi-generator-cli:v7.12.0
|
||||
tags:
|
||||
- linux
|
||||
- docker
|
||||
- bash
|
||||
rules:
|
||||
- if: '$CI_COMMIT_BRANCH == "main"'
|
||||
- if: '$CI_COMMIT_TAG'
|
||||
before_script:
|
||||
- curl --insecure $KROHNE_CA_URL -o krohne-ca.crt
|
||||
- echo "$KROHNE_CA_CHECKSUM krohne-ca.crt" | md5sum -c
|
||||
- mv krohne-ca.crt /usr/local/share/ca-certificates
|
||||
- update-ca-certificates
|
||||
|
||||
|
||||
generate-dtrack-api:
|
||||
extends: .generate-api
|
||||
artifacts:
|
||||
paths:
|
||||
- dependencytrack-client
|
||||
expire_in: 1 week
|
||||
script:
|
||||
- curl ${DTRACK_API_URL}/api/openapi.json > dependencytrack-openapi.json
|
||||
- |
|
||||
docker-entrypoint.sh \
|
||||
author template \
|
||||
-g python \
|
||||
-o dependencytrack-openapi-custom-template
|
||||
- sed -i 's/import re/import regex as re/' dependencytrack-openapi-custom-template/model_anyof.mustache
|
||||
- sed -i 's/import re/import regex as re/' dependencytrack-openapi-custom-template/model_generic.mustache
|
||||
- |
|
||||
docker-entrypoint.sh \
|
||||
generate \
|
||||
-i dependencytrack-openapi.json \
|
||||
-g python \
|
||||
-o dependencytrack-client \
|
||||
--package-name dependencytrack_api \
|
||||
-t dependencytrack-openapi-custom-template
|
||||
|
||||
|
||||
generate-defectdojo-api:
|
||||
extends: .generate-api
|
||||
artifacts:
|
||||
paths:
|
||||
- defectdojo-client
|
||||
expire_in: 1 week
|
||||
script:
|
||||
- curl ${DEFECTDOJO_API_URL}/api/v2/oa3/schema/?format=json > defectdojo-openapi.json
|
||||
- |
|
||||
docker-entrypoint.sh \
|
||||
generate \
|
||||
-i defectdojo-openapi.json \
|
||||
-g python \
|
||||
-o defectdojo-client \
|
||||
--package-name defectdojo_api
|
||||
|
||||
dockerize:
|
||||
stage: build
|
||||
image: devnexus.krohne.com:18079/repository/docker-krohne/krohnedockerbash:0.5
|
||||
tags:
|
||||
- linux
|
||||
- docker
|
||||
- bash
|
||||
rules:
|
||||
- if: '$CI_COMMIT_TAG'
|
||||
script:
|
||||
- docker build --build-arg ADDITIONAL_CA_URL="$KROHNE_CA_URL"
|
||||
--build-arg ADDITIONAL_CA_CHECKSUM=$KROHNE_CA_CHECKSUM
|
||||
--tag $IMAGE_NAME:latest
|
||||
--tag $IMAGE_NAME:$CI_COMMIT_SHA
|
||||
--tag $IMAGE_NAME:$CI_COMMIT_TAG
|
||||
.
|
||||
- docker login -u $NEXUS_USER -p $NEXUS_PASSWORD $REGISTRY
|
||||
- docker push $IMAGE_NAME:latest
|
||||
- docker push $IMAGE_NAME:$CI_COMMIT_SHA
|
||||
- docker push $IMAGE_NAME:$CI_COMMIT_TAG
|
||||
|
||||
build-windows-binary:
|
||||
stage: build
|
||||
tags:
|
||||
- windows
|
||||
- pwsh
|
||||
- python3.13
|
||||
rules:
|
||||
- if: '$CI_COMMIT_TAG'
|
||||
artifacts:
|
||||
paths:
|
||||
- sbom-dt-dd.exe
|
||||
script:
|
||||
- |
|
||||
cd src
|
||||
mv ..\dependencytrack-client .
|
||||
mv ..\defectdojo-client .
|
||||
& 'C:\Program Files\Python313\python.exe' -m venv venv
|
||||
.\venv\Scripts\pip.exe install --upgrade pip
|
||||
.\venv\Scripts\pip.exe install -r requirements.txt
|
||||
.\venv\Scripts\pip.exe install -r dependencytrack-client\requirements.txt
|
||||
.\venv\Scripts\pip.exe install -r defectdojo-client\requirements.txt
|
||||
.\venv\Scripts\pip.exe install pyinstaller
|
||||
.\venv\Scripts\pyinstaller.exe --onefile --add-data "dependencytrack-client;dependencytrack-client" --add-data "defectdojo-client;defectdojo-client" sbom-dt-dd.py
|
||||
mv dist\sbom-dt-dd.exe ..
|
||||
|
||||
|
@ -41,14 +41,16 @@ steps:
|
||||
repo: ${FORGE_NAME}/${CI_REPO}
|
||||
registry:
|
||||
from_secret: container_registry
|
||||
tags: latest,${CI_COMMIT_SHA},${CI_COMMIT_TAG}
|
||||
tags:
|
||||
- latest
|
||||
- ${CI_COMMIT_SHA}
|
||||
username:
|
||||
from_secret: container_registry_username
|
||||
password:
|
||||
from_secret: container_registry_password
|
||||
dockerfile: Dockerfile
|
||||
when:
|
||||
- event: [ push, tag ]
|
||||
- event: [ push ]
|
||||
|
||||
build-for-quay:
|
||||
image: plugins/kaniko
|
||||
|
17
Dockerfile
17
Dockerfile
@ -6,18 +6,33 @@ ENV DEFECTDOJO_URL=""
|
||||
ENV DEFECTDOJO_TOKEN=""
|
||||
|
||||
ARG APP_DIR=/opt/app
|
||||
ARG ADDITIONAL_CA_URL="x"
|
||||
ARG ADDITIONAL_CA_CHECKSUM="y"
|
||||
|
||||
RUN \
|
||||
set -e &&\
|
||||
apk add --no-cache syft &&\
|
||||
adduser -s /bin/sh -D user &&\
|
||||
mkdir -p $APP_DIR &&\
|
||||
chown user:user $APP_DIR
|
||||
chown user:user $APP_DIR &&\
|
||||
echo $ADDITIONAL_CA_URL &&\
|
||||
echo $ADDITIONAL_CA_CHECKSUM &&\
|
||||
if [ "$ADDITIONAL_CA_URL" != "x" ]; then \
|
||||
cd /usr/local/share/ca-certificates; \
|
||||
wget --no-check-certificate -O custom-ca.crt $ADDITIONAL_CA_URL; \
|
||||
echo "$ADDITIONAL_CA_CHECKSUM custom-ca.crt" | md5sum -c; \
|
||||
/usr/sbin/update-ca-certificates; \
|
||||
echo "custom ca added"; \
|
||||
else \
|
||||
echo "no additional ca"; \
|
||||
fi
|
||||
|
||||
USER user
|
||||
WORKDIR $APP_DIR
|
||||
|
||||
COPY src/requirements.txt .
|
||||
COPY src/sbom-dt-dd.py .
|
||||
COPY src/converter.py .
|
||||
COPY src/entrypoint.sh .
|
||||
COPY dependencytrack-client/ ./dependencytrack-client
|
||||
COPY defectdojo-client/ ./defectdojo-client
|
||||
|
100
readme.md
100
readme.md
@ -1,6 +1,96 @@
|
||||
# Python Client Packages for the DependencyTrack and DefectDojo API
|
||||
# DependencyTrack and DefectDojo Automation
|
||||
|
||||
## Download the OpenAPI definitions
|
||||
|
||||
## Using
|
||||
|
||||
### Distribution
|
||||
|
||||
The glue logic comes in a docker image and can be started as a docker container. Due to the dependencies, especially the ones related to the
|
||||
APIs of DependencyTrack and DefectDojo this approach has been chosen.
|
||||
|
||||
The image is available at
|
||||
|
||||
```
|
||||
quay.io/wollud1969/dtrack-defectdojo-automation
|
||||
```
|
||||
|
||||
and at
|
||||
|
||||
```
|
||||
devnexus.krohne.com:18079/repository/docker-krohne/dtrack-defectdojo-automation
|
||||
```
|
||||
|
||||
The tag to be used at the moment is `1.0.5`.
|
||||
|
||||
|
||||
|
||||
|
||||
### Start script
|
||||
|
||||
On Linux I've created two files to start the beast:
|
||||
|
||||
env-sbom-dd-dt
|
||||
```
|
||||
DTRACK_API_URL=https://dtrack-api-rd.krohne.com
|
||||
DEFECTDOJO_URL=https://defectdojo-rd.krohne.com
|
||||
DTRACK_TOKEN=...
|
||||
DEFECTDOJO_TOKEN=...
|
||||
```
|
||||
|
||||
The correct values for the tokens must be set here, obviously.
|
||||
|
||||
sbom-dd-dt.sh
|
||||
```
|
||||
#!/bin/bash
|
||||
|
||||
docker run -t -v $PWD:/work --rm --env-file ~/env-sbom-dt-dd devnexus.krohne.com:18079/repository/docker-krohne/dtrack-defectdojo-automation:1.0.5 "$@"
|
||||
```
|
||||
|
||||
I've both files directly in my home-dir.
|
||||
|
||||
### File locations
|
||||
|
||||
When using the container and the script, you must consider that the container has no full access to your filesystem and you need to mount required parts of your filesystem into the container. In the above script I do this with the option `-v $PWD:/work`. This option mounts the current directory (the one from where you are starting the script and thus the container) into the directory `/work` within the container.
|
||||
|
||||
This is required when scanning a directory or uploading a prepared SBOM file.
|
||||
|
||||
### Options of the container/script
|
||||
|
||||
The container has the glue logic script as entrypoint. To find out about the options, call
|
||||
|
||||
```
|
||||
dehottgw@DE01RDDEV01:~$ docker run -t -v $PWD:/work --rm --env-file ~/env-sbom-dt-dd devnexus.krohne.com:18079/repository/docker-krohne/dtrack-defectdojo-automation:1.0.5 -- -h
|
||||
usage: sbom-dt-dd.py [-h] --name NAME --version VERSION --description DESCRIPTION --type TYPE --classifier
|
||||
{APPLICATION,FRAMEWORK,LIBRARY,CONTAINER,OPERATING_SYSTEM,DEVICE,FIRMWARE,FILE,PLATFORM,DEVICE_DRIVER,MACHINE_LEARNING_MODEL,DATA}
|
||||
[--uploadsbom] [--sbomfile SBOMFILE] [--target TARGET] [--verbose]
|
||||
sbom-dt-dd.py: error: the following arguments are required: --name/-n, --version/-v, --description/-d, --type/-t, --classifier/-c
|
||||
dehottgw@DE01RDDEV01:~$
|
||||
```
|
||||
|
||||
Note the double-dash at the end of the commandline before the `-h`. It is necessary, otherwise the `-h` would be considered as an option for the docker command itself.
|
||||
|
||||
|
||||
### SBOM upload example
|
||||
|
||||
For this example I've a file `combined-sbom.json` in the directory `software1`:
|
||||
|
||||
```
|
||||
cd software1/
|
||||
~/sbom-dt-dd.sh --name software1-server --version 0.0.1 --description "Server software for the Software1 platform" --type 1 --classifier APPLICATION --uploadsbom --sbomfile /work/combined-sbom.json -V
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## Building
|
||||
|
||||
### Python Client Packages for the DependencyTrack and DefectDojo API
|
||||
|
||||
#### Download the OpenAPI definitions
|
||||
|
||||
```
|
||||
curl https://dtrack-api.hottis.de/api/openapi.json \
|
||||
@ -10,7 +100,7 @@ curl https://defectdojo.hottis.de/api/v2/oa3/schema/?format=json \
|
||||
```
|
||||
|
||||
|
||||
## Naive Generation of the Client Package for DefectDojo
|
||||
#### Naive Generation of the Client Package for DefectDojo
|
||||
|
||||
```
|
||||
docker run \
|
||||
@ -28,7 +118,7 @@ docker run \
|
||||
|
||||
For DefectDojo the naive code generation works.
|
||||
|
||||
## Naive Generation of the Client Package for DependencyTrack
|
||||
#### Naive Generation of the Client Package for DependencyTrack
|
||||
|
||||
```
|
||||
docker run \
|
||||
@ -43,7 +133,7 @@ docker run \
|
||||
--package-name dependencytrack_api
|
||||
```
|
||||
|
||||
## Fixed Generation of the Client Package for DependencyTrack
|
||||
#### Fixed Generation of the Client Package for DependencyTrack
|
||||
|
||||
In the OpenAPI definition of DependencyTrack a regex is used which is not understood by Python's
|
||||
default regex implement `re`, which in turn is hardwired in the openapi-generator provided code.
|
||||
|
10
src/ENV-krohne.asc
Normal file
10
src/ENV-krohne.asc
Normal file
@ -0,0 +1,10 @@
|
||||
-----BEGIN PGP MESSAGE-----
|
||||
|
||||
jA0ECQMC0qbzN9I9kGP/0sAlARybIFvSNy12iziCC4waAcAPBvvvVrutjyIYtaV1
|
||||
z9WeoBv7TlHB9aKAgxj8LuSh44iDH6uz9FvZfYcZ2BpC9PQYr5IkIw9+iqq9hODM
|
||||
P90Kr9CPazMR8BQUb+4iJjNlHKJL1HCYaFnSHdquzCD4KGqUkkRPPt4Oj/5baJVi
|
||||
kfhU6bKuM6rarcVL0ebSbc2jUIEaugXhnvEWRTiAfOE8v6o7CneoK5hdMbhVA1iC
|
||||
j3sVIcCWgfgMOGDfL2P8DCr7GsGoOxMXvfsPZZL1BRNIf8WXWGpml/TA5Q1vw8TM
|
||||
z8l6SIHklQ==
|
||||
=T8wW
|
||||
-----END PGP MESSAGE-----
|
5
src/ENV-python
Normal file
5
src/ENV-python
Normal file
@ -0,0 +1,5 @@
|
||||
export PYTHONPATH=./locallibs/defectdojo-client:./locallibs/dependencytrack-client
|
||||
|
||||
|
||||
|
||||
|
117
src/converter.py
Normal file
117
src/converter.py
Normal file
@ -0,0 +1,117 @@
|
||||
from loguru import logger
|
||||
import yaml
|
||||
import uuid
|
||||
from packageurl import PackageURL
|
||||
|
||||
from cyclonedx.builder.this import this_component as cdx_lib_component
|
||||
from cyclonedx.factory.license import LicenseFactory
|
||||
from cyclonedx.model.bom import Bom
|
||||
from cyclonedx.model.component import Component, ComponentType
|
||||
from cyclonedx.model.contact import OrganizationalEntity
|
||||
from cyclonedx.model import XsUri
|
||||
from cyclonedx.model import ExternalReference
|
||||
from cyclonedx.output.json import JsonV1Dot5
|
||||
|
||||
class MyLocalConverterException(Exception): pass
|
||||
|
||||
def __converterClassifierToComponentType(classifier):
|
||||
componentType = ''
|
||||
match classifier:
|
||||
case 'APPLICATION':
|
||||
componentType = ComponentType.APPLICATION
|
||||
case 'FRAMEWORK':
|
||||
componentType = ComponentType.FRAMEWORK
|
||||
case 'LIBRARY':
|
||||
componentType = ComponentType.LIBRARY
|
||||
case 'CONTAINER':
|
||||
componentType = ComponentType.CONTAINER
|
||||
case 'OPERATING_SYSTEM':
|
||||
componentType = ComponentType.OPERATING_SYSTEM
|
||||
case 'DEVICE':
|
||||
componentType = ComponentType.DEVICE
|
||||
case 'FIRMWARE':
|
||||
componentType = ComponentType.FIRMWARE
|
||||
case 'FILE':
|
||||
componentType = ComponentType.FILE
|
||||
case 'PLATFORM':
|
||||
componentType = ComponentType.PLATFORM
|
||||
case 'DEVICE_DRIVER':
|
||||
componentType = ComponentType.DEVICE_DRIVER
|
||||
case 'MACHINE_LEARNING_MODEL':
|
||||
componentType = ComponentType.MACHINE_LEARNING_MODEL
|
||||
case 'DATA':
|
||||
componentType = ComponentType.DATA
|
||||
case _:
|
||||
raise MyLocalConverterException(f"No componentType for {classifier} found")
|
||||
return componentType
|
||||
|
||||
|
||||
|
||||
def minimalSbomFormatConverter(minimalSbom, classifier):
|
||||
logger.info(f"Minimal input: {minimalSbom}")
|
||||
|
||||
lc_factory = LicenseFactory()
|
||||
|
||||
minimalSbomObject = yaml.safe_load(minimalSbom)
|
||||
logger.debug(f"{minimalSbomObject=}")
|
||||
|
||||
bom = Bom(
|
||||
version=minimalSbomObject['sbomVersion']
|
||||
)
|
||||
bom.metadata.tools.components.add(cdx_lib_component())
|
||||
bom.metadata.tools.components.add(Component(
|
||||
name='sbom-dt-dd',
|
||||
type=ComponentType.APPLICATION
|
||||
))
|
||||
|
||||
bom.metadata.component = root_component = Component(
|
||||
name=minimalSbomObject['product'],
|
||||
type=__converterClassifierToComponentType(minimalSbomObject['classifier']),
|
||||
description=minimalSbomObject['description'],
|
||||
version=minimalSbomObject['version'],
|
||||
licenses=[lc_factory.make_from_string(minimalSbomObject['license'])],
|
||||
supplier=OrganizationalEntity(
|
||||
name=minimalSbomObject['supplier']['name'],
|
||||
urls=[XsUri(minimalSbomObject['supplier']['url'])]
|
||||
),
|
||||
bom_ref = f"urn:uuid:{uuid.uuid4()}"
|
||||
)
|
||||
|
||||
component = Component(
|
||||
type=__converterClassifierToComponentType(minimalSbomObject['classifier']),
|
||||
name=f"{minimalSbomObject['supplier']['name']}´s own code",
|
||||
version=minimalSbomObject['version'],
|
||||
licenses=[lc_factory.make_from_string(minimalSbomObject['license'])],
|
||||
supplier=OrganizationalEntity(
|
||||
name=minimalSbomObject['supplier']['name'],
|
||||
urls=[XsUri(minimalSbomObject['supplier']['url'])]
|
||||
),
|
||||
bom_ref = f"urn:uuid:{uuid.uuid4()}"
|
||||
)
|
||||
bom.components.add(component)
|
||||
bom.register_dependency(root_component, [component])
|
||||
|
||||
|
||||
for minimalComponentDescription in minimalSbomObject['components']:
|
||||
component = Component(
|
||||
type=ComponentType.LIBRARY,
|
||||
name=minimalComponentDescription['name'],
|
||||
version=minimalComponentDescription['version'],
|
||||
licenses=[lc_factory.make_from_string(minimalComponentDescription['license'])],
|
||||
bom_ref = f"urn:uuid:{uuid.uuid4()}"
|
||||
)
|
||||
if 'cpe' in minimalComponentDescription:
|
||||
component.cpe = minimalComponentDescription['cpe']
|
||||
if 'purl' in minimalComponentDescription:
|
||||
component.purl = PackageURL.from_string(minimalComponentDescription['purl'])
|
||||
bom.components.add(component)
|
||||
bom.register_dependency(root_component, [component])
|
||||
|
||||
outputSbom = JsonV1Dot5(bom).output_as_string(indent=2)
|
||||
logger.info(outputSbom)
|
||||
|
||||
with open('/tmp/bom.json', 'w') as f:
|
||||
f.write(outputSbom)
|
||||
|
||||
return (outputSbom, minimalSbomObject['product'], minimalSbomObject['version'], minimalSbomObject['classifier'], minimalSbomObject['description'])
|
||||
|
@ -1,11 +1,11 @@
|
||||
#!/bin/sh
|
||||
# entrypoint.sh
|
||||
|
||||
source ./.venv/bin/activate
|
||||
source /opt/app/.venv/bin/activate
|
||||
|
||||
PYTHONPATH="$PYTHONPATH:./dependencytrack-client"
|
||||
PYTHONPATH="$PYTHONPATH:./defectdojo-client"
|
||||
PYTHONPATH="$PYTHONPATH:/opt/app/dependencytrack-client"
|
||||
PYTHONPATH="$PYTHONPATH:/opt/app/defectdojo-client"
|
||||
export PYTHONPATH
|
||||
|
||||
exec python sbom-dt-dd.py "$@"
|
||||
exec python /opt/app/sbom-dt-dd.py "$@"
|
||||
|
||||
|
47
src/prepare-local-env.sh
Executable file
47
src/prepare-local-env.sh
Executable file
@ -0,0 +1,47 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
. ./ENV
|
||||
|
||||
LOCALLBIS=./locallibs
|
||||
OPENAPI_GENERATOR=openapitools/openapi-generator-cli:v7.12.0
|
||||
|
||||
mkdir $LOCALLBIS && cd $LOCALLBIS
|
||||
|
||||
# --- DependencyTrack Client Library -----------------------------------------------------
|
||||
curl ${DTRACK_API_URL}/api/openapi.json >dependencytrack-openapi.json
|
||||
|
||||
docker run -v $PWD:/work -u $UID $OPENAPI_GENERATOR \
|
||||
author template \
|
||||
-g python \
|
||||
-o /work/dependencytrack-openapi-custom-template
|
||||
|
||||
sed -i -e 's/import re/import regex as re/' dependencytrack-openapi-custom-template/model_anyof.mustache
|
||||
sed -i -e 's/import re/import regex as re/' dependencytrack-openapi-custom-template/model_generic.mustache
|
||||
|
||||
docker run -v $PWD:/work -u $UID $OPENAPI_GENERATOR \
|
||||
generate \
|
||||
-i /work/dependencytrack-openapi.json \
|
||||
-g python \
|
||||
-o /work/dependencytrack-client \
|
||||
--package-name dependencytrack_api \
|
||||
-t /work/dependencytrack-openapi-custom-template
|
||||
|
||||
# --- Defectdojo Client Library ----------------------------------------------------------
|
||||
curl ${DEFECTDOJO_URL}/api/v2/oa3/schema/?format=json >defectdojo-openapi.json
|
||||
|
||||
docker run -v $PWD:/work -u $UID $OPENAPI_GENERATOR \
|
||||
generate \
|
||||
-i /work/defectdojo-openapi.json \
|
||||
-g python \
|
||||
-o /work/defectdojo-client \
|
||||
--package-name defectdojo_api
|
||||
|
||||
cd ..
|
||||
|
||||
python3 -m venv .venv
|
||||
. .venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
pip install -r $LOCALLBIS/dependencytrack-client/requirements.txt
|
||||
pip install -r $LOCALLBIS/defectdojo-client/requirements.txt
|
@ -1,3 +1,5 @@
|
||||
regex==2024.11.6
|
||||
loguru==0.7.3
|
||||
PyYAML==6.0.2
|
||||
cyclonedx-python-lib==10.4.1
|
||||
|
||||
|
@ -4,19 +4,28 @@ import argparse
|
||||
import subprocess
|
||||
import json
|
||||
|
||||
import defectdojo_api
|
||||
from defectdojo_api.rest import ApiException as DefectDojoApiException
|
||||
import datetime
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'defectdojo-client'))
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'dependencytrack-client'))
|
||||
|
||||
import defectdojo_api
|
||||
from defectdojo_api.rest import ApiException as DefectDojoApiException
|
||||
|
||||
import dependencytrack_api
|
||||
from dependencytrack_api.rest import ApiException as DependencyTrackApiException
|
||||
|
||||
from converter import minimalSbomFormatConverter
|
||||
|
||||
|
||||
class MyLocalException(Exception): pass
|
||||
|
||||
def executeApiCall(apiClient, ApiClass, EndpointMethod, RequestClass, requestParams, additionalParams=[]):
|
||||
try:
|
||||
logger.info(f"Calling {ApiClass}.{EndpointMethod} with {RequestClass} ({additionalParams}, {requestParams})")
|
||||
logger.info(f"Calling {ApiClass=}.{EndpointMethod=} with {RequestClass=})")
|
||||
if VERBOSE:
|
||||
logger.debug(f"{additionalParams=}, {requestParams=}")
|
||||
instance = ApiClass(apiClient)
|
||||
if RequestClass:
|
||||
request = RequestClass(**requestParams)
|
||||
@ -58,13 +67,16 @@ except KeyError as e:
|
||||
parser = argparse.ArgumentParser(description='sbom-dt-dd glue logic')
|
||||
parser.add_argument('--name', '-n',
|
||||
help='Project Name',
|
||||
required=True)
|
||||
required=False,
|
||||
default=''),
|
||||
parser.add_argument('--version', '-v',
|
||||
help='Project Version',
|
||||
required=True)
|
||||
required=False,
|
||||
default='')
|
||||
parser.add_argument('--description', '-d',
|
||||
help='Project Description',
|
||||
required=True)
|
||||
required=False,
|
||||
default='')
|
||||
parser.add_argument('--type', '-t',
|
||||
help='Product Type from DefectDojo',
|
||||
type=int,
|
||||
@ -73,7 +85,8 @@ parser.add_argument('--classifier', '-c',
|
||||
help='Project Classifier from DependencyTrack',
|
||||
choices=['APPLICATION', 'FRAMEWORK', 'LIBRARY', 'CONTAINER', 'OPERATING_SYSTEM', 'DEVICE',
|
||||
'FIRMWARE', 'FILE', 'PLATFORM', 'DEVICE_DRIVER', 'MACHINE_LEARNING_MODEL', 'DATA'],
|
||||
required=True)
|
||||
required=False,
|
||||
default='')
|
||||
parser.add_argument('--uploadsbom', '-U',
|
||||
help='Upload a already existing SBOM instead of generating it. Give the SBOM file at -F instead of a target',
|
||||
required=False,
|
||||
@ -82,22 +95,50 @@ parser.add_argument('--uploadsbom', '-U',
|
||||
parser.add_argument('--sbomfile', '-F',
|
||||
help='Filename of existing SBOM file to upload, use together with -U, do not use together with -T',
|
||||
required=False)
|
||||
parser.add_argument('--minimalsbomformat', '-K',
|
||||
help='SBOM file comes in dedicated minimal format and will be converted into cyclonedx before uploading',
|
||||
action='store_true',
|
||||
default=False)
|
||||
parser.add_argument('--overwritemetadata', '-O',
|
||||
help='Overwrite name, version, description and classifier with data from minimal SBOM',
|
||||
action='store_true',
|
||||
default=False)
|
||||
parser.add_argument('--target', '-T',
|
||||
help='Target to scan, either path name for sources or docker image tag',
|
||||
required=False)
|
||||
parser.add_argument('--reimport', '-R',
|
||||
help='Import the SBOM for an existing project/product once again',
|
||||
required=False,
|
||||
action='store_true',
|
||||
default=False)
|
||||
parser.add_argument('--verbose', '-V',
|
||||
help='A lot of debug output',
|
||||
required=False,
|
||||
action='store_true',
|
||||
default=False)
|
||||
args = parser.parse_args()
|
||||
projectName = args.name
|
||||
projectVersion = args.version
|
||||
projectDescription = args.description
|
||||
productType = args.type
|
||||
projectClassifier = args.classifier
|
||||
reImport = args.reimport
|
||||
|
||||
uploadSbomFlag = args.uploadsbom
|
||||
if uploadSbomFlag:
|
||||
sbomFileName = args.sbomfile
|
||||
minimalSbomFormat = args.minimalsbomformat
|
||||
else:
|
||||
target = args.target
|
||||
|
||||
if minimalSbomFormat:
|
||||
overwriteMetadata = args.overwritemetadata
|
||||
|
||||
if not overwriteMetadata and not (projectName and projectVersion and projectClassifier and projectDescription):
|
||||
raise MyLocalException("If overwriteMetadata is not selected, projectName, projectVersion, projectClassifier and projectDescription must be set.")
|
||||
|
||||
VERBOSE = args.verbose
|
||||
|
||||
|
||||
# ---- main starts here --------------------------------------------------------------------------------------------------
|
||||
|
||||
@ -106,6 +147,16 @@ if uploadSbomFlag:
|
||||
logger.info(f"Reading SBOM from file {sbomFileName}")
|
||||
with open(sbomFileName, 'r') as sbomFile:
|
||||
sbom = sbomFile.read()
|
||||
logger.info("SBOM file read.")
|
||||
if minimalSbomFormat:
|
||||
logger.info("Start converting from minimal format into cyclonedx")
|
||||
(sbom, nameFromMinimalSbom, versionFromMinimalSbom, classifierFromMinimalSbom, descriptionFromMinimalSbom) = minimalSbomFormatConverter(sbom, projectClassifier)
|
||||
logger.info("Converted")
|
||||
if overwriteMetadata:
|
||||
projectName = nameFromMinimalSbom
|
||||
projectVersion = versionFromMinimalSbom
|
||||
projectClassifier = classifierFromMinimalSbom
|
||||
projectDescription = descriptionFromMinimalSbom
|
||||
logger.info("Done.")
|
||||
else:
|
||||
# ------- generate SBOM ------------
|
||||
@ -117,44 +168,46 @@ else:
|
||||
|
||||
|
||||
# ------- create product and engagement in DefectDojo -------
|
||||
defectdojo_configuration = defectdojo_api.Configuration(
|
||||
host = DEFECTDOJO_URL
|
||||
)
|
||||
defectdojo_configuration.api_key['tokenAuth'] = DEFECTDOJO_TOKEN
|
||||
defectdojo_configuration.api_key_prefix['tokenAuth'] = 'Token'
|
||||
if not reImport:
|
||||
# in case of a reimport no modification on DefectDojo are required
|
||||
defectdojo_configuration = defectdojo_api.Configuration(
|
||||
host = DEFECTDOJO_URL
|
||||
)
|
||||
defectdojo_configuration.api_key['tokenAuth'] = DEFECTDOJO_TOKEN
|
||||
defectdojo_configuration.api_key_prefix['tokenAuth'] = 'Token'
|
||||
|
||||
with defectdojo_api.ApiClient(defectdojo_configuration) as defectdojo_api_client:
|
||||
print("Create product in DefectDojo")
|
||||
productName = f"{projectName}:{projectVersion}"
|
||||
product_response = \
|
||||
executeApiCall(
|
||||
defectdojo_api_client,
|
||||
defectdojo_api.ProductsApi,
|
||||
defectdojo_api.ProductsApi.products_create,
|
||||
defectdojo_api.ProductRequest,
|
||||
{ 'name': productName, 'description': projectDescription, 'prod_type': productType },
|
||||
[]
|
||||
)
|
||||
with defectdojo_api.ApiClient(defectdojo_configuration) as defectdojo_api_client:
|
||||
print("Create product in DefectDojo")
|
||||
productName = f"{projectName}:{projectVersion}"
|
||||
product_response = \
|
||||
executeApiCall(
|
||||
defectdojo_api_client,
|
||||
defectdojo_api.ProductsApi,
|
||||
defectdojo_api.ProductsApi.products_create,
|
||||
defectdojo_api.ProductRequest,
|
||||
{ 'name': productName, 'description': projectDescription, 'prod_type': productType },
|
||||
[]
|
||||
)
|
||||
|
||||
product_id = product_response.id
|
||||
print(f"{product_id=}")
|
||||
product_id = product_response.id
|
||||
print(f"{product_id=}")
|
||||
|
||||
print("Create engagement in DefectDojo")
|
||||
start_time = datetime.date.today()
|
||||
end_time = start_time + relativedelta(years=10)
|
||||
engagementName = f"{productName} DTrack Link"
|
||||
engagement_response = \
|
||||
executeApiCall(
|
||||
defectdojo_api_client,
|
||||
defectdojo_api.EngagementsApi,
|
||||
defectdojo_api.EngagementsApi.engagements_create,
|
||||
defectdojo_api.EngagementRequest,
|
||||
{ 'name': engagementName, 'target_start': start_time, 'target_end': end_time, 'status': 'In Progress', 'product': product_id },
|
||||
[]
|
||||
)
|
||||
|
||||
engagement_id = engagement_response.id
|
||||
print(f"{engagement_id=}")
|
||||
print("Create engagement in DefectDojo")
|
||||
start_time = datetime.date.today()
|
||||
end_time = start_time + relativedelta(years=10)
|
||||
engagementName = f"{productName} DTrack Link"
|
||||
engagement_response = \
|
||||
executeApiCall(
|
||||
defectdojo_api_client,
|
||||
defectdojo_api.EngagementsApi,
|
||||
defectdojo_api.EngagementsApi.engagements_create,
|
||||
defectdojo_api.EngagementRequest,
|
||||
{ 'name': engagementName, 'target_start': start_time, 'target_end': end_time, 'status': 'In Progress', 'product': product_id },
|
||||
[]
|
||||
)
|
||||
|
||||
engagement_id = engagement_response.id
|
||||
print(f"{engagement_id=}")
|
||||
|
||||
|
||||
# ------- create project in DependencyTrack, connect project to engagement in DefectDojo, upload SBOM --------
|
||||
@ -165,36 +218,38 @@ dependencytrack_configuration.debug = False
|
||||
dependencytrack_configuration.api_key['ApiKeyAuth'] = DTRACK_TOKEN
|
||||
|
||||
with dependencytrack_api.ApiClient(dependencytrack_configuration) as dependencytrack_api_client:
|
||||
project_response = \
|
||||
executeApiCall(
|
||||
dependencytrack_api_client,
|
||||
dependencytrack_api.ProjectApi,
|
||||
dependencytrack_api.ProjectApi.create_project,
|
||||
dependencytrack_api.Project,
|
||||
{ 'name': projectName, 'version': projectVersion, 'classifier': projectClassifier, 'uuid': "", 'last_bom_import': 0 },
|
||||
[]
|
||||
)
|
||||
|
||||
project_uuid = project_response.uuid
|
||||
print(f"{project_uuid=}")
|
||||
|
||||
properties = [
|
||||
{ 'group_name': "integrations", 'property_name': "defectdojo.engagementId",
|
||||
'property_value': str(engagement_id), 'property_type': "STRING" },
|
||||
{ 'group_name': "integrations", 'property_name': "defectdojo.doNotReactivate",
|
||||
'property_value': "true", 'property_type': "BOOLEAN" },
|
||||
{ 'group_name': "integrations", 'property_name': "defectdojo.reimport",
|
||||
'property_value': "true", 'property_type': "BOOLEAN" }
|
||||
]
|
||||
for property in properties:
|
||||
executeApiCall(
|
||||
if not reImport:
|
||||
# in case of a reimport it is not necessary to create the project
|
||||
project_response = \
|
||||
executeApiCall(
|
||||
dependencytrack_api_client,
|
||||
dependencytrack_api.ProjectPropertyApi,
|
||||
dependencytrack_api.ProjectPropertyApi.create_property1,
|
||||
dependencytrack_api.ProjectProperty,
|
||||
property,
|
||||
[ project_uuid ]
|
||||
)
|
||||
dependencytrack_api.ProjectApi,
|
||||
dependencytrack_api.ProjectApi.create_project,
|
||||
dependencytrack_api.Project,
|
||||
{ 'name': projectName, 'version': projectVersion, 'classifier': projectClassifier, 'uuid': "", 'last_bom_import': 0 },
|
||||
[]
|
||||
)
|
||||
|
||||
project_uuid = project_response.uuid
|
||||
print(f"{project_uuid=}")
|
||||
|
||||
properties = [
|
||||
{ 'group_name': "integrations", 'property_name': "defectdojo.engagementId",
|
||||
'property_value': str(engagement_id), 'property_type': "STRING" },
|
||||
{ 'group_name': "integrations", 'property_name': "defectdojo.doNotReactivate",
|
||||
'property_value': "true", 'property_type': "BOOLEAN" },
|
||||
{ 'group_name': "integrations", 'property_name': "defectdojo.reimport",
|
||||
'property_value': "true", 'property_type': "BOOLEAN" }
|
||||
]
|
||||
for property in properties:
|
||||
executeApiCall(
|
||||
dependencytrack_api_client,
|
||||
dependencytrack_api.ProjectPropertyApi,
|
||||
dependencytrack_api.ProjectPropertyApi.create_property1,
|
||||
dependencytrack_api.ProjectProperty,
|
||||
property,
|
||||
[ project_uuid ]
|
||||
)
|
||||
|
||||
bom_response = \
|
||||
executeApiCall(
|
||||
|
Reference in New Issue
Block a user