1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import errno
import json
import os
import shutil
import tarfile
import docker.errors
try:
import podman.errors.exceptions
except (ImportError, ModuleNotFoundError):
pass
import git
import requests
from requests import exceptions as requests_exc
from kolla.common import task # noqa
from kolla.common import utils # noqa
from kolla.engine_adapter import engine
from kolla.image.utils import Status
from kolla.image.utils import STATUS_ERRORS
class ArchivingError(Exception):
pass
class EngineTask(task.Task):
def __init__(self, conf):
super(EngineTask, self).__init__()
self._ec = None
self.conf = conf
@property
def engine_client(self):
if self._ec is not None:
return self._ec
self._ec = engine.getEngineClient(self.conf)
return self._ec
class PushIntoQueueTask(task.Task):
"""Task that pushes some other task into a queue."""
def __init__(self, push_task, push_queue):
super(PushIntoQueueTask, self).__init__()
self.push_task = push_task
self.push_queue = push_queue
@property
def name(self):
return 'PushIntoQueueTask(%s)' % (self.push_task.name)
def run(self):
self.push_queue.put(self.push_task)
self.success = True
class PushError(Exception):
"""Raised when there is a problem with pushing image to repository."""
pass
class PushTask(EngineTask):
"""Task that pushes an image to a container image repository."""
def __init__(self, conf, image):
super(PushTask, self).__init__(conf)
self.conf = conf
self.image = image
self.logger = image.logger
@property
def name(self):
return 'PushTask(%s)' % self.image.name
def run(self):
image = self.image
self.logger.info('Trying to push the image')
try:
self.push_image(image)
except requests_exc.ConnectionError:
self.logger.exception('Make sure container engine daemon is '
'running and that you have the correct '
'privileges to run the '
'container engine (root)')
image.status = Status.CONNECTION_ERROR
except PushError as exception:
self.logger.error(exception)
image.status = Status.PUSH_ERROR
except Exception:
self.logger.exception('Unknown error when pushing')
image.status = Status.PUSH_ERROR
finally:
if (image.status not in STATUS_ERRORS and
image.status != Status.UNPROCESSED):
self.logger.info('Pushed successfully')
self.success = True
else:
self.success = False
def push_image(self, image):
kwargs = dict(stream=True, decode=True)
for response in self.engine_client.images.push(image.canonical_name,
**kwargs):
if 'stream' in response:
self.logger.info(response['stream'])
elif 'errorDetail' in response:
raise PushError(response['errorDetail']['message'])
# Reset any previous errors.
image.status = Status.BUILT
class BuildTask(EngineTask):
"""Task that builds out an image."""
def __init__(self, conf, image, push_queue):
super(BuildTask, self).__init__(conf)
self.conf = conf
self.image = image
self.push_queue = push_queue
self.forcerm = not conf.keep
self.logger = image.logger
@property
def name(self):
return 'BuildTask(%s)' % self.image.name
def run(self):
self.builder(self.image)
if self.image.status in (Status.BUILT, Status.SKIPPED):
self.success = True
@property
def followups(self):
followups = []
if self.conf.push and self.success:
followups.extend([
# If we are supposed to push the image into a
# container image repository,
# then make sure we do that...
PushIntoQueueTask(
PushTask(self.conf, self.image),
self.push_queue),
])
if self.image.children and self.success:
for image in self.image.children:
if image.status in (Status.UNMATCHED, Status.SKIPPED,
Status.UNBUILDABLE):
continue
followups.append(BuildTask(self.conf, image, self.push_queue))
return followups
def process_source(self, image, source):
if not source['enabled']:
self.logger.debug("Skipping disabled source %s", source['name'])
return
dest_archive = os.path.join(image.path, source['name'] + '-archive')
# NOTE(mgoddard): Change ownership of files to root:root. This
# avoids an issue introduced by the fix for git CVE-2022-24765,
# which breaks PBR when the source checkout is not owned by the
# user installing it. LP#1969096
def reset_userinfo(tarinfo):
tarinfo.uid = tarinfo.gid = 0
tarinfo.uname = tarinfo.gname = "root"
return tarinfo
if source.get('type') == 'url':
self.logger.debug("Getting archive from %s", source['source'])
try:
r = requests.get(source['source'], timeout=self.conf.timeout)
except requests_exc.Timeout:
self.logger.exception(
'Request timed out while getting archive from %s',
source['source'])
image.status = Status.ERROR
return
if r.status_code == 200:
with open(dest_archive, 'wb') as f:
f.write(r.content)
else:
self.logger.error(
'Failed to download archive: status_code %s',
r.status_code)
image.status = Status.ERROR
return
elif source.get('type') == 'git':
clone_dir = '{}-{}'.format(dest_archive,
source['reference'].replace('/', '-'))
if os.path.exists(clone_dir):
self.logger.info("Clone dir %s exists. Removing it.",
clone_dir)
shutil.rmtree(clone_dir)
try:
self.logger.debug("Cloning from %s", source['source'])
git.Git().clone(source['source'], clone_dir)
git.Git(clone_dir).checkout(source['reference'])
reference_sha = git.Git(clone_dir).rev_parse('HEAD')
self.logger.debug("Git checkout by reference %s (%s)",
source['reference'], reference_sha)
except Exception as e:
self.logger.error("Failed to get source from git: %s",
source['source'])
self.logger.error("Error: %s", e)
# clean-up clone folder to retry
shutil.rmtree(clone_dir)
image.status = Status.ERROR
return
with tarfile.open(dest_archive, 'w') as tar:
tar.add(clone_dir, arcname=os.path.basename(clone_dir),
filter=reset_userinfo)
elif source.get('type') == 'local':
self.logger.debug("Getting local archive from %s",
source['source'])
if os.path.isdir(source['source']):
with tarfile.open(dest_archive, 'w') as tar:
tar.add(source['source'],
arcname=os.path.basename(source['source']),
filter=reset_userinfo)
else:
shutil.copyfile(source['source'], dest_archive)
else:
self.logger.error("Wrong source type '%s'", source.get('type'))
image.status = Status.ERROR
return
# Set time on destination archive to epoch 0
os.utime(dest_archive, (0, 0))
return dest_archive
def update_buildargs(self):
buildargs = dict()
if self.conf.build_args:
buildargs = dict(self.conf.build_args)
proxy_vars = ('HTTP_PROXY', 'http_proxy', 'HTTPS_PROXY',
'https_proxy', 'FTP_PROXY', 'ftp_proxy',
'NO_PROXY', 'no_proxy')
for proxy_var in proxy_vars:
if proxy_var in os.environ and proxy_var not in buildargs:
buildargs[proxy_var] = os.environ.get(proxy_var)
if not buildargs:
return None
return buildargs
def builder(self, image):
def _test_malicious_tarball(archive, path):
tar_file = tarfile.open(archive, 'r|*')
for n in tar_file.getnames():
if not os.path.abspath(os.path.join(path, n)).startswith(path):
tar_file.close()
self.logger.error(f'Unsafe filenames in archive {archive}')
raise ArchivingError
def make_an_archive(items, arcname, item_child_path=None):
if not item_child_path:
item_child_path = arcname
archives = list()
items_path = os.path.join(image.path, item_child_path)
for item in items:
archive_path = self.process_source(image, item)
if image.status in STATUS_ERRORS:
raise ArchivingError
if archive_path:
archives.append(archive_path)
if archives:
for archive in archives:
_test_malicious_tarball(archive, items_path)
with tarfile.open(archive, 'r') as archive_tar:
archive_tar.extractall(path=items_path) # nosec
else:
try:
os.mkdir(items_path)
except OSError as e:
if e.errno == errno.EEXIST:
self.logger.info(
'Directory %s already exist. Skipping.',
items_path)
else:
self.logger.error('Failed to create directory %s: %s',
items_path, e)
image.status = Status.CONNECTION_ERROR
raise ArchivingError
arc_path = os.path.join(image.path, '%s-archive' % arcname)
# NOTE(jneumann): Change ownership of files to root:root. This
# avoids an issue introduced by the fix for git CVE-2022-24765,
# which breaks PBR when the source checkout is not owned by the
# user installing it. LP#1969096
def reset_userinfo(tarinfo):
tarinfo.uid = tarinfo.gid = 0
tarinfo.uname = tarinfo.gname = "root"
return tarinfo
with tarfile.open(arc_path, 'w') as tar:
tar.add(items_path, arcname=arcname, filter=reset_userinfo)
return len(os.listdir(items_path))
self.logger.debug('Processing')
if image.status in [Status.SKIPPED, Status.UNBUILDABLE]:
self.logger.info('Skipping %s' % image.name)
return
if image.status == Status.UNMATCHED:
return
if (image.parent is not None and
image.parent.status in STATUS_ERRORS):
self.logger.error('Parent image error\'d with message "%s"',
image.parent.status)
image.status = Status.PARENT_ERROR
return
image.status = Status.BUILDING
image.start = datetime.datetime.now()
self.logger.info('Building started at %s' % image.start)
if image.source and 'source' in image.source:
self.process_source(image, image.source)
if image.status in STATUS_ERRORS:
return
try:
plugins_am = make_an_archive(image.plugins, 'plugins')
except ArchivingError:
self.logger.error(
"Failed turning any plugins into a plugins archive")
return
else:
self.logger.debug(
"Turned %s plugins into plugins archive",
plugins_am)
try:
additions_am = make_an_archive(image.additions, 'additions')
except ArchivingError:
self.logger.error(
"Failed turning any additions into a additions archive")
return
else:
self.logger.debug(
"Turned %s additions into additions archive",
additions_am)
# Pull the latest image for the base distro only
pull = self.conf.pull if image.parent is None else False
buildargs = self.update_buildargs()
kwargs = {}
if self.conf.engine == engine.Engine.PODMAN.value:
# TODO(kevko): dockerfile path is a workaround,
# should be removed as soon as it will be fixed in podman-py
# https://github.com/containers/podman-py/issues/177
kwargs["dockerfile"] = image.path + '/Dockerfile'
# Podman squash is different by default
# https://github.com/containers/buildah/issues/1234
if self.conf.squash:
kwargs["squash"] = False
kwargs["layers"] = False
else:
kwargs["layers"] = True
try:
for stream in self.engine_client.images.build(
path=image.path,
tag=image.canonical_name,
nocache=not self.conf.cache,
rm=True,
network_mode=self.conf.network_mode,
pull=pull,
forcerm=self.forcerm,
buildargs=buildargs,
**kwargs)[1]:
if self.conf.engine == engine.Engine.PODMAN.value:
stream = json.loads(stream)
if 'stream' in stream:
for line in stream['stream'].split('\n'):
if line:
self.logger.info('%s', line)
if 'errorDetail' in stream:
image.status = Status.ERROR
self.logger.error('Error\'d with the following message')
for line in stream['errorDetail']['message'].split('\n'):
if line:
self.logger.error('%s', line)
return
if image.status != Status.ERROR and self.conf.squash and \
self.conf.engine == engine.Engine.DOCKER.value:
self.squash()
except engine.getEngineException(self.conf) as e:
image.status = Status.ERROR
if isinstance(e, docker.errors.BuildError):
for line in e.build_log:
if 'stream' in line:
self.logger.error(line['stream'].strip())
if isinstance(e, podman.errors.exceptions.BuildError):
for line in e.build_log:
line = json.loads(line)
if 'stream' in line:
self.logger.error(line['stream'].strip())
self.logger.exception('Unknown container engine '
'error when building')
except Exception:
image.status = Status.ERROR
self.logger.exception('Unknown error when building')
else:
image.status = Status.BUILT
now = datetime.datetime.now()
self.logger.info('Built at %s (took %s)' %
(now, now - image.start))
def squash(self):
image_tag = self.image.canonical_name
image_id = self.engine_client.images.get(image_tag).id
parent = self.engine_client.images.get(self.image.parent_name)
parent_history = parent.history()
parent_last_layer = ""
for layer in parent_history:
if layer["Tags"] is not None and \
self.image.parent_name in layer["Tags"]:
parent_last_layer = layer["Id"]
break
if not parent_last_layer:
self.logger.error('Parent last layer is not found, '
'cannot squash.')
self.image.status = Status.ERROR
return
self.logger.info('Parent last layer is: %s' % parent_last_layer)
utils.squash(image_id, image_tag, from_layer=parent_last_layer,
cleanup=self.conf.squash_cleanup,
tmp_dir=self.conf.squash_tmp_dir)
self.logger.info('Image is squashed successfully')
|