blob: efb67b3e73703c9b51d57d9ad39cc274595ff678 [file] [log] [blame]
koder aka kdanilove06762a2015-03-22 23:32:09 +02001import re
koder aka kdanilov962ee5f2016-12-19 02:40:08 +02002import yaml
koder aka kdanilov3a6633e2015-03-26 18:20:00 +02003import getpass
koder aka kdanilov962ee5f2016-12-19 02:40:08 +02004from typing import List, Dict, Any
koder aka kdanilov22d134e2016-11-08 11:33:19 +02005
koder aka kdanilove06762a2015-03-22 23:32:09 +02006
koder aka kdanilov73084622016-11-16 21:51:08 +02007from .common_types import IPAddr
koder aka kdanilove06762a2015-03-22 23:32:09 +02008
9
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020010class URIsNamespace:
11 class ReParts:
koder aka kdanilove06762a2015-03-22 23:32:09 +020012 user_rr = "[^:]*?"
koder aka kdanilov7e0f7cf2015-05-01 17:24:35 +030013 host_rr = "[^:@]*?"
koder aka kdanilove06762a2015-03-22 23:32:09 +020014 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 kdanilov7e0f7cf2015-05-01 17:24:35 +030029 "^{host_rr}:{port_rr}$",
koder aka kdanilov416b87a2015-05-12 00:26:04 +030030 "^{host_rr}::{key_file_rr}$",
31 "^{host_rr}:{port_rr}:{key_file_rr}$",
koder aka kdanilov7e0f7cf2015-05-01 17:24:35 +030032 "^{user_rr}@{host_rr}$",
33 "^{user_rr}@{host_rr}:{port_rr}$",
koder aka kdanilove06762a2015-03-22 23:32:09 +020034 "^{user_rr}@{host_rr}::{key_file_rr}$",
35 "^{user_rr}@{host_rr}:{port_rr}:{key_file_rr}$",
koder aka kdanilov7e0f7cf2015-05-01 17:24:35 +030036 "^{user_rr}:{passwd_rr}@{host_rr}$",
37 "^{user_rr}:{passwd_rr}@{host_rr}:{port_rr}$",
koder aka kdanilove06762a2015-03-22 23:32:09 +020038 ]
39
koder aka kdanilov22d134e2016-11-08 11:33:19 +020040 uri_reg_exprs = [] # type: List[str]
koder aka kdanilove06762a2015-03-22 23:32:09 +020041 for templ in templs:
42 uri_reg_exprs.append(templ.format(**re_dct))
43
44
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020045class ConnCreds(yaml.YAMLObject):
46 yaml_tag = '!ConnCreds'
47
48 def __init__(self, host: str, user: str, passwd: str = None, port: str = '22',
koder aka kdanilov73084622016-11-16 21:51:08 +020049 key_file: str = None, key: bytes = None) -> None:
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020050 self.user = user
51 self.passwd = passwd
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +020052 self.addr = IPAddr(host, int(port))
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020053 self.key_file = key_file
koder aka kdanilov73084622016-11-16 21:51:08 +020054 self.key = key
koder aka kdanilov22d134e2016-11-08 11:33:19 +020055
56 def __str__(self) -> str:
koder aka kdanilov73084622016-11-16 21:51:08 +020057 return "{}@{}:{}".format(self.user, self.addr.host, self.addr.port)
koder aka kdanilov22d134e2016-11-08 11:33:19 +020058
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +020059 def __repr__(self) -> str:
60 return str(self)
61
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020062 @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 kdanilov22d134e2016-11-08 11:33:19 +020078
koder aka kdanilov22d134e2016-11-08 11:33:19 +020079def parse_ssh_uri(uri: str) -> ConnCreds:
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020080 """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 kdanilove06762a2015-03-22 23:32:09 +020084
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030085 if uri.startswith("ssh://"):
86 uri = uri[len("ssh://"):]
87
koder aka kdanilov22d134e2016-11-08 11:33:19 +020088 for rr in URIsNamespace.uri_reg_exprs:
koder aka kdanilove06762a2015-03-22 23:32:09 +020089 rrm = re.match(rr, uri)
90 if rrm is not None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020091 params = {"user": getpass.getuser()} # type: Dict[str, str]
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +020092 params.update(rrm.groupdict())
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020093 return ConnCreds(**params) # type: ignore
koder aka kdanilov652cd802015-04-13 12:21:07 +030094
koder aka kdanilove06762a2015-03-22 23:32:09 +020095 raise ValueError("Can't parse {0!r} as ssh uri value".format(uri))
96
97