Skip to content
This repository has been archived by the owner on Jun 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #45 from csirtgadgets/fix/43
Browse files Browse the repository at this point in the history
Fix/43
  • Loading branch information
wesyoung committed May 14, 2016
2 parents 17c2591 + 48683d3 commit e837fec
Show file tree
Hide file tree
Showing 31 changed files with 1,126 additions and 181 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.yml
coverage_html_report/
deployment/aws/site.yml
bearded-avenger3.tar.gz
Expand Down
16 changes: 8 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ python:

script:
- sudo pip install pytest-cov
- py.test --cov=cif --cov-fail-under=45
- python setup.py sdist
- cp -f dist/bearded-avenger-*.tar.gz deployment/ubuntu14/roles/bearded-avenger/files/bearded-avenger3.tar.gz
- sudo pip install 'ansible==1.9.6'
- ansible-playbook -i "localhost," -c local deployment/ubuntu14/travis.yml
- cif -d -p
- cif -d --itype ipv4 --limit 5
- cif -d --search example.com
- py.test --cov=cif --cov-fail-under=35
#- python setup.py sdist
#- cp -f dist/bearded-avenger-*.tar.gz deployment/ubuntu14/roles/bearded-avenger/files/bearded-avenger3.tar.gz
#- sudo pip install 'ansible==1.9.6'
#- ansible-playbook -i "localhost," -c local deployment/ubuntu14/travis.yml
#- cif -d -p
#- cif -d --itype ipv4 --limit 5
#- cif -d --search example.com

notifications:
email:
Expand Down
8 changes: 4 additions & 4 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
echo GH_TOKEN=#{ENV['GH_TOKEN']} >> /home/vagrant/.profile
END

config.vm.provision "ansible" do |ansible|
ansible.playbook = "deployment/ubuntu14/vagrant.yml"
ansible.extra_vars = { development: 'true' }
#config.vm.provision "ansible" do |ansible|
#ansible.playbook = "deployment/ubuntu14/vagrant.yml"
#ansible.extra_vars = { development: 'true' }
#ansible.verbose = 'vvv'
end
#end

if File.file?(VAGRANTFILE_LOCAL)
external = File.read VAGRANTFILE_LOCAL
Expand Down
32 changes: 26 additions & 6 deletions cif/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from argparse import ArgumentParser
from argparse import RawDescriptionHelpFormatter

from cif.constants import REMOTE_ADDR, SEARCH_LIMIT
from cif.constants import REMOTE_ADDR, SEARCH_LIMIT, CONFIG_PATH
from cif.format.table import Table
from cif.indicator import Indicator
from cif.utils import setup_logging, get_argument_parser
from cif.utils import setup_logging, get_argument_parser, read_config
from cif.exceptions import AuthError

TOKEN = os.environ.get('CIF_TOKEN', None)
REMOTE_ADDR = os.environ.get('CIF_REMOTE', REMOTE_ADDR)
Expand Down Expand Up @@ -47,33 +48,48 @@ def main():
parents=[p]
)

p.add_argument('--token', help='specify api token', default=str(1234))
p.add_argument('--token', help='specify api token', default=TOKEN)
p.add_argument('--remote', help='specify API remote [default %(default)s]', default=REMOTE_ADDR)
p.add_argument('-p', '--ping', action="store_true") # meg?
p.add_argument('-q', '--search', help="search")
p.add_argument('--itype', help='filter by indicator type') ## need to fix sqlite for non-ascii stuff first
p.add_argument("--submit", action="store_true", help="submit an indicator")
p.add_argument('--limit', help='limit results [default %(default)s]', default=SEARCH_LIMIT)
p.add_argument('--nolog', help='do not log search', default=False)
p.add_argument('--nolog', help='do not log search', action='store_true')

p.add_argument('--indicator')
p.add_argument('--tags', nargs='+')

p.add_argument("--zmq", dest="zmq", help="use zmq as a transport instead of http", action="store_true")
p.add_argument("--zmq", help="use zmq as a transport instead of http", action="store_true")

p.add_argument('--config', help='specify config file [default %(default)s]', default=CONFIG_PATH)

args = p.parse_args()

setup_logging(args)
logger = logging.getLogger(__name__)

o = read_config(args)
options = vars(args)
for v in options:
if options[v] is None:
options[v] = o.get(v)

if not options.get('token'):
raise RuntimeError('missing --token')

verify_ssl = True
if o.get('no_verify_ssl') or options.get('no_verify_ssl'):
verify_ssl = False

options = vars(args)

