示例#1
0
    def test_check_status_before_index_sent_raise_status(self):
        # Arrange
        index = Index(sha256='a', index_as=consts.IndexType.TRUSTED)

        # Act + Assert
        with self.assertRaises(errors.IntezerError):
            index.check_status()
示例#2
0
    def test_index_by_sha256_succeeded_status_changed_to_finish(self):
        # Arrange
        with responses.RequestsMock() as mock:
            mock.add('POST',
                     url=self.full_url + '/files/{}/index'.format('a'),
                     status=201,
                     json={'result_url': '/files/index/testindex'})
            mock.add('GET',
                     url=self.full_url + '/files/index/testindex',
                     status=202)
            mock.add('GET',
                     url=self.full_url + '/files/index/testindex',
                     status=202)
            mock.add('GET',
                     url=self.full_url + '/files/index/testindex',
                     status=200,
                     json={
                         'result_url': '/files/index/testindex',
                         'status': 'succeeded'
                     })
            index = Index(sha256='a', index_as=consts.IndexType.TRUSTED)

            # Act
            index.send(wait=True)

        # Assert
        self.assertEqual(index.status, consts.IndexStatusCode.FINISH)
示例#3
0
    def test_index_by_file_succeeded_status_changed_to_finish(self):
        # Arrange
        with responses.RequestsMock() as mock:
            mock.add('POST',
                     url=self.full_url + '/files/index',
                     status=201,
                     json={'result_url': '/files/index/testindex'})
            mock.add('GET',
                     url=self.full_url + '/files/index/testindex',
                     status=202)
            mock.add('GET',
                     url=self.full_url + '/files/index/testindex',
                     status=202)
            mock.add('GET',
                     url=self.full_url + '/files/index/testindex',
                     status=200,
                     json={
                         'result_url': '/files/index/testindex',
                         'status': 'succeeded'
                     })
            index = Index(file_path='a', index_as=consts.IndexType.TRUSTED)

            with patch(self.patch_prop, mock_open(read_data='data')):
                # Act
                index.send(wait=True)

        # Assert
        self.assertEqual(index.status, consts.IndexStatusCode.FINISH)
示例#4
0
def index_hash_command(sha256: str, index_as: str, family_name: Optional[str]):
    try:
        index_operation = Index(index_as=sdk_consts.IndexType.from_str(index_as),
                                sha256=sha256,
                                family_name=family_name)
        index_operation.send(wait=False)
        return index_operation, None
    except sdk_errors.IntezerError as e:
        return None, 'Index error: {} Error occurred with hash: {}'.format(e, sha256)
示例#5
0
def index_file_command(file_path: str, index_as: str, family_name: Optional[str]):
    if not utilities.is_supported_file(file_path):
        click.echo('File is not PE, ELF, DEX or APK')
        return
    try:
        index = Index(index_as=sdk_consts.IndexType.from_str(index_as), file_path=file_path, family_name=family_name)
        index.send(wait=True)
        click.echo('Finish index: {} with status: {}'.format(index.index_id, index.status))
    except sdk_errors.IntezerError as e:
        click.echo('Index error: {}'.format(e))
示例#6
0
    def test_index_by_sha256_raise_sha256_do_not_exist(self):
        # Arrange
        with responses.RequestsMock() as mock:
            mock.add('POST',
                     url=self.full_url + '/files/{}/index'.format('a'),
                     status=404)
            index = Index(sha256='a', index_as=consts.IndexType.TRUSTED)

            # Act + Assert
            with self.assertRaises(errors.HashDoesNotExistError):
                index.send(wait=True)
示例#7
0
    def test_trusted_index_by_sha256_status_change_to_created(self):
        # Arrange
        with responses.RequestsMock() as mock:
            mock.add('POST',
                     url=self.full_url + '/files/{}/index'.format('a'),
                     status=201,
                     json={'result_url': '/files/index/testindex'})
            index = Index(sha256='a', index_as=consts.IndexType.TRUSTED)

            # Act
            index.send()

        # Assert
        self.assertEqual(index.status, consts.IndexStatusCode.CREATED)
示例#8
0
    def test_send_index_by_file_status_changed_to_created(self):
        # Arrange
        with responses.RequestsMock() as mock:
            mock.add('POST',
                     url=self.full_url + '/files/index',
                     status=201,
                     json={'result_url': '/files/index/testindex'})
            index = Index(file_path='a', index_as=consts.IndexType.TRUSTED)

            with patch(self.patch_prop, mock_open(read_data='data')):
                # Act
                index.send()

        # Assert
        self.assertEqual(index.status, consts.IndexStatusCode.CREATED)
