コード例 #1
0
 def test_create_occurrence(self):
     created = samples.create_occurrence(self.image_url, self.note_id,
                                         PROJECT_ID, PROJECT_ID)
     retrieved = samples.get_occurrence(basename(created.name), PROJECT_ID)
     assert created.name == retrieved.name
     # clean up
     samples.delete_occurrence(basename(created.name), PROJECT_ID)
コード例 #2
0
    def test_poll_discovery_occurrence(self):
        # create discovery occurrence
        note_id = 'discovery-note-{}'.format(uuid.uuid4())
        client = containeranalysis_v1.ContainerAnalysisClient()
        grafeas_client = client.get_grafeas_client()
        note = {'discovery': {'analysis_kind': NoteKind.DISCOVERY}}
        grafeas_client.\
            create_note(parent=f"projects/{PROJECT_ID}", note_id=note_id, note=note)
        occurrence = {
            'note_name': f"projects/{PROJECT_ID}/notes/{note_id}",
            'resource_uri': self.image_url,
            'discovery': {
                'analysis_status':
                DiscoveryOccurrence.AnalysisStatus.FINISHED_SUCCESS
            }
        }
        created = grafeas_client.\
            create_occurrence(parent=f"projects/{PROJECT_ID}",
                              occurrence=occurrence)

        disc = samples.poll_discovery_finished(self.image_url, 10, PROJECT_ID)
        status = disc.discovery.analysis_status
        assert disc is not None
        assert status == DiscoveryOccurrence.AnalysisStatus.FINISHED_SUCCESS

        # clean up
        samples.delete_occurrence(basename(created.name), PROJECT_ID)
        samples.delete_note(note_id, PROJECT_ID)
コード例 #3
0
    def test_find_high_severity_vulnerabilities(self):
        occ_list = samples.find_high_severity_vulnerabilities_for_image(
            self.image_url, PROJECT_ID)
        assert len(occ_list) == 0

        # create new high severity vulnerability
        note_id = 'discovery-note-{}'.format(int(time()))
        client = containeranalysis_v1.ContainerAnalysisClient()
        grafeas_client = client.get_grafeas_client()
        note = {
            'vulnerability': {
                'severity':
                Severity.CRITICAL,
                'details': [{
                    'affected_cpe_uri': 'your-uri-here',
                    'affected_package': 'your-package-here',
                    'min_affected_version': {
                        'kind': Version.VersionKind.MINIMUM
                    },
                    'fixed_version': {
                        'kind': Version.VersionKind.MAXIMUM
                    }
                }]
            }
        }
        grafeas_client.\
            create_note(grafeas_client.project_path(PROJECT_ID), note_id, note)
        occurrence = {
            'note_name': client.note_path(PROJECT_ID, note_id),
            'resource_uri': self.image_url,
            'vulnerability': {
                'package_issue': [{
                    'affected_cpe_uri': 'your-uri-here',
                    'affected_package': 'your-package-here',
                    'min_affected_version': {
                        'kind': Version.VersionKind.MINIMUM
                    },
                    'fixed_version': {
                        'kind': Version.VersionKind.MAXIMUM
                    }
                }]
            }
        }
        created = grafeas_client.\
            create_occurrence(grafeas_client.project_path(PROJECT_ID),
                              occurrence)
        # query again
        tries = 0
        count = 0
        while count != 1 and tries < TRY_LIMIT:
            tries += 1
            occ_list = samples.find_vulnerabilities_for_image(
                self.image_url, PROJECT_ID)
            count = len(occ_list)
            sleep(SLEEP_TIME)
        assert len(occ_list) == 1
        # clean up
        samples.delete_occurrence(basename(created.name), PROJECT_ID)
        samples.delete_note(note_id, PROJECT_ID)
コード例 #4
0
 def test_create_occurrence(self):
     created = samples.create_occurrence(self.image_url, self.note_id,
                                         PROJECT_ID, PROJECT_ID)
     retrieved = samples.get_occurrence(basename(created.name), PROJECT_ID)
     if created.name != retrieved.name:
         raise AssertionError
     # clean up
     samples.delete_occurrence(basename(created.name), PROJECT_ID)
