Passed
Pull Request — master (#136)
by
unknown
01:36
created

OSCAPService.setup_kickstart()   A

Complexity

Conditions 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nop 2
dl 0
loc 15
rs 9.75
c 0
b 0
f 0
1
#
2
# Copyright (C) 2020 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
import logging
19
20
from pyanaconda.core.configuration.anaconda import conf
21
from pyanaconda.core.dbus import DBus
22
from pyanaconda.core.signal import Signal
23
from pyanaconda.modules.common.base import KickstartService
24
from pyanaconda.modules.common.containers import TaskContainer
25
from pyanaconda.modules.common.structures.requirement import Requirement
26
27
from org_fedora_oscap import common
28
from org_fedora_oscap.constants import OSCAP
29
from org_fedora_oscap.service.installation import FetchContentTask, CheckFingerprintTask, \
30
    EvaluateRulesTask, InstallContentTask, RemediateSystemTask
31
from org_fedora_oscap.service.kickstart import OSCAPKickstartSpecification
32
from org_fedora_oscap.service.oscap_interface import OSCAPInterface
33
from org_fedora_oscap.structures import PolicyData
34
35
log = logging.getLogger(__name__)
36
37
__all__ = ["OSCAPService"]
38
39
40
class OSCAPService(KickstartService):
41
    """The implementation of the OSCAP service."""
42
43
    def __init__(self):
44
        """Create a service."""
45
        super().__init__()
46
        self._policy_enabled = True
47
        self.policy_enabled_changed = Signal()
48
49
        self._policy_data = PolicyData()
50
        self.policy_data_changed = Signal()
51
52
    @property
53
    def policy_enabled(self):
54
        """Is the security policy enabled?
55
56
        :return: True or False
57
        """
58
        return self._policy_enabled
59
60
    @policy_enabled.setter
61
    def policy_enabled(self, value):
62
        """Should be the security policy enabled?
63
64
        :param value: True or False
65
        """
66
        self._policy_enabled = value
67
        self.policy_enabled_changed.emit()
68
        log.debug("Policy enabled is set to '%s'.", value)
69
70
    @property
71
    def policy_data(self):
72
        """The security policy data.
73
74
        :return: an instance of PolicyData
75
        """
76
        return self._policy_data
77
78
    @policy_data.setter
79
    def policy_data(self, value):
80
        """Set the security policy data.
81
82
        :param value: an instance of PolicyData
83
        """
84
        self._policy_data = value
85
        self.policy_data_changed.emit()
86
        log.debug("Policy data is set to '%s'.", value)
87
88
    def publish(self):
89
        """Publish the DBus objects."""
90
        TaskContainer.set_namespace(OSCAP.namespace)
91
        DBus.publish_object(OSCAP.object_path, OSCAPInterface(self))
92
        DBus.register_service(OSCAP.service_name)
93
94
    @property
95
    def kickstart_specification(self):
96
        """Return the kickstart specification."""
97
        return OSCAPKickstartSpecification
98
99
    def process_kickstart(self, data):
100
        """Process the kickstart data."""
101
        addon_data = data.addons.org_fedora_oscap
102
        policy_data = PolicyData()
103
104
        policy_data.content_type = addon_data.content_type
105
        policy_data.content_url = addon_data.content_url
106
        policy_data.datastream_id = addon_data.datastream_id
107
        policy_data.xccdf_id = addon_data.xccdf_id
108
        policy_data.profile_id = addon_data.profile_id
109
        policy_data.content_path = addon_data.content_path
110
        policy_data.cpe_path = addon_data.cpe_path
111
        policy_data.tailoring_path = addon_data.tailoring_path
112
        policy_data.fingerprint = addon_data.fingerprint
113
        policy_data.certificates = addon_data.certificates
114
115
        self.policy_data = policy_data
116
117
    def setup_kickstart(self, data):
118
        """Set the given kickstart data."""
119
        policy_data = self.policy_data
120
        addon_data = data.addons.org_fedora_oscap
121
122
        addon_data.content_type = policy_data.content_type
123
        addon_data.content_url = policy_data.content_url
124
        addon_data.datastream_id = policy_data.datastream_id
125
        addon_data.xccdf_id = policy_data.xccdf_id
126
        addon_data.profile_id = policy_data.profile_id
127
        addon_data.content_path = policy_data.content_path
128
        addon_data.cpe_path = policy_data.cpe_path
129
        addon_data.tailoring_path = policy_data.tailoring_path
130
        addon_data.fingerprint = policy_data.fingerprint
131
        addon_data.certificates = policy_data.certificates
132
133
    def collect_requirements(self):
134
        """Return installation requirements.
135
136
        :return: a list of requirements
137
        """
138
        requirements = []
139
140
        if self.policy_enabled and self.policy_data.profile_id:
141
            requirements.extend([
142
                Requirement.for_package(
143
                    package_name="openscap",
144
                    reason="Required by oscap add-on."
145
                ),
146
                Requirement.for_package(
147
                    package_name="openscap-scanner",
148
                    reason="Required by oscap add-on."
149
                )
150
            ])
151
152
            if self.policy_data.content_type == "scap-security-guide":
153
                requirements.append(
154
                    Requirement.for_package(
155
                        package_name="scap-security-guide",
156
                        reason="Required by oscap add-on."
157
                    )
158
                )
159
160
        return requirements
161
162
    def configure_with_tasks(self):
163
        """Return configuration tasks.
164
165
        :return: a list of tasks
166
        """
167
        return [
168
            FetchContentTask(
169
                policy_enabled=self.policy_enabled,
170
                policy_data=self.policy_data,
171
                file_path=common.get_raw_preinst_content_path(self.policy_data),
172
                content_path=common.get_preinst_content_path(self.policy_data),
173
            ),
174
            CheckFingerprintTask(
175
                policy_enabled=self.policy_enabled,
176
                policy_data=self.policy_data,
177
                file_path=common.get_raw_preinst_content_path(self.policy_data),
178
            ),
179
            EvaluateRulesTask(
180
                policy_enabled=self.policy_enabled,
181
                policy_data=self.policy_data,
182
                content_path=common.get_preinst_content_path(self.policy_data),
183
                tailoring_path=common.get_preinst_tailoring_path(self.policy_data),
184
            ),
185
        ]
186
187
    def install_with_tasks(self):
188
        """Return installation tasks.
189
190
        :return: a list of tasks
191
        """
192
        return [
193
            InstallContentTask(
194
                sysroot=conf.target.system_root,
195
                policy_enabled=self.policy_enabled,
196
                policy_data=self.policy_data,
197
                file_path=common.get_raw_preinst_content_path(self.policy_data),
198
                content_path=common.get_preinst_content_path(self.policy_data),
199
                tailoring_path=common.get_preinst_tailoring_path(self.policy_data),
200
                target_directory=common.TARGET_CONTENT_DIR,
201
            ),
202
            RemediateSystemTask(
203
                sysroot=conf.target.system_root,
204
                policy_enabled=self.policy_enabled,
205
                policy_data=self.policy_data,
206
                target_content_path=common.get_postinst_content_path(self.policy_data),
207
                target_tailoring_path=common.get_preinst_tailoring_path(self.policy_data)
208
            )
209
        ]
210