Skip to content

Commit

Permalink
globalized logger and removed some extraneous code. Im sorry, pythoni…
Browse files Browse the repository at this point in the history
…c gods. I have sinned today.
  • Loading branch information
damouse committed Jun 23, 2015
1 parent 54100db commit f9d4f66
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 197 deletions.
21 changes: 0 additions & 21 deletions docs/build system.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,3 @@ Push a snap to a running instance of snappy::
snappy-remote --url=ssh://localhost:8022 install SNAPNAME


Creating chutes
--------------------

In progress.


## Contributing
All contributions must follow [PEP8](https://www.python.org/dev/peps/pep-0008/) and have relevant tests. Please document as needed.

Compiling documentation
```
pip install sphinx sphinx-autobuild sphinx-rtd-build
cd docs
make html
```

sphinx-apidoc -f -o docs paradrop/paradrop/




27 changes: 26 additions & 1 deletion docs/doctests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,29 @@ To create the documentation locally, run::
make html
python -m SimpleHTTPServer 9999

Open your web browser of choice and point it to http://localhost:9999/_build/html/index.html.
Open your web browser of choice and point it to http://localhost:9999/_build/html/index.html.


Testing
-------

As mentioned above, all testing is automatically run by travis-ci, a continuous integration service.

To manually run tests, install nosetest::

pip install nose

Run all tests::

nosetests

Well thats easy. How does nose detect tests? All tests live in the ``tests/`` directory. Nose adheres to a simple principle: anything marked with ``test`` in its name is most likely a test. When writing tests, make sure all functions begin with ``test``.

Coverage analysis detects how much of the code is used by a test suite. If the result of the coverage is less than 100%, someone slacked. Install coveralls::

pip install coveralls

Run tests with coverage analysis::

nosetests --with-coverage --cover-package=paradrop

3 changes: 0 additions & 3 deletions src/paradrop/backend/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
'''
Testing package documetation
'''
7 changes: 0 additions & 7 deletions src/paradrop/backend/connection.py

This file was deleted.

82 changes: 0 additions & 82 deletions src/paradrop/backend/main.py

This file was deleted.

15 changes: 5 additions & 10 deletions src/paradrop/backend/pdfcd/apichute.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from paradrop.lib.utils.output import out, logPrefix
from paradrop.lib.utils.output import logPrefix
from paradrop.lib.utils.pdutils import json2str, str2json, timeint, urlDecodeMe

from paradrop.lib.api.pdrest import APIDecorator
Expand All @@ -11,12 +11,11 @@
#########################################################################################################################

class ChuteAPI:

def __init__(self, rest):
self.rest = rest
self.rest.register('POST', '^/v1/chute/create$', self.POST_createChute)



@APIDecorator(requiredArgs=["sessionToken"])
def POST_createChute(self, apiPackage):
"""
Expand All @@ -35,17 +34,13 @@ def POST_createChute(self, apiPackage):
On failure: A string explain the reason of failure
"""
#token, apid = pdutils.explode(apiPackage.inputArgs, "sessionToken", "apid")

out.info('-- {} Creating chute...\n'.format(logPrefix()))

# TODO implement
result = dict(success=True, message='Successfully launched chute')

if(result is None):
apiPackage.setFailure(errType=pdapi.ERR_BADAUTH, countFailure=False)
else:
apiPackage.setSuccess(json2str(result))




17 changes: 10 additions & 7 deletions src/paradrop/backend/pdfcd/apiutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Contains helper functions specific to the backend API code.
'''


def getIP(req):
"""
Returns the str IP addr from the request.
Expand All @@ -22,6 +23,7 @@ def getIP(req):
else:
return ip


def addressInNetwork(ipaddr, netTuple):
"""
This function allows you to check if on IP belongs to a Network.
Expand All @@ -35,12 +37,14 @@ def addressInNetwork(ipaddr, netTuple):
network, netmask = netTuple
return (ipaddr & netmask) == (network & netmask)


def calcDottedNetmask(mask):
"""Returns quad dot format of IP address."""
bits = 0
for i in xrange(32-mask,32):
for i in xrange(32 - mask, 32):
bits |= (1 << i)
return "%d.%d.%d.%d" % ((bits & 0xff000000) >> 24, (bits & 0xff0000) >> 16, (bits & 0xff00) >> 8 , (bits & 0xff))
return "%d.%d.%d.%d" % ((bits & 0xff000000) >> 24, (bits & 0xff0000) >> 16, (bits & 0xff00) >> 8, (bits & 0xff))


def unpackIPAddrWithSlash(net):
"""
Expand All @@ -50,16 +54,15 @@ def unpackIPAddrWithSlash(net):
# Take "0.0.0.0/16" and split into 2 components
netaddr, bits = net.split('/')
# Get the quad dot addr of netmask
netmask = struct.unpack('=L',socket.inet_aton(calcDottedNetmask(int(bits))))[0]
netmask = struct.unpack('=L', socket.inet_aton(calcDottedNetmask(int(bits))))[0]
# Get the unpacked addr of the IP
network = struct.unpack('=L',socket.inet_aton(netaddr))[0]
network = struct.unpack('=L', socket.inet_aton(netaddr))[0]
return (network, netmask)


def unpackIPAddr(ip):
"""
Unpacks the 'IP' str.
Returns a binary form of the ipaddr such that (ipaddr & netmask) will work.
"""
return struct.unpack('=L',socket.inet_aton(ip))[0]


return struct.unpack('=L', socket.inet_aton(ip))[0]
38 changes: 17 additions & 21 deletions src/paradrop/backend/pdfcd/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from twisted.web.server import Site
from twisted.internet import reactor

from paradrop.lib.utils.output import out, logPrefix
from paradrop.lib.utils.output import logPrefix
from paradrop.lib.utils.pdutils import timeflt, str2json, json2str
from paradrop.lib.api import pdapi
from paradrop.lib.api import pdrest
Expand All @@ -25,14 +25,14 @@
# API Callbacks
###########################################################################################################################
class ParadropAPIServer(pdrest.APIResource):

def __init__(self, lclreactor):
pdrest.APIResource.__init__(self)
self.reactor = lclreactor

# Allow each module to register their API calls
apichute.ChuteAPI(self)


def preprocess(self, request, checkThresh, tictoc):
"""
Check if failure attempts for the user name has met the threshold.
Expand All @@ -48,10 +48,10 @@ def preprocess(self, request, checkThresh, tictoc):
str: if threshold is met
None: if ok
"""

if(checkThresh):
ip, token, key, failureDict = checkThresh


# Check if IP is in whitelist
ipaddr = apiutils.unpackIPAddr(ip)
for ipnet in self.WHITELIST_IP:
Expand All @@ -68,13 +68,13 @@ def preprocess(self, request, checkThresh, tictoc):
usage = (tictoc, None)
self.failprocess(ip, request, (ip, self.clientFailures), None, usage, pdapi.getResponse(pdapi.ERR_THRESHOLD))
duration = self.perf.toc(tictoc)
# Log the info of this call
# Log the info of this call
# TODO self.usageTracker.addTrackInfo(ip, 'Null', request.path, self.usageTracker.FAIL_THRESH, duration, request.content.getvalue())

return "Threshold Met!"
# Otherwise everything is ok
return None

def postprocess(self, request, key, failureDict, logUsage):
"""
If the client is successful in their request, we should:
Expand Down Expand Up @@ -122,25 +122,25 @@ def failprocess(self, ip, request, logFailure, errorStmt, logUsage, errType):
request.setResponseCode(*pdapi.getResponse(errType, ""))
else:
request.setResponseCode(*pdapi.getResponse(errType))

headers = request.received_headers
if(logUsage is not None):
tictoc, devid = logUsage

if(devid is None):
devid = "Null"
duration = self.perf.toc(tictoc)
# Log the info of this call
# Log the info of this call
# TODO self.usageTracker.addTrackInfo(ip, devid , request.path, self.usageTracker.FAIL_AUTH, duration, request.content.getvalue())

if(logFailure is not None):
key, failureDict = logFailure
# update the accessInfo
if(key in failureDict):
failureDict[key].update(ip, headers, time)
else:
failureDict[key] = AccessInfo(ip, headers, time)

attempts = failureDict[key].attempts
out.warn('** %s Failure access recorded: %s Attempts: %d\n' % (logPrefix(), key, attempts))

Expand All @@ -150,43 +150,40 @@ def failprocess(self, ip, request, logFailure, errorStmt, logUsage, errType):
if(errorStmt):
return errorStmt % ("Null")



@pdrest.GET('^/v1/test')
def GET_test(self, request):
request.setHeader('Access-Control-Allow-Origin', settings.PDFCD_HEADER_VALUE)
ip = apiutils.getIP(request)
out.info('-- %s Test called (%s)\n' % (logPrefix(), ip))
request.setResponseCode(*pdapi.getResponse(pdapi.OK))
return "SUCCESS\n"

@pdrest.ALL('^/')
def default(self, request):
ip = apiutils.getIP(request)
uri = request.uri
method = request.method
# Get data about who done it
out.err("!! %s Default caught API call (%s => %s:%s)\n" % (logPrefix(), ip, method, uri))

# Someone might be trying something bad, track their IP
res = self.preprocess(request, (ip, None, ip, self.defaultFailures), tictoc)
if(res):
return res

self.failprocess(ip, request, (ip, self.defaultFailures), None, (tictoc, None), pdapi.ERR_BADMETHOD)
return ""



###############################################################################
# Main function
###############################################################################
def setup(args=None):

# Setup API server
api = ParadropAPIServer(reactor)
site = Site(api, timeout=None)

# Development mode
if(args and args.development):
thePort = settings.PDFCD_PORT + 10000
Expand All @@ -195,16 +192,15 @@ def setup(args=None):
site.displayTracebacks = True
elif(args and args.unittest):
thePort = settings.DBAPI_PORT + 20000
out.info('-- %s Running under unittest mode\n' % (logPrefix()))
out.info('-- %s Running under unittest mode\n' % (logPrefix()))
site.displayTracebacks = True
else:
thePort = settings.PDFCD_PORT
site.displayTracebacks = False

# Setup the port we listen on
out.info('-- %s Establishing API server, port: %d\n' % (logPrefix(), thePort))
reactor.listenTCP(thePort, site)

# Never return from here
reactor.run()

0 comments on commit f9d4f66

Please sign in to comment.