diff options
-rw-r--r-- | lib/net/ssh/config.rb | 41 | ||||
-rw-r--r-- | test/configs/match | 3 | ||||
-rw-r--r-- | test/test_config.rb | 50 |
3 files changed, 75 insertions, 19 deletions
diff --git a/lib/net/ssh/config.rb b/lib/net/ssh/config.rb index 6236831..8b9db86 100644 --- a/lib/net/ssh/config.rb +++ b/lib/net/ssh/config.rb @@ -122,7 +122,7 @@ module Net block_seen = true settings[key] = host elsif key == 'match' - block_matched = eval_match_condition(value, host, settings) + block_matched = eval_match_conditions(value, host, settings) block_seen = true elsif !block_seen case key @@ -325,22 +325,33 @@ module Net str.scan(/([^"\s]+)?(?:"([^"]+)")?\s*/).map(&:join) end - def eval_match_condition(condition, host, settings) - if condition.start_with?('!') - negated = true - condition = condition[1..-1] - else - negated = false - end + def eval_match_conditions(condition, host, settings) + conditions = condition.split(/\s+/) + return true if conditions == ["all"] - condition_met = - case condition - when 'all' - true + conditions = conditions.each_slice(2) + matching = true + conditions.each do |(kind,exprs)| + case kind.downcase + when "all" + raise "all cannot be mixed with other conditions" + when "host" + if exprs.start_with?('!') + negated = true + exprs = exprs[1..-1] + else + negated = false + end + condition_met = false + exprs.split(",").each do |expr| + condition_met = condition_met || host =~ pattern2regex(expr) + end + matching = matching && negated ^ condition_met + # else + # warn "net-ssh: Unsupported expr in Match block: #{kind}" end - - # return false for unsupported conditions - condition_met.nil? ? false : (negated ^ condition_met) + end + matching end end end diff --git a/test/configs/match b/test/configs/match index c610e8c..ceb031b 100644 --- a/test/configs/match +++ b/test/configs/match @@ -4,9 +4,6 @@ Host test.host Match all Compression yes -Match !all - Port 1234 - Match unsupported Port 2345 diff --git a/test/test_config.rb b/test/test_config.rb index ca27cdc..3a10cd2 100644 --- a/test/test_config.rb +++ b/test/test_config.rb @@ -310,7 +310,55 @@ class TestConfig < NetSSHTest net_ssh = Net::SSH::Config.translate(config) assert_equal true, net_ssh[:forward_agent] assert_equal true, net_ssh[:compression] - assert_equal 22, net_ssh[:port] + assert_equal 2345, net_ssh[:port] + end + + def test_load_with_match_block_with_host + data = %q{ + Match Host foo + Port 1234 + Compression no + } + with_config_from_data data do |f| + config = Net::SSH::Config.load(f, "bar") + assert_nil config['port'] + config = Net::SSH::Config.load(f, "foo") + assert_equal 1234, config['port'] + end + end + + def test_load_with_match_block_with_hosts + data = %q{ + Match Host foo,bar + Port 1234 + Compression no + } + with_config_from_data data do |f| + config = Net::SSH::Config.load(f, "bar2") + assert_nil config['port'] + config = Net::SSH::Config.load(f, "bar") + assert_equal 1234, config['port'] + config = Net::SSH::Config.load(f, "foo") + assert_equal 1234, config['port'] + end + end + + def test_load_with_match_block_with_hosts_wildcard + data = %q{ + Match Host foo,*.baz.com + Port 1234 + Compression no + } + with_config_from_data data do |f| + config = Net::SSH::Config.load(f, "bar2") + assert_nil config['port'] + config = Net::SSH::Config.load(f, "bbaz.com") + assert_nil config['port'] + config = Net::SSH::Config.load(f, "bar.baz.com") + assert_equal 1234, config['port'] + config = Net::SSH::Config.load(f, "foo") + assert_equal 1234, config['port'] + end end private |