Sha256: 0c6ff9144ce88758a38daea4e998b1b6894305dc4261e4b98dc384fd35574647

Contents?: true

Size: 1.88 KB

Versions: 25

Compression:

Stored size: 1.88 KB

Contents

module Vagrant
  module Util
    # Utility class to remove credential information from strings
    class CredentialScrubber
      # String used to replace credential information
      REPLACEMENT_TEXT = "*****".freeze

      # Attempt to remove detected credentials from string
      #
      # @param [String] string
      # @return [String]
      def self.scrub(string)
        string = url_scrubber(string)
      end

      # Detect URLs and remove any embedded credentials
      #
      # @param [String] string
      # @return [String]
      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

      # Remove sensitive information from string
      #
      # @param [String] string
      # @return [String]
      def self.desensitize(string)
        string = string.to_s.dup
        sensitive_strings.each do |remove|
          string.gsub!(remove, REPLACEMENT_TEXT)
        end
        string
      end

      # Register a sensitive string to be scrubbed
      def self.sensitive(string)
        string = string.to_s.dup
        if string.length > 0
          sensitive_strings.push(string).uniq!
        end
        nil
      end

      # Deregister a sensitive string and allow output
      def self.unsensitive(string)
        sensitive_strings.delete(string)
        nil
      end

      # @return [Array<string>]
      def self.sensitive_strings
        if !defined?(@_sensitive_strings)
          @_sensitive_strings = []
        end
        @_sensitive_strings
      end

      # @private
      # Reset the cached values for scrubber. This is not considered a public
      # API and should only be used for testing.
      def self.reset!
        instance_variables.each(&method(:remove_instance_variable))
      end
    end
  end
end

Version data entries

25 entries across 21 versions & 3 rubygems

Version Path
vagrant-unbundled-2.2.9.0 lib/vagrant/util/credential_scrubber.rb
vagrant-unbundled-2.2.8.0 lib/vagrant/util/credential_scrubber.rb
vagrant-unbundled-2.2.7.0 lib/vagrant/util/credential_scrubber.rb
vagrant-unbundled-2.2.6.2 lib/vagrant/util/credential_scrubber.rb
vagrant-unbundled-2.2.6.1 lib/vagrant/util/credential_scrubber.rb
vagrant-unbundled-2.2.6.0 lib/vagrant/util/credential_scrubber.rb
vagrant-unbundled-2.2.5.0 lib/vagrant/util/credential_scrubber.rb
vagrant-unbundled-2.2.4.0 lib/vagrant/util/credential_scrubber.rb
vagrant-unbundled-2.2.3.0 lib/vagrant/util/credential_scrubber.rb
vagrant-unbundled-2.2.2.0 lib/vagrant/util/credential_scrubber.rb
vagrant-unbundled-2.2.0.0 lib/vagrant/util/credential_scrubber.rb
vagrant-unbundled-2.1.4.0 lib/vagrant/util/credential_scrubber.rb
vagrant-unbundled-2.1.2.0 lib/vagrant/util/credential_scrubber.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.5.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/credential_scrubber.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/credential_scrubber.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/credential_scrubber.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/credential_scrubber.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/credential_scrubber.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/credential_scrubber.rb
vagrant-unbundled-2.1.1.0 lib/vagrant/util/credential_scrubber.rb