#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
We should include some Numbrs copyright here

"""

from dojo.models import Finding
import json

class KubebenchJSONParser(object):
    """
    The objective of this class is to parse a json file generated by the kubebench tool.

    @param zap_xml_filepath A proper xml generated by zap
    """

    def __init__(self, json_output, test):

        cnt = 0
        for line in json_output:
            cnt += 1
            if cnt == 1:
                node_name = json.loads(line.split()[-1])["message"]
            if cnt == 2:
                node_ip = json.loads(line.split()[-1])["message"]
                print(node_ip)
            if cnt == 4:
                jsons = str.join(" ", line.split()[2:])

        try:
            tree = json.loads(jsons)
        except:
            raise Exception("Invalid format")

        if tree:
            self.items = [data for data in self.get_items(tree, test, node_ip)]
        else:
            self.items = []

    def get_items(self, tree, test, node_ip):
        items = {}

        t_tree = json.loads(tree['message'])

        for p in t_tree['tests']:
            for d in p['results']:
                # we should get each item here

                if d['status'] == "FAIL":
                    severity = "High"
                if d['status'] == "WARN":
                    severity = "Medium"
                if d['status'] == "PASS":
                    continue

                finding = Finding(title= node_ip + " - " + d['test_desc'],
                                  description=d['test_desc'],
                                  test=test,
                                  severity=severity,
                                  mitigation=(str.join(" ", d['test_info'])).replace("\n", ""),
                                  active=False,
                                  verified=False,
                                  false_p=False,
                                  duplicate=False,
                                  out_of_scope=False,
                                  mitigated=None,
                                  impact="No impact provided",
                                  numerical_severity=Finding.get_numerical_severity(severity))

                unique_key = d['test_number']
                items[unique_key] = finding

        print(items.values())
        return items.values()
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
We should include some Numbrs copyright here

"""

from dojo.models import Finding
import json

class KubebenchJSONParser(object):
    """
    The objective of this class is to parse a json file generated by the kubebench tool.

    @param zap_xml_filepath A proper xml generated by zap
    """

    def __init__(self, json_output, test):

        cnt = 0
        for line in json_output:
            cnt += 1
            if cnt == 1:
                node_name = json.loads(line.split()[-1])["message"]
            if cnt == 2:
                node_ip = json.loads(line.split()[-1])["message"]
                print(node_ip)
            if cnt == 4:
                jsons = str.join(" ", line.split()[2:])

        try:
            tree = json.loads(jsons)
        except:
            raise Exception("Invalid format")

        if tree:
            self.items = [data for data in self.get_items(tree, test, node_ip)]
        else:
            self.items = []

    def get_items(self, tree, test, node_ip):
        items = {}

        t_tree = json.loads(tree['message'])

        for p in t_tree['tests']:
            for d in p['results']:
                # we should get each item here

                if d['status'] == "FAIL":
                    severity = "High"
                if d['status'] == "WARN":
                    severity = "Medium"
                if d['status'] == "PASS":
                    continue

                finding = Finding(title= node_ip + " - " + d['test_desc'],
                                  description=d['test_desc'],
                                  test=test,
                                  severity=severity,
                                  mitigation=(str.join(" ", d['test_info'])).replace("\n", ""),
                                  active=False,
                                  verified=False,
                                  false_p=False,
                                  duplicate=False,
                                  out_of_scope=False,
                                  mitigated=None,
                                  impact="No impact provided",
                                  numerical_severity=Finding.get_numerical_severity(severity))

                unique_key = d['test_number']
                items[unique_key] = finding

        print(items.values())
        return items.values()
#!/usr/bin/env python # -*- coding: utf-8 -*- We should include some Numbrs copyright here The objective of this class is to parse a json file generated by the kubebench tool.      @param zap_xml_filepath A proper xml generated by zap # we should get each item here