pushkey: Fix comparisions for private keys
The original pushkey code was broken when it came to comparisons for the private
key comparision.
Change-Id: I7facddfbdeb4fdc0991a877f7ecbdc826f3c5a6d
diff --git a/mdt/keys.py b/mdt/keys.py
index 40a07be..4b3d02e 100644
--- a/mdt/keys.py
+++ b/mdt/keys.py
@@ -40,7 +40,7 @@
def GenerateAuthorizedKeysLine(paramiko_key):
public_key = paramiko_key.get_base64()
- authorized_keys_line = 'ssh-rsa {0} mdt'.format(public_key)
+ authorized_keys_line = 'ssh-rsa {0} mdt\r\n'.format(public_key)
return authorized_keys_line
diff --git a/mdt/shell.py b/mdt/shell.py
index 313c79a..f2a2e2f 100644
--- a/mdt/shell.py
+++ b/mdt/shell.py
@@ -15,10 +15,13 @@
'''
+import io
import re
import os
import sys
+from paramiko.rsakey import RSAKey
+
from mdt import command
from mdt import console
from mdt import keys
@@ -145,8 +148,13 @@
return 1
source_key = ''
- with open(keyfile, 'rb') as fp:
- source_key = fp.read()
+ with open(keyfile, 'r') as fp:
+ source_key = fp.readline()
+
+ # Paramiko RSA private key -- get the public part by converting
+ if source_key.startswith('-----BEGIN RSA PRIVATE KEY-----'):
+ pkey = RSAKey.from_private_key_file(keyfile)
+ source_key = keys.GenerateAuthorizedKeysLine(pkey)
try:
sftp.chdir('/home/mendel/.ssh')
@@ -154,13 +162,6 @@
sftp.mkdir('/home/mendel/.ssh', mode=0o700)
with sftp.open('/home/mendel/.ssh/authorized_keys', 'a+b') as fp:
- # Paramiko RSA private key -- get the public part by converting
- if source_key.startswith('-----BEGIN RSA PRIVATE KEY-----'):
- keyfp = io.StringIO(source_key)
- pkey = RSAKey.from_private_key_file(keyfp)
- source_key = keys.GenerateAuthorizedKeysLine(pkey)
-
- fp.write('\r\n')
fp.write(source_key)
print("Key {0} pushed.".format(keyfile))