Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

big commit, adds in a createsuperuser, persistence backend, etc

  • Loading branch information...
commit 1e9493c1623081c9342d4b97e508ef8c7d898ae0 1 parent 92c9c0c
@chrisdickinson chrisdickinson authored
View
9 nappingcat/app.py
@@ -22,10 +22,7 @@ def setup_environ(self):
Your nappingcat.conf file does not include a %s section!
""".strip() % config.SECTION_NAME)
- kitty_config = dict(settings.items(config.SECTION_NAME))
- if kitty_config.get('paths', None) is not None:
- sys.path[0:0] = [i for i in kitty_config['paths'].split('\n') if i]
-
+ kitty_config = config.setup_environ(settings)
self.global_settings = settings
self.nappingcat_settings = kitty_config
@@ -38,8 +35,8 @@ def run(cls, *args, **kwargs):
result = instance.main()
except NappingCatException, e:
result = (str(e))
- self.stderr.write(str(result))
- self.stderr.flush()
+ instance.stderr.write(str(result))
+ instance.stderr.flush()
def main(self, *args, **kwargs):
raise NappingCatUnhandled("""
View
44 nappingcat/auth.py
@@ -1,30 +1,58 @@
from nappingcat.util import import_module
from nappingcat import config
-import StringIO
+
class AuthBackend(object):
def __init__(self, settings):
+ self.users = {}
self.settings = settings
+ self.require_update = False
def has_permission(self, user, permission):
- pass
+ full_query = (user,) + permission
+ result = self.users
+ for i in full_query:
+ result = result.get(i, {})
+ return bool(result)
def add_permission(self, user, permission):
- pass
+ self.require_update = True
+ full_query = (user,) + permission
+ result = self.users
+ for i in full_query[:-1]:
+ level = result.get(i, None)
+ if level is None:
+ result[i] = {}
+ result = result[i]
+ result[full_query[-1]] = True
def remove_permission(self, user, permission):
- pass
+ self.require_update = True
+ full_query = (user,) + permission
+ result = self.users
+ for i in full_query[:-1]:
+ level = result.get(i, None)
+ if level is None:
+ result[i] = {}
+ result = result[i]
+ del result[full_query[-1]]
def add_user(self, username):
- pass
+ self.require_update = True
+ self.users[username] = {'keys':[]}
def add_key_to_user(self, user, key):
- pass
+ self.require_update = True
+ self.users[user]['keys'].append(key)
def get_keys(self, username):
- pass
+ return self.users[username]['keys']
+
+ def finish(self, pubkey_handler):
+ if self.require_update:
+ pubkey_handler.flush_keys(self)
def get_users(self):
- pass
+ return self.users.keys()
def get_auth_backend_from_settings(settings):
settings_dict = dict(settings.items(config.SECTION_NAME))
View
7 nappingcat/config.py
@@ -1,4 +1,5 @@
import os
+import sys
from ConfigParser import ConfigParser
SECTION_NAME = 'kittyconfig'
@@ -6,3 +7,9 @@ def build_settings():
config = ConfigParser()
config.read([os.path.expanduser('~/nappingcat.conf'), '/etc/nappingcat.conf'])
return config
+
+def setup_environ(settings):
+ kitty_config = dict(settings.items(SECTION_NAME))
+ if kitty_config.get('paths', None) is not None:
+ sys.path[0:0] = [i for i in kitty_config['paths'].split('\n') if i]
+ return kitty_config
View
27 nappingcat/contrib/auth/backends/json.py
@@ -0,0 +1,27 @@
+from nappingcat.auth import AuthBackend
+import os
+try:
+ import json as simplejson
+except ImportError:
+ import simplejson
+
+class JSONAuthBackend(AuthBackend):
+ def __init__(self, *args, **kwargs):
+ super(JSONAuthBackend, self).__init__(*args, **kwargs)
+ settings_dict = dict(self.settings.items(config.SECTION_NAME))
+ filename = os.path.expanduser(settings_dict.get('jsonauth', '~/nappingcat_auth.json'))
+ try:
+ with open(filename, 'r') as input:
+ self.users = simplejson.loads(input.read())
+ except IOError:
+ self.users = {}
+ with open(filename, 'w') as fallback:
+ fallback.write(simplejson.dumps({})
+
+ def finish(self, pubkey_handler):
+ super(JSONAuthBackend, self).finish(pubkey_handler)
+ if self.require_update:
+ settings_dict = dict(self.settings.items(config.SECTION_NAME))
+ filename = os.path.expanduser(settings_dict.get('jsonauth', '~/nappingcat_auth.json'))
+ with open(filename, 'w') as output:
+ output.write(simplejson.dumps(self.users))
View
2  nappingcat/contrib/auth/bin/nappingcat_createsuperuser.py
@@ -5,7 +5,7 @@
def main(name=None, key=None):
name = sys.argv[1] if name is None else name
- key = stdin.read() if key is None else key
+ key = sys.stdin.read() if key is None else key
settings = build_settings()
auth = get_auth_backend_from_settings(settings)
auth.add_user(name)
View
3  nappingcat/contrib/discoverable/handlers.py
@@ -17,6 +17,7 @@ def discover(request):
def recurse(pattern, base_regex):
for regex, target in pattern.map:
+ target = getattr(pattern.module, target) if isinstance(target, str) else target
if isinstance(target, DiscoverableEndpoint):
target_info = target.to_dict()
target_info.update({
@@ -26,7 +27,7 @@ def recurse(pattern, base_regex):
elif isinstance(target, CommandPatterns):
recurse(target, base_regex + regex)
- recurse(root_patterns)
+ recurse(root_patterns, '')
return Success({
'message':'Successfully ran discover.',
'endpoints':output,
View
10 nappingcat/patterns.py
@@ -8,16 +8,6 @@ def __init__(self, path, map):
if self.path:
self.module = import_module(self.path)
- def find_func(self, func):
- for regex, target in self.map:
- if isinstance(target, CommandPatterns):
- result = target.find_func(func)
- if result:
- return (regex + result[0], result[1])
- elif target is func:
- return (regex, target)
- return (None, func)
-
def match(self, command):
for regex, target in self.map:
match = re.search(regex, command)
View
17 nappingcat/pubkey_handlers.py
@@ -0,0 +1,17 @@
+import os
+import StringIO
+
+class AuthorizedKeysFile(object):
+ def __init__(self, key_file='~/.ssh/authorized_keys'):
+ self.key_file = os.path.expanduser(key_file)
+
+ def flush_keys(self, auth):
+ io = StringIO()
+ SSH_TEMPLATE = 'command="nappingcat-serve %s",no-port-forwarding,no-X11-forwarding,no-pty,no-agent-forwarding %s\n'
+ for user in auth.get_users():
+ for key in auth.get_keys(user):
+ io.write(SSH_TEMPLATE % (user, key))
+ io.seek(0)
+ with open(self.key_file, 'w') as keys_file:
+ keys_file.write(io.read())
+
View
9 nappingcat/serve.py
@@ -1,7 +1,7 @@
from nappingcat import config
from nappingcat.app import App
from nappingcat.patterns import patterns, include, CommandPatterns
-from nappingcat.util import import_module
+from nappingcat.util import import_module, import_class_from_module
from nappingcat.request import Request
from nappingcat.exceptions import NappingCatBadArguments
import sys
@@ -25,10 +25,15 @@ def setup_environ(self):
router_module_names = self.nappingcat_settings.get('routers')
router_module_names = "" if not router_module_names else router_module_names
self.routers = [(r'^', include(i)) for i in router_module_names.split('\n') if i]
+ pubkey_handler_name = self.nappingcat_settings.get('public_key_handler', 'nappingcat.pubkey_handlers.AuthorizedKeysFile')
+ self.public_key_handler = import_class_from_module(pubkey_handler_name)()
+
def main(self):
cmdpatterns = CommandPatterns('', self.routers)
request = self.create_request(cmdpatterns)
target, match = cmdpatterns.match(request.command)
- return target(request, **match.groupdict())
+ result = target(request, **match.groupdict())
+ request.auth_backend.finish(self.public_key_handler)
+ return result
Please sign in to comment.
Something went wrong with that request. Please try again.