Skip to content

Commit

Permalink
(PUP-7326) Add ADSI / Principal localizations
Browse files Browse the repository at this point in the history
 - Add _() calls for localizable strings, so that it will be easier
   after the next merge (which will have a few easily resolved
   merge conflicts) to perform automated merge-ups
  • Loading branch information
Iristyle committed Mar 1, 2018
1 parent ed204ce commit 92191ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
26 changes: 13 additions & 13 deletions lib/puppet/util/windows/adsi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def connect(uri)
begin
WIN32OLE.connect(uri)
rescue WIN32OLERuntimeError => e
raise Puppet::Error.new( "ADSI connection error: #{e}", e )
raise Puppet::Error.new( _("ADSI connection error: %{e}") % { e: e }, e )
end
end

Expand All @@ -39,7 +39,7 @@ def computer_name
buffer_size.write_dword(max_length) # length in TCHARs

if GetComputerNameW(buffer, buffer_size) == FFI::WIN32_FALSE
raise Puppet::Util::Windows::Error.new("Failed to get computer name")
raise Puppet::Util::Windows::Error.new(_("Failed to get computer name"))
end
@computer_name = buffer.read_wide_string(buffer_size.read_dword)
end
Expand Down Expand Up @@ -77,7 +77,7 @@ def sid_uri_safe(sid)
# used for IAdsGroup::Add / IAdsGroup::Remove. These URIs are not useable
# to resolve an account with WIN32OLE.connect
def sid_uri(sid)
raise Puppet::Error.new( "Must use a valid SID::Principal" ) if !sid.kind_of?(Puppet::Util::Windows::SID::Principal)
raise Puppet::Error.new( _("Must use a valid SID::Principal") ) if !sid.kind_of?(Puppet::Util::Windows::SID::Principal)

"WinNT://#{sid.sid}"
end
Expand Down Expand Up @@ -129,7 +129,7 @@ def uri(name, host = '.')

def parse_name(name)
if name =~ /\//
raise Puppet::Error.new( "Value must be in DOMAIN\\user style syntax" )
raise Puppet::Error.new( _("Value must be in DOMAIN\\user style syntax") )
end

matches = name.scan(/((.*)\\)?(.*)/)
Expand All @@ -155,7 +155,7 @@ def name_sid_hash(names)

sids = names.map do |name|
sid = Puppet::Util::Windows::SID.name_to_principal(name)
raise Puppet::Error.new( "Could not resolve name: #{name}" ) if !sid
raise Puppet::Error.new( _("Could not resolve name: %{name}") % { name: name } ) if !sid
[sid.sid, sid]
end

Expand Down Expand Up @@ -213,12 +213,12 @@ def commit
# ERROR_BAD_USERNAME 2202L from winerror.h
if e.message =~ /8007089A/m
raise Puppet::Error.new(
"Puppet is not able to create/delete domain users with the user resource.",
_("Puppet is not able to create/delete domain users with the user resource."),
e
)
end

raise Puppet::Error.new( "User update failed: #{e}", e )
raise Puppet::Error.new( _("User update failed: %{e}") % { e: e }, e )
end
self
end
Expand Down Expand Up @@ -313,7 +313,7 @@ def set_groups(desired_groups, minimum = true)

def self.create(name)
# Windows error 1379: The specified local group already exists.
raise Puppet::Error.new( "Cannot create user if group '#{name}' exists." ) if Puppet::Util::Windows::ADSI::Group.exists? name
raise Puppet::Error.new(_("Cannot create user if group '%{name}' exists.") % { name: name }) if Puppet::Util::Windows::ADSI::Group.exists? name
new(name, Puppet::Util::Windows::ADSI.create(name, 'user'))
end

Expand All @@ -327,7 +327,7 @@ def self.current_user_name
buffer_size.write_dword(max_length) # length in TCHARs

if GetUserNameW(buffer, buffer_size) == FFI::WIN32_FALSE
raise Puppet::Util::Windows::Error.new("Failed to get user name")
raise Puppet::Util::Windows::Error.new(_("Failed to get user name"))
end
# buffer_size includes trailing NULL
user_name = buffer.read_wide_string(buffer_size.read_dword - 1)
Expand Down Expand Up @@ -405,7 +405,7 @@ def self.delete(sid)
# but warn if we fail
raise e unless e.message.include?('80041010')

