Skip to content

Commit 5773f66

Browse files
Merge pull request #254 from reidmv/fix-munge-validate
Add possibility to produce a detailed error message
2 parents b4cbcd7 + 0465a40 commit 5773f66

File tree

1 file changed

+48
-25
lines changed

1 file changed

+48
-25
lines changed

lib/puppet/type/registry_value.rb

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,47 +94,61 @@ def self.title_patterns
9494
@doc = <<-EOT
9595
The data stored in the registry value.
9696
EOT
97-
defaultto ''
97+
98+
# We probably shouldn't set default values for this property at all. For
99+
# dword and qword specifically, the legacy default value will not pass
100+
# validation. As such, no default value will be set for those types. At
101+
# least for now, other types will still have the legacy empty-string
102+
# default value.
103+
defaultto { [:dword, :qword].include?(resource[:type]) ? nil : '' }
98104

99105
validate do |value|
100106
case resource[:type]
101107
when :array
102108
raise('An array registry value can not contain empty values') if value.empty?
103-
else
109+
when :dword
110+
munged = munge(value)
111+
unless munged && (munged.abs >> 32) <= 0
112+
raise("The data must be a valid DWORD: received '#{value}'")
113+
end
114+
when :qword
115+
munged = munge(value)
116+
unless munged && (munged.abs >> 64) <= 0
117+
raise("The data must be a valid QWORD: received '#{value}'")
118+
end
119+
when :binary
120+
munged = munge(value)
121+
unless munged =~ %r{^([a-f\d]{2} ?)+$}i || value.empty?
122+
raise("The data must be a hex encoded string of the form: '00 01 02 ...': received '#{value}'")
123+
end
124+
else #:string, :expand, :array
104125
true
105126
end
106127
end
107128

108129
munge do |value|
109130
case resource[:type]
110-
when :dword
111-
val = begin
112-
Integer(value)
113-
rescue
114-
nil
115-
end
116-
raise("The data must be a valid DWORD: #{value}") unless val && (val.abs >> 32) <= 0
117-
val
118-
when :qword
119-
val = begin
120-
Integer(value)
121-
rescue
122-
nil
123-
end
124-
raise("The data must be a valid QWORD: #{value}") unless val && (val.abs >> 64) <= 0
125-
val
126-
when :binary
127-
if (value.respond_to?(:length) && value.length == 1) || (value.is_a?(Integer) && value <= 9)
128-
value = "0#{value}"
129-
end
130-
unless value =~ %r{^([a-f\d]{2} ?)*$}i
131-
raise("The data must be a hex encoded string of the form: '00 01 02 ...'")
131+
when :dword, :qword
132+
begin
133+
Integer(value)
134+
rescue
135+
nil
132136
end
137+
when :binary
138+
munged = if (value.respond_to?(:length) && value.length == 1) || (value.is_a?(Integer) && value <= 9)
139+
"0#{value}"
140+
else
141+
value
142+
end
143+
133144
# First, strip out all spaces from the string in the manfest. Next,
134145
# put a space after each pair of hex digits. Strip off the rightmost
135146
# space if it's present. Finally, downcase the whole thing. The final
136147
# result should be: "CaFE BEEF" => "ca fe be ef"
137-
value.gsub(%r{\s+}, '').gsub(%r{([0-9a-f]{2})}i) { "#{Regexp.last_match(1)} " }.rstrip.downcase
148+
munged.gsub(%r{\s+}, '')
149+
.gsub(%r{([0-9a-f]{2})}i) { "#{Regexp.last_match(1)} " }
150+
.rstrip
151+
.downcase
138152
else #:string, :expand, :array
139153
value
140154
end
@@ -161,6 +175,15 @@ def change_to_s(currentvalue, newvalue)
161175
end
162176
end
163177

178+
validate do
179+
# To ensure consistent behavior, always require a value for the data
180+
# property. This validation can be removed if we remove the default value
181+
# for the data property, for all data types.
182+
if property(:data).nil?
183+
raise ArgumentError, "No value supplied for required property 'data'"
184+
end
185+
end
186+
164187
# Autorequire the nearest ancestor registry_key found in the catalog.
165188
autorequire(:registry_key) do
166189
req = []

0 commit comments

Comments
 (0)