diff options
Diffstat (limited to 'lib/chef/resource')
-rw-r--r-- | lib/chef/resource/cron.rb | 10 | ||||
-rw-r--r-- | lib/chef/resource/cron_d.rb | 12 | ||||
-rw-r--r-- | lib/chef/resource/helpers/cron_validations.rb | 75 |
3 files changed, 86 insertions, 11 deletions
diff --git a/lib/chef/resource/cron.rb b/lib/chef/resource/cron.rb index afb8b627be..d010cfc9bf 100644 --- a/lib/chef/resource/cron.rb +++ b/lib/chef/resource/cron.rb @@ -18,7 +18,7 @@ # require_relative "../resource" -require_relative "../mixin/cron_validations" +require_relative "helpers/cron_validations" require_relative "../provider/cron" # do not remove. we actually need this below class Chef @@ -43,25 +43,25 @@ class Chef property :minute, [Integer, String], description: "The minute at which the cron entry should run (0 - 59).", default: "*", callbacks: { - "should be a valid minute spec" => ->(spec) { Chef::Mixin::CronValidations.validate_numeric(spec, 0, 59) }, + "should be a valid minute spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 0, 59) }, } property :hour, [Integer, String], description: "The hour at which the cron entry is to run (0 - 23).", default: "*", callbacks: { - "should be a valid hour spec" => ->(spec) { Chef::Mixin::CronValidations.validate_numeric(spec, 0, 23) }, + "should be a valid hour spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 0, 23) }, } property :day, [Integer, String], description: "The day of month at which the cron entry should run (1 - 31).", default: "*", callbacks: { - "should be a valid day spec" => ->(spec) { Chef::Mixin::CronValidations.validate_numeric(spec, 1, 31) }, + "should be a valid day spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 1, 31) }, } property :month, [Integer, String], description: "The month in the year on which a cron entry is to run (1 - 12, jan-dec, or *).", default: "*", callbacks: { - "should be a valid month spec" => ->(spec) { Chef::Mixin::CronValidations.validate_month(spec) }, + "should be a valid month spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_month(spec) }, } def weekday(arg = nil) diff --git a/lib/chef/resource/cron_d.rb b/lib/chef/resource/cron_d.rb index 98e962ac66..e440b8341b 100644 --- a/lib/chef/resource/cron_d.rb +++ b/lib/chef/resource/cron_d.rb @@ -16,7 +16,7 @@ # require_relative "../resource" -require_relative "../mixin/cron_validations" +require_relative "helpers/cron_validations" require "shellwords" unless defined?(Shellwords) require_relative "../dist" @@ -87,31 +87,31 @@ class Chef property :minute, [Integer, String], description: "The minute at which the cron entry should run (0 - 59).", default: "*", callbacks: { - "should be a valid minute spec" => ->(spec) { Chef::Mixin::CronValidations.validate_numeric(spec, 0, 59) }, + "should be a valid minute spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 0, 59) }, } property :hour, [Integer, String], description: "The hour at which the cron entry is to run (0 - 23).", default: "*", callbacks: { - "should be a valid hour spec" => ->(spec) { Chef::Mixin::CronValidations.validate_numeric(spec, 0, 23) }, + "should be a valid hour spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 0, 23) }, } property :day, [Integer, String], description: "The day of month at which the cron entry should run (1 - 31).", default: "*", callbacks: { - "should be a valid day spec" => ->(spec) { Chef::Mixin::CronValidations.validate_numeric(spec, 1, 31) }, + "should be a valid day spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 1, 31) }, } property :month, [Integer, String], description: "The month in the year on which a cron entry is to run (1 - 12, jan-dec, or *).", default: "*", callbacks: { - "should be a valid month spec" => ->(spec) { Chef::Mixin::CronValidations.validate_month(spec) }, + "should be a valid month spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_month(spec) }, } property :weekday, [Integer, String], description: "The day of the week on which this entry is to run (0-7, mon-sun, or *), where Sunday is both 0 and 7.", default: "*", callbacks: { - "should be a valid weekday spec" => ->(spec) { Chef::Mixin::CronValidations.validate_dow(spec) }, + "should be a valid weekday spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_dow(spec) }, } property :command, String, diff --git a/lib/chef/resource/helpers/cron_validations.rb b/lib/chef/resource/helpers/cron_validations.rb new file mode 100644 index 0000000000..d09113d64c --- /dev/null +++ b/lib/chef/resource/helpers/cron_validations.rb @@ -0,0 +1,75 @@ +# +# Copyright:: Copyright 2020, Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +class Chef + module ResourceHelpers + # a collection of methods for validating cron times. Used in the various cron-like resources + module CronValidations + # validate a provided value is between two other provided values + # we also allow * as a valid input + # @param spec the value to validate + # @param min the lowest value allowed + # @param max the highest value allowed + # @return [Boolean] valid or not? + def self.validate_numeric(spec, min, max) + return true if spec == "*" + + if spec.respond_to? :to_int + return spec >= min && spec <= max + end + + # Lists of invidual values, ranges, and step values all share the validity range for type + spec.split(%r{\/|-|,}).each do |x| + next if x == "*" + return false unless x =~ /^\d+$/ + + x = x.to_i + return false unless x >= min && x <= max + end + true + end + + # validate the provided month value to be jan - dec, 1 - 12, or * + # @param spec the value to validate + # @return [Boolean] valid or not? + def self.validate_month(spec) + return true if spec == "*" + + if spec.respond_to? :to_int + validate_numeric(spec, 1, 12) + elsif spec.respond_to? :to_str + # Named abbreviations are permitted but not as part of a range or with stepping + return true if %w{jan feb mar apr may jun jul aug sep oct nov dec}.include? spec.downcase + + # 1-12 are legal for months + validate_numeric(spec, 1, 12) + else + false + end + end + + # validate the provided day of the week is sun-sat, 0-7, or * + # @param spec the value to validate + # @return [Boolean] valid or not? + def self.validate_dow(spec) + spec == "*" || + validate_numeric(spec, 0, 7) || + %w{sun mon tue wed thu fri sat}.include?(String(spec).downcase) + end + end + end +end |