示例#9
0
def index_directory_command(directory_path: str,
                            index_as: str,
                            family_name: Optional[str],
                            ignore_directory_count_limit: bool):
    indexes_results = []

    for root, dirs, files in os.walk(directory_path):
        files = [f for f in files if not is_hidden(os.path.join(root, f))]
        dirs[:] = [d for d in dirs if not is_hidden(os.path.join(root, d))]

        number_of_files = len(files)
        if not ignore_directory_count_limit:
            utilities.check_should_continue_for_large_dir(number_of_files, default_config.unusual_amount_in_dir)
        with click.progressbar(length=number_of_files,
                               label='Index files',
                               show_pos=True,
                               width=0) as progressbar:
            for file_name in files:
                file_path = os.path.join(root, file_name)

                if not utilities.is_supported_file(file_path):
                    click.echo('Could not open {} because it is not a supported file type'.format(file_name))
                    progressbar.update(1)
                    continue

                try:
                    index = Index(index_as=sdk_consts.IndexType.from_str(index_as),
                                  file_path=file_path,
                                  family_name=family_name)
                    index.send()
                    indexes_results.append({'file_name': file_name, 'index': index})
                except sdk_errors.IntezerError:
                    click.echo('error occurred during indexing of {}'.format(file_name))
                    progressbar.update(1)

            for index_result in indexes_results:
                try:
                    index_result['index'].wait_for_completion()
                    click.echo('Index: {} , File: {} , finished with status: {}'.format(index_result['index'].index_id,
                                                                                        index_result['file_name'],
                                                                                        index_result['index'].status))
                    progressbar.update(1)
                except Exception:
                    click.echo('error occurred during indexing of {}'.format(index_result['file_name']))
                    progressbar.update(1)
示例#10
0
    def test_failed_index_raise_index_failed(self):
        # Arrange
        with responses.RequestsMock() as mock:
            mock.add('POST',
                     url=self.full_url + '/files/{}/index'.format('a'),
                     status=201,
                     json={'result_url': '/files/index/testindex'})
            mock.add('GET',
                     url=self.full_url + '/files/index/testindex',
                     status=200,
                     json={
                         'result_url': '/files/index/testindex',
                         'status': 'failed'
                     })
            index = Index(sha256='a', index_as=consts.IndexType.TRUSTED)

            # Act + Assert
            with self.assertRaises(errors.IndexFailedError):
                index.send(wait=True)
示例#11
0
def index_by_file_without_wait(file_path, index_as, family_name=None):  # type: (str, IndexType, str) -> None
    api.set_global_api('<api_key>')

    index = Index(file_path=file_path, index_as=consts.IndexType.from_str(index_as), family_name=family_name)
    index.send()
    index.wait_for_completion()
    pprint('Index operation:{}, Index ID:{}'.format(index.status.value, index.index_id))
示例#12
0
    def test_index_by_sha256_waits_specific_time_until_compilation(self):
        # Arrange
        with responses.RequestsMock() as mock:
            mock.add('POST',
                     url=self.full_url + '/files/{}/index'.format('a'),
                     status=201,
                     json={'result_url': '/files/index/testindex'})
            mock.add('GET',
                     url=self.full_url + '/files/index/testindex',
                     status=200,
                     json={
                         'result_url': '/files/index/testindex',
                         'status': 'succeeded'
                     })
            index = Index(sha256='a', index_as=consts.IndexType.TRUSTED)
            wait = 1
            # Act
            start = datetime.datetime.utcnow()
            index.send(wait=1)
            duration = (datetime.datetime.utcnow() - start).total_seconds()

        # Assert
        self.assertEqual(index.status, consts.IndexStatusCode.FINISH)
        self.assertGreater(duration, wait)
示例#13
0
    def test_parallel_index_by_sha256_succeeded_status_changed_to_finish(self):
        # Arrange
        with responses.RequestsMock() as mock:
            first_index_name = 'a'
            second_index_name = 'b'
            mock.add('POST',
                     url=self.full_url +
                     '/files/{}/index'.format(first_index_name),
                     status=201,
                     json={'result_url': '/files/index/first'})
            mock.add('POST',
                     url=self.full_url +
                     '/files/{}/index'.format(second_index_name),
                     status=201,
                     json={'result_url': '/files/index/second'})
            mock.add('GET',
                     url=self.full_url + '/files/index/first',
                     status=200,
                     json={
                         'result_url': '/files/index/testindex',
                         'status': 'succeeded'
                     })
            mock.add('GET',
                     url=self.full_url + '/files/index/second',
                     status=200,
                     json={
                         'result_url': '/files/index/testindex',
                         'status': 'succeeded'
                     })
            first_index = Index(sha256=first_index_name,
                                index_as=consts.IndexType.TRUSTED)
            second_index = Index(sha256=second_index_name,
                                 index_as=consts.IndexType.TRUSTED)

            # Act
            first_index.send()
            second_index.send()
            first_index.wait_for_completion()
            second_index.wait_for_completion()

        # Assert
        self.assertEqual(first_index.status, consts.IndexStatusCode.FINISH)
        self.assertEqual(second_index.status, consts.IndexStatusCode.FINISH)
示例#14
0
 def test_index_malicious_without_family_name_raise_value_error(self):
     # Act + Assert
     with self.assertRaises(ValueError):
         Index(sha256='a', index_as=consts.IndexType.MALICIOUS)
示例#15
0
def index_by_sha256_with_wait(sha256, index_as, family_name=None):  # type: (str, str, str) -> None
    api.set_global_api('<api_key>')

    index = Index(sha256=sha256, index_as=consts.IndexType.from_str(index_as), family_name=family_name)
    index.send(wait=True)
    pprint('Index operation:{}, Index ID:{}'.format(index.status.value, index.index_id))