Skip to content

Commit d835ef6

Browse files
authored
Merge pull request #9121 from cthorn42/maint/7.x/cherry-pick-PUP-11938
(PUP-11938) Handle more errors around Windows SID and ASID
2 parents 8da0fa2 + fc88443 commit d835ef6

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

lib/puppet/util/windows/adsi.rb

+7
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ def get_sids(adsi_child_collection)
175175
sids = []
176176
adsi_child_collection.each do |m|
177177
sids << Puppet::Util::Windows::SID.ads_to_principal(m)
178+
rescue Puppet::Util::Windows::Error => e
179+
case e.code
180+
when Puppet::Util::Windows::SID::ERROR_TRUSTED_RELATIONSHIP_FAILURE, Puppet::Util::Windows::SID::ERROR_TRUSTED_DOMAIN_FAILURE
181+
sids << Puppet::Util::Windows::SID.unresolved_principal(m.name, m.sid)
182+
else
183+
raise e
184+
end
178185
end
179186

180187
sids

lib/puppet/util/windows/sid.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ module SID
66
extend FFI::Library
77

88
# missing from Windows::Error
9-
ERROR_NONE_MAPPED = 1332
10-
ERROR_INVALID_SID_STRUCTURE = 1337
9+
ERROR_NONE_MAPPED = 1332
10+
ERROR_INVALID_SID_STRUCTURE = 1337
11+
ERROR_TRUSTED_DOMAIN_FAILURE = 1788
12+
ERROR_TRUSTED_RELATIONSHIP_FAILURE = 1789
1113

1214
# Well Known SIDs
1315
Null = 'S-1-0'

spec/unit/util/windows/adsi_spec.rb

+25
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,31 @@
9595
end
9696
end
9797

98+
describe '.get_sids' do
99+
it 'returns an array of SIDs given two an array of ADSI children' do
100+
child1 = double('child1', name: 'Administrator', sid: 'S-1-5-21-3882680660-671291151-3888264257-500')
101+
child2 = double('child2', name: 'Guest', sid: 'S-1-5-21-3882680660-671291151-3888264257-501')
102+
allow(Puppet::Util::Windows::SID).to receive(:ads_to_principal).with(child1).and_return('Administrator')
103+
allow(Puppet::Util::Windows::SID).to receive(:ads_to_principal).with(child2).and_return('Guest')
104+
sids = Puppet::Util::Windows::ADSI::ADSIObject.get_sids([child1, child2])
105+
expect(sids).to eq(['Administrator', 'Guest'])
106+
end
107+
108+
it 'returns an array of SIDs given an ADSI child and ads_to_principal returning domain failure' do
109+
child = double('child1', name: 'Administrator', sid: 'S-1-5-21-3882680660-671291151-3888264257-500')
110+
allow(Puppet::Util::Windows::SID).to receive(:ads_to_principal).with(child).and_raise(Puppet::Util::Windows::Error.new('', Puppet::Util::Windows::SID::ERROR_TRUSTED_DOMAIN_FAILURE))
111+
sids = Puppet::Util::Windows::ADSI::ADSIObject.get_sids([child])
112+
expect(sids[0]).to eq(Puppet::Util::Windows::SID::Principal.new(child.name, child.sid, child.name, nil, :SidTypeUnknown))
113+
end
114+
115+
it 'returns an array of SIDs given an ADSI child and ads_to_principal returning relationship failure' do
116+
child = double('child1', name: 'Administrator', sid: 'S-1-5-21-3882680660-671291151-3888264257-500')
117+
allow(Puppet::Util::Windows::SID).to receive(:ads_to_principal).with(child).and_raise(Puppet::Util::Windows::Error.new('', Puppet::Util::Windows::SID::ERROR_TRUSTED_RELATIONSHIP_FAILURE))
118+
sids = Puppet::Util::Windows::ADSI::ADSIObject.get_sids([child])
119+
expect(sids[0]).to eq(Puppet::Util::Windows::SID::Principal.new(child.name, child.sid, child.name, nil, :SidTypeUnknown))
120+
end
121+
end
122+
98123
describe Puppet::Util::Windows::ADSI::User do
99124
let(:username) { 'testuser' }
100125
let(:domain) { 'DOMAIN' }

0 commit comments

Comments
 (0)