Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to produce a detailed error message #254

Merged
merged 3 commits into from
Aug 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 48 additions & 25 deletions lib/puppet/type/registry_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,47 +94,61 @@ def self.title_patterns
@doc = <<-EOT
The data stored in the registry value.
EOT
defaultto ''

# We probably shouldn't set default values for this property at all. For
# dword and qword specifically, the legacy default value will not pass
# validation. As such, no default value will be set for those types. At
# least for now, other types will still have the legacy empty-string
# default value.
defaultto { [:dword, :qword].include?(resource[:type]) ? nil : '' }

validate do |value|
case resource[:type]
when :array
raise('An array registry value can not contain empty values') if value.empty?
else
when :dword
munged = munge(value)
unless munged && (munged.abs >> 32) <= 0
raise("The data must be a valid DWORD: received '#{value}'")
end
when :qword
munged = munge(value)
unless munged && (munged.abs >> 64) <= 0
raise("The data must be a valid QWORD: received '#{value}'")
end
when :binary
munged = munge(value)
unless munged =~ %r{^([a-f\d]{2} ?)+$}i || value.empty?
raise("The data must be a hex encoded string of the form: '00 01 02 ...': received '#{value}'")
end
else #:string, :expand, :array
true
end
end

munge do |value|
case resource[:type]
when :dword
val = begin
Integer(value)
rescue
nil
end
raise("The data must be a valid DWORD: #{value}") unless val && (val.abs >> 32) <= 0
val
when :qword
val = begin
Integer(value)
rescue
nil
end
raise("The data must be a valid QWORD: #{value}") unless val && (val.abs >> 64) <= 0
val
when :binary
if (value.respond_to?(:length) && value.length == 1) || (value.is_a?(Integer) && value <= 9)
value = "0#{value}"
end
unless value =~ %r{^([a-f\d]{2} ?)*$}i
raise("The data must be a hex encoded string of the form: '00 01 02 ...'")
when :dword, :qword
begin
Integer(value)
rescue
nil
end
when :binary
munged = if (value.respond_to?(:length) && value.length == 1) || (value.is_a?(Integer) && value <= 9)
"0#{value}"
else
value
end

# First, strip out all spaces from the string in the manfest. Next,
# put a space after each pair of hex digits. Strip off the rightmost
# space if it's present. Finally, downcase the whole thing. The final
# result should be: "CaFE BEEF" => "ca fe be ef"
value.gsub(%r{\s+}, '').gsub(%r{([0-9a-f]{2})}i) { "#{Regexp.last_match(1)} " }.rstrip.downcase
munged.gsub(%r{\s+}, '')
.gsub(%r{([0-9a-f]{2})}i) { "#{Regexp.last_match(1)} " }
.rstrip
.downcase
else #:string, :expand, :array
value
end
Expand All @@ -161,6 +175,15 @@ def change_to_s(currentvalue, newvalue)
end
end

validate do
# To ensure consistent behavior, always require a value for the data
# property. This validation can be removed if we remove the default value
# for the data property, for all data types.
if property(:data).nil?
raise ArgumentError, "No value supplied for required property 'data'"
end
end

# Autorequire the nearest ancestor registry_key found in the catalog.
autorequire(:registry_key) do
req = []
Expand Down