|
| 1 | +require 'util/vmdb-logger' |
| 2 | +require 'util/miq-password' |
| 3 | + |
| 4 | +describe VMDBLogger do |
| 5 | + describe "#log_hashes" do |
| 6 | + let(:buffer) { StringIO.new } |
| 7 | + let(:logger) { described_class.new(buffer) } |
| 8 | + |
| 9 | + it "filters out passwords when keys are symbols" do |
| 10 | + hash = {:a => {:b => 1, :password => "pa$$w0rd"}} |
| 11 | + logger.log_hashes(hash) |
| 12 | + |
| 13 | + buffer.rewind |
| 14 | + expect(buffer.read).to include(":password: [FILTERED]") |
| 15 | + end |
| 16 | + |
| 17 | + it "filters out passwords when keys are strings" do |
| 18 | + hash = {"a" => {"b" => 1, "password" => "pa$$w0rd"}} |
| 19 | + logger.log_hashes(hash) |
| 20 | + |
| 21 | + buffer.rewind |
| 22 | + expect(buffer.read).to include("password: [FILTERED]") |
| 23 | + end |
| 24 | + |
| 25 | + it "with :filter option, filters out given keys and passwords" do |
| 26 | + hash = {:a => {:b => 1, :extra_key => "pa$$w0rd", :password => "pa$$w0rd"}} |
| 27 | + logger.log_hashes(hash, :filter => :extra_key) |
| 28 | + |
| 29 | + buffer.rewind |
| 30 | + message = buffer.read |
| 31 | + expect(message).to include(':extra_key: [FILTERED]') |
| 32 | + expect(message).to include(':password: [FILTERED]') |
| 33 | + end |
| 34 | + |
| 35 | + it "when :filter option is a Set object, filters out the given Set elements" do |
| 36 | + hash = {:a => {:b => 1, :bind_pwd => "pa$$w0rd", :amazon_secret => "pa$$w0rd", :password => "pa$$w0rd"}} |
| 37 | + logger.log_hashes(hash, :filter => %i(bind_pwd password amazon_secret).to_set) |
| 38 | + |
| 39 | + buffer.rewind |
| 40 | + message = buffer.read |
| 41 | + expect(message).to include(':bind_pwd: [FILTERED]') |
| 42 | + expect(message).to include(':amazon_secret: [FILTERED]') |
| 43 | + expect(message).to include(':password: [FILTERED]') |
| 44 | + end |
| 45 | + |
| 46 | + it "filters out encrypted value" do |
| 47 | + hash = {:a => {:b => 1, :extra_key => "v2:{c5qTeiuz6JgbBOiDqp3eiQ==}"}} |
| 48 | + logger.log_hashes(hash) |
| 49 | + |
| 50 | + buffer.rewind |
| 51 | + expect(buffer.read).to include(':extra_key: [FILTERED]') |
| 52 | + end |
| 53 | + |
| 54 | + it "filters out root_password" do |
| 55 | + hash = {"a" => {"b" => 1, "root_password" => "pa$$w0rd"}} |
| 56 | + logger.log_hashes(hash) |
| 57 | + |
| 58 | + buffer.rewind |
| 59 | + expect(buffer.read).to include("root_password: [FILTERED]") |
| 60 | + end |
| 61 | + |
| 62 | + it "filters out password_for_important_thing" do |
| 63 | + hash = {:a => {:b => 1, :password_for_important_thing => "pa$$w0rd"}} |
| 64 | + logger.log_hashes(hash) |
| 65 | + |
| 66 | + buffer.rewind |
| 67 | + expect(buffer.read).to include(":password_for_important_thing: [FILTERED]") |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + it ".contents with no log returns empty string" do |
| 72 | + allow(File).to receive_messages(:file? => false) |
| 73 | + expect(VMDBLogger.contents("mylog.log")).to eq("") |
| 74 | + end |
| 75 | + |
| 76 | + it ".contents with empty log returns empty string" do |
| 77 | + require 'util/miq-system' |
| 78 | + allow(MiqSystem).to receive_messages(:tail => "") |
| 79 | + |
| 80 | + allow(File).to receive_messages(:file? => true) |
| 81 | + expect(VMDBLogger.contents("mylog.log")).to eq("") |
| 82 | + end |
| 83 | + |
| 84 | + context "with evm log snippet with invalid utf8 byte sequence data" do |
| 85 | + before(:each) do |
| 86 | + @log = File.expand_path(File.join(File.dirname(__FILE__), "data/redundant_utf8_byte_sequence.log")) |
| 87 | + end |
| 88 | + |
| 89 | + context "accessing the invalid data directly" do |
| 90 | + before(:each) do |
| 91 | + @data = File.read(@log) |
| 92 | + end |
| 93 | + |
| 94 | + it "should have content with the invalid utf8 lines" do |
| 95 | + expect(@data).not_to be_nil |
| 96 | + expect(@data.kind_of?(String)).to be_truthy |
| 97 | + end |
| 98 | + |
| 99 | + it "should unpack raw data as UTF-8 characters and raise ArgumentError" do |
| 100 | + expect { @data.unpack("U*") }.to raise_error(ArgumentError) |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + context "using VMDBLogger with no width" do |
| 105 | + before(:each) do |
| 106 | + logger = VMDBLogger.new(@log) |
| 107 | + @contents = logger.contents(nil, 1000) |
| 108 | + end |
| 109 | + |
| 110 | + it "should have content but without the invalid utf8 lines" do |
| 111 | + expect(@contents).not_to be_nil |
| 112 | + expect(@contents.kind_of?(String)).to be_truthy |
| 113 | + end |
| 114 | + |
| 115 | + it "should unpack logger.consents as UTF-8 characters and raise nothing" do |
| 116 | + expect { @contents.unpack("U*") }.not_to raise_error |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + context "using VMDBLogger with a provided width" do |
| 121 | + before(:each) do |
| 122 | + logger = VMDBLogger.new(@log) |
| 123 | + @contents = logger.contents(120, 5000) |
| 124 | + end |
| 125 | + |
| 126 | + it "should have content but without the invalid utf8 lines" do |
| 127 | + expect(@contents).not_to be_nil |
| 128 | + expect(@contents.kind_of?(String)).to be_truthy |
| 129 | + end |
| 130 | + |
| 131 | + it "should unpack logger.consents as UTF-8 characters and raise nothing" do |
| 132 | + expect { @contents.unpack("U*") }.not_to raise_error |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + context "using VMDBLogger no limit on lines read" do |
| 137 | + before(:each) do |
| 138 | + logger = VMDBLogger.new(@log) |
| 139 | + @contents = logger.contents(120, nil) |
| 140 | + end |
| 141 | + |
| 142 | + it "should have content but without the invalid utf8 lines" do |
| 143 | + expect(@contents).not_to be_nil |
| 144 | + expect(@contents.kind_of?(String)).to be_truthy |
| 145 | + end |
| 146 | + |
| 147 | + it "should unpack logger.consents as UTF-8 characters and raise nothing" do |
| 148 | + expect { @contents.unpack("U*") }.not_to raise_error |
| 149 | + end |
| 150 | + end |
| 151 | + |
| 152 | + context "encoding" do |
| 153 | + it "with ascii file" do |
| 154 | + log = File.expand_path(File.join(File.dirname(__FILE__), "data/miq_ascii.log")) |
| 155 | + expect(VMDBLogger.new(log).contents.encoding.name).to eq("UTF-8") |
| 156 | + expect(VMDBLogger.new(log).contents(100, nil).encoding.name).to eq("UTF-8") |
| 157 | + end |
| 158 | + |
| 159 | + it "with utf-8 file" do |
| 160 | + log = File.expand_path(File.join(File.dirname(__FILE__), "data/miq_utf8.log")) |
| 161 | + expect(VMDBLogger.new(log).contents.encoding.name).to eq("UTF-8") |
| 162 | + expect(VMDBLogger.new(log).contents(100, nil).encoding.name).to eq("UTF-8") |
| 163 | + end |
| 164 | + end |
| 165 | + end |
| 166 | +end |
0 commit comments