def test_favored_other_value(): # arrange bounty_filter = ConfidenceModifier( [Filter('mimetype', FilterComparison.EQ, 'text/plain')], None) # act confidence = bounty_filter.modify({'mimetype': 'text/plain'}, .5) # assert assert confidence == .6
def test_not_favored(): # arrange bounty_filter = ConfidenceModifier( [Filter('mimetype', FilterComparison.EQ, 'text/plain')], None) # act confidence = bounty_filter.modify({'mimetype': 'text/html'}, 1.0) # assert assert confidence == 1.0
def test_penalized(): # arrange bounty_filter = ConfidenceModifier( None, [Filter('mimetype', FilterComparison.EQ, 'text/plain')]) # act confidence = bounty_filter.modify({'mimetype': 'text/plain'}, 1.0) # assert assert confidence == 0.8
def test_offset(): # arrange bounty_filter = ConfidenceModifier( [Filter('mimetype', FilterComparison.EQ, 'text/plain')], [Filter('filesize', FilterComparison.LT, '68')]) # act confidence = bounty_filter.modify( { 'mimetype': 'text/plain', 'filesize': '21' }, 1.0) # assert assert confidence == 1.0
def test_multiple_favored(): # arrange bounty_filter = ConfidenceModifier([ Filter('mimetype', FilterComparison.EQ, 'text/plain'), Filter('filesize', FilterComparison.LT, '68') ], None) # act confidence = bounty_filter.modify( { 'mimetype': 'text/plain', 'filesize': '21' }, 1.0) # assert assert confidence == 1.2
def __init__(self, client, testing=0, scanner=None, chains=None, artifact_types=None, bid_strategy=None, bounty_filter=BountyFilter(None, None), confidence_modifier=ConfidenceModifier(None, None)): self.client = client self.chains = chains self.scanner = scanner if artifact_types is None: self.valid_artifact_types = [ArtifactType.FILE] else: self.valid_artifact_types = artifact_types self.bounty_filter = bounty_filter self.confidence_modifier = confidence_modifier self.client.on_run.register(self.__handle_run) self.client.on_stop.register(self.__handle_stop) self.client.on_new_bounty.register(self.__handle_new_bounty) self.client.on_reveal_assertion_due.register( self.__handle_reveal_assertion) self.client.on_quorum_reached.register(self.__handle_quorum_reached) self.client.on_settle_bounty_due.register(self.__handle_settle_bounty) self.client.on_deprecated.register(self.__handle_deprecated) self.bid_strategy = bid_strategy self.testing = testing self.bounties_seen = 0 self.reveals_posted = 0 self.settles_posted = 0
def main(log, client_log, polyswarmd_addr, keyfile, password, api_key, backend, testing, insecure_transport, allow_key_over_http, chains, log_format, artifact_type, bid_strategy, accept, exclude, filter, confidence): """ Entrypoint for the microengine driver """ utils.fast_deprecation() loglevel = getattr(logging, log.upper(), None) clientlevel = getattr(logging, client_log.upper(), None) if not isinstance(loglevel, int) or not isinstance(clientlevel, int): logging.error('invalid log level') raise FatalError('Invalid log level', 1) polyswarmd_addr = utils.finalize_polyswarmd_addr(polyswarmd_addr, api_key, allow_key_over_http, insecure_transport) if insecure_transport: warnings.warn( '--insecure-transport will be removed soon. Please add http:// or https:// to polyswarmd-addr`', DeprecationWarning) logger_name, microengine_class = choose_backend(backend) bid_logger_name, bid_strategy_class = choose_bid_strategy(bid_strategy) artifact_types = None init_logging(['microengine', logger_name], log_format, loglevel) init_logging(['polyswarmclient'], log_format, clientlevel) if artifact_type: artifact_types = [ ArtifactType.from_string(artifact) for artifact in artifact_type ] filter_accept = filter.get('accept', []) filter_reject = filter.get('reject', []) if accept or exclude: warnings.warn( 'Options `--exclude|accept key:value` are deprecated, please switch to' ' `--filter accept|reject key comparison value`', DeprecationWarning) filter_accept.extend(accept) filter_reject.extend(exclude) favor = confidence.get('favor', []) penalize = confidence.get('penalize', []) microengine_class.connect( polyswarmd_addr, keyfile, password, api_key=api_key, artifact_types=artifact_types, bid_strategy=bid_strategy_class(), bounty_filter=BountyFilter(filter_accept, filter_reject), chains=set(chains), confidence_modifier=ConfidenceModifier(favor, penalize), testing=testing).run()
def main(log, client_log, polyswarmd_addr, keyfile, password, api_key, backend, testing, insecure_transport, chains, log_format, artifact_type, bid_strategy, accept, exclude, filter, confidence): """Entrypoint for the microengine driver """ loglevel = getattr(logging, log.upper(), None) clientlevel = getattr(logging, client_log.upper(), None) if not isinstance(loglevel, int) or not isinstance(clientlevel, int): logging.error('invalid log level') sys.exit(-1) logger_name, microengine_class = choose_backend(backend) bid_logger_name, bid_strategy_class = choose_bid_strategy(bid_strategy) artifact_types = None init_logging(['microengine', logger_name], log_format, loglevel) init_logging(['polyswarmclient'], log_format, clientlevel) if artifact_type: artifact_types = [ ArtifactType.from_string(artifact) for artifact in artifact_type ] filter_accept = filter.get('accept', []) filter_reject = filter.get('reject', []) if accept or exclude: logger.warning( 'Options `--exclude|accept key:value` are deprecated, please switch to `--filter ' 'accept|reject key comparison value`') filter_accept.extend(accept) filter_reject.extend(exclude) favor = confidence.get('favor', []) penalize = confidence.get('penalize', []) microengine_class.connect( polyswarmd_addr, keyfile, password, api_key=api_key, artifact_types=artifact_types, bid_strategy=bid_strategy_class(), bounty_filter=BountyFilter(filter_accept, filter_reject), chains=set(chains), confidence_modifier=ConfidenceModifier(favor, penalize), insecure_transport=insecure_transport, testing=testing).run()
def connect(cls, polyswarmd_addr, keyfile, password, api_key=None, testing=0, insecure_transport=False, scanner=None, chains=None, artifact_types=None, bid_strategy=None, bounty_filter=BountyFilter(None, None), confidence_modifier=ConfidenceModifier(None, None)): """Connect the Microengine to a Client. Args: polyswarmd_addr (str): URL of polyswarmd you are referring to. keyfile (str): Keyfile filename. password (str): Password associated with Keyfile. api_key (str): Your PolySwarm API key. testing (int): Number of testing bounties to use. insecure_transport (bool): Allow insecure transport such as HTTP? scanner (Scanner): `Scanner` instance to use. chains (set(str)): Set of chains you are acting on. artifact_types (list(ArtifactType)): List of artifact types you support bid_strategy (BidStrategyBase): Bid Strategy for bounties bounty_filter (BountyFilter): Filters to accept/reject artifacts confidence_modifier (ConfidenceModifier): Filters to adjust confidence based on metadata Returns: AbstractMicroengine: Microengine instantiated with a Client. """ client = Client(polyswarmd_addr, keyfile, password, api_key, testing > 0, insecure_transport) return cls(client, testing, scanner, chains, artifact_types, bid_strategy=bid_strategy, bounty_filter=bounty_filter, confidence_modifier=confidence_modifier)