Skip to content

Commit 31515a6

Browse files
authored
Merge pull request #8652 from luchihoratiu/PUP-10967
(PUP-10967) Debug logs clean up when resolving account SID on Windows
2 parents 8e1ded9 + 658b3f0 commit 31515a6

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

lib/puppet/util/windows/sid.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ 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+
# Avoid debug logs pollution with valid account names
79+
# https://docs.microsoft.com/en-us/windows/win32/api/sddl/nf-sddl-convertstringsidtosidw#return-value
80+
Puppet.debug("Could not retrieve raw SID bytes from '#{name}': #{e.message}") unless e.code == ERROR_INVALID_SID_STRUCTURE
7981
end
8082

8183
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 a debug message for invalid sid" do
150+
expect(Puppet).not_to receive(:debug).with(/Could not retrieve raw SID bytes from/)
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('NT AUTHORITY\SYSTEM')
166201
end
167202
end
168203

0 commit comments

Comments
 (0)