Skip to content
Snippets Groups Projects
Commit fa4e8acf authored by Maxime Bombar's avatar Maxime Bombar
Browse files

[remote] Correctly handles Include directive in SSH config

parent b9ef74ef
No related branches found
No related tags found
No related merge requests found
Pipeline #3281 passed with warnings with stages
in 3 minutes and 15 seconds
...@@ -11,8 +11,10 @@ SPDX-License-Identifier: GPL-3.0-or-later ...@@ -11,8 +11,10 @@ SPDX-License-Identifier: GPL-3.0-or-later
import base64 import base64
import json import json
import logging import logging
import re
from configparser import ConfigParser
from functools import lru_cache from functools import lru_cache
from getpass import getpass from getpass import getpass, getuser
from hashlib import sha1, sha256 from hashlib import sha1, sha256
from pathlib import Path from pathlib import Path
...@@ -117,12 +119,30 @@ def create_ssh_client(host, password=None): ...@@ -117,12 +119,30 @@ def create_ssh_client(host, password=None):
client.set_missing_host_key_policy(AskUserOrDNSPolicy) client.set_missing_host_key_policy(AskUserOrDNSPolicy)
# Load config file and use the right username # Load config file and use the right username
try: # As of June 2020, paramiko doesn't support `Include` directive ...
config = SSHConfig() # See https://github.com/paramiko/paramiko/issues/1609
config.parse(Path.home().joinpath(".ssh/config").open()) config_path = Path.home().joinpath(".ssh/config")
username = config.lookup(host).get("user", None) # Match anything right after `Include` directive in a non commented line.
except FileNotFoundError: include_regex = re.compile("^(?!#).*(?<=Include )([^\s]+)")
username=None config_files = [config_path]
with open(config_path) as cpath:
for line in cpath.readlines():
m = include_regex.match(line)
if m:
config_files.append(Path(m.group(1)))
username=None
for conf in config_files:
try:
config = SSHConfig()
config.parse(conf.expanduser().open())
except FileNotFoundError:
continue
username = config.lookup(host).get("user")
if username:
break
if not username:
username = getuser()
log.debug(f"Will connect to {host} as {username}")
# Load system private keys # Load system private keys
client.load_system_host_keys() client.load_system_host_keys()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment