Passed
Pull Request — master (#178)
by Matěj
01:58
created

org_fedora_oscap.common.extract_data()   D

Complexity

Conditions 12

Size

Total Lines 72
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 72
rs 4.8
c 0
b 0
f 0
cc 12
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like org_fedora_oscap.common.extract_data() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#
2
# Copyright (C) 2013  Red Hat, Inc.
3
#
4
# This copyrighted material is made available to anyone wishing to use,
5
# modify, copy, or redistribute it subject to the terms and conditions of
6
# the GNU General Public License v.2, or (at your option) any later version.
7
# This program is distributed in the hope that it will be useful, but WITHOUT
8
# ANY WARRANTY expressed or implied, including the implied warranties of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
10
# Public License for more details.  You should have received a copy of the
11
# GNU General Public License along with this program; if not, write to the
12
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
13
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
14
# source code or documentation are not subject to the GNU General Public
15
# License and may only be used or replicated with the express permission of
16
# Red Hat, Inc.
17
#
18
# Red Hat Author(s): Vratislav Podzimek <[email protected]>
19
#
20
21
"""
22
Module with various classes and functions needed by the OSCAP addon that are
23
not specific to any installation mode (tui, gui, ks).
24
25
"""
26
27
import os
28
import tempfile
29
import subprocess
30
import zipfile
31
import tarfile
32
33
import re
34
import logging
35
36
from collections import namedtuple
37
import gettext
38
from functools import wraps
39
40
from dasbus.identifier import DBusServiceIdentifier
41
from pyanaconda.core import constants
42
from pyanaconda.core.dbus import DBus
43
from pyanaconda.core.constants import PAYLOAD_TYPE_DNF
44
from pyanaconda.modules.common.constants.namespaces import ADDONS_NAMESPACE
45
from pyanaconda.modules.common.constants.services import NETWORK, PAYLOADS
46
from pyanaconda.modules.common.structures.packages import PackagesSelectionData
47
from pyanaconda.threading import threadMgr, AnacondaThread
48
49
from org_fedora_oscap import utils
50
from org_fedora_oscap import cpioarchive
51
52
53
log = logging.getLogger("anaconda")
54
55
56
# mimick pyanaconda/core/i18n.py
57
def _(string):
58
    if string:
59
        return gettext.translation("oscap-anaconda-addon", fallback=True).gettext(string)
60
    else:
61
        return ""
62
63
64
def N_(string): return string
65
66
67
# everything else should be private
68
__all__ = ["run_oscap_remediate", "get_fix_rules_pre",
69
           "extract_data", "strip_content_dir",
70
           "OSCAPaddonError", "get_payload_proxy", "get_packages_data",
71
           "set_packages_data"]
72
73
INSTALLATION_CONTENT_DIR = "/tmp/openscap_data/"
74
TARGET_CONTENT_DIR = "/root/openscap_data/"
75
76
SSG_DIR = "/usr/share/xml/scap/ssg/content/"
77
78
# Make it easy to change e.g. by sed substitution in spec files
79
# First name is the canonical addon name, rest are adapters
80
ADDON_NAMES = ["com_redhat_oscap", "org_fedora_oscap"]
81
82
COMPLAIN_ABOUT_NON_CANONICAL_NAMES = True
83
84
# Enable patches that set the content name at package-time
85
DEFAULT_SSG_CONTENT_NAME = ""
86
SSG_CONTENT = DEFAULT_SSG_CONTENT_NAME
87
if not SSG_CONTENT:
88
    if constants.shortProductName != 'anaconda':
89
        if constants.shortProductName == 'fedora':
90
            SSG_CONTENT = "ssg-fedora-ds.xml"
91
        else:
92
            SSG_CONTENT = (
93
                "ssg-{name}{version}-ds.xml"
94
                .format(
95
                    name=constants.shortProductName,
96
                    version=constants.productVersion.strip(".")[0]))
97
98
RESULTS_PATH = utils.join_paths(TARGET_CONTENT_DIR,
99
                                "eval_remediate_results.xml")
100
REPORT_PATH = utils.join_paths(TARGET_CONTENT_DIR,
101
                               "eval_remediate_report.html")
102
103
PRE_INSTALL_FIX_SYSTEM_ATTR = "urn:redhat:anaconda:pre"
104
105
THREAD_FETCH_DATA = "AnaOSCAPdataFetchThread"
106
107
SUPPORTED_ARCHIVES = (".zip", ".tar", ".tar.gz", ".tar.bz2", )
108
109
SUPPORTED_CONTENT_TYPES = (
110
    "datastream", "rpm", "archive", "scap-security-guide",
111
)
112
113
SUPPORTED_URL_PREFIXES = (
114
    "http://", "https://", "ftp://",  # LABEL:?, hdaX:?,
115
)
116
117
# buffer size for reading and writing out data (in bytes)
118
IO_BUF_SIZE = 2 * 1024 * 1024
119
120
# DBus constants
121
KDUMP = DBusServiceIdentifier(
122
    namespace=ADDONS_NAMESPACE,
123
    basename="Kdump",
124
    message_bus=DBus
125
)
126
127
128
class OSCAPaddonError(Exception):
129
    """Exception class for OSCAP addon related errors."""
130
131
    pass
132
133
134
class OSCAPaddonNetworkError(OSCAPaddonError):
135
    """Exception class for OSCAP addon related network errors."""
136
137
    pass
138
139
140
class ExtractionError(OSCAPaddonError):
141
    """Exception class for the extraction errors."""
142
143
    pass
144
145
146
MESSAGE_TYPE_FATAL = 0
147
MESSAGE_TYPE_WARNING = 1
148
MESSAGE_TYPE_INFO = 2
149
150
# namedtuple for messages returned from the rules evaluation
151
#   origin -- class (inherited from RuleHandler) that generated the message
152
#   type -- one of the MESSAGE_TYPE_* constants defined above
153
#   text -- the actual message that should be displayed, logged, ...
154
RuleMessage = namedtuple("RuleMessage", ["origin", "type", "text"])
155
156
157
class SubprocessLauncher(object):
158
    def __init__(self, args):
159
        self.args = args
160
        self.stdout = ""
161
        self.stderr = ""
162
        self.messages = []
163
        self.returncode = None
164
165
    def execute(self, ** kwargs):
166
        command_string = " ".join(self.args)
167
        log.info(
168
            "OSCAP addon: Executing subprocess: '{command_string}'"
169
            .format(command_string=command_string))
170
        try:
171
            proc = subprocess.Popen(self.args, stdout=subprocess.PIPE,
172
                                    stderr=subprocess.PIPE, ** kwargs)
173
        except OSError as oserr:
174
            msg = "Failed to run the oscap tool: %s" % oserr
175
            raise OSCAPaddonError(msg)
176
177
        (stdout, stderr) = proc.communicate()
178
        self.stdout = stdout.decode()
179
        self.stderr = stderr.decode(errors="replace")
180
        self.messages = re.findall(r'OpenSCAP Error:.*', self.stderr)
181
        self.messages = self.messages + re.findall(r'E: oscap:.*', self.stderr)
182
183
        self.returncode = proc.returncode
184
185
    def log_messages(self):
186
        for message in self.messages:
187
            log.warning("OSCAP addon: " + message)
188
189
190
def get_fix_rules_pre(profile, fpath, ds_id="", xccdf_id="", tailoring=""):
191
    """
192
    Get fix rules for the pre-installation environment for a given profile in a
193
    given datastream and checklist in a given file.
194
195
    :see: run_oscap_remediate
196
    :see: _run_oscap_gen_fix
197
    :return: fix rules for a given profile
198
    :rtype: str
199
200
    """
201
202
    return _run_oscap_gen_fix(profile, fpath, PRE_INSTALL_FIX_SYSTEM_ATTR,
203
                              ds_id=ds_id, xccdf_id=xccdf_id,
204
                              tailoring=tailoring)
205
206
207
def _run_oscap_gen_fix(profile, fpath, template, ds_id="", xccdf_id="",
208
                       tailoring=""):
209
    """
210
    Run oscap tool on a given file to get the contents of fix elements with the
211
    'system' attribute equal to a given template for a given datastream,
212
    checklist and profile.
213
214
    :see: run_oscap_remediate
215
    :param template: the value of the 'system' attribute of the fix elements
216
    :type template: str
217
    :return: oscap tool's stdout
218
    :rtype: str
219
220
    """
221
222
    if not profile:
223
        return ""
224
225
    args = ["oscap", "xccdf", "generate", "fix"]
226
    args.append("--template=%s" % template)
227
228
    # oscap uses the default profile by default
229
    if profile.lower() != "default":
230
        args.append("--profile=%s" % profile)
231
    if ds_id:
232
        args.append("--datastream-id=%s" % ds_id)
233
    if xccdf_id:
234
        args.append("--xccdf-id=%s" % xccdf_id)
235
    if tailoring:
236
        args.append("--tailoring-file=%s" % tailoring)
237
238
    args.append(fpath)
239
240
    proc = SubprocessLauncher(args)
241
    proc.execute()
242
    proc.log_messages()
243
    if proc.returncode != 0:
244
        msg = "Failed to generate fix rules with the oscap tool: %s" % proc.stderr
245
        raise OSCAPaddonError(msg)
246
247
    return proc.stdout
248
249
250
def run_oscap_remediate(profile, fpath, ds_id="", xccdf_id="", tailoring="",
251
                        chroot=""):
252
    """
253
    Run the evaluation and remediation with the oscap tool on a given file,
254
    doing the remediation as defined in a given profile defined in a given
255
    checklist that is a part of a given datastream. If requested, run in
256
    chroot.
257
258
    :param profile: id of the profile that will drive the remediation
259
    :type profile: str
260
    :param fpath: path to a file with SCAP content
261
    :type fpath: str
262
    :param ds_id: ID of the datastream that contains the checklist defining
263
                  the profile
264
    :type ds_id: str
265
    :param xccdf_id: ID of the checklist that defines the profile
266
    :type xccdf_id: str
267
    :param tailoring: path to a tailoring file
268
    :type tailoring: str
269
    :param chroot: path to the root the oscap tool should be run in
270
    :type chroot: str
271
    :return: oscap tool's stdout (summary of the rules, checks and fixes)
272
    :rtype: str
273
274
    """
275
276
    if not profile:
277
        return ""
278
279
    def do_chroot():
280
        """Helper function doing the chroot if requested."""
281
        if chroot and chroot != "/":
282
            os.chroot(chroot)
283
            os.chdir("/")
284
285
    # make sure the directory for the results exists
286
    results_dir = os.path.dirname(RESULTS_PATH)
287
    if chroot:
288
        results_dir = os.path.normpath(chroot + "/" + results_dir)
289
    utils.ensure_dir_exists(results_dir)
290
291
    args = ["oscap", "xccdf", "eval"]
292
    args.append("--remediate")
293
    args.append("--results=%s" % RESULTS_PATH)
294
    args.append("--report=%s" % REPORT_PATH)
295
296
    # oscap uses the default profile by default
297
    if profile.lower() != "default":
298
        args.append("--profile=%s" % profile)
299
    if ds_id:
300
        args.append("--datastream-id=%s" % ds_id)
301
    if xccdf_id:
302
        args.append("--xccdf-id=%s" % xccdf_id)
303
    if tailoring:
304
        args.append("--tailoring-file=%s" % tailoring)
305
306
    args.append(fpath)
307
308
    proc = SubprocessLauncher(args)
309
    proc.execute(preexec_fn=do_chroot)
310
    proc.log_messages()
311
312
    if proc.returncode not in (0, 2):
313
        # 0 -- success; 2 -- no error, but checks/remediation failed
314
        msg = "Content evaluation and remediation with the oscap tool "\
315
            "failed: %s" % proc.stderr
316
        raise OSCAPaddonError(msg)
317
318
    return proc.stdout
319
320
321
def extract_data(archive, out_dir, ensure_has_files=None):
322
    """
323
    Fuction that extracts the given archive to the given output directory. It
324
    tries to find out the archive type by the file name.
325
326
    :param archive: path to the archive file that should be extracted
327
    :type archive: str
328
    :param out_dir: output directory the archive should be extracted to
329
    :type out_dir: str
330
    :param ensure_has_files: relative paths to the files that must exist in the
331
                             archive
332
    :type ensure_has_files: iterable of strings or None
333
    :return: a list of files and directories extracted from the archive
334
    :rtype: [str]
335
336
    """
337
338
    if not ensure_has_files:
339
        ensure_has_files = []
340
341
    # get rid of empty file paths
342
    if not ensure_has_files:
343
        ensure_has_files = []
344
    else:
345
        ensure_has_files = [fpath for fpath in ensure_has_files if fpath]
346
347
    msg = "OSCAP addon: Extracting {archive}".format(archive=archive)
348
    if ensure_has_files:
349
        msg += ", expecting to find {files} there.".format(files=tuple(ensure_has_files))
350
    log.info(msg)
351
352
    result = []
353
    if archive.endswith(".zip"):
354
        # ZIP file
355
        try:
356
            zfile = zipfile.ZipFile(archive, "r")
357
        except Exception as exc:
358
            msg = _(f"Error extracting archive as a zipfile: {exc}")
359
            raise ExtractionError(msg)
360
361
        # generator for the paths of the files found in the archive (dirs end
362
        # with "/")
363
        files = set(info.filename for info in zfile.filelist
364
                    if not info.filename.endswith("/"))
365
        for fpath in ensure_has_files or ():
366
            if fpath not in files:
367
                msg = "File '%s' not found in the archive '%s'" % (fpath,
368
                                                                   archive)
369
                raise ExtractionError(msg)
370
371
        utils.ensure_dir_exists(out_dir)
372
        zfile.extractall(path=out_dir)
373
        result = [utils.join_paths(out_dir, info.filename) for info in zfile.filelist]
374
        zfile.close()
375
    elif archive.endswith(".tar"):
376
        # plain tarball
377
        result = _extract_tarball(archive, out_dir, ensure_has_files, None)
378
    elif archive.endswith(".tar.gz"):
379
        # gzipped tarball
380
        result = _extract_tarball(archive, out_dir, ensure_has_files, "gz")
381
    elif archive.endswith(".tar.bz2"):
382
        # bzipped tarball
383
        result = _extract_tarball(archive, out_dir, ensure_has_files, "bz2")
384
    elif archive.endswith(".rpm"):
385
        # RPM
386
        result = _extract_rpm(archive, out_dir, ensure_has_files)
387
    # elif other types of archives
388
    else:
389
        raise ExtractionError("Unsuported archive type")
390
    log.info("OSCAP addon: Extracted {files} from the supplied content"
391
             .format(files=result))
392
    return result
393
394
395
def _extract_tarball(archive, out_dir, ensure_has_files, alg):
396
    """
397
    Extract the given TAR archive to the given output directory and make sure
398
    the given file exists in the archive.
399
400
    :see: extract_data
401
    :param alg: compression algorithm used for the tarball
402
    :type alg: str (one of "gz", "bz2") or None
403
    :return: a list of files and directories extracted from the archive
404
    :rtype: [str]
405
406
    """
407
408
    if alg and alg not in ("gz", "bz2",):
409
        raise ExtractionError("Unsupported compression algorithm")
410
411
    mode = "r"
412
    if alg:
413
        mode += ":%s" % alg
414
415
    try:
416
        tfile = tarfile.TarFile.open(archive, mode)
417
    except tarfile.TarError as err:
418
        raise ExtractionError(str(err))
419
420
    # generator for the paths of the files found in the archive
421
    files = set(member.path for member in tfile.getmembers()
422
                if member.isfile())
423
424
    for fpath in ensure_has_files or ():
425
        if fpath not in files:
426
            msg = "File '%s' not found in the archive '%s'" % (fpath, archive)
427
            raise ExtractionError(msg)
428
429
    utils.ensure_dir_exists(out_dir)
430
    tfile.extractall(path=out_dir)
431
    result = [utils.join_paths(out_dir, member.path) for member in tfile.getmembers()]
432
    tfile.close()
433
434
    return result
435
436
437
def _extract_rpm(rpm_path, root="/", ensure_has_files=None):
438
    """
439
    Extract the given RPM into the directory tree given by the root argument
440
    and make sure the given file exists in the archive.
441
442
    :param rpm_path: path to the RPM file that should be extracted
443
    :type rpm_path: str
444
    :param root: root of the directory tree the RPM should be extracted into
445
    :type root: str
446
    :param ensure_has_files: relative paths to the files that must exist in the
447
                             RPM
448
    :type ensure_has_files: iterable of strings or None
449
    :return: a list of files and directories extracted from the archive
450
    :rtype: [str]
451
452
    """
453
454
    # run rpm2cpio and process the output with the cpioarchive module
455
    temp_fd, temp_path = tempfile.mkstemp(prefix="oscap_rpm")
456
    proc = subprocess.Popen(["rpm2cpio", rpm_path], stdout=temp_fd)
457
    proc.wait()
458
    if proc.returncode != 0:
459
        msg = "Failed to convert RPM '%s' to cpio archive" % rpm_path
460
        raise ExtractionError(msg)
461
462
    os.close(temp_fd)
463
464
    try:
465
        archive = cpioarchive.CpioArchive(temp_path)
466
    except cpioarchive.CpioError as err:
467
        raise ExtractionError(str(err))
468
469
    # get entries from the archive (supports only iteration over entries)
470
    entries = set(entry for entry in archive)
471
472
    # cpio entry names (paths) start with the dot
473
    entry_names = [entry.name.lstrip(".") for entry in entries]
474
475
    for fpath in ensure_has_files or ():
476
        # RPM->cpio entries have absolute paths
477
        if fpath not in entry_names and \
478
           os.path.join("/", fpath) not in entry_names:
479
            msg = "File '%s' not found in the archive '%s'" % (fpath, rpm_path)
480
            raise ExtractionError(msg)
481
482
    try:
483
        for entry in entries:
484
            if entry.size == 0:
485
                continue
486
            dirname = os.path.dirname(entry.name.lstrip("."))
487
            out_dir = os.path.normpath(root + dirname)
488
            utils.ensure_dir_exists(out_dir)
489
490
            out_fpath = os.path.normpath(root + entry.name.lstrip("."))
491
            if os.path.exists(out_fpath):
492
                continue
493
            with open(out_fpath, "wb") as out_file:
494
                buf = entry.read(IO_BUF_SIZE)
495
                while buf:
496
                    out_file.write(buf)
497
                    buf = entry.read(IO_BUF_SIZE)
498
    except (IOError, cpioarchive.CpioError) as e:
499
        raise ExtractionError(e)
500
501
    # cleanup
502
    archive.close()
503
    os.unlink(temp_path)
504
505
    return [os.path.normpath(root + name) for name in entry_names]
506
507
508
def strip_content_dir(fpaths, phase="preinst"):
509
    """
510
    Strip content directory prefix from the file paths for either
511
    pre-installation or post-installation phase.
512
513
    :param fpaths: iterable of file paths to strip content directory prefix
514
                   from
515
    :type fpaths: iterable of strings
516
    :param phase: specifies pre-installation or post-installation phase
517
    :type phase: "preinst" or "postinst"
518
    :return: the same iterable of file paths as given with the content
519
             directory prefix stripped
520
    :rtype: same type as fpaths
521
522
    """
523
524
    if phase == "preinst":
525
        remove_prefix = lambda x: x[len(INSTALLATION_CONTENT_DIR):]
526
    else:
527
        remove_prefix = lambda x: x[len(TARGET_CONTENT_DIR):]
528
529
    return utils.keep_type_map(remove_prefix, fpaths)
530
531
532
def get_ssg_path(root="/"):
533
    return utils.join_paths(root, SSG_DIR + SSG_CONTENT)
534
535
536
def ssg_available(root="/"):
537
    """
538
    Tries to find the SCAP Security Guide under the given root.
539
540
    :return: True if SSG was found under the given root, False otherwise
541
542
    """
543
544
    return os.path.exists(get_ssg_path(root))
545
546
547
def get_content_name(data):
548
    if data.content_type == "scap-security-guide":
549
        raise ValueError("Using scap-security-guide, no single content file")
550
551
    rest = "/anonymous_content"
552
    for prefix in SUPPORTED_URL_PREFIXES:
553
        if data.content_url.startswith(prefix):
554
            rest = data.content_url[len(prefix):]
555
            break
556
557
    parts = rest.rsplit("/", 1)
558
    if len(parts) != 2:
559
        raise ValueError("Unsupported url '%s'" % data.content_url)
560
561
    return parts[1]
562
563
564
def get_raw_preinst_content_path(data):
565
    """Path to the raw (unextracted, ...) pre-installation content file"""
566
    if data.content_type == "scap-security-guide":
567
        log.debug("OSCAP addon: Using scap-security-guide, no single content file")
568
        return None
569
570
    content_name = get_content_name(data)
571
    return utils.join_paths(INSTALLATION_CONTENT_DIR, content_name)
572
573
574
def get_preinst_content_path(data):
575
    """Path to the pre-installation content file"""
576
    if data.content_type == "scap-security-guide":
577
        # SSG is not copied to the standard place
578
        return data.content_path
579
580
    if data.content_type == "datastream":
581
        return get_raw_preinst_content_path(data)
582
583
    return utils.join_paths(
584
        INSTALLATION_CONTENT_DIR,
585
        data.content_path
586
    )
587
588
589
def get_postinst_content_path(data):
590
    """Path to the post-installation content file"""
591
    if data.content_type == "datastream":
592
        return utils.join_paths(
593
            TARGET_CONTENT_DIR,
594
            get_content_name(data)
595
        )
596
597
    if data.content_type in ("rpm", "scap-security-guide"):
598
        # no path magic in case of RPM (SSG is installed as an RPM)
599
        return data.content_path
600
601
    return utils.join_paths(
602
        TARGET_CONTENT_DIR,
603
        data.content_path
604
    )
605
606
607
def get_preinst_tailoring_path(data):
608
    """Path to the pre-installation tailoring file (if any)"""
609
    if not data.tailoring_path:
610
        return ""
611
612
    return utils.join_paths(
613
        INSTALLATION_CONTENT_DIR,
614
        data.tailoring_path
615
    )
616
617
618
def get_postinst_tailoring_path(data):
619
    """Path to the post-installation tailoring file (if any)"""
620
    if not data.tailoring_path:
621
        return ""
622
623
    if data.content_type == "rpm":
624
        # no path magic in case of RPM
625
        return data.tailoring_path
626
627
    return utils.join_paths(
628
        TARGET_CONTENT_DIR,
629
        data.tailoring_path
630
    )
631
632
633
def get_payload_proxy():
634
    """Get the DBus proxy of the active payload.
635
636
    :return: a DBus proxy
637
    """
638
    payloads_proxy = PAYLOADS.get_proxy()
639
    object_path = payloads_proxy.ActivePayload
640
641
    if not object_path:
642
        raise ValueError("Active payload is not set.")
643
644
    return PAYLOADS.get_proxy(object_path)
645
646
647
def get_packages_data() -> PackagesSelectionData:
648
    """Get the DBus data with the packages configuration.
649
650
    :return: a packages configuration
651
    """
652
    payload_proxy = get_payload_proxy()
653
654
    if payload_proxy.Type != PAYLOAD_TYPE_DNF:
655
        return PackagesSelectionData()
656
657
    return PackagesSelectionData.from_structure(
658
        payload_proxy.Packages
659
    )
660
661
662
def set_packages_data(data: PackagesSelectionData):
663
    """Set the DBus data with the packages configuration.
664
665
    :param data: a packages configuration
666
    """
667
    payload_proxy = get_payload_proxy()
668
669
    if payload_proxy.Type != PAYLOAD_TYPE_DNF:
670
        log.debug("OSCAP addon: The payload doesn't support packages.")
671
        return
672
673
    return payload_proxy.SetPackages(
674
        PackagesSelectionData.to_structure(data)
675
    )
676