Skip to content

Commit

Permalink
graveldb, ssh_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
zielmicha committed Jul 15, 2013
1 parent d9ba861 commit 3269ae8
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Gravelfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
name: gravel-common
requires-apt: python2.7 python-gdbm python-yaml
requires-apt: python2.7 python-tdb python-yaml
12 changes: 12 additions & 0 deletions dbtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import graveldb

class Screw(graveldb.Table('Screw', '/tmp')):
default = lambda self: graveldb.Object(size=1)

s = Screw('foo')
with s:
s.data.size += 1
s.save()

s = Screw('foo')
print s.data.size
38 changes: 38 additions & 0 deletions graveldb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import shelve
import tdb
import os

class TDBShelf(shelve.Shelf):
def __init__(self, path):
self.db = tdb.open(path, flags=os.O_RDWR | os.O_CREAT)
shelve.Shelf.__init__(self, self.db)

def lock_all(self):
self.db.lock_all()

def unlock_all(self):
self.db.unlock_all()

def Table(name, path):
return type(name, (_Table,), {'table': TDBShelf(path + '/' + name)})

class _Table(object):
def __init__(self, name):
self.name = name
self.data = self.table.get(name, self.default())

def save(self):
self.table[self.name] = self.data

def __enter__(self):
self.table.lock_all()

def __exit__(self, *args):
self.table.unlock_all()

default = lambda self: Object()

class Object(object):
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
55 changes: 55 additions & 0 deletions ssh_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import tempfile
import os

def reformat_ssh_key(data):
''' Returns data as a well formed ssh-rsa key '''
split = data.split(None, 2)
if len(split) < 2:
raise ValueError('Not enough values in SSH key.')

if len(split) == 2:
split = split + ['',]

type, value, author = split

if type != 'ssh-rsa':
raise ValueError('Wrong key type.')

data = value.decode('base64').encode('base64').replace('\n', '')
author = urllib.quote(author.strip())

return '%s %s %s' % (type, data, author)

def get_ssh_key_fingerprint(data):
key = reformat_ssh_key(data).split(None, 2)[1]
fp_plain = hashlib.md5(key).hexdigest()
return ':'.join( a + b for a,b in zip(fp_plain[::2], fp_plain[1::2]) )

def write_authorized_keys(keys):
'''
Replaces autogenerated part of authorized_keys.
Command from keys is not escaped in any way, but keys are!!!
'''
AUTOGEN_START = '### START AUTOGENERATED ###\n'
AUTOGEN_END = '### END AUTOGENERATED ###\n'
path = os.path.expandsuer('~/.ssh/authorized_keys')
out = tempfile.NamedTemporaryFile(delete=False)

if os.path.exists(path):
autogen = False
for line in open(path):
if line == AUTOGEN_START:
autogen = True
elif line == AUTOGEN_END:
autogen = False
elif not autogen:
out.write(line)

out.write(AUTOGEN_START)
for command, key in keys:
escaped_key = reformat_ssh_key(key)
out.write('command="%s",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty %s\n'
% (command, escaped_key))
out.write(AUTOGEN_END)
out.close()
os.rename(out.name, path)

0 comments on commit 3269ae8

Please sign in to comment.