コード例 #5
0
 def test_delete_occurrence(self):
     created = samples.create_occurrence(self.image_url, self.note_id,
                                         PROJECT_ID, PROJECT_ID)
     samples.delete_occurrence(basename(created.name), PROJECT_ID)
     try:
         samples.get_occurrence(basename(created.name), PROJECT_ID)
     except NotFound:
         pass
     else:
         # didn't raise exception we expected
         assert False
コード例 #6
0
 def test_occurrences_for_note(self):
     orig_count = samples.get_occurrences_for_note(self.note_id, PROJECT_ID)
     occ = samples.create_occurrence(self.image_url, self.note_id,
                                     PROJECT_ID, PROJECT_ID)
     new_count = 0
     tries = 0
     while new_count != 1 and tries < TRY_LIMIT:
         tries += 1
         new_count = samples.get_occurrences_for_note(
             self.note_id, PROJECT_ID)
         sleep(SLEEP_TIME)
     assert new_count == 1
     assert orig_count == 0
     # clean up
     samples.delete_occurrence(basename(occ.name), PROJECT_ID)
コード例 #7
0
    def test_find_vulnerabilities_for_image(self):
        occ_list = samples.find_vulnerabilities_for_image(
            self.image_url, PROJECT_ID)
        assert len(occ_list) == 0

        created = samples.create_occurrence(self.image_url, self.note_id,
                                            PROJECT_ID, PROJECT_ID)
        tries = 0
        count = 0
        while count != 1 and tries < TRY_LIMIT:
            tries += 1
            occ_list = samples.find_vulnerabilities_for_image(
                self.image_url, PROJECT_ID)
            count = len(occ_list)
            sleep(SLEEP_TIME)
        assert len(occ_list) == 1
        samples.delete_occurrence(basename(created.name), PROJECT_ID)
コード例 #8
0
    def test_poll_discovery_occurrence(self):
        # try with no discovery occurrence
        try:
            samples.poll_discovery_finished(self.image_url, 5, PROJECT_ID)
        except RuntimeError:
            pass
        else:
            # we expect timeout error
            assert False

        # create discovery occurrence
        note_id = 'discovery-note-{}'.format(uuid.uuid4())
        client = containeranalysis_v1.ContainerAnalysisClient()
        grafeas_client = client.get_grafeas_client()
        note = {
            'discovery': {
                'analysis_kind': NoteKind.DISCOVERY
            }
        }
        grafeas_client.\
            create_note(grafeas_client.project_path(PROJECT_ID), note_id, note)
        occurrence = {
            'note_name': grafeas_client.note_path(PROJECT_ID, note_id),
            'resource_uri': self.image_url,
            'discovery': {
                'analysis_status': DiscoveryOccurrence.AnalysisStatus
                                                      .FINISHED_SUCCESS
            }
        }
        created = grafeas_client.\
            create_occurrence(grafeas_client.project_path(PROJECT_ID),
                              occurrence)

        # poll again
        disc = samples.poll_discovery_finished(self.image_url, 10, PROJECT_ID)
        status = disc.discovery.analysis_status
        assert disc is not None
        assert status == DiscoveryOccurrence.AnalysisStatus.FINISHED_SUCCESS

        # clean up
        samples.delete_occurrence(basename(created.name), PROJECT_ID)
        samples.delete_note(note_id, PROJECT_ID)
