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
29
30
# 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
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



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

#cloneObject



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=(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

#hostnameObject



114
115
116
117
# File 'lib/logstash/util/safe_uri.rb', line 114

def hostname
  # Alias from the ruby library
  host
end

#inspectObject



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

def inspect
  sanitized.to_s
end

#normalizeObject



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

#passwordObject



104
105
106
107
108
# File 'lib/logstash/util/safe_uri.rb', line 104

def password
  if @uri.userInfo
    @uri.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=(new_path) ⇒ Object



132
133
134
# File 'lib/logstash/util/safe_uri.rb', line 132

def path=(new_path)
  update(:path, new_path)
end

#portObject



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=(new_query) ⇒ Object



136
137
138
# File 'lib/logstash/util/safe_uri.rb', line 136

def query=(new_query)
  update(:query, new_query)
end

#sanitizedObject



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 ? "#{user}:#{PASS_PLACEHOLDER}" : nil

  make_uri(scheme, , host, port, path, query, fragment)
end

#to_sObject



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

   = new_user
  if new_user && new_password
     += ":" + new_password
  end

  @uri = make_uri(new_scheme, , new_host, new_port, new_path, new_query, new_fragment)
end

#userObject



94
95
96
97
98
# File 'lib/logstash/util/safe_uri.rb', line 94

def user
  if @uri.userInfo
    @uri.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