Skip to content

Commit

Permalink
Make services use parse_config consistently.
Browse files Browse the repository at this point in the history
For services that absolutely must have a specific option set we should be
using parse_config() accordingly.
  • Loading branch information
wxsBSD committed Aug 8, 2014
1 parent 8f9df21 commit d7883d8
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 16 deletions.
5 changes: 5 additions & 0 deletions OPSWAT_Service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def get_config_details(config):

return display_config

@staticmethod
def parse_config(config):
if not config['url']:
raise ServiceConfigError("URL required.")

@classmethod
def generate_config_form(self, config):
html = render_to_string('services_config_form.html',
Expand Down
5 changes: 3 additions & 2 deletions OPSWAT_Service/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ class OPSWATConfigForm(forms.Form):
url = forms.CharField(required=True,
label="OPSWAT URL",
widget=forms.TextInput(),
help_text="URL for the OPSWAT REST API.",
initial='http://example.org:8008/metascan_rest/scanner?method=scan&archive_pwd=infected')
initial='',
help_text="URL for the OPSWAT REST API, example: "
"http://example.org:8008/metascan_rest/scanner?method=scan&archive_pwd=infected")

def __init__(self, *args, **kwargs):
super(OPSWATConfigForm, self).__init__(*args, **kwargs)
21 changes: 16 additions & 5 deletions clamd_service/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# (c) 2014, Adam Polkosnik <adam.polkosnik@ny.frb.org>
#
# (c) 2014, Adam Polkosnik <adam.polkosnik@ny.frb.org>
#
import logging
import os
import pyclamd
Expand Down Expand Up @@ -41,6 +41,17 @@ def get_config(existing_config):
config[key] = value
return config

@staticmethod
def parse_config(config):
clamd_sock_path = config.get('clamd_sock_path', '')
clamd_host_name = config.get('clamd_host_name', '')
# Must have one of socket or host.
if not clamd_sock_path and not clamd_host_name:
raise ServiceConfigError("Socket path or hostname required.")

# If socket is provided check it exists.
if not os.path.exists(clamd_sock_path):
raise ServiceConfigError('Socket path not found.')

@staticmethod
def get_config_details(config):
Expand Down Expand Up @@ -85,12 +96,12 @@ def run(self, obj, config):
self._debug('Attempting Unix socket connection to clamd')
cd = pyclamd.ClamdUnixSocket(clamd_sock_path)
cd.ping()
except pyclamd.ConnectionError:
except pyclamd.ConnectionError:
try:
self._debug('Attempting Network connection to clamd')
cd = pyclamd.ClamdNetworkSocket(clamd_host_name, clamd_host_port)
cd.ping()
except pyclamd.ConnectionError:
except pyclamd.ConnectionError:
logger.error("clamd: Can\'t connect to Clamd\'s network socket.")
self._error("clamd: Can\'t connect to Clamd\'s network socket.")
return
Expand All @@ -110,7 +121,7 @@ def run(self, obj, config):
self._error("clamd: Can\'t connect to Clamd\'s socket.")
return

if output:
if output:
out = output['stream']
self._add_result('clamd',out[1], {'Status': out[0]})

Expand Down
9 changes: 9 additions & 0 deletions cuckoo_service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def parse_config(config):
machines = config.get('machine', [])
if isinstance(machines, basestring):
config['machine'] = [machine for machine in machines.split('\r\n')]
errors = []
if not config['host']:
errors.append("Cuckoo host required.")
if not config['port']:
errors.append("Cuckoo port required.")
if not config['machine']:
errors.append("List of machines required.")
if errors:
raise ServiceConfigError(errors)

