Skip to content

Commit 3fa9551

Browse files
committed
automatic topology detection
1 parent 86d1849 commit 3fa9551

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

examples/scan_docker_image_lite.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@
7070
python3 scan_docker_image_lite.py <imagespec> --detect-options='--detect.clone.project.version.name=version --detect.project.clone.categories=COMPONENT_DATA,VULN_DATA'
7171
7272
There is not validation of extra parameters passed, use with care.
73+
74+
MK 2022-10-24 Automating grouping.
75+
By adding specific markers to the Dockerfile, it is possible to enable automating group detection.
76+
77+
In the Dockerfile, once the specific group is complete, add the following command:
78+
79+
RUN echo <groupname>_group_end
80+
81+
e.g.
82+
83+
. . .
84+
RUN echo base_group_end
85+
. . .
86+
RUN echo app_group_end
87+
. . .
88+
89+
that will generate grouping as N:base,N:app
90+
91+
7392
'''
7493

7594
from blackduck.HubRestApi import HubInstance
@@ -83,6 +102,7 @@
83102
import sys
84103
from argparse import ArgumentParser
85104
import argparse
105+
import re
86106

87107
#hub = HubInstance()
88108

@@ -129,7 +149,15 @@ def pull_container_image(self, image_name):
129149
args.append('pull')
130150
args.append(image_name)
131151
return subprocess.run(args)
132-
152+
153+
def get_container_image_history(self, image_name):
154+
args = []
155+
args.append(self.docker_path)
156+
args.append('history')
157+
args.append(image_name)
158+
result = subprocess.run(args, capture_output=True)
159+
return result
160+
133161
def save_container_image(self, image_name):
134162
args = []
135163
args.append(self.docker_path)
@@ -216,9 +244,25 @@ def __init__(
216244
def prepare_container_image(self):
217245
self.docker.initdir()
218246
self.docker.pull_container_image(self.container_image_name)
247+
result = self.docker.get_container_image_history(self.container_image_name)
248+
history = result.stdout.splitlines()
249+
layer_count = 0
250+
history_grouping = ''
251+
for line in reversed(history):
252+
print (line)
253+
if not line.rstrip().endswith(b' 0B'):
254+
layer_count +=1
255+
match = re.search('echo (.+?)_group_end', str(line))
256+
if match:
257+
found = match.group(1)
258+
if len(history_grouping):
259+
history_grouping += ','
260+
history_grouping += str(layer_count) + ":" + found
261+
if len(history_grouping) and self.grouping == '1024:everything':
262+
self.grouping = history_grouping
219263
self.docker.save_container_image(self.container_image_name)
220264
self.docker.unravel_container()
221-
265+
222266
def process_container_image_by_user_defined_groups(self):
223267
self.manifest = self.docker.read_manifest()
224268
print(self.manifest)

0 commit comments

Comments
 (0)