summaryrefslogtreecommitdiff
path: root/lib/chef/resource/helpers/cron_validations.rb
blob: 29cd4dcd5549b5f1613687212e85ebb0318069d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#
# Copyright:: Copyright 2020-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 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 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 validate_dow(spec)
        spec == "*" ||
          validate_numeric(spec, 0, 7) ||
          %w{sun mon tue wed thu fri sat}.include?(String(spec).downcase)
      end

      # validate the day of the month is 1-31
      # @param spec the value to validate
      # @return [Boolean] valid or not?
      def validate_day(spec)
        validate_numeric(spec, 1, 31)
      end

      # validate the hour is 0-23
      # @param spec the value to validate
      # @return [Boolean] valid or not?
      def validate_hour(spec)
        validate_numeric(spec, 0, 23)
      end

      # validate the minute is 0-59
      # @param spec the value to validate
      # @return [Boolean] valid or not?
      def validate_minute(spec)
        validate_numeric(spec, 0, 59)
      end

      extend self
    end
  end
end