def add(cm_id, caller_id, key, name): """ Adds given Key named @prm{name} with content @prm{key} to caller's keys list. @clmview_user @param_post{key,string} key's content @param_post{name,string} key's name @response{None} """ if len(Key.objects.filter(user_id__exact=caller_id)) > 5: # magic value, keys limit raise CLMException('ssh_key_limit') k = Key() k.user_id = caller_id k.data = key k.name = name r = re.search('ssh-rsa (.*) (.*)', key) if not r: raise CLMException('ssh_key_format') s = hashlib.md5(base64.b64decode(r.groups()[0])).hexdigest() k.fingerprint = ':'.join([s[i:i + 2] for i in xrange(0, 30, 2)]) try: k.save() except: raise CLMException('ssh_key_add')
def generate(cm_id, caller_id, name): """ Generates Key pair named @prm{name} for caller. @clmview_user @parameter{name,string} Key's name @response{string} content of private Key's file """ if len(Key.objects.filter(user_id__exact=caller_id)) > 5: # magic value, keys limit raise CLMException('ssh_key_limit') if Key.objects.filter(user_id__exact=caller_id).filter(name__exact=name).exists(): raise CLMException('ssh_key_already_exist') if subprocess.call(['ssh-keygen', '-q', '-f', '/tmp/' + str(caller_id) + '_' + name, '-N', '']) != 0: raise CLMException('ssh_key_generate') f = open('/tmp/' + str(caller_id) + '_' + name, 'r') f2 = open('/tmp/' + str(caller_id) + '_' + name + '.pub', 'r') k = Key() k.user_id = caller_id k.data = f2.read() k.name = name s = hashlib.md5(base64.b64decode(k.data.split()[1])).hexdigest() k.fingerprint = ':'.join([s[i:i + 2] for i in xrange(0, 30, 2)]) try: k.save() except: raise CLMException('ssh_key_generate') finally: private = f.read() os.remove('/tmp/' + str(caller_id) + '_' + name) os.remove('/tmp/' + str(caller_id) + '_' + name + '.pub') return private
def add(cm_id, caller_id, key, name): """ Adds given Key named @prm{name} with content @prm{key} to caller's keys list. @clmview_user @param_post{key,string} key's content @param_post{name,string} key's name @response{None} """ if len(Key.objects.filter( user_id__exact=caller_id)) > 5: # magic value, keys limit raise CLMException('ssh_key_limit') k = Key() k.user_id = caller_id k.data = key k.name = name r = re.search('ssh-rsa (.*) (.*)', key) if not r: raise CLMException('ssh_key_format') s = hashlib.md5(base64.b64decode(r.groups()[0])).hexdigest() k.fingerprint = ':'.join([s[i:i + 2] for i in xrange(0, 30, 2)]) try: k.save() except: raise CLMException('ssh_key_add')
def generate(cm_id, caller_id, name): """ Generates Key pair named @prm{name} for caller. Public part of that Key is stored in database with specified name, whereas content of the private Key part is returned. Neither public, nor private part of the key is saved to file. Private part of the key is never stored - it's only returned once. @clmview_user @param_post{name,string} Key's name @response{string} content of private Key's file """ if len(Key.objects.filter( user_id__exact=caller_id)) > 5: # magic value, keys limit raise CLMException('ssh_key_limit') if Key.objects.filter(user_id__exact=caller_id).filter( name__exact=name).exists(): raise CLMException('ssh_key_already_exist') if subprocess.call([ 'ssh-keygen', '-q', '-f', '/tmp/' + str(caller_id) + '_' + name, '-N', '' ]) != 0: raise CLMException('ssh_key_generate') f = open('/tmp/' + str(caller_id) + '_' + name, 'r') f2 = open('/tmp/' + str(caller_id) + '_' + name + '.pub', 'r') k = Key() k.user_id = caller_id k.data = f2.read() k.name = name s = hashlib.md5(base64.b64decode(k.data.split()[1])).hexdigest() k.fingerprint = ':'.join([s[i:i + 2] for i in xrange(0, 30, 2)]) try: k.save() except: raise CLMException('ssh_key_generate') finally: private = f.read() os.remove('/tmp/' + str(caller_id) + '_' + name) os.remove('/tmp/' + str(caller_id) + '_' + name + '.pub') return private