blob: ed857b664a4fca62d785608bece9de532a0b65f3 [file] [log] [blame]
koder aka kdanilove06762a2015-03-22 23:32:09 +02001import re
koder aka kdanilov3a6633e2015-03-26 18:20:00 +02002import getpass
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +02003import logging
koder aka kdanilovf2865172016-12-30 03:35:11 +02004from typing import List, Dict
koder aka kdanilov22d134e2016-11-08 11:33:19 +02005
koder aka kdanilove06762a2015-03-22 23:32:09 +02006
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +02007from . import utils
koder aka kdanilovf2865172016-12-30 03:35:11 +02008from .common_types import ConnCreds
koder aka kdanilove06762a2015-03-22 23:32:09 +02009
10
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020011logger = logging.getLogger("wally")
12
13
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020014class URIsNamespace:
15 class ReParts:
koder aka kdanilove06762a2015-03-22 23:32:09 +020016 user_rr = "[^:]*?"
koder aka kdanilov7e0f7cf2015-05-01 17:24:35 +030017 host_rr = "[^:@]*?"
koder aka kdanilove06762a2015-03-22 23:32:09 +020018 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 kdanilov7e0f7cf2015-05-01 17:24:35 +030033 "^{host_rr}:{port_rr}$",
koder aka kdanilov416b87a2015-05-12 00:26:04 +030034 "^{host_rr}::{key_file_rr}$",
35 "^{host_rr}:{port_rr}:{key_file_rr}$",
koder aka kdanilov7e0f7cf2015-05-01 17:24:35 +030036 "^{user_rr}@{host_rr}$",
37 "^{user_rr}@{host_rr}:{port_rr}$",
koder aka kdanilove06762a2015-03-22 23:32:09 +020038 "^{user_rr}@{host_rr}::{key_file_rr}$",
39 "^{user_rr}@{host_rr}:{port_rr}:{key_file_rr}$",
koder aka kdanilov7e0f7cf2015-05-01 17:24:35 +030040 "^{user_rr}:{passwd_rr}@{host_rr}$",
41 "^{user_rr}:{passwd_rr}@{host_rr}:{port_rr}$",
koder aka kdanilove06762a2015-03-22 23:32:09 +020042 ]
43
koder aka kdanilov22d134e2016-11-08 11:33:19 +020044 uri_reg_exprs = [] # type: List[str]
koder aka kdanilove06762a2015-03-22 23:32:09 +020045 for templ in templs:
46 uri_reg_exprs.append(templ.format(**re_dct))
47
48
koder aka kdanilov22d134e2016-11-08 11:33:19 +020049def parse_ssh_uri(uri: str) -> ConnCreds:
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020050 """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 kdanilove06762a2015-03-22 23:32:09 +020054
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030055 if uri.startswith("ssh://"):
56 uri = uri[len("ssh://"):]
57
koder aka kdanilov22d134e2016-11-08 11:33:19 +020058 for rr in URIsNamespace.uri_reg_exprs:
koder aka kdanilove06762a2015-03-22 23:32:09 +020059 rrm = re.match(rr, uri)
60 if rrm is not None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020061 params = {"user": getpass.getuser()} # type: Dict[str, str]
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +020062 params.update(rrm.groupdict())
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020063 params['host'] = utils.to_ip(params['host'])
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020064 return ConnCreds(**params) # type: ignore
koder aka kdanilov652cd802015-04-13 12:21:07 +030065
koder aka kdanilove06762a2015-03-22 23:32:09 +020066 raise ValueError("Can't parse {0!r} as ssh uri value".format(uri))
67
68