Puppet.warning "Cannot delete user profile for '#{sid}' prior to Vista SP1"
Puppet.warning _("Cannot delete user profile for '%{sid}' prior to Vista SP1") % { sid: sid }
end
end
end
Expand Down Expand Up @@ -443,12 +443,12 @@ def commit
# ERROR_BAD_USERNAME 2202L from winerror.h
if e.message =~ /8007089A/m
raise Puppet::Error.new(
"Puppet is not able to create/delete domain groups with the group resource.",
_("Puppet is not able to create/delete domain groups with the group resource."),
e
)
end

raise Puppet::Error.new( "Group update failed: #{e}", e )
raise Puppet::Error.new( _("Group update failed: %{error}") % { error: e }, e )
end
self
end
Expand Down Expand Up @@ -499,7 +499,7 @@ def set_members(desired_members, inclusive = true)

def self.create(name)
# Windows error 2224: The account already exists.
raise Puppet::Error.new( "Cannot create group if user '#{name}' exists." ) if Puppet::Util::Windows::ADSI::User.exists? name
raise Puppet::Error.new( _("Cannot create group if user '%{name}' exists.") % { name: name } ) if Puppet::Util::Windows::ADSI::User.exists? name
new(name, Puppet::Util::Windows::ADSI.create(name, 'group'))
end

Expand Down
11 changes: 6 additions & 5 deletions lib/puppet/util/windows/principal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ def self.lookup_account_name(system_name = nil, account_name)
last_error = FFI.errno

if (success == FFI::WIN32_FALSE && last_error != ERROR_INSUFFICIENT_BUFFER)
raise Puppet::Util::Windows::Error.new("Failed to call LookupAccountNameW with account: #{account_name}", last_error)
raise Puppet::Util::Windows::Error.new(_('Failed to call LookupAccountNameW with account: %{account_name}') % { account_name: account_name}, last_error)
end

FFI::MemoryPointer.new(:lpwstr, domain_length_ptr.read_dword) do |domain_ptr|
if LookupAccountNameW(system_name_ptr, account_name_ptr,
sid_ptr, sid_length_ptr,
domain_ptr, domain_length_ptr, name_use_enum_ptr) == FFI::WIN32_FALSE
raise Puppet::Util::Windows::Error.new("Failed to call LookupAccountNameW with account: #{account_name}")
raise Puppet::Util::Windows::Error.new(_('Failed to call LookupAccountNameW with account: %{account_name}') % { account_name: account_name} )
end

# with a SID returned, loop back through lookup_account_sid to retrieve official name
Expand All @@ -94,7 +94,8 @@ def self.lookup_account_name(system_name = nil, account_name)
def self.lookup_account_sid(system_name = nil, sid_bytes)
system_name_ptr = FFI::Pointer::NULL
if (sid_bytes.nil? || (!sid_bytes.is_a? Array) || (sid_bytes.length == 0))
raise Puppet::Util::Windows::Error.new('Byte array for lookup_account_sid must not be nil and must be at least 1 byte long')
#TRANSLATORS `lookup_account_sid` is a variable name and should not be translated
raise Puppet::Util::Windows::Error.new(_('Byte array for lookup_account_sid must not be nil and must be at least 1 byte long'))
end

begin
Expand All @@ -116,14 +117,14 @@ def self.lookup_account_sid(system_name = nil, sid_bytes)
last_error = FFI.errno

if (success == FFI::WIN32_FALSE && last_error != ERROR_INSUFFICIENT_BUFFER)
raise Puppet::Util::Windows::Error.new("Failed to call LookupAccountSidW with bytes: #{sid_bytes}", last_error)
raise Puppet::Util::Windows::Error.new(_('Failed to call LookupAccountSidW with bytes: %{sid_bytes}') % { sid_bytes: sid_bytes}, last_error)
end

FFI::MemoryPointer.new(:lpwstr, name_length_ptr.read_dword) do |name_ptr|
FFI::MemoryPointer.new(:lpwstr, domain_length_ptr.read_dword) do |domain_ptr|
if LookupAccountSidW(system_name_ptr, sid_ptr, name_ptr, name_length_ptr,
domain_ptr, domain_length_ptr, name_use_enum_ptr) == FFI::WIN32_FALSE
raise Puppet::Util::Windows::Error.new("Failed to call LookupAccountSidW with bytes: #{sid_bytes}")
raise Puppet::Util::Windows::Error.new(_('Failed to call LookupAccountSidW with bytes: %{sid_bytes}') % { sid_bytes: sid_bytes} )
end

return new(
Expand Down

0 comments on commit 92191ac

Please sign in to comment.