2828from ssh2 .exceptions import AgentConnectionError , AgentListIdentitiesError , \
2929 AgentAuthenticationError , AgentGetIdentityError
3030
31- from ..common import _validate_pkey_path
31+ from ..common import _validate_pkey
3232from ...constants import DEFAULT_RETRIES , RETRY_DELAY
3333from ..reader import ConcurrentRWBuffer
3434from ...exceptions import UnknownHostError , AuthenticationError , \
@@ -182,12 +182,15 @@ def __init__(self, host,
182182 self .session = None
183183 self ._host = proxy_host if proxy_host else host
184184 self ._port = proxy_port if proxy_port else self .port
185- self .pkey = _validate_pkey_path (pkey , self . host )
185+ self .pkey = _validate_pkey (pkey )
186186 self .identity_auth = identity_auth
187187 self ._keepalive_greenlet = None
188188 self .ipv6_only = ipv6_only
189189 self ._init ()
190190
191+ def _pkey_from_memory (self , pkey_data ):
192+ raise NotImplementedError
193+
191194 def _init (self ):
192195 self ._connect (self ._host , self ._port )
193196 self ._init_session ()
@@ -309,7 +312,7 @@ def _identity_auth(self):
309312 "Trying to authenticate with identity file %s" ,
310313 identity_file )
311314 try :
312- self ._pkey_auth (identity_file , password = self .password )
315+ self ._pkey_file_auth (identity_file , password = self .password )
313316 except Exception as ex :
314317 logger .debug (
315318 "Authentication with identity file %s failed with %s, "
@@ -331,8 +334,8 @@ def _keepalive(self):
331334 def auth (self ):
332335 if self .pkey is not None :
333336 logger .debug (
334- "Proceeding with private key file authentication" )
335- return self ._pkey_auth (self .pkey , password = self . password )
337+ "Proceeding with private key authentication" )
338+ return self ._pkey_auth (self .pkey )
336339 if self .allow_agent :
337340 try :
338341 self ._agent_auth ()
@@ -364,7 +367,17 @@ def _agent_auth(self):
364367 def _password_auth (self ):
365368 raise NotImplementedError
366369
367- def _pkey_auth (self , pkey_file , password = None ):
370+ def _pkey_auth (self , pkey ):
371+ _pkey = pkey
372+ if isinstance (pkey , str ):
373+ logger .debug ("Private key is provided as str, loading from private key file path" )
374+ with open (pkey , 'rb' ) as fh :
375+ _pkey = fh .read ()
376+ elif isinstance (pkey , bytes ):
377+ logger .debug ("Private key is provided in bytes, using as private key data" )
378+ return self ._pkey_from_memory (_pkey )
379+
380+ def _pkey_file_auth (self , pkey_file , password = None ):
368381 raise NotImplementedError
369382
370383 def _open_session (self ):
0 commit comments