From f8fe93970dda793012b44c4fff9bc8b41fef0a1f Mon Sep 17 00:00:00 2001 From: Kushal Das Date: Aug 04 2016 17:36:34 +0000 Subject: Public keys are saved as .key file in the server --- diff --git a/brumecloud/controller/__init__.py b/brumecloud/controller/__init__.py index 8e9c405..596e165 100644 --- a/brumecloud/controller/__init__.py +++ b/brumecloud/controller/__init__.py @@ -39,14 +39,29 @@ SESSION = database.create_session(CONFIG["DB_URL"]) -def generate_sshkey(bits: int=2048) -> Tuple[str,str]: +def generate_sshkey(data: Dict[str,str]) -> Tuple[bool,str]: ''' - Returns private key and public key + Returns the private key ''' - key = RSA.generate(bits, e=65537) - public_key = key.publickey().exportKey("OpenSSH") - private_key = key.exportKey("PEM") - return private_key, public_key + bits = 2048 + private_key = "" + name = data['keyname'] + # Broken https://github.com/dlitz/pycrypto/issues/99 + #key = RSA.generate(bits, e=65537) + #public_key = key.publickey().exportKey("OpenSSH") + #private_key = key.exportKey("PEM") + filename = os.path.join(CONFIG["DATA_PATH"], data['user'], '%s.pub' % name) + if os.path.exists(filename): + return False, "Key already exists in server." + cmd = 'ssh-keygen -t rsa -f {0} -q -N ""'.format(filename[:-4]) + brumeutils.system(cmd) + with open(filename[:-4]) as fobj: + private_key = fobj.read() + brumeutils.system("rm -f {0}".format(filename[:-4])) + return True, private_key + + + def authenticate(x_user: str, x_sig: str, data: str) -> bool: """ @@ -77,7 +92,7 @@ def save_ssh_key(data: Dict[str,str]) -> Tuple[bool,str]: name = data['keyname'] content = data['content'] # We expect the user directory is already created. - filename = os.path.join(CONFIG["DATA_PATH"], data['user'], '%s.key' % name) + filename = os.path.join(CONFIG["DATA_PATH"], data['user'], '%s.pub' % name) if os.path.exists(filename): return False, "Key already exists in server." with open(filename, 'w') as fobj: @@ -107,7 +122,7 @@ def create_instance(data: Dict[str,Union[str,int]]) -> Tuple[bool, str, str]: image_name = data.get('image_name', None) ssh_key_name = data.get('ssh_key') # Let us get the ssh key path - ssh_key = os.path.join(CONFIG["DATA_PATH"], data['user'],'{0}.key'.format(ssh_key_name)) + ssh_key = os.path.join(CONFIG["DATA_PATH"], data['user'],'{0}.pub'.format(ssh_key_name)) if not os.path.exists(ssh_key): return False, "That ssh key does not exists.", "Failed" instance_name = data.get('instance_name') @@ -145,10 +160,10 @@ def get_images() -> List[Tuple[str,str]]: def get_keys(data: Dict[str, str]) -> List[str]: result = [] user = data['user'] - path = os.path.join(CONFIG["DATA_PATH"], data['user'], '*.key') + path = os.path.join(CONFIG["DATA_PATH"], data['user'], '*.pub') files = glob(path) for name in files: - if name.endswith('.key'): + if name.endswith('.pub'): result.append(os.path.basename(name[:-4])) return result @@ -201,6 +216,9 @@ def api(): elif user_input["command"] == 'list_keys': allkeys = get_keys(user_input) return json.dumps({'output': True, "keys": allkeys}) + elif user_input["command"] == 'create_key': + status, key = generate_sshkey(user_input) + return json.dumps({'output': status, "message": key}) return "All Okay"