@staticmethod
def get_config(existing_config):
Expand Down
6 changes: 3 additions & 3 deletions cuckoo_service/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ class CuckooConfigForm(forms.Form):
widget=forms.TextInput(),
help_text="Hostname or IP of the API server.")
port = forms.IntegerField(required=True,
label="Cuckoo API server port.",
label="Cuckoo API server port",
initial=8090)
proxy_host = forms.CharField(required=False,
label="Proxy host",
initial='',
widget=forms.TextInput(),
help_text="Proxy host, if needed.")
proxy_port = forms.IntegerField(required=False,
label="Proxy port.",
label="Proxy port",
initial=0)
webui_host = forms.CharField(required=False,
label="WebUI host",
initial='',
widget=forms.TextInput(),
help_text="Hostname of Cuckoo web interface.")
webui_port = forms.IntegerField(required=False,
label="WebUI port.",
label="WebUI port",
initial=0)
machine = forms.CharField(required=True,
label="Machine",
Expand Down
15 changes: 12 additions & 3 deletions metacap_service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,24 @@ class MetaCapService(Service):
@staticmethod
def parse_config(config):
# Make sure basedir exists.
errors = []
basedir = config.get('basedir', '')
if basedir:
shop_path = "%s/shop" % basedir
if not os.path.exists(basedir):
raise ServiceConfigError("Base directory does not exist.")
errors.append("Base directory does not exist.")
elif not os.path.exists(shop_path):
raise ServiceConfigError("'shop' does not exist in base.")
errors.append("'shop' does not exist in base.")
else:
raise ServiceConfigError("Base directory must be defined.")
errors.append("Base directory must be defined.")
tcpdump = config.get('tcpdump', '')
if not tcpdump:
errors.append('tcpdump binary not found.')
tshark = config.get('tshark', '')
if not tshark:
errors.append('tshark binary not found.')
if errors:
raise ServiceConfigError(errors)

@staticmethod
def get_config(existing_config):
Expand Down
7 changes: 6 additions & 1 deletion opendns_service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from django.template.loader import render_to_string

from crits.services.core import Service
from crits.services.core import Service, ServiceConfigError

from . import forms

Expand Down Expand Up @@ -34,6 +34,11 @@ def get_config(existing_config):
config[key] = value
return config

@staticmethod
def parse_config(config):
if not config['Investigate_API_Token']:
raise ServiceConfigError("API token required.")

@staticmethod
def get_config_details(config):
display_config = {}
Expand Down
5 changes: 5 additions & 0 deletions passivetotal_service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def get_config(existing_config):
config[key] = value
return config

@staticmethod
def parse_config(config):
if not config['pt_api_key']:
raise ServiceConfigError("API key required.")

@staticmethod
def get_config_details(config):
display_config = {}
Expand Down
7 changes: 6 additions & 1 deletion threatrecon_service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from django.template.loader import render_to_string

from crits.services.core import Service
from crits.services.core import Service, ServiceConfigError

from . import forms

Expand All @@ -26,6 +26,11 @@ class ThreatreconService(Service):
def save_runtime_config(config):
del config['tr_api_key']

@staticmethod
def parse_config(config):
if not config['tr_api_key']:
raise ServiceConfigError("API key required.")

@staticmethod
def get_config(existing_config):
# Generate default config from form and initial values.
Expand Down
7 changes: 6 additions & 1 deletion virustotal_service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.conf import settings
from django.template.loader import render_to_string

from crits.services.core import Service
from crits.services.core import Service, ServiceConfigError

from . import forms

Expand Down Expand Up @@ -48,6 +48,11 @@ def get_config(existing_config):
config[key] = value
return config

@staticmethod
def parse_config(config):
if not config['vt_api_key']:
raise ServiceConfigError("API key required.")

@classmethod
def generate_config_form(self, config):
# Convert sigfiles to newline separated strings
Expand Down
3 changes: 3 additions & 0 deletions yara_service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def parse_config(config):
# When validating an existing config it will be a list.
# Convert it to a list of strings.
sigfiles = config.get('sigfiles', [])
if not sigfiles:
raise ServiceConfigError("Must specify signature files.")

if isinstance(sigfiles, basestring):
config['sigfiles'] = [sigfile for sigfile in sigfiles.split('\r\n')]
# This will raise ServiceConfigError
Expand Down

0 comments on commit d7883d8

Please sign in to comment.