Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #21 from dr-strangecode/fix_issue_14
Browse files Browse the repository at this point in the history
Fix issue 14
  • Loading branch information
atarola committed May 14, 2014
2 parents ba6bba0 + fb1aa7c commit c6a3304
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 118 deletions.
62 changes: 62 additions & 0 deletions pyjojo/options.py
@@ -0,0 +1,62 @@
#!/usr/bin/env python

from optparse import OptionParser, IndentedHelpFormatter

from pyjojo.config import config

def command_line_options():
""" command line configuration """

parser = OptionParser(usage="usage: %prog [options] <htpasswd>")

parser.formatter = PlainHelpFormatter()
parser.description = """Expose a directory of bash scripts as an API.
Note: This application gives you plenty of bullets to shoot yourself in the
foot! Please use the SSL config options, give a password file, and either
whitelist access to it via a firewall or keep it in a private network.
You can use the apache htpasswd utility to create your htpasswd files. If
you do, I recommend passing the -d flag, forcing the encryption type pyjojo
recognises."""

parser.add_option('-d', '--debug', action="store_true", dest="debug", default=False,
help="Start the application in debugging mode.")

parser.add_option('--dir', action="store", dest="directory", default="/srv/pyjojo",
help="Base directory to parse the scripts out of")

parser.add_option('-p', '--port', action="store", dest="port", default=3000,
help="Set the port to listen to on startup.")

parser.add_option('-a', '--address', action ="store", dest="address", default=None,
help="Set the address to listen to on startup. Can be a hostname or an IPv4/v6 address.")

parser.add_option('-c', '--certfile', action="store", dest="certfile", default=None,
help="SSL Certificate File")

parser.add_option('-k', '--keyfile', action="store", dest="keyfile", default=None,
help="SSL Private Key File")

parser.add_option('-u', '--unix-socket', action="store", dest="unix_socket", default=None,
help="Bind pyjojo to a unix domain socket")

options, args = parser.parse_args()

# TODO: only do this if they specify the ssl certfile and keyfile
if len(args) >= 1:
config['passfile'] = args[0]
else:
config['passfile'] = None

config['directory'] = options.directory

return options


class PlainHelpFormatter(IndentedHelpFormatter):
def format_description(self, description):
if description:
return description + "\n"
else:
return ""
1 change: 1 addition & 0 deletions pyjojo/scripts.py
Expand Up @@ -203,6 +203,7 @@ def create_script(script_name, filename):
continue

filtered_params.append(value)
continue

# lock
if in_block and key == "lock":
Expand Down
34 changes: 34 additions & 0 deletions pyjojo/server.py
@@ -0,0 +1,34 @@
#!/usr/bin/env python

import logging

from tornado.ioloop import IOLoop

from pyjojo.options import command_line_options
from pyjojo.util import setup_logging, create_application
from pyjojo.servers import http_server, https_server, unix_socket_server

log = logging.getLogger(__name__)

def main():
""" entry point for the application """

# get the command line options
options = command_line_options()
setup_logging()

# setup the application
log.info("Setting up the application")
application = create_application(options.debug)

# server startup
if options.unix_socket:
unix_socket_server(application, options)
elif options.certfile and options.keyfile:
https_server(application, options)
else:
http_server(application, options)

# start the ioloop
log.info("Starting the IOLoop")
IOLoop.instance().start()
43 changes: 43 additions & 0 deletions pyjojo/servers.py
@@ -0,0 +1,43 @@
#!/usr/bin/env python

import logging
import sys

from tornado.httpserver import HTTPServer
from tornado.netutil import bind_unix_socket

log = logging.getLogger(__name__)

def https_server(application, options):
""" https server """

log.info("Binding application to unix socket {0}".format(options.unix_socket))
if sys.version_info < (2,7,0):
server = HTTPServer(application, ssl_options={
"certfile": options.certfile,
"keyfile": options.keyfile
})
else:
server = HTTPServer(application, ssl_options={
"certfile": options.certfile,
"keyfile": options.keyfile,
"ciphers": "HIGH,MEDIUM"
})
server.bind(options.port, options.address)
server.start()

def http_server(application, options):
""" http server """

log.warn("Application is running in HTTP mode, this is insecure. Pass in the --certfile and --keyfile to use SSL.")
server = HTTPServer(application)
server.bind(options.port, options.address)
server.start()

def unix_socket_server(application, options):
""" unix socket server """

