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)


35
36
37
38
39
40
41
# File 'lib/vagrant/util/credential_scrubber.rb', line 35

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.



69
70
71
# File 'lib/vagrant/util/credential_scrubber.rb', line 69

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)


15
16
17
# File 'lib/vagrant/util/credential_scrubber.rb', line 15

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

.sensitive(string) ⇒ Object

Register a sensitive string to be scrubbed



44
45
46
47
48
49
50
# File 'lib/vagrant/util/credential_scrubber.rb', line 44

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>)


59
60
61
62
63
64
# File 'lib/vagrant/util/credential_scrubber.rb', line 59

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

.unsensitive(string) ⇒ Object

Deregister a sensitive string and allow output



53
54
55
56
# File 'lib/vagrant/util/credential_scrubber.rb', line 53

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)


23
24
25
26
27
28
29
# File 'lib/vagrant/util/credential_scrubber.rb', line 23

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