Class: LogStash::Util::SafeURI
- Inherits:
-
Object
- Object
- LogStash::Util::SafeURI
- Extended by:
- Forwardable
- Defined in:
- lib/logstash/util/safe_uri.rb
Overview
This class exists to quietly wrap a password string so that, when printed or logged, you don’t accidentally print the password itself.
Constant Summary collapse
- PASS_PLACEHOLDER =
"xxxxxx".freeze
- HOSTNAME_PORT_REGEX =
/\A(?<hostname>([A-Za-z0-9\.\-]+)|\[[0-9A-Fa-f\:]+\])(:(?<port>\d+))?\Z/
Instance Attribute Summary collapse
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #clone ⇒ Object
- #fragment ⇒ Object
- #fragment=(new_fragment) ⇒ Object
- #host=(new_host) ⇒ Object
- #hostname ⇒ Object
-
#initialize(arg) ⇒ SafeURI
constructor
A new instance of SafeURI.
- #inspect ⇒ Object
- #normalize ⇒ Object
-
#normalize! ⇒ Object
Same algorithm as Ruby’s URI class uses.
- #password ⇒ Object
- #password=(new_password) ⇒ Object
- #path ⇒ Object
- #path=(new_path) ⇒ Object
- #port ⇒ Object
- #port=(new_port) ⇒ Object
- #query ⇒ Object
- #query=(new_query) ⇒ Object
- #sanitized ⇒ Object
- #to_s ⇒ Object
- #update(field, value) ⇒ Object
- #user ⇒ Object
- #user=(new_user) ⇒ Object
- #userinfo ⇒ Object
Constructor Details
#initialize(arg) ⇒ SafeURI
Returns a new instance of SafeURI.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/logstash/util/safe_uri.rb', line 17 def initialize(arg) @uri = case arg when String arg = "//#{arg}" if HOSTNAME_PORT_REGEX.match(arg) java.net.URI.new(arg) when java.net.URI arg when URI java.net.URI.new(arg.to_s) else raise ArgumentError, "Expected a string, java.net.URI, or URI, got a #{arg.class} creating a URL" end raise ArgumentError, "URI is not valid - host is not specified" if @uri.host.nil? end |
Instance Attribute Details
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
14 15 16 |
# File 'lib/logstash/util/safe_uri.rb', line 14 def uri @uri end |
Instance Method Details
#==(other) ⇒ Object
48 49 50 |
# File 'lib/logstash/util/safe_uri.rb', line 48 def ==(other) other.is_a?(::LogStash::Util::SafeURI) ? @uri == other.uri : false end |
#clone ⇒ Object
52 53 54 55 |
# File 'lib/logstash/util/safe_uri.rb', line 52 def clone # No need to clone the URI, in java its immutable self.class.new(uri) end |
#fragment ⇒ Object
171 172 173 |
# File 'lib/logstash/util/safe_uri.rb', line 171 def fragment @uri.raw_fragment end |
#fragment=(new_fragment) ⇒ Object
140 141 142 |
# File 'lib/logstash/util/safe_uri.rb', line 140 def fragment=(new_fragment) update(:fragment, new_fragment) end |
#host=(new_host) ⇒ Object
119 120 121 |
# File 'lib/logstash/util/safe_uri.rb', line 119 def host=(new_host) update(:host, new_host) end |
#hostname ⇒ Object
114 115 116 117 |
# File 'lib/logstash/util/safe_uri.rb', line 114 def hostname # Alias from the ruby library host end |
#inspect ⇒ Object
36 37 38 |
# File 'lib/logstash/util/safe_uri.rb', line 36 def inspect sanitized.to_s end |
#normalize ⇒ Object
157 158 159 160 161 |
# File 'lib/logstash/util/safe_uri.rb', line 157 def normalize d = self.dup d.normalize! d end |
#normalize! ⇒ Object
Same algorithm as Ruby’s URI class uses
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/logstash/util/safe_uri.rb', line 145 def normalize! if path && path == '' path = '/' end if scheme && scheme != scheme.downcase scheme = self.scheme.downcase end if host && host != host.downcase host = self.host.downcase end end |
#password ⇒ Object
104 105 106 107 108 |
# File 'lib/logstash/util/safe_uri.rb', line 104 def password if userinfo userinfo.split(":")[1] end end |
#password=(new_password) ⇒ Object
110 111 112 |
# File 'lib/logstash/util/safe_uri.rb', line 110 def password=(new_password) update(:password, new_password) end |
#path ⇒ Object
163 164 165 |
# File 'lib/logstash/util/safe_uri.rb', line 163 def path @uri.raw_path end |
#path=(new_path) ⇒ Object
132 133 134 |
# File 'lib/logstash/util/safe_uri.rb', line 132 def path=(new_path) update(:path, new_path) end |
#port ⇒ Object
123 124 125 126 |
# File 'lib/logstash/util/safe_uri.rb', line 123 def port # In java this is an int uri.port < 1 ? nil : uri.port end |
#port=(new_port) ⇒ Object
128 129 130 |
# File 'lib/logstash/util/safe_uri.rb', line 128 def port=(new_port) update(:port, new_port) end |
#query ⇒ Object
167 168 169 |
# File 'lib/logstash/util/safe_uri.rb', line 167 def query @uri.raw_query end |
#query=(new_query) ⇒ Object
136 137 138 |
# File 'lib/logstash/util/safe_uri.rb', line 136 def query=(new_query) update(:query, new_query) end |
#sanitized ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/logstash/util/safe_uri.rb', line 40 def sanitized return uri unless password # nothing to sanitize here! user_info = user ? "#{user}:#{PASS_PLACEHOLDER}" : nil make_uri(scheme, user_info, host, port, path, query, fragment) end |
#to_s ⇒ Object
32 33 34 |
# File 'lib/logstash/util/safe_uri.rb', line 32 def to_s sanitized.to_s end |
#update(field, value) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/logstash/util/safe_uri.rb', line 57 def update(field, value) new_scheme = scheme new_user = user new_password = password new_host = host new_port = port new_path = path new_query = query new_fragment = fragment case field when :scheme new_scheme = value when :user new_user = value when :password new_password = value when :host new_host = value when :port new_port = value when :path new_path = value when :query new_query = value when :fragment new_fragment = value end user_info = new_user if new_user && new_password user_info += ":" + new_password end @uri = make_uri(new_scheme, user_info, new_host, new_port, new_path, new_query, new_fragment) end |
#user ⇒ Object
94 95 96 97 98 |
# File 'lib/logstash/util/safe_uri.rb', line 94 def user if userinfo userinfo.split(":")[0] end end |
#user=(new_user) ⇒ Object
100 101 102 |
# File 'lib/logstash/util/safe_uri.rb', line 100 def user=(new_user) update(:user, new_user) end |
#userinfo ⇒ Object
175 176 177 |
# File 'lib/logstash/util/safe_uri.rb', line 175 def userinfo @uri.raw_user_info end |