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.



17
18
19
20
21
22
23
24
25
26
27
28
29
# 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
end

Instance Attribute Details

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



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
  # No need to clone the URI, in java its immutable
  self.class.new(uri)
end

#fragmentObject



170
171
172
# File 'lib/logstash/util/safe_uri.rb', line 170

def fragment
  @uri.raw_fragment
end

#fragment=(new_fragment) ⇒ Object



139
140
141
# File 'lib/logstash/util/safe_uri.rb', line 139

def fragment=(new_fragment)
  update(:fragment, new_fragment)
end

#host=(new_host) ⇒ Object



118
119
120
# File 'lib/logstash/util/safe_uri.rb', line 118

def host=(new_host)
  update(:host, new_host)
end

#hostnameObject



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

def hostname
  # Alias from the ruby library
  host
end

#inspectObject



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

def inspect
  sanitized.to_s
end

#normalizeObject



156
157
158
159
160
# File 'lib/logstash/util/safe_uri.rb', line 156

def normalize
  d = self.dup
  d.normalize!
  d
end

#normalize!Object

Same algorithm as Ruby’s URI class uses



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/logstash/util/safe_uri.rb', line 144

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



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

def password
  if userinfo
    userinfo.split(":")[1]
  end
end

#password=(new_password) ⇒ Object



109
110
111
# File 'lib/logstash/util/safe_uri.rb', line 109

def password=(new_password)
  update(:password, new_password)
end

#pathObject



162
163
164
# File 'lib/logstash/util/safe_uri.rb', line 162

def path
  @uri.raw_path
end

#path=(new_path) ⇒ Object



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

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

#portObject



122
123
124
125
# File 'lib/logstash/util/safe_uri.rb', line 122

def port
  # In java this is an int
  uri.port < 1 ? nil : uri.port
end

#port=(new_port) ⇒ Object



127
128
129
# File 'lib/logstash/util/safe_uri.rb', line 127

def port=(new_port)
  update(:port, new_port)
end

#queryObject



166
167
168
# File 'lib/logstash/util/safe_uri.rb', line 166

def query
  @uri.raw_query
end

#query=(new_query) ⇒ Object



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

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

#sanitizedObject



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

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



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

def to_s
  sanitized.to_s
end

#update(field, value) ⇒ Object



56
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
# File 'lib/logstash/util/safe_uri.rb', line 56

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



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

def user
  if userinfo
    userinfo.split(":")[0]
  end
end

#user=(new_user) ⇒ Object



99
100
101
# File 'lib/logstash/util/safe_uri.rb', line 99

def user=(new_user)
  update(:user, new_user)
end

#userinfoObject



174
175
176
# File 'lib/logstash/util/safe_uri.rb', line 174

def userinfo
  @uri.
end