log.info("Binding application to unix socket {0}".format(options.unix_socket))
server = HTTPServer(application)
socket = bind_unix_socket(options.unix_socket)
server.add_socket(socket)
119 changes: 3 additions & 116 deletions pyjojo/util.py
Expand Up @@ -4,14 +4,8 @@
import pkgutil
import logging
import sys
from optparse import OptionParser, IndentedHelpFormatter
from pkg_resources import resource_filename

import passlib
import tornado.web
from tornado.ioloop import IOLoop
from tornado.httpserver import HTTPServer
from tornado.netutil import bind_unix_socket

from pyjojo.config import config
from pyjojo.scripts import create_collection
Expand Down Expand Up @@ -45,64 +39,6 @@ def get_routes(self):
return self._routes


class PlainHelpFormatter(IndentedHelpFormatter):
def format_description(self, description):
if description:
return description + "\n"
else:
return ""


def command_line_options():
""" command line configuration """

parser = OptionParser(usage="usage: %prog [options] <htpasswd>")

parser.formatter = PlainHelpFormatter()
parser.description = """Expose a directory of bash scripts as an API.
Note: This application gives you plenty of bullets to shoot yourself in the
foot! Please use the SSL config options, give a password file, and either
whitelist access to it via a firewall or keep it in a private network.
You can use the apache htpasswd utility to create your htpasswd files. If
you do, I recommend passing the -d flag, forcing the encryption type pyjojo
recognises."""

parser.add_option('-d', '--debug', action="store_true", dest="debug", default=False,
help="Start the application in debugging mode.")

parser.add_option('--dir', action="store", dest="directory", default="/srv/pyjojo",
help="Base directory to parse the scripts out of")

parser.add_option('-p', '--port', action="store", dest="port", default=3000,
help="Set the port to listen to on startup.")

parser.add_option('-a', '--address', action ="store", dest="address", default=None,
help="Set the address to listen to on startup. Can be a hostname or an IPv4/v6 address.")

parser.add_option('-c', '--certfile', action="store", dest="certfile", default=None,
help="SSL Certificate File")

parser.add_option('-k', '--keyfile', action="store", dest="keyfile", default=None,
help="SSL Private Key File")

parser.add_option('-u', '--unix-socket', action="store", dest="unix_socket", default=None,
help="Bind pyjojo to a unix domain socket")

options, args = parser.parse_args()

# TODO: only do this if they specify the ssl certfile and keyfile
if len(args) >= 1:
config['passfile'] = args[0]
else:
config['passfile'] = None

config['directory'] = options.directory

return options


def setup_logging():
""" setup the logging system """

Expand All @@ -114,62 +50,13 @@ def setup_logging():
return handler

def create_application(debug):
# import the handler file, this will fill out the route.get_routes() call.
import pyjojo.handlers

application = tornado.web.Application(
route.get_routes(),
scripts=create_collection(config['directory']),
debug=debug
)

return application


def main():
""" entry point for the application """

root = os.path.dirname(__file__)

# get the command line options
options = command_line_options()
handler = setup_logging()

# import the handler file, this will fill out the route.get_routes() call.
import pyjojo.handlers

# setup the application
log.info("Setting up the application")
application = create_application(options.debug)

# unix domain socket
if options.unix_socket:
log.info("Binding application to unix socket {0}".format(options.unix_socket))
server = HTTPServer(application)
socket = bind_unix_socket(options.unix_socket)
server.add_socket(socket)

# https server
elif options.certfile and options.keyfile:
log.info("Binding application to unix socket {0}".format(options.unix_socket))
if sys.version_info < (2,7,0):
server = HTTPServer(application, ssl_options={
"certfile": options.certfile,
"keyfile": options.keyfile
})
else:
server = HTTPServer(application, ssl_options={
"certfile": options.certfile,
"keyfile": options.keyfile,
"ciphers": "HIGH,MEDIUM"
})
server.bind(options.port, options.address)
server.start()

# http server
else:
log.warn("Application is running in HTTP mode, this is insecure. Pass in the --certfile and --keyfile to use SSL.")
server = HTTPServer(application)
server.bind(options.port, options.address)
server.start()

# start the ioloop
log.info("Starting the IOLoop")
IOLoop.instance().start()
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -23,7 +23,7 @@
setup(
# metadata
name="pyjojo",
version="0.7",
version="0.8",
author="Anthony Tarola",
author_email="anthony.tarola@gmail.com",
description="Expose a set of shell scripts as an API.",
Expand All @@ -35,7 +35,7 @@

entry_points={
'console_scripts': [
'pyjojo = pyjojo.util:main'
'pyjojo = pyjojo.server:main'
]
},

Expand Down

0 comments on commit c6a3304

Please sign in to comment.