diff options
author | Jeff Forcier <jeff@bitprophet.org> | 2019-10-07 11:34:56 -0400 |
---|---|---|
committer | Jeff Forcier <jeff@bitprophet.org> | 2019-10-07 11:34:56 -0400 |
commit | ee4822e722c4a5643604571334d02505fca8e9cb (patch) | |
tree | e57fcf2000ec6b9fa074436557b007c9f43c04e9 | |
parent | 5a3d550f771ea6ddb1923209d116721c55d42966 (diff) | |
download | paramiko-ee4822e722c4a5643604571334d02505fca8e9cb.tar.gz |
[REBASE ME] Implement 'Match localuser'
-rw-r--r-- | paramiko/config.py | 4 | ||||
-rw-r--r-- | tests/configs/match-localuser | 14 | ||||
-rw-r--r-- | tests/configs/match-localuser-no-arg | 2 | ||||
-rw-r--r-- | tests/test_config.py | 47 |
4 files changed, 53 insertions, 14 deletions
diff --git a/paramiko/config.py b/paramiko/config.py index a23bf309..1cecab0e 100644 --- a/paramiko/config.py +++ b/paramiko/config.py @@ -353,7 +353,9 @@ class SSHConfig(object): if self._should_fail(passed, candidate): return False if type_ == "localuser": - return False + passed = self._pattern_matches(param, local_username) + if self._should_fail(passed, candidate): + return False # Made it all the way here? Everything matched! matched.append(candidate) # Did anything match? (To be treated as bool, usually.) diff --git a/tests/configs/match-localuser b/tests/configs/match-localuser new file mode 100644 index 00000000..fe4a276c --- /dev/null +++ b/tests/configs/match-localuser @@ -0,0 +1,14 @@ +Match localuser gandalf + HostName gondor + +Match localuser b* + HostName shire + +Match localuser aragorn,frodo + HostName moria + +Match localuser gimli,!legolas + Port 7373 + +Match !localuser sauron + HostName mordor diff --git a/tests/configs/match-localuser-no-arg b/tests/configs/match-localuser-no-arg new file mode 100644 index 00000000..6623553a --- /dev/null +++ b/tests/configs/match-localuser-no-arg @@ -0,0 +1,2 @@ +Match localuser + User oops diff --git a/tests/test_config.py b/tests/test_config.py index 7fa74e2d..e0730ce2 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -821,26 +821,47 @@ class TestMatchUser(object): load_config("match-user-no-arg") +# NOTE: highly derivative of previous suite due to the former's use of +# localuser fallback. Doesn't seem worth conflating/refactoring right now. class TestMatchLocalUser(object): - def test_matches_local_username(self): - # TODO: may require refactoring wherever we grab this now - assert False + @patch("paramiko.config.getpass.getuser") + def test_matches_local_username(self, getuser): + getuser.return_value = "gandalf" + result = load_config("match-localuser").lookup("anything") + assert result["hostname"] == "gondor" - def test_may_be_globbed(self): - # TODO: probably just use a partial glob as it is a stronger test - assert False + @patch("paramiko.config.getpass.getuser") + def test_may_be_globbed(self, getuser): + for user in ("bilbo", "bombadil"): + getuser.return_value = user + result = load_config("match-localuser").lookup("anything") + assert result["hostname"] == "shire" - def test_may_be_comma_separated_list(self): - assert False + @patch("paramiko.config.getpass.getuser") + def test_may_be_comma_separated_list(self, getuser): + for user in ("aragorn", "frodo"): + getuser.return_value = user + result = load_config("match-localuser").lookup("anything") + assert result["hostname"] == "moria" - def test_comma_separated_list_may_have_internal_negation(self): - assert False + @patch("paramiko.config.getpass.getuser") + def test_comma_separated_list_may_have_internal_negation(self, getuser): + getuser.return_value = "legolas" + result = load_config("match-localuser").lookup("anything") + assert "port" not in result + getuser.return_value = "gimli" + result = load_config("match-localuser").lookup("anything") + assert result["port"] == "7373" - def test_may_be_negated(self): - assert False + @patch("paramiko.config.getpass.getuser") + def test_may_be_negated(self, getuser): + getuser.return_value = "saruman" + result = load_config("match-localuser").lookup("anything") + assert result["hostname"] == "mordor" def test_requires_an_argument(self): - assert False + with raises(ConfigParseError): + load_config("match-localuser-no-arg") class TestComplexMatching(object): |