This repository has been archived by the owner on Apr 15, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from dr-strangecode/fix_issue_14
Fix issue 14
- Loading branch information
Showing
6 changed files
with
145 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters