mirror of
https://github.com/inspec/inspec
synced 2025-02-17 06:28:40 +00:00
Initial commit for regex pattern option in inputs
Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>
This commit is contained in:
parent
6e55c9ed90
commit
85ca124809
2 changed files with 47 additions and 7 deletions
|
@ -19,12 +19,17 @@ module Inspec
|
|||
attr_accessor :input_name
|
||||
attr_accessor :input_value
|
||||
attr_accessor :input_type
|
||||
attr_accessor :input_pattern
|
||||
end
|
||||
|
||||
class TypeError < Error
|
||||
attr_accessor :input_type
|
||||
end
|
||||
|
||||
class PatternError < Error
|
||||
attr_accessor :input_pattern
|
||||
end
|
||||
|
||||
class RequiredError < Error
|
||||
attr_accessor :input_name
|
||||
end
|
||||
|
@ -45,6 +50,7 @@ module Inspec
|
|||
:file, # File containing the input-changing action, if known
|
||||
:line, # Line in file containing the input-changing action, if known
|
||||
:hit, # if action is :fetch, true if the remote source had the input
|
||||
:pattern, # Regex Pattern to validate input value
|
||||
].freeze
|
||||
|
||||
# Value has a special handler
|
||||
|
@ -56,7 +62,6 @@ module Inspec
|
|||
|
||||
def initialize(properties = {})
|
||||
@value_has_been_set = false
|
||||
|
||||
properties.each do |prop_name, prop_value|
|
||||
if EVENT_PROPERTIES.include? prop_name
|
||||
# OK, save the property
|
||||
|
@ -174,7 +179,7 @@ module Inspec
|
|||
# are free to go higher.
|
||||
DEFAULT_PRIORITY_FOR_VALUE_SET = 60
|
||||
|
||||
attr_reader :description, :events, :identifier, :name, :required, :sensitive, :title, :type
|
||||
attr_reader :description, :events, :identifier, :name, :required, :sensitive, :title, :type, :pattern
|
||||
|
||||
def initialize(name, options = {})
|
||||
@name = name
|
||||
|
@ -192,7 +197,6 @@ module Inspec
|
|||
# debugging record of when and how the value changed.
|
||||
@events = []
|
||||
events.push make_creation_event(options)
|
||||
|
||||
update(options)
|
||||
end
|
||||
|
||||
|
@ -213,6 +217,7 @@ module Inspec
|
|||
def update(options)
|
||||
_update_set_metadata(options)
|
||||
normalize_type_restriction!
|
||||
normalize_pattern_restriction!
|
||||
|
||||
# Values are set by passing events in; but we can also infer an event.
|
||||
if options.key?(:value) || options.key?(:default)
|
||||
|
@ -225,8 +230,8 @@ module Inspec
|
|||
end
|
||||
end
|
||||
events << options[:event] if options.key? :event
|
||||
|
||||
enforce_type_restriction!
|
||||
enforce_pattern_restriction!
|
||||
end
|
||||
|
||||
# We can determine a value:
|
||||
|
@ -255,6 +260,7 @@ module Inspec
|
|||
end
|
||||
end
|
||||
event.value = options[:value] if options.key?(:value)
|
||||
event.pattern = options[:pattern] if options.key?(:pattern)
|
||||
options[:event] = event
|
||||
end
|
||||
|
||||
|
@ -268,6 +274,7 @@ module Inspec
|
|||
@identifier = options[:identifier] if options.key?(:identifier) # TODO: determine if this is ever used
|
||||
@type = options[:type] if options.key?(:type)
|
||||
@sensitive = options[:sensitive] if options.key?(:sensitive)
|
||||
@pattern = options[:pattern] if options.key?(:pattern)
|
||||
end
|
||||
|
||||
def make_creation_event(options)
|
||||
|
@ -276,7 +283,8 @@ module Inspec
|
|||
action: :create,
|
||||
provider: options[:provider],
|
||||
file: loc.path,
|
||||
line: loc.lineno
|
||||
line: loc.lineno,
|
||||
pattern: options[:pattern]
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -302,7 +310,7 @@ module Inspec
|
|||
def value=(new_value, priority = DEFAULT_PRIORITY_FOR_VALUE_SET)
|
||||
# Inject a new Event with the new value.
|
||||
location = Event.probe_stack
|
||||
events << Event.new(
|
||||
event = Event.new(
|
||||
action: :set,
|
||||
provider: :value_setter,
|
||||
priority: priority,
|
||||
|
@ -310,7 +318,10 @@ module Inspec
|
|||
file: location.path,
|
||||
line: location.lineno
|
||||
)
|
||||
event.pattern = pattern if pattern
|
||||
events << event
|
||||
enforce_type_restriction!
|
||||
enforce_pattern_restriction!
|
||||
end
|
||||
|
||||
def value
|
||||
|
@ -324,7 +335,7 @@ module Inspec
|
|||
|
||||
def to_hash
|
||||
as_hash = { name: name, options: {} }
|
||||
%i{description title identifier type required value sensitive}.each do |field|
|
||||
%i{description title identifier type required value sensitive pattern}.each do |field|
|
||||
val = send(field)
|
||||
next if val.nil?
|
||||
|
||||
|
@ -407,6 +418,34 @@ module Inspec
|
|||
@type = type_req
|
||||
end
|
||||
|
||||
def enforce_pattern_restriction!
|
||||
return unless pattern
|
||||
return unless has_value?
|
||||
|
||||
string_value = current_value(false).to_s
|
||||
|
||||
valid_pattern = string_value.match?(pattern)
|
||||
unless valid_pattern
|
||||
error = Inspec::Input::ValidationError.new
|
||||
error.input_name = @name
|
||||
error.input_value = string_value
|
||||
error.input_pattern = pattern
|
||||
raise error, "Input '#{error.input_name}' with value '#{error.input_value}' does not validate to pattern '#{error.input_pattern}'."
|
||||
end
|
||||
end
|
||||
|
||||
def normalize_pattern_restriction!
|
||||
return unless pattern
|
||||
|
||||
unless valid_regexp?(pattern)
|
||||
error = Inspec::Input::PatternError.new
|
||||
error.input_pattern = pattern
|
||||
raise error, "Pattern '#{error.input_pattern}' is not a valid regex pattern."
|
||||
end
|
||||
@pattern = pattern
|
||||
end
|
||||
|
||||
|
||||
def valid_numeric?(value)
|
||||
Float(value)
|
||||
true
|
||||
|
|
|
@ -325,6 +325,7 @@ module Inspec
|
|||
type: input_options[:type],
|
||||
required: input_options[:required],
|
||||
sensitive: input_options[:sensitive],
|
||||
pattern: input_options[:pattern],
|
||||
event: evt
|
||||
)
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue