koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 1 | import re |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 2 | import getpass |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 3 | import logging |
koder aka kdanilov | f286517 | 2016-12-30 03:35:11 +0200 | [diff] [blame] | 4 | from typing import List, Dict |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 5 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 6 | |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 7 | from . import utils |
koder aka kdanilov | f286517 | 2016-12-30 03:35:11 +0200 | [diff] [blame] | 8 | from .common_types import ConnCreds |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 9 | |
| 10 | |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 11 | logger = logging.getLogger("wally") |
| 12 | |
| 13 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 14 | class URIsNamespace: |
| 15 | class ReParts: |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 16 | user_rr = "[^:]*?" |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 17 | host_rr = "[^:@]*?" |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 18 | port_rr = "\\d+" |
| 19 | key_file_rr = "[^:@]*" |
| 20 | passwd_rr = ".*?" |
| 21 | |
| 22 | re_dct = ReParts.__dict__ |
| 23 | |
| 24 | for attr_name, val in re_dct.items(): |
| 25 | if attr_name.endswith('_rr'): |
| 26 | new_rr = "(?P<{0}>{1})".format(attr_name[:-3], val) |
| 27 | setattr(ReParts, attr_name, new_rr) |
| 28 | |
| 29 | re_dct = ReParts.__dict__ |
| 30 | |
| 31 | templs = [ |
| 32 | "^{host_rr}$", |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 33 | "^{host_rr}:{port_rr}$", |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 34 | "^{host_rr}::{key_file_rr}$", |
| 35 | "^{host_rr}:{port_rr}:{key_file_rr}$", |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 36 | "^{user_rr}@{host_rr}$", |
| 37 | "^{user_rr}@{host_rr}:{port_rr}$", |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 38 | "^{user_rr}@{host_rr}::{key_file_rr}$", |
| 39 | "^{user_rr}@{host_rr}:{port_rr}:{key_file_rr}$", |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 40 | "^{user_rr}:{passwd_rr}@{host_rr}$", |
| 41 | "^{user_rr}:{passwd_rr}@{host_rr}:{port_rr}$", |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 42 | ] |
| 43 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 44 | uri_reg_exprs = [] # type: List[str] |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 45 | for templ in templs: |
| 46 | uri_reg_exprs.append(templ.format(**re_dct)) |
| 47 | |
| 48 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 49 | def parse_ssh_uri(uri: str) -> ConnCreds: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 50 | """Parse ssh connection URL from one of following form |
| 51 | [ssh://]user:passwd@host[:port] |
| 52 | [ssh://][user@]host[:port][:key_file] |
| 53 | """ |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 54 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 55 | if uri.startswith("ssh://"): |
| 56 | uri = uri[len("ssh://"):] |
| 57 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 58 | for rr in URIsNamespace.uri_reg_exprs: |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 59 | rrm = re.match(rr, uri) |
| 60 | if rrm is not None: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 61 | params = {"user": getpass.getuser()} # type: Dict[str, str] |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 62 | params.update(rrm.groupdict()) |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 63 | params['host'] = utils.to_ip(params['host']) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 64 | return ConnCreds(**params) # type: ignore |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 65 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 66 | raise ValueError("Can't parse {0!r} as ssh uri value".format(uri)) |
| 67 | |
| 68 | |