Skip to content

Commit c820732

Browse files
committed
Add optional password to destination string
1 parent c1527d7 commit c820732

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

tej/submission.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ def escape_queue(s):
159159

160160
_re_ssh = re.compile(r'^'
161161
r'(?:ssh://)?' # 'ssh://' prefix
162-
r'(?:([a-zA-Z0-9_.-]+)@)?' # 'user@'
162+
r'(?:([a-zA-Z0-9_.-]+)' # 'user@'
163+
r'(?::([^ @]+))?' # ':password'
164+
r'@)?' # '@'
163165
r'([a-zA-Z0-9_.-]+)' # 'host'
164166
r'(?::([0-9]+))?' # ':port'
165167
r'$')
@@ -171,12 +173,14 @@ def parse_ssh_destination(destination):
171173
match = _re_ssh.match(destination)
172174
if not match:
173175
raise InvalidDestination("Invalid destination: %s" % destination)
174-
user, host, port = match.groups()
176+
user, password, host, port = match.groups()
175177
info = {}
176178
if user:
177179
info['username'] = user
178180
else:
179181
info['username'] = getpass.getuser()
182+
if password:
183+
info['password'] = password
180184
if port:
181185
info['port'] = int(port)
182186
info['hostname'] = host
@@ -185,12 +189,17 @@ def parse_ssh_destination(destination):
185189

186190

187191
def destination_as_string(destination):
192+
if 'password' in destination:
193+
user = '%s:%s' % (destination['username'], destination['password'])
194+
else:
195+
user = destination['username']
196+
188197
if destination.get('port', 22) != 22:
189-
return 'ssh://%s@%s:%d' % (destination['username'],
198+
return 'ssh://%s@%s:%d' % (user,
190199
destination['hostname'],
191200
destination['port'])
192201
else:
193-
return 'ssh://%s@%s' % (destination['username'],
202+
return 'ssh://%s@%s' % (user,
194203
destination['hostname'])
195204

196205

tests/test_parsing.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,17 @@ def test_parse(self):
3939
self.assertEqual(parse('me@host:12'), {'hostname': 'host',
4040
'username': 'me',
4141
'port': 12})
42+
self.assertEqual(parse('me:p4$$w0rd@host:12'), {'hostname': 'host',
43+
'username': 'me',
44+
'password': 'p4$$w0rd',
45+
'port': 12})
4246
self.assertEqual(parse('ssh://me@host:12'), {'hostname': 'host',
4347
'username': 'me',
4448
'port': 12})
49+
self.assertEqual(parse('ssh://me:p@host:12'), {'hostname': 'host',
50+
'username': 'me',
51+
'password': 'p',
52+
'port': 12})
4553
self.assertEqual(parse('ssh://me@host:22'), {'hostname': 'host',
4654
'username': 'me',
4755
'port': 22})
@@ -54,9 +62,15 @@ def test_string(self):
5462
self.assertEqual(string({'hostname': '127.0.0.1', 'port': 12,
5563
'username': 'somebody'}),
5664
'ssh://[email protected]:12')
65+
self.assertEqual(string({'hostname': '127.0.0.1', 'port': 12,
66+
'username': 'somebody', 'password': '$$'}),
67+
'ssh://somebody:[email protected]:12')
5768
self.assertEqual(string({'hostname': '127.0.0.1', 'port': 22,
5869
'username': 'somebody'}),
5970
71+
self.assertEqual(string({'hostname': '127.0.0.1', 'port': 22,
72+
'username': 'somebody', 'password': 'pass'}),
73+
'ssh://somebody:[email protected]')
6074
self.assertEqual(string({'hostname': '127.0.0.1',
6175
'username': 'somebody'}),
6276

0 commit comments

Comments
 (0)