Exemplo n.º 1
0
        def collect_metrics(scan: 'ScanResult', start: 'float', artifact_type: 'ArtifactType'):
            """Collect application metrics from this scan"""
            # Collect timing information
            statsd.timing(SCAN_TIME, perf_counter() - start)

            type_tag = 'type:%s' % ArtifactType.to_string(artifact_type)

            if scan.bit is True:
                if scan.verdict is True:
                    verdict_tag = 'verdict:malicious'
                elif scan.verdict is False:
                    verdict_tag = 'verdict:benign'
                elif scan.verdict is None:
                    verdict_tag = 'verdict:none'
                else:
                    verdict_tag = 'verdict:invalid.%s' % type(scan.verdict).__name__

                if verbose:
                    statsd.increment(SCAN_VERDICT, tags=[type_tag, verdict_tag])

                statsd.increment(SCAN_SUCCESS, tags=[type_tag, verdict_tag])

            elif scan.bit is False:
                try:
                    # Treat any scan result w/ bit=False & 'scan_error' in metadata as an error
                    statsd.increment(SCAN_FAIL, tags=[
                        type_tag,
                        'scan_error:%s' % extract_verdict(scan).__dict__['scan_error']
                    ])
                except (AttributeError, KeyError):
                    # otherwise, the engine is just reporting no result
                    statsd.increment(SCAN_NO_RESULT, tags=[type_tag])

            else:
                statsd.increment(SCAN_TYPE_INVALID, tags=[type_tag])
    async def post_bounty(self,
                          artifact_type,
                          amount,
                          artifact_uri,
                          duration,
                          chain,
                          api_key=None,
                          metadata=None):
        """Post a bounty to polyswarmd.

        Args:
            artifact_type (ArtifactType): The artifact type in this bounty
            amount (int): The amount to put up as a bounty
            artifact_uri (str): URI of artifacts
            duration (int): Number of blocks to accept new assertions
            chain (str): Which chain to operate on
            api_key (str): Override default API key
            metadata (str): Optional IPFS hash for metadata
        Returns:
            Response JSON parsed from polyswarmd containing emitted events
        """
        bounty_fee = await self.parameters[chain].get('bounty_fee')
        bloom = await self.calculate_bloom(artifact_uri)
        num_artifacts = await self.__client.get_artifact_count(artifact_uri)
        transaction = PostBountyTransaction(
            self.__client, ArtifactType.to_string(artifact_type), amount,
            bounty_fee, artifact_uri, num_artifacts, duration, bloom, metadata)
        success, result = await transaction.send(chain, api_key=api_key)

        if not success or 'bounties' not in result:
            logger.error('Expected bounty, received', extra={'extra': result})

        return result.get('bounties', [])
Exemplo n.º 3
0
def test_scanalytics(statsd, engine_info, use_async, scan_result, verbose_metrics, artifact_kind):
    is_error = isinstance(scan_result, Exception)
    args = (None, str(uuid4()), artifact_kind, b'content', {}, 'home')
    type_tag = 'type:%s' % ArtifactType.to_string(artifact_kind)
    if use_async:

        @scanalytics(statsd=statsd, engine_info=engine_info, verbose=verbose_metrics)
        async def scanfn(self, guid, artifact_type, content, metadata, chain):
            if is_error:
                raise scan_result
            return scan_result

        result = asyncio.run(scanfn(*args))
    else:

        @scanalytics(statsd=statsd, engine_info=engine_info, verbose=verbose_metrics)
        def scanfn(self, guid, artifact_type, content, metadata, chain):
            if is_error:
                raise scan_result
            return scan_result

        result = scanfn(*args)

    statsd.timing.assert_called_once()

    assert isinstance(result.metadata, str)
    result_meta = Verdict.parse_raw(result.metadata)
    assert result_meta.scanner.signatures_version == engine_info.definitions_version
    assert result_meta.scanner.vendor_version == engine_info.engine_version

    if is_error:
        assert result_meta.__dict__['scan_error'] == scan_result.event_name
        assert result.bit is False
        statsd.increment.assert_called_once_with(
            SCAN_FAIL, tags=[type_tag, f'scan_error:{scan_result.event_name}']
        )
    else:
        assert result.verdict is scan_result.verdict
        assert result.bit is scan_result.bit
        verdict_tag = 'verdict:malicious' if scan_result.verdict else 'verdict:benign'

        if scan_result.bit is True:
            statsd.increment.assert_any_call(SCAN_SUCCESS, tags=[type_tag, verdict_tag])

            if verbose_metrics:
                statsd.increment.assert_any_call(
                    SCAN_VERDICT,
                    tags=[type_tag, verdict_tag],
                )
                assert statsd.increment.call_count == 2
            else:
                assert statsd.increment.call_count == 1

        elif scan_result.bit is False:
            if verbose_metrics:
                statsd.increment.assert_called_once_with(SCAN_NO_RESULT, tags=[type_tag])
