def test_smrt_archiver_lasttime_clear():
    tmpfile = tempfile.mktemp()
    archiver = Archiver(dbfile=tmpfile)
    rule = 'test/smrt/rules/archiver.yml'
    feed = 'lasttime'

    with Smrt(REMOTE_ADDR, 1234, client='stdout', archiver=archiver) as s:
        assert type(s) is Smrt

        for r, f in s.load_feeds(rule, feed=feed):
            x = list(s.process(r, f))
            assert len(x) > 0

            f = {i.indicator: i.__dict__() for i in x}
            assert f['216.243.31.2']['lasttime'] == '2016-03-23T20:22:27.000000Z'

    archiver.clear_memcache()
    with Smrt(REMOTE_ADDR, 1234, client='stdout', archiver=archiver) as s:
        assert type(s) is Smrt

        for r, f in s.load_feeds(rule, feed=feed):
            x = list(s.process(r, f))
            assert len(x) == 0
Exemple #2
0
def _run_smrt(options, **kwargs):
    args = kwargs.get('args')
    goback = kwargs.get('goback')
    verify_ssl = kwargs.get('verify_ssl')
    data = kwargs.get('data')
    service_mode = kwargs.get("service_mode")

    archiver = None
    if args.remember:
        archiver = Archiver(dbfile=args.remember_path)
    else:
        archiver = NOOPArchiver()

    logger.info('starting run...')

    with Smrt(options.get('token'), options.get('remote'), client=args.client, username=args.user,
              feed=args.feed, archiver=archiver, fireball=args.fireball, no_fetch=args.no_fetch,
              verify_ssl=verify_ssl, goback=goback, skip_invalid=args.skip_invalid, send_retries=args.send_retries,
              send_retries_wait=args.send_retries_wait) as s:

        if s.client:
            s.client.ping(write=True)

        filters = {}
        if args.filter_indicator:
            filters['indicator'] = args.filter_indicator

        indicators = []
        for r, f in s.load_feeds(args.rule, feed=args.feed):
            logger.info('processing: {} - {}:{}'.format(args.rule, r.defaults['provider'], f))
            try:
                for i in s.process(r, f, limit=args.limit, data=data, filters=filters):
                    if args.client == 'stdout':
                        indicators.append(i)
            except Exception as e:
                if not service_mode and not args.skip_broken:
                    logger.error('may need to remove the old cache file: %s' % s.last_cache)
                    import traceback
                    logger.error(traceback.print_exc())
                    raise e

                logger.error(e)
                logger.info('skipping: {}'.format(args.feed))

        if args.client == 'stdout':
            print(FORMATS[options.get('format')](data=indicators, cols=args.fields.split(',')))

    logger.info('cleaning up')
    archiver.cleanup()
    archiver.clear_memcache()

    logger.info('finished run')
Exemple #3
0
def test_smrt_archiver_lasttime_clear():
    tmpfile = tempfile.mktemp()
    archiver = Archiver(dbfile=tmpfile)
    rule = 'test/smrt/rules/archiver.yml'
    feed = 'lasttime'

    with Smrt(REMOTE_ADDR, 1234, client='stdout', archiver=archiver) as s:
        assert type(s) is Smrt

        for r, f in s.load_feeds(rule, feed=feed):
            x = list(s.process(r, f))
            assert len(x) > 0

            f = {i.indicator: i.__dict__() for i in x}
            assert f['216.243.31.2'][
                'lasttime'] == '2016-03-23T20:22:27.000000Z'

    archiver.clear_memcache()
    with Smrt(REMOTE_ADDR, 1234, client='stdout', archiver=archiver) as s:
        assert type(s) is Smrt

        for r, f in s.load_feeds(rule, feed=feed):
            x = list(s.process(r, f))
            assert len(x) == 0
Exemple #4
0
def _run_smrt(options, **kwargs):
    args = kwargs.get('args')
    goback = kwargs.get('goback')
    verify_ssl = kwargs.get('verify_ssl')
    data = kwargs.get('data')
    service_mode = kwargs.get("service_mode")

    archiver = None
    if args.remember:
        archiver = Archiver(dbfile=args.remember_path)
    else:
        archiver = NOOPArchiver()

    logger.info('starting run...')

    with Smrt(options.get('token'), options.get('remote'), client=args.client, username=args.user,
              feed=args.feed, archiver=archiver, fireball=args.fireball, no_fetch=args.no_fetch,
              verify_ssl=verify_ssl, goback=goback, skip_invalid=args.skip_invalid, send_retries=args.send_retries,
              send_retries_wait=args.send_retries_wait) as s:

        if s.client:
            s.client.ping(write=True)

        filters = {}
        if args.filter_indicator:
            filters['indicator'] = args.filter_indicator

        indicators = []
        for r, f in s.load_feeds(args.rule, feed=args.feed):
            logger.info('processing: {} - {}:{}'.format(args.rule, r.defaults['provider'], f))
            try:
                for i in s.process(r, f, limit=args.limit, data=data, filters=filters):
                    if args.client == 'stdout':
                        indicators.append(i)
            except Exception as e:
                if not service_mode and not args.skip_broken:
                    logger.error('may need to remove the old cache file: %s' % s.last_cache)
                    import traceback
                    logger.error(traceback.print_exc())
                    raise e

                logger.error(e)
                logger.info('skipping: {}'.format(args.feed))

        if args.client == 'stdout':
            print(FORMATS[options.get('format')](data=indicators, cols=args.fields.split(',')))

    logger.info('cleaning up')
    archiver.cleanup()
    archiver.clear_memcache()

    logger.info('finished run')
Exemple #5
0
def test_smrt_archiver_neither():
    tmpfile = tempfile.mktemp()
    archiver = Archiver(dbfile=tmpfile)
    rule = 'test/smrt/rules/archiver.yml'
    feed = 'neither'

    with Smrt(REMOTE_ADDR, 1234, client='stdout', archiver=archiver) as s:
        assert type(s) is Smrt

        for r, f in s.load_feeds(rule, feed=feed):
            x = list(s.process(r, f))
            assert len(x) > 0

            f = {i.indicator: i.__dict__() for i in x}

            assert f['216.243.31.2'].get('lasttime') is None

    with Smrt(REMOTE_ADDR, 1234, client='stdout', archiver=archiver) as s:
        assert type(s) is Smrt

        for r, f in s.load_feeds(rule, feed=feed):
            x = list(s.process(r, f))
            assert len(x) == 0