Class: Vagrant::Util::CredentialScrubber

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/util/credential_scrubber.rb

Overview

Utility class to remove credential information from strings

Constant Summary collapse

REPLACEMENT_TEXT =

String used to replace credential information

"*****".freeze

Class Method Summary collapse

Class Method Details

.desensitize(string) ⇒ String

Remove sensitive information from string

Parameters:

  • string (String)

Returns:

  • (String)

32
33
34
35
36
37
38
# File 'lib/vagrant/util/credential_scrubber.rb', line 32

def self.desensitize(string)
  string = string.to_s.dup
  sensitive_strings.each do |remove|
    string.gsub!(/(\W|^)#{Regexp.escape(remove)}(\W|$)/, "\\1#{REPLACEMENT_TEXT}\\2")
  end
  string
end

.reset!Object

Reset the cached values for scrubber. This is not considered a public API and should only be used for testing.


66
67
68
# File 'lib/vagrant/util/credential_scrubber.rb', line 66

def self.reset!
  instance_variables.each(&method(:remove_instance_variable))
end

.scrub(string) ⇒ String

Attempt to remove detected credentials from string

Parameters:

  • string (String)

Returns:

  • (String)

12
13
14
# File 'lib/vagrant/util/credential_scrubber.rb', line 12

def self.scrub(string)
  string = url_scrubber(string)
end

.sensitive(string) ⇒ Object

Register a sensitive string to be scrubbed


41
42
43
44
45
46
47
# File 'lib/vagrant/util/credential_scrubber.rb', line 41

def self.sensitive(string)
  string = string.to_s.dup
  if string.length > 0
    sensitive_strings.push(string).uniq!
  end
  nil
end

.sensitive_stringsArray<string>

Returns:

  • (Array<string>)

56
57
58
59
60
61
# File 'lib/vagrant/util/credential_scrubber.rb', line 56

def self.sensitive_strings
  if !defined?(@_sensitive_strings)
    @_sensitive_strings = []
  end
  @_sensitive_strings
end

.unsensitive(string) ⇒ Object

Deregister a sensitive string and allow output


50
51
52
53
# File 'lib/vagrant/util/credential_scrubber.rb', line 50

def self.unsensitive(string)
  sensitive_strings.delete(string)
  nil
end

.url_scrubber(string) ⇒ String

Detect URLs and remove any embedded credentials

Parameters:

  • string (String)

Returns:

  • (String)

20
21
22
23
24
25
26
# File 'lib/vagrant/util/credential_scrubber.rb', line 20

def self.url_scrubber(string)
  string.gsub(%r{(ftp|https?)://[^\s][email protected][^\s]+}) do |address|
    uri = URI.parse(address)
    uri.user = uri.password = REPLACEMENT_TEXT
    uri.to_s
  end
end