Beispiel #1
0
class OS(models.Model):
    RHEL5 = 5
    RHEL6 = 6
    RHEL7 = 7

    MAJOR_VERSIONS = Enum([
        (RHEL5, 'RHEL-5', 'Red Hat Enterprise Linux 5'),
        (RHEL6, 'RHEL-6', 'Red Hat Enterprise Linux 6'),
        (RHEL7, 'RHEL-7', 'Red Hat Enterprise Linux 7'),
    ])
    major = models.SmallIntegerField(choices=MAJOR_VERSIONS.get_mapping())
    minor = models.SmallIntegerField()

    class Meta:
        ordering = ('major', 'minor')

    def __unicode__(self):
        return u"%s.%d" % (self.MAJOR_VERSIONS.get_help(
            self.major), self.minor)
Beispiel #2
0
class TestResult(models.Model):
    """ this model represents rule-result """
    # order, shortcut of the state
    PASSED = '06p'  # (p)assed
    WARNING = '03w'  # (w)arning
    FAILURE = '02f'  # (f)ailure
    NEEDS_INSPECTION = '03ni'  # (n)eeds (i)nspection
    NEEDS_ACTION = '03na'  # (n)eeds (a)ction
    ERROR = '01e'  # (e)rror
    NOTAPPLICABLE = '07n'  # (n)otapplicable
    NOTCHECKED = '08c'  # not(c)hecked
    INFO = '05i'  # (i)nfo
    FIXED = '04x'  # fi(x)ed

    TEST_STATES = Enum([
        # DB value, used in css/html, helper text, printable value
        (ERROR, 'error', 'Unexpected problem during analysis.', 'Error'),
        (FAILURE, 'fail', 'Test failed, some serious problems were found.',
         'Failed'),
        (NEEDS_ACTION, 'needs_action',
         'Issues found by this test should be resolved.', 'Needs Action'),
        (NEEDS_INSPECTION, 'needs_inspection',
         'Issues found by this test should be inspected.', 'Needs Inspection'),
        (FIXED, 'fixed', 'Fixed.', 'Fixed'),
        (PASSED, 'pass', 'Test passed, no problems found.', 'Passed'),
        (INFO, 'informational', 'Test is only informative.', 'Informational'),
        (NOTAPPLICABLE, 'notapplicable', 'Test is not applicable.',
         'Not Applicable'),
        #(WARNING, 'warning', 'Test passed but there are some issues.', 'warning'),
        (NOTCHECKED, 'notchecked', 'Test couldn\'t be run.', 'Not Checked'),
    ])
    state = models.CharField(max_length=3,
                             choices=TEST_STATES.get_mapping(),
                             db_index=True)
    date = models.DateTimeField()
    test = models.ForeignKey(Test)
    group = models.ForeignKey(TestGroupResult, related_name="testresult_set")
    root_group = models.ForeignKey(TestGroupResult, related_name="all_tests")
    # since groups are nested, there is no easy way to get to result from tr
    result = models.ForeignKey(Result)

    objects = TestResultManager()

    class Meta:
        ordering = ('state', )

    def __unicode__(self):
        return u"%s [%s] [%s]" % (self.get_state_display().upper(), self.test,
                                  self.group)

    def logs(self):
        return self.testlog_set.all()

    def risks(self):
        return self.risk_set.all()

    def set_state(self, state, save=True):
        self.state = self.TEST_STATES.get_key(state)
        if save:
            self.save()

    def get_state(self):
        return TestResult.TEST_STATES[self.state]

    def display_state(self):
        return TestResult.TEST_STATES.display(self.state)

    def should_display_solution(self):
        return self.get_state() not in ['pass', 'notapplicable']
Beispiel #3
0
class HostRun(models.Model):
    """
    M2M relationship between run and hosts -- Run on each Host
    """
    RUNNING = 'r'
    FINISHED = 'f'

    RUN_STATES = Enum([
        (RUNNING, 'running', 'Scan is active.'),
        (FINISHED, 'finished', 'Scan has finished.'),
    ])
    dt_finished = models.DateTimeField(blank=True, null=True)
    host = models.ForeignKey(Host)
    run = models.ForeignKey(Run)
    state = models.CharField(max_length=1,
                             choices=RUN_STATES.get_mapping(),
                             default=RUNNING,
                             db_index=True)
    risk = models.CharField(max_length=16,
                            blank=True,
                            null=True,
                            db_index=True)

    objects = HostRunManager()

    def __unicode__(self):
        return u"%s %s %s" % (self.get_state_display(), self.host, self.run)

    class Meta:
        ordering = ('-run__dt_submitted', )

    @property
    def running(self):
        return self.state == self.RUNNING

    @property
    def finished(self):
        return self.state == self.FINISHED

    def display_groups(self):
        return self.testgroupresult.filter(parent__isnull=True)

    def set_finished(self):
        self.state = self.FINISHED
        self.dt_finished = datetime.datetime.now()
        self.save(update_fields=["state"])

    def set_risk(self):
        risks = Risk.objects.for_hostrun(self)
        risk = "slight"
        if risks:
            risks_list = list(risks.values_list('level', flat=True).distinct())
            sorted_risks_list = sorted(risks_list,
                                       key=lambda x: Risk.RISK_LEVELS[x])
            risk = sorted_risks_list[-1]
        self.risk = risk
        self.save(update_fields=["risk"])

    def delete(self):
        if self.result:
            self.result.delete()
        super(HostRun, self).delete()