koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 1 | import re |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame^] | 2 | import yaml |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 3 | import getpass |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame^] | 4 | from typing import List, Dict, Any |
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 | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 7 | from .common_types import IPAddr |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 8 | |
| 9 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 10 | class URIsNamespace: |
| 11 | class ReParts: |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 12 | user_rr = "[^:]*?" |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 13 | host_rr = "[^:@]*?" |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 14 | port_rr = "\\d+" |
| 15 | key_file_rr = "[^:@]*" |
| 16 | passwd_rr = ".*?" |
| 17 | |
| 18 | re_dct = ReParts.__dict__ |
| 19 | |
| 20 | for attr_name, val in re_dct.items(): |
| 21 | if attr_name.endswith('_rr'): |
| 22 | new_rr = "(?P<{0}>{1})".format(attr_name[:-3], val) |
| 23 | setattr(ReParts, attr_name, new_rr) |
| 24 | |
| 25 | re_dct = ReParts.__dict__ |
| 26 | |
| 27 | templs = [ |
| 28 | "^{host_rr}$", |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 29 | "^{host_rr}:{port_rr}$", |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 30 | "^{host_rr}::{key_file_rr}$", |
| 31 | "^{host_rr}:{port_rr}:{key_file_rr}$", |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 32 | "^{user_rr}@{host_rr}$", |
| 33 | "^{user_rr}@{host_rr}:{port_rr}$", |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 34 | "^{user_rr}@{host_rr}::{key_file_rr}$", |
| 35 | "^{user_rr}@{host_rr}:{port_rr}:{key_file_rr}$", |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 36 | "^{user_rr}:{passwd_rr}@{host_rr}$", |
| 37 | "^{user_rr}:{passwd_rr}@{host_rr}:{port_rr}$", |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 38 | ] |
| 39 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 40 | uri_reg_exprs = [] # type: List[str] |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 41 | for templ in templs: |
| 42 | uri_reg_exprs.append(templ.format(**re_dct)) |
| 43 | |
| 44 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame^] | 45 | class ConnCreds(yaml.YAMLObject): |
| 46 | yaml_tag = '!ConnCreds' |
| 47 | |
| 48 | def __init__(self, host: str, user: str, passwd: str = None, port: str = '22', |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 49 | key_file: str = None, key: bytes = None) -> None: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 50 | self.user = user |
| 51 | self.passwd = passwd |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 52 | self.addr = IPAddr(host, int(port)) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 53 | self.key_file = key_file |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 54 | self.key = key |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 55 | |
| 56 | def __str__(self) -> str: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 57 | return "{}@{}:{}".format(self.user, self.addr.host, self.addr.port) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 58 | |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 59 | def __repr__(self) -> str: |
| 60 | return str(self) |
| 61 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame^] | 62 | @classmethod |
| 63 | def to_yaml(cls, dumper: Any, data: 'ConnCreds') -> Any: |
| 64 | dict_representation = { |
| 65 | 'user': data.user, |
| 66 | 'host': data.addr.host, |
| 67 | 'port': data.addr.port, |
| 68 | 'passwd': data.passwd, |
| 69 | 'key_file': data.key_file |
| 70 | } |
| 71 | return dumper.represent_mapping(data.yaml_tag, dict_representation) |
| 72 | |
| 73 | @classmethod |
| 74 | def from_yaml(cls, loader: Any, node: Any) -> 'ConnCreds': |
| 75 | dct = loader.construct_mapping(node) |
| 76 | return cls(**dct) |
| 77 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 78 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 79 | def parse_ssh_uri(uri: str) -> ConnCreds: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 80 | """Parse ssh connection URL from one of following form |
| 81 | [ssh://]user:passwd@host[:port] |
| 82 | [ssh://][user@]host[:port][:key_file] |
| 83 | """ |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 84 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 85 | if uri.startswith("ssh://"): |
| 86 | uri = uri[len("ssh://"):] |
| 87 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 88 | for rr in URIsNamespace.uri_reg_exprs: |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 89 | rrm = re.match(rr, uri) |
| 90 | if rrm is not None: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame^] | 91 | params = {"user": getpass.getuser()} # type: Dict[str, str] |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 92 | params.update(rrm.groupdict()) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame^] | 93 | return ConnCreds(**params) # type: ignore |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 94 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 95 | raise ValueError("Can't parse {0!r} as ssh uri value".format(uri)) |
| 96 | |
| 97 | |