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.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/logstash/util/safe_uri.rb', line 19

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.



16
17
18
# File 'lib/logstash/util/safe_uri.rb', line 16

def uri
  @uri
end

Instance Method Details

#==(other) ⇒ Object



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

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

#cloneObject



51
52
53
54
# File 'lib/logstash/util/safe_uri.rb', line 51

def clone
  cloned_uri = uri.clone
  self.class.new(cloned_uri)
end

#inspectObject



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

def inspect
  sanitized.to_s
end

#sanitizedObject



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

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

#to_sObject



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

def to_s
  sanitized.to_s
end