Skip to content

Fix and improve get_hostnames() for configs with Match directives#1991

Open
phil-blain wants to merge 3 commits intoparamiko:mainfrom
phil-blain:get-hostnames-with-match
Open

Fix and improve get_hostnames() for configs with Match directives#1991
phil-blain wants to merge 3 commits intoparamiko:mainfrom
phil-blain:get-hostnames-with-match

Conversation

@phil-blain
Copy link

Fix and improve 'get_hostnames()' for configs with 'Match' directives

SSHConfig.get_hostnames() is unprepared to be called for configs with
'Match' directives, as this function expects every entry in the config
to have a "host" key, which is not the case when the config contains
"Match" directives.

Improve the function by checking for "matches" entries, and
additionnally check if the condition of the 'Match' directive is "host",
in which case add the listed hosts to the set of hosts returned.


Additionnally:

This should supersede #1674.

SSHConfig.get_hostnames() is unprepared to be called for configs with
'Match' directives, as this function expects every entry in the config
to have a "host" key, which is not the case when the config contains
"Match" directives.

Improve the function by checking for "matches" entries, and
additionnally check if the condition of the 'Match' directive is "host",
in which case add the listed hosts to the set of hosts returned.
'ssh_config' specifies keywords are case-insensitive, and their value
case-sensitive.

The 'Match' criteria (all, host, canonical, etc.) are listed as
lowercase but are actually also accepted when capitalized by OpenSSH
(maybe because they are labelled criteria "keywords"?).

Be lenient and allow them to be capitalized by making sure to lowercase
them when we read the config. Capitalize one keyword in a test config
for good measure.
Mark Python files as such with the 'diff' Git attribute [1]. This allows
various Git commands (diff, grep, log, blame) to show in which function
changes are located, i.e. the previous commit would be shown as

    diff --git a/paramiko/config.py b/paramiko/config.py
    index a8891c8..517bce4 100644
    --- a/paramiko/config.py
    +++ b/paramiko/config.py
    @@ -520,7 +520,7 @@ def _get_matches(self, match):
                 if type_.startswith("!"):
                     match["negate"] = True
                     type_ = type_[1:]
    -            match["type"] = type_
    +            match["type"] = type_.lower()
                 # all/canonical have no params (everything else does)
                 if type_ in ("all", "canonical"):
                     matches.append(match)

Notice how the hunk header indicates the changes are in '_get_matches'.

[1] https://git-scm.com/docs/gitattributes#_defining_a_custom_hunk_header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants