Beispiel #1
0
def test_streamingdifftracker_offset_new():
    added = []

    tracker = StreamingDiffTracker(added.append, 5)
    tracker.push_new(['b', 'c', 'd', 'e'])
    tracker.push_old(['a', 'b', 'c'])
    tracker.done()

    assert added == ['d', 'e']
Beispiel #2
0
def test_streamingdifftracker_offset_new():
    added = []

    tracker = StreamingDiffTracker(added.append, 5)
    tracker.push_new(["b", "c", "d", "e"])
    tracker.push_old(["a", "b", "c"])
    tracker.done()

    assert added == ["d", "e"]
Beispiel #3
0
def test_streamingdifftracker_same_streams():
    added = []

    tracker = StreamingDiffTracker(added.append, 3)
    tracker.push_new(['a', 'b', 'c'])
    tracker.push_old(['a', 'b', 'c'])
    tracker.done()

    assert added == []
Beispiel #4
0
def test_streamingdifftracker_basic():
    added = []

    tracker = StreamingDiffTracker(added.append, 3)
    tracker.push_new(["a", "b", "c"])
    tracker.push_old(["b"])
    tracker.done()

    assert added == ["a", "c"]
Beispiel #5
0
    def process_notification_page_data(self, notification_page_data):
        """
        Processes the given notification page data to spawn vulnerability notifications as
        necessary.

        Returns the status of the processing.
        """
        if not "New" in notification_page_data:
            return self._done()

        new_data = notification_page_data["New"]
        old_data = notification_page_data.get("Old", {})

        new_vuln = new_data["Vulnerability"]
        old_vuln = old_data.get("Vulnerability", {})

        self.vulnerability_info = new_vuln

        new_layer_ids = new_data.get("LayersIntroducingVulnerability", [])
        old_layer_ids = old_data.get("LayersIntroducingVulnerability", [])

        new_severity = PRIORITY_LEVELS.get(new_vuln.get("Severity", "Unknown"),
                                           {"index": sys.maxint})
        old_severity = PRIORITY_LEVELS.get(old_vuln.get("Severity", "Unknown"),
                                           {"index": sys.maxint})

        # Check if the severity of the vulnerability has increased. If so, then we report this
        # vulnerability for *all* layers, rather than a difference, as it is important for everyone.
        if new_severity["index"] < old_severity["index"]:
            # The vulnerability has had its severity increased. Report for *all* layers.
            all_layer_ids = set(new_layer_ids) | set(old_layer_ids)
            for layer_id in all_layer_ids:
                self._report(layer_id)

            if "NextPage" not in notification_page_data:
                return self._done()
            else:
                return ProcessNotificationPageResult.FINISHED_PAGE

        # Otherwise, only send the notification to new layers. To find only the new layers, we
        # need to do a streaming diff vs the old layer IDs stream.

        # Check for ordered data. If found, we use the indexed tracker, which is faster and
        # more memory efficient.
        is_indexed = False
        if ("OrderedLayersIntroducingVulnerability" in new_data
                or "OrderedLayersIntroducingVulnerability" in old_data):

            def tuplize(stream):
                return [(entry["LayerName"], entry["Index"])
                        for entry in stream]

            new_layer_ids = tuplize(
                new_data.get("OrderedLayersIntroducingVulnerability", []))
            old_layer_ids = tuplize(
                old_data.get("OrderedLayersIntroducingVulnerability", []))
            is_indexed = True

        # If this is the first call, initialize the tracker.
        if self.stream_tracker is None:
            self.stream_tracker = (IndexedStreamingDiffTracker(
                self._report, self.results_per_stream)
                                   if is_indexed else StreamingDiffTracker(
                                       self._report, self.results_per_stream))

        # Call to add the old and new layer ID streams to the tracker. The tracker itself will
        # call _report whenever it has determined a new layer has been found.
        self.stream_tracker.push_new(new_layer_ids)
        self.stream_tracker.push_old(old_layer_ids)

        # Check to see if there are any additional pages to process.
        if "NextPage" not in notification_page_data:
            return self._done()
        else:
            return ProcessNotificationPageResult.FINISHED_PAGE
Beispiel #6
0
def test_streamingdifftracker_more_new2():
    added = []

    tracker = StreamingDiffTracker(added.append, 4)
    tracker.push_new(['a', 'b', 'c', 'd'])
    tracker.push_old(['r'])

    tracker.push_new(['e', 'f', 'g', 'h'])
    tracker.push_old([])

    tracker.push_new(['i', 'j', 'r', 'z'])
    tracker.push_old([])
    tracker.done()

    assert added == ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'z']
Beispiel #7
0
def test_streamingdifftracker_more_old():
    added = []

    tracker = StreamingDiffTracker(added.append, 2)
    tracker.push_new(['c', 'd'])
    tracker.push_old(['a', 'b'])

    tracker.push_new([])
    tracker.push_old(['c'])
    tracker.done()

    assert added == ['d']
Beispiel #8
0
def test_streamingdifftracker_empty_old():
    added = []

    tracker = StreamingDiffTracker(added.append, 3)
    tracker.push_new(['a', 'b', 'c'])
    tracker.push_old([])

    tracker.push_new(['f', 'g', 'h'])
    tracker.push_old([])
    tracker.done()

    assert added == ['a', 'b', 'c', 'f', 'g', 'h']
Beispiel #9
0
def test_streamingdifftracker_multiple_calls():
    added = []

    tracker = StreamingDiffTracker(added.append, 3)
    tracker.push_new(['a', 'b', 'c'])
    tracker.push_old(['b', 'd', 'e'])

    tracker.push_new(['f', 'g', 'h'])
    tracker.push_old(['g', 'h'])
    tracker.done()

    assert added == ['a', 'c', 'f']
Beispiel #10
0
def test_streamingdifftracker_more_new2():
    added = []

    tracker = StreamingDiffTracker(added.append, 4)
    tracker.push_new(["a", "b", "c", "d"])
    tracker.push_old(["r"])

    tracker.push_new(["e", "f", "g", "h"])
    tracker.push_old([])

    tracker.push_new(["i", "j", "r", "z"])
    tracker.push_old([])
    tracker.done()

    assert added == ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "z"]
Beispiel #11
0
def test_streamingdifftracker_more_old():
    added = []

    tracker = StreamingDiffTracker(added.append, 2)
    tracker.push_new(["c", "d"])
    tracker.push_old(["a", "b"])

    tracker.push_new([])
    tracker.push_old(["c"])
    tracker.done()

    assert added == ["d"]
Beispiel #12
0
def test_streamingdifftracker_empty_old():
    added = []

    tracker = StreamingDiffTracker(added.append, 3)
    tracker.push_new(["a", "b", "c"])
    tracker.push_old([])

    tracker.push_new(["f", "g", "h"])
    tracker.push_old([])
    tracker.done()

    assert added == ["a", "b", "c", "f", "g", "h"]
Beispiel #13
0
def test_streamingdifftracker_multiple_calls():
    added = []

    tracker = StreamingDiffTracker(added.append, 3)
    tracker.push_new(["a", "b", "c"])
    tracker.push_old(["b", "d", "e"])

    tracker.push_new(["f", "g", "h"])
    tracker.push_old(["g", "h"])
    tracker.done()

    assert added == ["a", "c", "f"]