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.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/logstash/util/safe_uri.rb', line 18 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.
15 16 17 |
# File 'lib/logstash/util/safe_uri.rb', line 15 def uri @uri end |
Instance Method Details
#==(other) ⇒ Object
49 50 51 |
# File 'lib/logstash/util/safe_uri.rb', line 49 def ==(other) other.is_a?(::LogStash::Util::SafeURI) ? @uri == other.uri : false end |
#clone ⇒ Object
53 54 55 56 |
# File 'lib/logstash/util/safe_uri.rb', line 53 def clone # No need to clone the URI, in java its immutable self.class.new(uri) end |
#fragment ⇒ Object
172 173 174 |
# File 'lib/logstash/util/safe_uri.rb', line 172 def fragment @uri.raw_fragment end |
#fragment=(new_fragment) ⇒ Object
141 142 143 |
# File 'lib/logstash/util/safe_uri.rb', line 141 def fragment=(new_fragment) update(:fragment, new_fragment) end |
#host=(new_host) ⇒ Object
120 121 122 |
# File 'lib/logstash/util/safe_uri.rb', line 120 def host=(new_host) update(:host, new_host) end |
#hostname ⇒ Object
115 116 117 118 |
# File 'lib/logstash/util/safe_uri.rb', line 115 def hostname # Alias from the ruby library host end |
#inspect ⇒ Object
37 38 39 |
# File 'lib/logstash/util/safe_uri.rb', line 37 def inspect sanitized.to_s end |
#normalize ⇒ Object
158 159 160 161 162 |
# File 'lib/logstash/util/safe_uri.rb', line 158 def normalize d = self.dup d.normalize! d end |
#normalize! ⇒ Object
Same algorithm as Ruby’s URI class uses
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/logstash/util/safe_uri.rb', line 146 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
105 106 107 108 109 |
# File 'lib/logstash/util/safe_uri.rb', line 105 def password if userinfo userinfo.split(":")[1] end end |
#password=(new_password) ⇒ Object
111 112 113 |
# File 'lib/logstash/util/safe_uri.rb', line 111 def password=(new_password) update(:password, new_password) end |
#path ⇒ Object
164 165 166 |
# File 'lib/logstash/util/safe_uri.rb', line 164 def path @uri.raw_path end |
#path=(new_path) ⇒ Object
133 134 135 |
# File 'lib/logstash/util/safe_uri.rb', line 133 def path=(new_path) update(:path, new_path) end |
#port ⇒ Object
124 125 126 127 |
# File 'lib/logstash/util/safe_uri.rb', line 124 def port # In java this is an int uri.port < 1 ? nil : uri.port end |
#port=(new_port) ⇒ Object
129 130 131 |
# File 'lib/logstash/util/safe_uri.rb', line 129 def port=(new_port) update(:port, new_port) end |
#query ⇒ Object
168 169 170 |
# File 'lib/logstash/util/safe_uri.rb', line 168 def query @uri.raw_query end |
#query=(new_query) ⇒ Object
137 138 139 |
# File 'lib/logstash/util/safe_uri.rb', line 137 def query=(new_query) update(:query, new_query) end |
#sanitized ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/logstash/util/safe_uri.rb', line 41 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
33 34 35 |
# File 'lib/logstash/util/safe_uri.rb', line 33 def to_s sanitized.to_s end |
#update(field, value) ⇒ Object
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 93 |
# File 'lib/logstash/util/safe_uri.rb', line 58 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
95 96 97 98 99 |
# File 'lib/logstash/util/safe_uri.rb', line 95 def user if userinfo userinfo.split(":")[0] end end |
#user=(new_user) ⇒ Object
101 102 103 |
# File 'lib/logstash/util/safe_uri.rb', line 101 def user=(new_user) update(:user, new_user) end |
#userinfo ⇒ Object
176 177 178 |
# File 'lib/logstash/util/safe_uri.rb', line 176 def userinfo @uri.raw_user_info end |