Skip to content

Commit 4f685f0

Browse files
committed
(PUP-10967) Debug logs clean up when resolving account SID on Windows
This commit restricts the `Could not retrieve raw SID bytes from` debug log to when an actual SID is passed to `ConvertStringSidToSidW`. The order in which the `Puppet::Util::Windows::SID.name_to_principal` method checks and tries to convert the input (be it an account name, domain qualified account name or SID) to a `Puppet::Util::Windows::SID::Principal` object made it print error messages to debug when it was actually succeeding in the end. Since every Puppet Agent run needs information about at least the currently logged on user, this was creating unnecessary noise in the debug logs.
1 parent 8e1ded9 commit 4f685f0

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

lib/puppet/util/windows/sid.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def name_to_principal(name, allow_unresolved = false)
7575
raw_sid_bytes = sid_ptr.read_array_of_uchar(get_length_sid(sid_ptr))
7676
end
7777
rescue => e
78-
Puppet.debug("Could not retrieve raw SID bytes from '#{name}': #{e.message}")
78+
Puppet.debug("Could not retrieve raw SID bytes from '#{name}': #{e.message}") if /^S-\d+-\d+/.match(name)
7979
end
8080

8181
raw_sid_bytes ? Principal.lookup_account_sid(raw_sid_bytes) : Principal.lookup_account_name(name)

spec/unit/util/windows/sid_spec.rb

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,38 +131,73 @@
131131
expect(subject.name_to_principal(unknown_name)).to be_nil
132132
end
133133

134+
it "should print a debug message if the account does not exist" do
135+
expect(Puppet).to receive(:debug).with(/No mapping between account names and security IDs was done/)
136+
subject.name_to_principal(unknown_name)
137+
end
138+
134139
it "should return a Puppet::Util::Windows::SID::Principal instance for any valid sid" do
135140
expect(subject.name_to_principal(sid)).to be_an_instance_of(Puppet::Util::Windows::SID::Principal)
136141
end
137142

143+
it "should not print debug messages for valid sid" do
144+
expect(Puppet).not_to receive(:debug).with(/Could not retrieve raw SID bytes from/)
145+
expect(Puppet).not_to receive(:debug).with(/No mapping between account names and security IDs was done/)
146+
subject.name_to_principal(sid)
147+
end
148+
149+
it "should print debug messages for invalid sid" do
150+
expect(Puppet).to receive(:debug).with(/Could not retrieve raw SID bytes from 'S-1-5-21-INVALID-SID'/)
151+
expect(Puppet).to receive(:debug).with(/No mapping between account names and security IDs was done/)
152+
subject.name_to_principal('S-1-5-21-INVALID-SID')
153+
end
154+
138155
it "should accept unqualified account name" do
139156
# NOTE: lookup by name works in localized environments only for a few instances
140157
# this works in French Windows, even though the account is really Syst\u00E8me
141158
expect(subject.name_to_principal('SYSTEM').sid).to eq(sid)
142159
end
143160

161+
it "should not print debug messages for unqualified account name" do
162+
expect(Puppet).not_to receive(:debug).with(/Could not retrieve raw SID bytes from/)
163+
expect(Puppet).not_to receive(:debug).with(/No mapping between account names and security IDs was done/)
164+
subject.name_to_principal('SYSTEM')
165+
end
166+
144167
it "should be case-insensitive" do
145168
# NOTE: lookup by name works in localized environments only for a few instances
146169
# this works in French Windows, even though the account is really Syst\u00E8me
147170
expect(subject.name_to_principal('SYSTEM')).to eq(subject.name_to_principal('system'))
148171
end
149172

173+
it "should not print debug messages for wrongly cased account name" do
174+
expect(Puppet).not_to receive(:debug).with(/Could not retrieve raw SID bytes from/)
175+
expect(Puppet).not_to receive(:debug).with(/No mapping between account names and security IDs was done/)
176+
subject.name_to_principal('system')
177+
end
178+
150179
it "should be leading and trailing whitespace-insensitive" do
151180
# NOTE: lookup by name works in localized environments only for a few instances
152181
# this works in French Windows, even though the account is really Syst\u00E8me
153182
expect(subject.name_to_principal('SYSTEM')).to eq(subject.name_to_principal(' SYSTEM '))
154183
end
155184

185+
it "should not print debug messages for account name with leading and trailing whitespace" do
186+
expect(Puppet).not_to receive(:debug).with(/Could not retrieve raw SID bytes from/)
187+
expect(Puppet).not_to receive(:debug).with(/No mapping between account names and security IDs was done/)
188+
subject.name_to_principal(' SYSTEM ')
189+
end
190+
156191
it "should accept domain qualified account names" do
157192
# NOTE: lookup by name works in localized environments only for a few instances
158193
# this works in French Windows, even though the account is really AUTORITE NT\\Syst\u00E8me
159194
expect(subject.name_to_principal('NT AUTHORITY\SYSTEM').sid).to eq(sid)
160195
end
161196

162-
it "should print a debug message on failures" do
163-
expect(Puppet).to receive(:debug).with(/Could not retrieve raw SID bytes from 'NonExistingUser'/)
164-
expect(Puppet).to receive(:debug).with(/No mapping between account names and security IDs was done/)
165-
subject.name_to_principal('NonExistingUser')
197+
it "should not print debug messages for domain qualified account names" do
198+
expect(Puppet).not_to receive(:debug).with(/Could not retrieve raw SID bytes from/)
199+
expect(Puppet).not_to receive(:debug).with(/No mapping between account names and security IDs was done/)
200+
subject.name_to_principal(' SYSTEM ')
166201
end
167202
end
168203

0 commit comments

Comments
 (0)