Skip to content

Commit 2a83900

Browse files
author
Pan
committed
Updated embedded openssh server to generate config from template with absolute paths.
Updated openssh server to set pkey permissions on startup. Updated tests to set pkey permissions on startup and check for auth on tests that require it. Updated travis cfg
1 parent ed66d76 commit 2a83900

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ addons:
2121
install:
2222
- pip install flake8
2323
- python setup.py build_ext --inplace
24-
- chmod 600 embedded_server/rsa.key
2524
- eval "$(ssh-agent -s)"
2625
script:
2726
- docker/build-packages.sh

embedded_server/openssh.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,38 @@
1616

1717
import os
1818
import socket
19-
2019
from subprocess import Popen
2120
from time import sleep
21+
from sys import version_info
2222

23+
from jinja2 import Template
2324

24-
SERVER_KEY = os.path.sep.join([os.path.dirname(__file__), 'rsa.key'])
25-
SSHD_CONFIG = os.path.sep.join([os.path.dirname(__file__), 'sshd_config'])
25+
DIR_NAME = os.path.dirname(__file__)
26+
SERVER_KEY = os.path.abspath(os.path.sep.join([DIR_NAME, 'rsa.key']))
27+
SSHD_CONFIG_TMPL = os.path.abspath(os.path.sep.join(
28+
[DIR_NAME, 'sshd_config.tmpl']))
29+
SSHD_CONFIG = os.path.abspath(os.path.sep.join([DIR_NAME, 'sshd_config']))
2630

2731
class OpenSSHServer(object):
2832

2933
def __init__(self, port=2222):
3034
self.port = port
3135
self.server_proc = None
36+
_mask = int('0600') if version_info <= (2,) else 0o600
37+
os.chmod(SERVER_KEY, _mask)
38+
self.make_config()
39+
40+
def make_config(self):
41+
with open(SSHD_CONFIG_TMPL) as fh:
42+
tmpl = fh.read()
43+
template = Template(tmpl)
44+
with open(SSHD_CONFIG, 'w') as fh:
45+
fh.write(template.render(parent_dir=os.path.abspath(DIR_NAME)))
46+
fh.write(os.linesep)
3247

3348
def start_server(self):
3449
cmd = ['/usr/sbin/sshd', '-D', '-p', str(self.port),
35-
'-q', '-h', SERVER_KEY, '-f', SSHD_CONFIG]
50+
'-h', SERVER_KEY, '-f', SSHD_CONFIG]
3651
server = Popen(cmd)
3752
self.server_proc = server
3853
self._wait_for_port()

embedded_server/sshd_config renamed to embedded_server/sshd_config.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ ServerKeyBits 1024
1111

1212
AcceptEnv LANG LC_*
1313
Subsystem sftp /usr/lib/openssh/sftp-server
14-
AuthorizedKeysFile ssh2-python/embedded_server/authorized_keys
14+
AuthorizedKeysFile {{parent_dir}}/authorized_keys

requirements_dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
cython
22
flake8
3+
jinja2

tests/test_ssh2.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import socket
55
import time
6+
from sys import version_info
67

78
from ssh2.session import Session
89
from ssh2.utils import wait_socket
@@ -24,6 +25,8 @@ class SSH2TestCase(unittest.TestCase):
2425

2526
@classmethod
2627
def setUpClass(cls):
28+
_mask = int('0600') if version_info <= (2,) else 0o600
29+
os.chmod(PKEY_FILENAME, _mask)
2730
cls.host = '127.0.0.1'
2831
cls.port = 2222
2932
cls.server = OpenSSHServer()
@@ -64,7 +67,7 @@ def test_agent(self):
6467
self.assertTrue(agent.connect() == 0)
6568

6669
def test_execute(self):
67-
self._auth()
70+
self.assertEqual(self._auth(), 0)
6871
chan = self.session.open_session()
6972
self.assertTrue(chan is not None)
7073
self.assertTrue(chan.execute(self.cmd) == 0)
@@ -76,7 +79,7 @@ def test_execute(self):
7679
self.assertTrue(chan.wait_eof() == 0)
7780

7881
def test_exit_code(self):
79-
self._auth()
82+
self.assertEqual(self._auth(), 0)
8083
chan = self.session.open_session()
8184
chan.execute('exit 2')
8285
chan.wait_eof()
@@ -86,7 +89,7 @@ def test_exit_code(self):
8689
self.assertEqual(exit_code, 2)
8790

8891
def test_long_running_execute(self):
89-
self._auth()
92+
self.assertEqual(self._auth(), 0)
9093
chan = self.session.open_session()
9194
chan.execute('sleep 1; exit 3')
9295
self.assertTrue(chan.wait_eof() == 0)
@@ -95,7 +98,7 @@ def test_long_running_execute(self):
9598
self.assertEqual(chan.get_exit_status(), 3)
9699

97100
def test_read_stderr(self):
98-
self._auth()
101+
self.assertEqual(self._auth(), 0)
99102
chan = self.session.open_session()
100103
expected = ['stderr output']
101104
chan.execute('echo "stderr output" >&2')
@@ -105,7 +108,7 @@ def test_read_stderr(self):
105108
self.assertListEqual(expected, lines)
106109

107110
def test_pty(self):
108-
self._auth()
111+
self.assertEqual(self._auth(), 0)
109112
chan = self.session.open_session()
110113
self.assertTrue(chan.pty() == 0)
111114
_out = u'stderr output'
@@ -118,7 +121,7 @@ def test_pty(self):
118121
self.assertListEqual(expected, lines)
119122

120123
def test_write_stdin(self):
121-
self._auth()
124+
self.assertEqual(self._auth(), 0)
122125
_in = u'writing to stdin'
123126
chan = self.session.open_session()
124127
chan.execute('cat')
@@ -131,7 +134,7 @@ def test_write_stdin(self):
131134
self.assertListEqual([_in], lines)
132135

133136
def test_write_ex(self):
134-
self._auth()
137+
self.assertEqual(self._auth(), 0)
135138
_in = u'writing to stdin'
136139
chan = self.session.open_session()
137140
chan.execute('cat')
@@ -144,7 +147,7 @@ def test_write_ex(self):
144147
self.assertListEqual([_in], lines)
145148

146149
def test_write_stderr(self):
147-
self._auth()
150+
self.assertEqual(self._auth(), 0)
148151
chan = self.session.open_session()
149152
chan.execute('echo something')
150153
_in = u'stderr'
@@ -173,7 +176,7 @@ def test_sftp(self):
173176
os.unlink(remote_filename)
174177

175178
def test_setenv(self):
176-
self._auth()
179+
self.assertEqual(self._auth(), 0)
177180
chan = self.session.open_session()
178181
_var = 'LC_MY_VAR'
179182
_val = 'value'

0 commit comments

Comments
 (0)