コード例 #9
0
    def test_pubsub(self):
        # create topic if needed
        client = SubscriberClient()
        try:
            topic_id = 'container-analysis-occurrences-v1'
            topic_name = {"name": f"projects/{PROJECT_ID}/topics/{topic_id}"}
            publisher = PublisherClient()
            publisher.create_topic(topic_name)
        except AlreadyExists:
            pass

        subscription_id = 'container-analysis-test-{}'.format(uuid.uuid4())
        subscription_name = client.subscription_path(PROJECT_ID,
                                                     subscription_id)
        samples.create_occurrence_subscription(subscription_id, PROJECT_ID)

        # I can not make it pass with multiple messages. My guess is
        # the server started to dedup?
        message_count = 1
        try:
            job_done = threading.Event()
            receiver = MessageReceiver(message_count, job_done)
            client.subscribe(subscription_name, receiver.pubsub_callback)

            for i in range(message_count):
                occ = samples.create_occurrence(self.image_url, self.note_id,
                                                PROJECT_ID, PROJECT_ID)
                time.sleep(SLEEP_TIME)
                samples.delete_occurrence(basename(occ.name), PROJECT_ID)
                time.sleep(SLEEP_TIME)
            # We saw occational failure with 60 seconds timeout, so we bumped it
            # to 180 seconds.
            # See also: python-docs-samples/issues/2894
            job_done.wait(timeout=180)
            print('done. msg_count = {}'.format(receiver.msg_count))
            assert message_count <= receiver.msg_count
        finally:
            # clean up
            client.delete_subscription({"subscription": subscription_name})
コード例 #10
0
    def test_pubsub(self):
        # create topic if needed
        client = SubscriberClient()
        try:
            topic_id = 'container-analysis-occurrences-v1'
            topic_name = client.topic_path(PROJECT_ID, topic_id)
            publisher = PublisherClient()
            publisher.create_topic(topic_name)
        except AlreadyExists:
            pass

        subscription_id = 'drydockOccurrences'
        subscription_name = client.subscription_path(PROJECT_ID,
                                                     subscription_id)
        samples.create_occurrence_subscription(subscription_id, PROJECT_ID)
        tries = 0
        success = False
        while not success and tries < TRY_LIMIT:
            print(tries)
            tries += 1
            receiver = samples.MessageReceiver()
            client.subscribe(subscription_name, receiver.pubsub_callback)

            # test adding 3 more occurrences
            total_created = 3
            for _ in range(total_created):
                occ = samples.create_occurrence(self.image_url, self.note_id,
                                                PROJECT_ID, PROJECT_ID)
                sleep(SLEEP_TIME)
                samples.delete_occurrence(basename(occ.name), PROJECT_ID)
                sleep(SLEEP_TIME)
            print('done. msg_count = {}'.format(receiver.msg_count))
            success = receiver.msg_count == total_created
        if receiver.msg_count != total_created:
            raise AssertionError
        # clean up
        client.delete_subscription(subscription_name)
コード例 #11
0
    def test_pubsub(self):
        # create topic if needed
        client = SubscriberClient()
        try:
            topic_id = 'container-analysis-occurrences-v1'
            topic_name = client.topic_path(PROJECT_ID, topic_id)
            publisher = PublisherClient()
            publisher.create_topic(topic_name)
        except AlreadyExists:
            pass

        subscription_id = 'container-analysis-test-{}'.format(uuid.uuid4())
        subscription_name = client.subscription_path(PROJECT_ID,
                                                     subscription_id)
        samples.create_occurrence_subscription(subscription_id, PROJECT_ID)

        # I can not make it pass with multiple messages. My guess is
        # the server started to dedup?
        message_count = 1
        try:
            job_done = threading.Event()
            receiver = MessageReceiver(message_count, job_done)
            client.subscribe(subscription_name, receiver.pubsub_callback)

            for i in range(message_count):
                occ = samples.create_occurrence(self.image_url, self.note_id,
                                                PROJECT_ID, PROJECT_ID)
                time.sleep(SLEEP_TIME)
                samples.delete_occurrence(basename(occ.name), PROJECT_ID)
                time.sleep(SLEEP_TIME)
            job_done.wait(timeout=60)
            print('done. msg_count = {}'.format(receiver.msg_count))
            assert message_count <= receiver.msg_count
        finally:
            # clean up
            client.delete_subscription(subscription_name)