File: create_sysctl.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 (120 lines) | stat: -rw-r--r-- 4,460 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
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
import re

from template_common import FilesGenerator, UnknownTargetError


class SysctlGenerator(FilesGenerator):
    def is_ipv6_id(self, var_id):
        return var_id.find("ipv6") >= 0

    def get_files_var_for_id(self, var_id):

        if self.is_ipv6_id(var_id):
            template_name = 'template_OVAL_sysctl_ipv6'
        else:
            template_name = 'template_OVAL_sysctl'

        return {
            'template_OVAL_sysctl_static_var': 'sysctl_static_',
            'template_OVAL_sysctl_runtime_var': 'sysctl_runtime_',
            template_name: 'sysctl_'
        }

    def get_files_for_id(self, var_id):
        if self.is_ipv6_id(var_id):
            template_name = 'template_OVAL_sysctl_ipv6'
        else:
            template_name = 'template_OVAL_sysctl'

        return {
            'template_OVAL_sysctl_static': 'sysctl_static_',
            'template_OVAL_sysctl_runtime': 'sysctl_runtime_',
            template_name: 'sysctl_'
        }

    def generate(self, target, serviceinfo):
        # get the items out of the list
        sysctl_var, sysctl_val = serviceinfo
        # convert variable name to a format suitable for 'id' tags
        sysctl_var_id = re.sub('[-\.]', '_', sysctl_var)

        if target == "bash":
            # if the sysctl value is not present, use the variable template
            if not sysctl_val.strip():
                # open the template and perform the conversions
                self.file_from_template(
                    "template_BASH_sysctl_var",
                    {
                        "%SYSCTLID%":  sysctl_var_id,
                        "%SYSCTLVAR%": sysctl_var
                    },
                    "./bash/sysctl_{0}.sh", sysctl_var_id
                )
            else:
                self.file_from_template(
                    "./template_BASH_sysctl",
                    {
                        "%SYSCTLID%":  sysctl_var_id,
                        "%SYSCTLVAR%": sysctl_var,
                        "%SYSCTLVAL%": sysctl_val
                    },
                    "./bash/sysctl_{0}.sh", sysctl_var_id
                )

        elif target == "ansible":
            # if the sysctl value is not present, use the variable template
            if not sysctl_val.strip():
                # open the template and perform the conversions
                self.file_from_template(
                    "template_ANSIBLE_sysctl_var",
                    {
                        "%SYSCTLID%":  sysctl_var_id,
                        "%SYSCTLVAR%": sysctl_var
                    },
                    "./ansible/sysctl_{0}.yml", sysctl_var_id
                )
            else:
                self.file_from_template(
                    "./template_ANSIBLE_sysctl",
                    {
                        "%SYSCTLID%":  sysctl_var_id,
                        "%SYSCTLVAR%": sysctl_var,
                        "%SYSCTLVAL%": sysctl_val
                    },
                    "./ansible/sysctl_{0}.yml", sysctl_var_id
                )

        elif target == "oval":
            # if the sysctl value is not present, use the variable template
            if not sysctl_val.strip():

                # open the template files and perform the conversions
                for sysctlfile, prefix in self.get_files_var_for_id(sysctl_var_id).items():
                    self.file_from_template(
                        sysctlfile,
                        {
                            "%SYSCTLID%":  sysctl_var_id,
                            "%SYSCTLVAR%": sysctl_var,
                        },
                        "./oval/{0}.xml", prefix + sysctl_var_id
                    )

            else:
                # open the template files and perform the conversions
                for sysctlfile, prefix in self.get_files_for_id(sysctl_var_id).items():
                    self.file_from_template(
                        sysctlfile,
                        {
                            "%SYSCTLID%":  sysctl_var_id,
                            "%SYSCTLVAR%": sysctl_var,
                            "%SYSCTLVAL%": sysctl_val
                        },
                        "./oval/{0}.xml", prefix + sysctl_var_id
                    )

        else:
            raise UnknownTargetError(target)

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