File: create_services_enabled.py

package info (click to toggle)
scap-security-guide 0.1.39-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 31,836 kB
  • sloc: xml: 129,736; python: 7,462; sh: 3,796; makefile: 27
file content (86 lines) | stat: -rw-r--r-- 2,913 bytes parent folder | download
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
#!/usr/bin/env python2

#
# create_services_enabled.py
#   automatically generate checks for enabled services

import sys

from template_common import FilesGenerator, UnknownTargetError


class ServiceEnabledGenerator(FilesGenerator):
    def generate(self, target, serviceinfo):
        try:
            # get the items out of the list
            servicename, packagename, daemonname = serviceinfo
        except ValueError as e:
            print("\tEntry: %s\n" % serviceinfo)
            print("\tError unpacking servicename, packagename, and daemonname: " + str(e))
            sys.exit(1)

        if not daemonname:
            daemonname = servicename

        if target == "oval":
            if packagename:
                self.file_from_template(
                    "./template_OVAL_service_enabled",
                    {
                        "%SERVICENAME%": servicename,
                        "%PACKAGENAME%": packagename,
                        "%DAEMONNAME%": daemonname
                    },
                    "./oval/service_{0}_enabled.xml", servicename
                )
            else:
                self.file_from_template(
                    "./template_OVAL_service_enabled",
                    {
                        "%SERVICENAME%": servicename,
                        "%DAEMONNAME%": daemonname
                    },
                    regex_replace=[
                        ("\n\s*<criteria.*>\n\s*<extend_definition.*/>", ""),
                        ("\s*</criteria>\n\s*</criteria>", "\n    </criteria>")
                    ],
                    filename_format="./oval/service_{0}_enabled.xml",
                    filename_value=servicename
                )

        elif target == "bash":
            self.file_from_template(
                "./template_BASH_service_enabled",
                {
                    "%SERVICENAME%": servicename,
                    "%DAEMONNAME%": daemonname
                },
                "./bash/service_{0}_enabled.sh", servicename
            )

        elif target == "ansible":
            self.file_from_template(
                "./template_ANSIBLE_service_enabled",
                {
                    "%SERVICENAME%": servicename,
                    "%DAEMONNAME%": daemonname
                },
                "./ansible/service_{0}_enabled.yml", servicename
            )

        elif target == "puppet":
            self.file_from_template(
                "./template_PUPPET_service_enabled",
                {
                    "%SERVICENAME%": servicename,
                    "%DAEMONNAME%": daemonname
                },
                "./puppet/service_{0}_enabled.yml", servicename
            )

        else:
            raise UnknownTargetError(target)

    def csv_format(self):
        return("CSV should contains lines of the format: " +
               "servicename,packagename")