def observe_observables():
    input_observables = get_observables()

    g.bundle = Bundle()

    key = get_key()

    if key is None:
        raise AuthenticationRequiredError

    url = current_app.config['AVOTX_URL']
    headers = {'User-Agent': current_app.config['CTR_USER_AGENT']}

    client = Client(key, url, headers=headers)
    limit = current_app.config['CTR_ENTITIES_LIMIT']

    prepared_observables = []
    for input_observable in input_observables:
        prepared_observable = Observable.instance_for(**input_observable)
        if prepared_observable is not None:
            prepared_observables.append(prepared_observable)

    def make_bundle(observable):
        return observable.observe(client, limit=limit)

    with ThreadPoolExecutor(
            max_workers=get_workers(prepared_observables)) as executor:
        bundles = executor.map(make_bundle, prepared_observables)

    for bundle in bundles:
        g.bundle |= bundle

    data = g.bundle.json()

    return jsonify_data(data)
def observe_observables():
    observables = get_observables()

    g.bundle = Bundle()

    key = get_key()

    if key is None:
        raise AuthenticationRequiredError

    url = current_app.config['AVOTX_URL']
    headers = {'User-Agent': current_app.config['CTR_USER_AGENT']}

    client = Client(key, url, headers=headers)
    limit = current_app.config['CTR_ENTITIES_LIMIT']

    for observable in observables:
        observable = Observable.instance_for(**observable)
        if observable is None:
            continue

        bundle = observable.observe(client, limit=limit)
        g.bundle |= bundle

    data = g.bundle.json()

    return jsonify_data(data)
def test_observable_of():
    assert isinstance(Observable.of('md5'), MD5)
    assert isinstance(Observable.of('sha256'), SHA256)
    assert isinstance(Observable.of('file_name'), FileName)
    assert isinstance(Observable.of('file_path'), FilePath)
    assert isinstance(Observable.of('ip'), IP)
    assert isinstance(Observable.of('domain'), Domain)
    assert isinstance(Observable.of('mutex'), Mutex)
    assert Observable.of('whatever') is None
def test_limits():
    with open('tests/unit/data/file_name.json', 'r') as file:
        data = json.loads(file.read())
    creds = {'user': '******', 'pass': '******'}
    observable = Observable.of('file_name')
    output = observable.observe('dummy', limit=3, creds=creds)

    assert_mapped_correctly(output, data['output'])
def test_map():
    with open('tests/unit/data/sha256.json', 'r') as file:
        data = json.loads(file.read())
    creds = {'user': '******', 'pass': '******'}
    observable = Observable.of(data['observable']['type'])
    output = observable.observe(data['observable']['value'],
                                limit=100,
                                creds=creds)

    assert_mapped_correctly(output, data['output'])
def refer_observables():
    observables = get_observables()

    data = []

    url = current_app.config['AVOTX_URL']

    for observable in observables:
        observable = Observable.instance_for(**observable)
        if observable is None:
            continue

        reference = observable.refer(url)
        data.append(reference)

    return jsonify_data(data)