if options.get("zmq"):
from cif.client.zeromq import ZMQ as ZMQClient
cli = ZMQClient(**options)
else:
from cif.client.http import HTTP as HTTPClient
cli = HTTPClient(args.remote, args.token)
cli = HTTPClient(args.remote, args.token, verify_ssl=verify_ssl)

if options.get('ping'):
logger.info('running ping')
Expand All @@ -92,6 +108,8 @@ def main():
'itype': options['itype'],
'limit': options['limit'],
})
except AuthError as e:
logger.error('unauthorized')
except RuntimeError as e:
import traceback
traceback.print_exc()
Expand All @@ -111,6 +129,8 @@ def main():
import traceback
traceback.print_exc()
logger.error(e)
except AuthError as e:
logger.error('unauthorized')
else:
print(Table(data=rv))
elif options.get("submit"):
Expand Down
112 changes: 98 additions & 14 deletions cif/client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import requests
import time
import json

from cif.exceptions import AuthError
from pprint import pprint

from cif.client import Client
Expand All @@ -24,23 +24,33 @@ def __init__(self, remote, token, proxy=None, timeout=300, verify_ssl=True, **kw
self.session.headers['Content-Type'] = 'application/json'

def _get(self, uri, params={}):
uri = self.remote + uri
if not uri.startswith('http'):
uri = self.remote + uri
body = self.session.get(uri, params=params, verify=self.verify_ssl)

if body.status_code > 303:
err = 'request failed: %s' % str(body.status_code)
self.logger.debug(err)
try:
err = json.loads(body.content).get('message')
except ValueError as e:
err = body.content

self.logger.error(err)
raise RuntimeError(err)
if body.status_code == 401:
raise AuthError('invalid token')
elif body.status_code == 404:
err = 'not found'
raise RuntimeError(err)
else:
try:
err = json.loads(body.content).get('message')
except ValueError as e:
err = body.content
self.logger.error(err)
raise RuntimeError(err)

return json.loads(body.content)

def _post(self, uri, data):
if type(data) == dict:
data = json.dumps(data)

body = self.session.post(uri, data=data)

if body.status_code > 303:
Expand All @@ -49,8 +59,59 @@ def _post(self, uri, data):
err = body.content

if body.status_code == 401:
err = 'unauthorized'
raise AuthError('unauthorized')
elif body.status_code == 404:
err = 'not found'
raise RuntimeError(err)
else:
try:
err = json.loads(err).get('message')
except ValueError as e:
err = body.content

self.logger.error(err)
raise RuntimeError(err)

self.logger.debug(body.content)
body = json.loads(body.content)
return body

def _delete(self, uri, data):
body = self.session.delete(uri, data=json.dumps(data))

if body.status_code > 303:
err = 'request failed: %s' % str(body.status_code)
self.logger.debug(err)
err = body.content

if body.status_code == 401:
raise AuthError('unauthorized')
elif body.status_code == 404:
err = 'not found'
raise RuntimeError(err)
else:
try:
err = json.loads(err).get('message')
except ValueError as e:
err = body.content

self.logger.error(err)
raise RuntimeError(err)

self.logger.debug(body.content)
body = json.loads(body.content)
return body

def _patch(self, uri, data):
body = self.session.patch(uri, data=json.dumps(data))

if body.status_code > 303:
err = 'request failed: %s' % str(body.status_code)
self.logger.debug(err)
err = body.content

if body.status_code == 401:
raise AuthError('unauthorized')
elif body.status_code == 404:
err = 'not found'
raise RuntimeError(err)
Expand Down Expand Up @@ -79,13 +140,36 @@ def submit(self, data):
rv = self._post(uri, data)
return rv["data"]

def ping(self):
def ping(self, write=False):
t0 = time.time()

self._get('/ping')
uri = '/ping'
if write:
uri = '/ping?write=1'

rv = self._get(uri)

if rv:
rv = (time.time() - t0)
self.logger.debug('return time: %.15f' % rv)

t1 = (time.time() - t0)
self.logger.debug('return time: %.15f' % t1)
return t1
return rv

def tokens_search(self, filters):
rv = self._get('{}/tokens'.format(self.remote), params=filters)
return rv['data']

def tokens_delete(self, data):
rv = self._delete('{}/tokens'.format(self.remote), data)
return rv['data']

def tokens_create(self, data):
self.logger.debug(data)
rv = self._post('{}/tokens'.format(self.remote), data)
return rv['data']

def token_edit(self, data):
rv = self._patch('{}/token'.format(self.remote), data)
return rv['data']

Plugin = HTTP
Loading

0 comments on commit e837fec

Please sign in to comment.