28
28
from ssh2 .exceptions import AgentConnectionError , AgentListIdentitiesError , \
29
29
AgentAuthenticationError , AgentGetIdentityError
30
30
31
- from ..common import _validate_pkey_path
31
+ from ..common import _validate_pkey
32
32
from ...constants import DEFAULT_RETRIES , RETRY_DELAY
33
33
from ..reader import ConcurrentRWBuffer
34
34
from ...exceptions import UnknownHostError , AuthenticationError , \
@@ -182,12 +182,15 @@ def __init__(self, host,
182
182
self .session = None
183
183
self ._host = proxy_host if proxy_host else host
184
184
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 )
186
186
self .identity_auth = identity_auth
187
187
self ._keepalive_greenlet = None
188
188
self .ipv6_only = ipv6_only
189
189
self ._init ()
190
190
191
+ def _pkey_from_memory (self , pkey_data ):
192
+ raise NotImplementedError
193
+
191
194
def _init (self ):
192
195
self ._connect (self ._host , self ._port )
193
196
self ._init_session ()
@@ -309,7 +312,7 @@ def _identity_auth(self):
309
312
"Trying to authenticate with identity file %s" ,
310
313
identity_file )
311
314
try :
312
- self ._pkey_auth (identity_file , password = self .password )
315
+ self ._pkey_file_auth (identity_file , password = self .password )
313
316
except Exception as ex :
314
317
logger .debug (
315
318
"Authentication with identity file %s failed with %s, "
@@ -331,8 +334,8 @@ def _keepalive(self):
331
334
def auth (self ):
332
335
if self .pkey is not None :
333
336
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 )
336
339
if self .allow_agent :
337
340
try :
338
341
self ._agent_auth ()
@@ -364,7 +367,17 @@ def _agent_auth(self):
364
367
def _password_auth (self ):
365
368
raise NotImplementedError
366
369
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 ):
368
381
raise NotImplementedError
369
382
370
383
def _open_session (self ):
0 commit comments