summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Wrock <matt@mattwrock.com>2016-02-03 11:55:20 -0800
committerMatt Wrock <matt@mattwrock.com>2016-02-03 11:55:20 -0800
commit0f4a623172676d010782d880a3bc7a9da084d25d (patch)
treefbb761dce55d6b130a93e409379ce302b3064e47
parent09227432c7a8afeac633023abbeec2e9c14cbd1b (diff)
parentb74d0028914b03bce785b940f0cad5390444ae3a (diff)
downloadchef-0f4a623172676d010782d880a3bc7a9da084d25d.tar.gz
Merge pull request #4516 from chef/invalid
fixes #4515 and use FormatMessage to get correct error messages from native API instead of retyping them with our fingers
-rw-r--r--lib/chef/exceptions.rb16
-rw-r--r--lib/chef/util/windows/net_group.rb24
-rw-r--r--lib/chef/win32/api/error.rb1
-rw-r--r--lib/chef/win32/api/net.rb10
-rw-r--r--lib/chef/win32/error.rb18
-rw-r--r--lib/chef/win32/net.rb63
-rw-r--r--spec/functional/resource/group_spec.rb2
7 files changed, 40 insertions, 94 deletions
diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb
index 6a0cdb9b95..9a2e9ae3a3 100644
--- a/lib/chef/exceptions.rb
+++ b/lib/chef/exceptions.rb
@@ -138,22 +138,6 @@ class Chef
# Errors originating from calls to the Win32 API
class Win32APIError < RuntimeError; end
- class Win32NetAPIError < Win32APIError
- attr_reader :msg, :error_code
- def initialize(msg, error_code)
- @msg = msg
- @error_code = error_code
-
- formatted_message = ""
- formatted_message << "---- Begin Win32 API output ----\n"
- formatted_message << "Net Api Error Code: #{error_code}\n"
- formatted_message << "Net Api Error Message: #{msg}\n"
- formatted_message << "---- End Win32 API output ----\n"
-
- super(formatted_message)
- end
- end
-
# Thrown when Win32 API layer binds to non-existent Win32 function. Occurs
# when older versions of Windows don't support newer Win32 API functions.
class Win32APIFunctionNotImplemented < NotImplementedError; end
diff --git a/lib/chef/util/windows/net_group.rb b/lib/chef/util/windows/net_group.rb
index f1c510591e..cfb06ed1f1 100644
--- a/lib/chef/util/windows/net_group.rb
+++ b/lib/chef/util/windows/net_group.rb
@@ -37,40 +37,40 @@ class Chef::Util::Windows::NetGroup
def local_get_members
begin
Chef::ReservedNames::Win32::NetUser::net_local_group_get_members(nil, groupname)
- rescue Chef::Exceptions::Win32NetAPIError => e
- raise ArgumentError, e.msg
+ rescue Chef::Exceptions::Win32APIError => e
+ raise ArgumentError, e
end
end
def local_add
begin
Chef::ReservedNames::Win32::NetUser::net_local_group_add(nil, groupname)
- rescue Chef::Exceptions::Win32NetAPIError => e
- raise ArgumentError, e.msg
+ rescue Chef::Exceptions::Win32APIError => e
+ raise ArgumentError, e
end
end
def local_set_members(members)
begin
Chef::ReservedNames::Win32::NetUser::net_local_group_set_members(nil, groupname, members)
- rescue Chef::Exceptions::Win32NetAPIError => e
- raise ArgumentError, e.msg
+ rescue Chef::Exceptions::Win32APIError => e
+ raise ArgumentError, e
end
end
def local_add_members(members)
begin
Chef::ReservedNames::Win32::NetUser::net_local_group_add_members(nil, groupname, members)
- rescue Chef::Exceptions::Win32NetAPIError => e
- raise ArgumentError, e.msg
+ rescue Chef::Exceptions::Win32APIError => e
+ raise ArgumentError, e
end
end
def local_delete_members(members)
begin
Chef::ReservedNames::Win32::NetUser::net_local_group_del_members(nil, groupname, members)
- rescue Chef::Exceptions::Win32NetAPIError => e
- raise ArgumentError, e.msg
+ rescue Chef::Exceptions::Win32APIError => e
+ raise ArgumentError, e
end
end
@@ -78,8 +78,8 @@ class Chef::Util::Windows::NetGroup
def local_delete
begin
Chef::ReservedNames::Win32::NetUser::net_local_group_del(nil, groupname)
- rescue Chef::Exceptions::Win32NetAPIError => e
- raise ArgumentError, e.msg
+ rescue Chef::Exceptions::Win32APIError => e
+ raise ArgumentError, e
end
end
end
diff --git a/lib/chef/win32/api/error.rb b/lib/chef/win32/api/error.rb
index ecc1625353..0b10fc6e3d 100644
--- a/lib/chef/win32/api/error.rb
+++ b/lib/chef/win32/api/error.rb
@@ -848,6 +848,7 @@ class Chef
ERROR_INVALID_COLORINDEX = 2022
ERROR_CONNECTED_OTHER_PASSWORD = 2108
ERROR_BAD_USERNAME = 2202
+ ERROR_USER_NOT_FOUND = 2221
ERROR_NOT_CONNECTED = 2250
ERROR_OPEN_FILES = 2401
ERROR_ACTIVE_CONNECTIONS = 2402
diff --git a/lib/chef/win32/api/net.rb b/lib/chef/win32/api/net.rb
index 848499b403..fdbe55579f 100644
--- a/lib/chef/win32/api/net.rb
+++ b/lib/chef/win32/api/net.rb
@@ -46,16 +46,6 @@ class Chef
USE_LOTS_OF_FORCE = 2 #every windows API should support this flag
NERR_Success = 0
- NERR_InvalidComputer = 2351
- NERR_NotPrimary = 2226
- NERR_SpeGroupOp = 2234
- NERR_LastAdmin = 2452
- NERR_BadUsername = 2202
- NERR_BadPassword = 2203
- NERR_PasswordTooShort = 2245
- NERR_UserNotFound = 2221
- NERR_GroupNotFound = 2220
- ERROR_ACCESS_DENIED = 5
ERROR_MORE_DATA = 234
ffi_lib "netapi32"
diff --git a/lib/chef/win32/error.rb b/lib/chef/win32/error.rb
index 1b3c104e85..8c3ff2f08f 100644
--- a/lib/chef/win32/error.rb
+++ b/lib/chef/win32/error.rb
@@ -59,13 +59,17 @@ class Chef
# Chef::Exceptions::Win32APIError:::
def self.raise!(message = nil, code = get_last_error)
msg = format_message(code).strip
- formatted_message = ""
- formatted_message << message if message
- formatted_message << "---- Begin Win32 API output ----\n"
- formatted_message << "System Error Code: #{code}\n"
- formatted_message << "System Error Message: #{msg}\n"
- formatted_message << "---- End Win32 API output ----\n"
- raise Chef::Exceptions::Win32APIError, msg + "\n" + formatted_message
+ if code == ERROR_USER_NOT_FOUND
+ raise Chef::Exceptions::UserIDNotFound, msg
+ else
+ formatted_message = ""
+ formatted_message << message if message
+ formatted_message << "---- Begin Win32 API output ----\n"
+ formatted_message << "System Error Code: #{code}\n"
+ formatted_message << "System Error Message: #{msg}\n"
+ formatted_message << "---- End Win32 API output ----\n"
+ raise Chef::Exceptions::Win32APIError, msg + "\n" + formatted_message
+ end
end
end
end
diff --git a/lib/chef/win32/net.rb b/lib/chef/win32/net.rb
index 62eb869195..4ee278aa28 100644
--- a/lib/chef/win32/net.rb
+++ b/lib/chef/win32/net.rb
@@ -69,39 +69,6 @@ class Chef
end
end
- def self.net_api_error!(code)
- msg = case code
- when NERR_InvalidComputer
- "The user does not have access to the requested information."
- when NERR_NotPrimary
- "The operation is allowed only on the primary domain controller of the domain."
- when NERR_SpeGroupOp
- "This operation is not allowed on this special group."
- when NERR_LastAdmin
- "This operation is not allowed on the last administrative account."
- when NERR_BadUsername
- "The user name or group name parameter is invalid."
- when NERR_BadPassword
- "The password parameter is invalid."
- when NERR_UserNotFound
- raise Chef::Exceptions::UserIDNotFound, code
- when NERR_PasswordTooShort
- <<END
-The password is shorter than required. (The password could also be too
-long, be too recent in its change history, not have enough unique characters,
-or not meet another password policy requirement.)
-END
- when NERR_GroupNotFound
- "The group name could not be found."
- when ERROR_ACCESS_DENIED
- "The user does not have access to the requested information."
- else
- "Received unknown error code (#{code})"
- end
-
- raise Chef::Exceptions::Win32NetAPIError.new(msg, code)
- end
-
def self.net_local_group_add(server_name, group_name)
server_name = wstring(server_name)
group_name = wstring(group_name)
@@ -111,7 +78,7 @@ END
rc = NetLocalGroupAdd(server_name, 0, buf, nil)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
end
@@ -121,7 +88,7 @@ END
rc = NetLocalGroupDel(server_name, group_name)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
end
@@ -153,7 +120,7 @@ END
end
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
group_members
@@ -170,7 +137,7 @@ END
rc = NetUserAdd(server_name, 3, buf, nil)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
end
@@ -183,7 +150,7 @@ END
rc = NetUserGetInfo(server_name, user_name, 3, ui3_p)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
ui3 = USER_INFO_3.new(ui3_p.read_pointer).as_ruby
@@ -191,7 +158,7 @@ END
rc = NetApiBufferFree(ui3_p.read_pointer)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
ui3
@@ -209,7 +176,7 @@ END
rc = NetUserSetInfo(server_name, user_name, 3, buf, nil)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
end
@@ -219,7 +186,7 @@ END
rc = NetUserDel(server_name, user_name)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
end
@@ -234,7 +201,7 @@ END
rc = NetLocalGroupAddMembers(server_name, group_name, 3, buf, 1)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
end
@@ -257,7 +224,7 @@ END
server_name, group_name, 3, lgrmi3s[0], members.size)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
end
@@ -270,7 +237,7 @@ END
server_name, group_name, 3, lgrmi3s[0], members.size)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
end
@@ -283,7 +250,7 @@ END
server_name, group_name, 3, lgrmi3s[0], members.size)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
end
@@ -303,7 +270,7 @@ END
rc = NetUseDel(server_name, use_name, force_const)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
end
@@ -314,7 +281,7 @@ END
rc = NetUseGetInfo(server_name, use_name, 2, ui2_p)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
ui2 = USE_INFO_2.new(ui2_p.read_pointer).as_ruby
@@ -335,7 +302,7 @@ END
rc = NetUseAdd(server_name, 2, buf, nil)
if rc != NERR_Success
- net_api_error!(rc)
+ Chef::ReservedNames::Win32::Error.raise!(nil, rc)
end
end
end
diff --git a/spec/functional/resource/group_spec.rb b/spec/functional/resource/group_spec.rb
index 8d52975bad..cb460fe7b4 100644
--- a/spec/functional/resource/group_spec.rb
+++ b/spec/functional/resource/group_spec.rb
@@ -58,7 +58,7 @@ describe Chef::Resource::Group, :requires_root_or_running_windows, :not_supporte
when "debian", "fedora", "rhel", "suse", "gentoo", "slackware", "arch"
expect { Etc::getgrnam(group) }.to raise_error(ArgumentError, "can't find group for #{group}")
when "windows"
- expect { Chef::Util::Windows::NetGroup.new(group).local_get_members }.to raise_error(ArgumentError, "The group name could not be found.")
+ expect { Chef::Util::Windows::NetGroup.new(group).local_get_members }.to raise_error(ArgumentError, /The group name could not be found./)
end
end