Skip to content

Commit 87686bc

Browse files
authored
Use UserWithPKey instead of pkey parameter (#144)
Fixes: #23 Signed-off-by: Lukas Bednar <[email protected]>
1 parent 341ddcf commit 87686bc

File tree

7 files changed

+71
-10
lines changed

7 files changed

+71
-10
lines changed

README.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@ that means SSH server must be running there already.
2121
h = Host("10.11.12.13")
2222
h.users.append(RootUser('123456'))
2323
24-
# Use with ssh key. export HOST_SSH_KEY to use specific ssh key, default is ~/.ssh/id_rsa
25-
host.executor_factory = rrmngmnt.ssh.RemoteExecutorFactory(use_pkey=True)
26-
2724
exec = h.executor()
2825
# Run with sudo
2926
exec = h.executor(sudo=True)
3027
3128
print exec.run_cmd(['echo', 'Hello World'])
3229
30+
Using SSH key for authentication
31+
32+
.. code:: python
33+
34+
from rrmngmnt import Host, UserWithPKey
35+
36+
h = Host("10.11.12.13")
37+
user = UserWithPKey('user', '/path/to/pkey'))
38+
39+
h.executor(user).run_cmd(['echo', 'Use pkey for auth instead of password'])
3340
3441
Features
3542
--------

rrmngmnt/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from rrmngmnt.host import Host
22
from rrmngmnt.user import (
33
User,
4+
UserWithPKey,
45
RootUser,
56
Domain,
67
InternalDomain,
@@ -12,6 +13,7 @@
1213
__all__ = [
1314
'Host',
1415
'User',
16+
'UserWithPKey',
1517
'RootUser',
1618
'Domain',
1719
'InternalDomain',

rrmngmnt/host.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def executor(self, user=None, pkey=False, sudo=False):
228228
if pkey:
229229
warnings.warn(
230230
"Parameter 'pkey' is deprecated and will be removed in future."
231-
"Please use ssh.RemoteExecutorFactory to set this parameter."
231+
"Please use user.UserWithPKey user instead."
232232
)
233233
ef = copy.copy(self.executor_factory)
234234
ef.use_pkey = pkey

rrmngmnt/ssh.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import paramiko
55
import contextlib
66
import subprocess
7+
import warnings
78
from rrmngmnt.common import normalize_string
89
from rrmngmnt.executor import Executor, ExecutorFactory
10+
from rrmngmnt.user import UserWithPKey
911

1012

1113
AUTHORIZED_KEYS = os.path.join("%s", ".ssh/authorized_keys")
@@ -42,7 +44,7 @@ def process(self, msg, kwargs):
4244
"[%s@%s/%s] %s" % (
4345
self.extra['self'].user.name,
4446
self.extra['self'].address,
45-
self.extra['self'].user.password,
47+
self.extra['self'].user.credentials,
4648
msg,
4749
),
4850
kwargs,
@@ -59,7 +61,11 @@ def __init__(self, executor, timeout=None):
5961
self._timeout = timeout
6062
self._ssh = paramiko.SSHClient()
6163
self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
62-
if self._executor.use_pkey:
64+
if isinstance(self._executor.user, UserWithPKey):
65+
self.pkey = paramiko.RSAKey.from_private_key_file(
66+
self._executor.user.private_key
67+
)
68+
elif self._executor.use_pkey:
6369
self.pkey = paramiko.RSAKey.from_private_key_file(
6470
os.getenv(
6571
"HOST_SSH_KEY", ID_RSA_PRV % os.path.expanduser('~')
@@ -223,6 +229,11 @@ def __init__(self, user, address, use_pkey=False, port=22, sudo=False):
223229
self.use_pkey = use_pkey
224230
self.port = port
225231
self.sudo = sudo
232+
if use_pkey:
233+
warnings.warn(
234+
"Parameter 'use_pkey' is deprecated and will be removed in "
235+
"future. Please use user.UserWithPKey user instead."
236+
)
226237

227238
def session(self, timeout=None):
228239
"""
@@ -310,6 +321,11 @@ class RemoteExecutorFactory(ExecutorFactory):
310321
def __init__(self, use_pkey=False, port=22):
311322
self.use_pkey = use_pkey
312323
self.port = port
324+
if use_pkey:
325+
warnings.warn(
326+
"Parameter 'use_pkey' is deprecated and will be removed in "
327+
"future. Please use user.UserWithPKey user instead."
328+
)
313329

314330
def build(self, host, user, sudo=False):
315331
return RemoteExecutor(

rrmngmnt/user.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from rrmngmnt.resource import Resource
23

34

@@ -19,6 +20,27 @@ def full_name(self):
1920
def get_full_name(self):
2021
return self.name
2122

23+
@property
24+
def credentials(self):
25+
return self.get_credentials()
26+
27+
def get_credentials(self):
28+
return self.password
29+
30+
31+
class UserWithPKey(User):
32+
def __init__(self, name, private_key):
33+
"""
34+
Args:
35+
private_key (str): Path to private key
36+
name (str): User name
37+
"""
38+
super(UserWithPKey, self).__init__(name, None)
39+
self.private_key = os.path.expanduser(private_key)
40+
41+
def get_credentials(self):
42+
return self.private_key
43+
2244

2345
class RootUser(User):
2446
NAME = 'root'

tests/test_host.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33

4-
from rrmngmnt import Host, User, RootUser
4+
from rrmngmnt import Host, User, RootUser, UserWithPKey
55
from rrmngmnt.executor import Executor
66
import pytest
77

@@ -27,12 +27,18 @@ def test_custom_user(self):
2727
e = get_host().executor(user=user)
2828
assert e.user.name == 'lukas'
2929

30+
def test_pkey_user(self):
31+
user = UserWithPKey('lukas', '/path/to/key')
32+
e = get_host().executor(user=user)
33+
assert e.user.name == 'lukas'
34+
assert e.user.credentials == '/path/to/key'
35+
3036
def test_executor_user(self):
3137
user = User('lukas', '123456')
3238
h = get_host()
3339
h.executor_user = user
3440
e = h.executor()
35-
e.user.name == 'lukas'
41+
assert e.user.name == 'lukas'
3642

3743
def test_executor_with_pkey(self):
3844
user = User('core', '12')

tests/test_user.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# -*- coding: utf-8 -*-
2-
from rrmngmnt import User, InternalDomain, ADUser, Domain
2+
from rrmngmnt import User, UserWithPKey, InternalDomain, ADUser, Domain
33

44

55
def test_user():
6-
assert "user" == User("user", 'pass').full_name
6+
user = User("user", 'pass')
7+
assert "user" == user.full_name
8+
assert user.credentials == user.password
79

810

911
def test_indernal_domain():
@@ -16,3 +18,9 @@ def test_domain():
1618
assert "[email protected]" == ADUser(
1719
"user", "pass", Domain("example.com"),
1820
).full_name
21+
22+
23+
def test_user_with_pkey():
24+
user = UserWithPKey("user", "/path/to/key")
25+
assert 'user' == user.full_name
26+
assert '/path/to/key' == user.credentials

0 commit comments

Comments
 (0)