Add support for hostname:port syntax to SafeURI class

This is a deviation from what the ruby URI class normally allows.
With this patch users can use "myhost:123" as an option and have it do the
right thing, as opposed to before where they'd get an error and have to
use "//myhost:123".

Fixes #5618
This commit is contained in:
Andrew Cholakian 2016-07-11 17:33:24 -05:00
parent 4045aea1ff
commit 8cd1a475fe
2 changed files with 30 additions and 9 deletions

View file

@ -6,6 +6,7 @@ require "logstash/util"
# logged, you don't accidentally print the password itself. # logged, you don't accidentally print the password itself.
class LogStash::Util::SafeURI class LogStash::Util::SafeURI
PASS_PLACEHOLDER = "xxxxxx".freeze PASS_PLACEHOLDER = "xxxxxx".freeze
HOSTNAME_PORT_REGEX=/\A(?<hostname>([A-Za-z0-9\.\-]+)|\[[0-9A-Fa-f\:]+\])(:(?<port>\d+))?\Z/
extend Forwardable extend Forwardable
@ -17,6 +18,7 @@ class LogStash::Util::SafeURI
def initialize(arg) def initialize(arg)
@uri = case arg @uri = case arg
when String when String
arg = "//#{arg}" if HOSTNAME_PORT_REGEX.match(arg)
URI.parse(arg) URI.parse(arg)
when URI when URI
arg arg

View file

@ -178,21 +178,15 @@ describe LogStash::Config::Mixin do
end end
end end
shared_examples("safe URI") do shared_examples("safe URI") do |options|
options ||= {}
subject { klass.new("uri" => uri_str) } subject { klass.new("uri" => uri_str) }
it "should be a SafeURI object" do it "should be a SafeURI object" do
expect(subject.uri).to(be_a(LogStash::Util::SafeURI)) expect(subject.uri).to(be_a(LogStash::Util::SafeURI))
end end
it "should make password values hidden with #to_s" do
expect(subject.uri.to_s).to eql(uri_hidden)
end
it "should make password values hidden with #inspect" do
expect(subject.uri.inspect).to eql(uri_hidden)
end
it "should correctly copy URI types" do it "should correctly copy URI types" do
clone = subject.class.new(subject.params) clone = subject.class.new(subject.params)
expect(clone.uri.to_s).to eql(uri_hidden) expect(clone.uri.to_s).to eql(uri_hidden)
@ -206,6 +200,18 @@ describe LogStash::Config::Mixin do
expect(subject.original_params['uri']).to(be_a(LogStash::Util::SafeURI)) expect(subject.original_params['uri']).to(be_a(LogStash::Util::SafeURI))
end end
if !options[:exclude_password_specs]
describe "passwords" do
it "should make password values hidden with #to_s" do
expect(subject.uri.to_s).to eql(uri_hidden)
end
it "should make password values hidden with #inspect" do
expect(subject.uri.inspect).to eql(uri_hidden)
end
end
end
context "attributes" do context "attributes" do
[:scheme, :user, :password, :hostname, :path].each do |attr| [:scheme, :user, :password, :hostname, :path].each do |attr|
it "should make #{attr} available" do it "should make #{attr} available" do
@ -215,6 +221,19 @@ describe LogStash::Config::Mixin do
end end
end end
context "with a host:port combination" do
let(:scheme) { nil }
let(:user) { nil }
let(:password) { nil }
let(:hostname) { "myhostname" }
let(:port) { 1234 }
let(:path) { "" }
let(:uri_str) { "#{hostname}:#{port}" }
let(:uri_hidden) { "//#{hostname}:#{port}" }
include_examples("safe URI", :exclude_password_specs => true)
end
context "with a username / password" do context "with a username / password" do
let(:scheme) { "myscheme" } let(:scheme) { "myscheme" }
let(:user) { "myuser" } let(:user) { "myuser" }