Exemplo n.º 4
0
 def make_bounty():
     return {
         'guid': str(uuid.uuid4()),
         'artifact_type': ArtifactType.to_string(ArtifactType.FILE),
         'author': random_address(),
         'amount': random.randint(0, 10**18),
         'uri': random_ipfs_uri(),
         'expiration': random.randint(0, 1000),
         'metadata': None,
     }
Exemplo n.º 5
0
def bounty_to_dict(bounty):
    return {
        'guid': str(uuid.UUID(int=bounty[0])),
        'artifact_type': ArtifactType.to_string(ArtifactType(bounty[1])),
        'author': bounty[2],
        'amount': str(bounty[3]),
        'uri': bounty[4],
        'num_artifacts': bounty[5],
        'expiration': bounty[6],
        'assigned_arbiter': bounty[7],
        'quorum_reached': bounty[8],
        'quorum_reached_block': bounty[9],
        'quorum_mask': safe_int_to_bool_list(bounty[10], bounty[5]),
        'metadata': bounty[11]
    }
Exemplo n.º 6
0
    async def handle_new_bounty(guid, artifact_type, author, amount, uri,
                                expiration, metadata, block_number, txhash,
                                chain):
        new_bounty = {
            'guid': guid,
            'artifact_type': ArtifactType.to_string(artifact_type),
            'author': author,
            'amount': amount,
            'uri': uri,
            'expiration': expiration,
            'metadata': metadata,
        }

        if chain == 'home':
            assert new_bounty == home_bounty
            home_done.set()
        elif chain == 'side':
            assert new_bounty == side_bounty
            side_done.set()
        else:
            raise ValueError('Invalid chain')
def test_url_artifact_type_to_string():
    # arrange
    # act
    # assert
    assert ArtifactType.to_string(ArtifactType.URL) == 'url'
def test_file_artifact_type_to_string():
    # arrange
    # act
    # assert
    assert ArtifactType.to_string(ArtifactType.FILE) == 'file'
Exemplo n.º 9
0
class NewBounty(WebsocketFilterMessage[NewBountyMessageData]):
    """NewBounty

    doctest:
    When the doctest runs, MetadataHandler._substitute_metadata is already defined outside this
    doctest (in __main__.py). Running this as a doctest will not trigger network IO.

    >>> event = mkevent({
    ... 'guid': 1066,
    ... 'artifactType': 1,
    ... 'author': addr1,
    ... 'amount': 10,
    ... 'artifactURI': '912bnadf01295',
    ... 'expirationBlock': 118,
    ... 'metadata': 'ZWassertionuri'})
    >>> decoded_msg(NewBounty.serialize_message(event))
    {'block_number': 117,
     'data': {'amount': '10',
              'artifact_type': 'url',
              'author': '0x00000000000000000000000000000001',
              'expiration': '118',
              'guid': '00000000-0000-0000-0000-00000000042a',
              'metadata': [{'bounty_id': 69540800813340,
                            'extended_type': 'EICAR virus test files',
                            'filename': 'eicar_true',
                            'md5': '44d88612fea8a8f36de82e1278abb02f',
                            'mimetype': 'text/plain',
                            'sha1': '3395856ce81f2b7382dee72602f798b642f14140',
                            'sha256': '275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f',
                            'size': 68,
                            'type': 'FILE'}],
              'uri': '912bnadf01295'},
     'event': 'bounty',
     'txhash': '0000000000000000000000000000000b'}
    """
    event: ClassVar[str] = 'bounty'
    schema: ClassVar[PSJSONSchema] = PSJSONSchema({
        'properties': {
            'guid': guid,
            'artifact_type': {
                'type':
                'string',
                'enum': [
                    name.lower()
                    for name, value in ArtifactType.__members__.items()
                ],
                'srckey':
                lambda k, e: ArtifactType.to_string(
                    ArtifactType(e['artifactType']))
            },
            'author': ethereum_address,
            'amount': {
                'type': 'string',
            },
            'uri': {
                'srckey': 'artifactURI'
            },
            'expiration': {
                'srckey': 'expirationBlock',
                'type': 'string',
            },
            'metadata': {
                'type': 'string'
            }
        }
    })

    @classmethod
    def to_message(cls, event) -> WebsocketEventMessage[NewBountyMessageData]:
        return MetadataHandler.fetch(super().to_message(event),
                                     validate=BountyMetadata.validate)