Skip to content

Commit

Permalink
Merge pull request #9119 from cthorn42/maint/main/PUP-11938_handle_wi…
Browse files Browse the repository at this point in the history
…ndows_sid_domain_failures

(PUP-11938) Handle more errors around Windows SID and ASID
  • Loading branch information
joshcooper committed Oct 9, 2023
2 parents 80226e9 + 9d6c9c9 commit 3d7a59c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/puppet/util/windows/adsi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ def get_sids(adsi_child_collection)
sids = []
adsi_child_collection.each do |m|
sids << Puppet::Util::Windows::SID.ads_to_principal(m)
rescue Puppet::Util::Windows::Error => e
case e.code
when Puppet::Util::Windows::SID::ERROR_TRUSTED_RELATIONSHIP_FAILURE, Puppet::Util::Windows::SID::ERROR_TRUSTED_DOMAIN_FAILURE
sids << Puppet::Util::Windows::SID.unresolved_principal(m.name, m.sid)
else
raise e
end
end

sids
Expand Down
6 changes: 4 additions & 2 deletions lib/puppet/util/windows/sid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ module SID
extend FFI::Library

# missing from Windows::Error
ERROR_NONE_MAPPED = 1332
ERROR_INVALID_SID_STRUCTURE = 1337
ERROR_NONE_MAPPED = 1332
ERROR_INVALID_SID_STRUCTURE = 1337
ERROR_TRUSTED_DOMAIN_FAILURE = 1788
ERROR_TRUSTED_RELATIONSHIP_FAILURE = 1789

# Well Known SIDs
Null = 'S-1-0'
Expand Down
25 changes: 25 additions & 0 deletions spec/unit/util/windows/adsi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@
end
end

describe '.get_sids' do
it 'returns an array of SIDs given two an array of ADSI children' do
child1 = double('child1', name: 'Administrator', sid: 'S-1-5-21-3882680660-671291151-3888264257-500')
child2 = double('child2', name: 'Guest', sid: 'S-1-5-21-3882680660-671291151-3888264257-501')
allow(Puppet::Util::Windows::SID).to receive(:ads_to_principal).with(child1).and_return('Administrator')
allow(Puppet::Util::Windows::SID).to receive(:ads_to_principal).with(child2).and_return('Guest')
sids = Puppet::Util::Windows::ADSI::ADSIObject.get_sids([child1, child2])
expect(sids).to eq(['Administrator', 'Guest'])
end

it 'returns an array of SIDs given an ADSI child and ads_to_principal returning domain failure' do
child = double('child1', name: 'Administrator', sid: 'S-1-5-21-3882680660-671291151-3888264257-500')
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))
sids = Puppet::Util::Windows::ADSI::ADSIObject.get_sids([child])
expect(sids[0]).to eq(Puppet::Util::Windows::SID::Principal.new(child.name, child.sid, child.name, nil, :SidTypeUnknown))
end

it 'returns an array of SIDs given an ADSI child and ads_to_principal returning relationship failure' do
child = double('child1', name: 'Administrator', sid: 'S-1-5-21-3882680660-671291151-3888264257-500')
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))
sids = Puppet::Util::Windows::ADSI::ADSIObject.get_sids([child])
expect(sids[0]).to eq(Puppet::Util::Windows::SID::Principal.new(child.name, child.sid, child.name, nil, :SidTypeUnknown))
end
end

describe Puppet::Util::Windows::ADSI::User do
let(:username) { 'testuser' }
let(:domain) { 'DOMAIN' }
Expand Down

0 comments on commit 3d7a59c

Please sign in to comment.