Class: LogStash::Util::SafeURI

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ SafeURI

Returns a new instance of SafeURI.



18
19
20
21
22
23
24
25
26
27
28
# 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)
           URI.parse(arg)
         when URI
           arg
         else
           raise ArgumentError, "Expected a string or URI, got a #{arg.class} creating a URL"
         end
end

Instance Attribute Details

#uriObject (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



46
47
48
# File 'lib/logstash/util/safe_uri.rb', line 46

def ==(other)
  other.is_a?(::LogStash::Util::SafeURI) ? @uri == other.uri : false
end

#inspectObject



34
35
36
# File 'lib/logstash/util/safe_uri.rb', line 34

def inspect
  sanitized.to_s
end

#sanitizedObject



38
39
40
41
42
43
44
# File 'lib/logstash/util/safe_uri.rb', line 38

def sanitized
  return uri unless uri.password # nothing to sanitize here!
  
  safe = uri.clone
  safe.password = PASS_PLACEHOLDER
  safe
end

#to_sObject



30
31
32
# File 'lib/logstash/util/safe_uri.rb', line 30

def to_s
  sanitized.to_s
end