lib/r509/config/subject_item_policy.rb in r509-0.10.0 vs lib/r509/config/subject_item_policy.rb in r509-1.0

- old
+ new

@@ -28,33 +28,32 @@ # "OU" => { :policy => "optional" }, # "ST" => { :policy => "required" }, # "C" => { :policy => "required" }, # "L" => { :policy => "match", :value => "Chicago" }, # "emailAddress" => { :policy => "optional" } - def initialize(hash={}) - if not hash.kind_of?(Hash) + def initialize(hash = {}) + unless hash.is_a?(Hash) raise ArgumentError, "Must supply a hash in form 'shortname'=>hash_with_policy_info" end @required = [] @optional = [] @match_values = {} @match = [] - if not hash.empty? - hash.each_pair do |key,value| - if not value.kind_of?(Hash) - raise ArgumentError, "Each value must be a hash with a :policy key" - end - case value[:policy] - when 'required' then @required.push(key) - when 'optional' then @optional.push(key) - when 'match' then - @match_values[key] = value[:value] - @match.push(key) - else - raise ArgumentError, "Unknown subject item policy value. Allowed values are required, optional, or match" - end + return if hash.empty? + hash.each_pair do |key, value| + unless value.is_a?(Hash) + raise ArgumentError, "Each value must be a hash with a :policy key" end + case value[:policy] + when 'required' then @required.push(key) + when 'optional' then @optional.push(key) + when 'match' then + @match_values[key] = value[:value] + @match.push(key) + else + raise ArgumentError, "Unknown subject item policy value. Allowed values are required, optional, or match" + end end end # @param [R509::Subject] subject # @return [R509::Subject] validated version of the subject or error @@ -64,29 +63,30 @@ validate_required_match(subject) # the validated subject contains only those subject components that are either # required, optional, or match R509::Subject.new(subject.to_a.select do |item| - @required.include?(item[0]) or @optional.include?(item[0]) or @match.include?(item[0]) + @required.include?(item[0]) || @optional.include?(item[0]) || @match.include?(item[0]) end) end - # @return [Hash] + # @return [Hash] def to_h hash = {} - @required.each { |r| hash[r] = {:policy => "required" } } - @optional.each { |o| hash[o] = {:policy => "optional" } } - @match.each { |m| hash[m] = {:policy => "match", :value => @match_values[m]} } + @required.each { |r| hash[r] = { :policy => "required" } } + @optional.each { |o| hash[o] = { :policy => "optional" } } + @match.each { |m| hash[m] = { :policy => "match", :value => @match_values[m] } } hash end - # @return [YAML] + # @return [YAML] def to_yaml self.to_h.to_yaml end private + # validates that the provided subject has the expected values for the # match policy def validate_match(subject) subject.to_a.each do |item| if @match.include?(item[0]) @@ -101,18 +101,15 @@ # subject item policy are present in the supplied subject def validate_required_match(subject) # convert the subject components into an array of component names that match # those that are on the required list supplied = subject.to_a.each do |item| - @required.include?(item[0]) or @match.include?(item[0]) - end.map do |item| - item[0] + @required.include?(item[0]) || @match.include?(item[0]) end + supplied = supplied.map { |item| item[0] } # so we can make sure they gave us everything that's required diff = @required + @match - supplied - if not diff.empty? - raise R509::R509Error, "This profile requires you supply "+(@required+@match).join(", ") - end + raise R509::R509Error, "This profile requires you supply " + (@required + @match).join(", ") unless diff.empty? end end end end