File: gen_profile_table.py

package info (click to toggle)
scap-security-guide 0.1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 110,644 kB
  • sloc: xml: 241,883; sh: 73,777; python: 32,527; makefile: 27
file content (70 lines) | stat: -rw-r--r-- 2,518 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
#!/usr/bin/python3

from __future__ import print_function

import os
import sys

import ssg.build_yaml

import tables.table_renderer


class HtmlOutput(tables.table_renderer.TableHtmlOutput):
    TEMPLATE_NAME = "tables/profile_tables_template.html"

    def __init__(self, * args, ** kwargs):
        profile_id = kwargs.pop("profile_id")
        super(HtmlOutput, self).__init__(* args, ** kwargs)

        profile_path = os.path.join(self.built_content_path, "profiles", (profile_id + ".profile"))
        self.profile = ssg.build_yaml.Profile.from_yaml(profile_path, self.env_yaml)
        self.profile_variables = self.profile.variables

    def _get_var_value(self, varname):
        try:
            return self.profile_variables[varname]
        except KeyError:
            return super(HtmlOutput, self)._get_var_value(varname)

    def _get_eligible_rules(self, refcat):
        eligible_rule_ids = self.profile.selected
        filenames = [os.path.join(self.rules_root, rid + ".yml") for rid in eligible_rule_ids]
        return [ssg.build_yaml.Rule.from_yaml(f, self.env_yaml) for f in filenames]

    def _generate_shortened_ref(self, reference, rule):
        shortened_ref = super(HtmlOutput, self)._generate_shortened_ref(reference, rule)
        if shortened_ref == self.DEFAULT_SHORTENED_REF and self.verbose:
            print("Rule '{rid}' is included in the profile {profile_id}, "
                  "but doesn't contain the {ref_id} reference"
                  .format(rid=rule.id_, profile_id=self.profile.id_, ref_id=reference.id),
                  file=sys.stderr)
        return shortened_ref

    def process_rules(self, reference):
        super(HtmlOutput, self).process_rules(reference)

        self.template_data["profile"] = self.profile


def update_parser(parser):
    parser.add_argument(
        "profile", metavar="PROFILE_ID", help="The ID of the profile")


def parse_args():
    parser = HtmlOutput.create_parser(
        "Generate HTML table that maps references to rules in context of a profile "
        "using compiled rules and compiled profiles as source of data.")
    tables.table_renderer.update_parser(parser)
    update_parser(parser)
    return parser.parse_args()


if __name__ == "__main__":
    args = parse_args()
    renderer = HtmlOutput(
        args.product, args.build_dir, profile_id=args.profile, verbose=args.verbose)
    reference = ssg.constants.REFERENCES[args.refcategory]
    renderer.process_rules(reference)
    renderer.output_results(args)