X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/bf9803ee5afb33231da7900dddfdfac34b7056a6..01ca27ba0a1ef84c53e223004249505435a788b6:/services/api/lib/config_loader.rb diff --git a/services/api/lib/config_loader.rb b/services/api/lib/config_loader.rb index 522aa73b0a..1d897b39bf 100644 --- a/services/api/lib/config_loader.rb +++ b/services/api/lib/config_loader.rb @@ -2,6 +2,16 @@ # # SPDX-License-Identifier: AGPL-3.0 +# When loading YAML, deserialize :foo as ":foo", rather than raising +# "Psych::DisallowedClass: Tried to load unspecified class: Symbol" +class Psych::ScalarScanner + alias :orig_tokenize :tokenize + def tokenize string + return string if string =~ /^:[a-zA-Z]/ + orig_tokenize(string) + end +end + module Psych module Visitors class YAMLTree < Psych::Visitors::Visitor @@ -147,14 +157,14 @@ class ConfigLoader 'Ki' => 1 << 10, 'M' => 1000000, 'Mi' => 1 << 20, - "G" => 1000000000, - "Gi" => 1 << 30, - "T" => 1000000000000, - "Ti" => 1 << 40, - "P" => 1000000000000000, - "Pi" => 1 << 50, - "E" => 1000000000000000000, - "Ei" => 1 << 60, + "G" => 1000000000, + "Gi" => 1 << 30, + "T" => 1000000000000, + "Ti" => 1 << 40, + "P" => 1000000000000000, + "Pi" => 1 << 50, + "E" => 1000000000000000000, + "Ei" => 1 << 60, }[mt[2]] end end @@ -180,8 +190,13 @@ class ConfigLoader end end - def self.parse_duration durstr, cfgkey: - duration_re = /-?(\d+(\.\d+)?)(s|m|h)/ + def self.parse_duration(durstr, cfgkey:) + sign = 1 + if durstr[0] == '-' + durstr = durstr[1..-1] + sign = -1 + end + duration_re = /(\d+(\.\d+)?)(s|m|h)/ dursec = 0 while durstr != "" mt = duration_re.match durstr @@ -189,7 +204,7 @@ class ConfigLoader raise "#{cfgkey} not a valid duration: '#{durstr}', accepted suffixes are s, m, h" end multiplier = {s: 1, m: 60, h: 3600} - dursec += (Float(mt[1]) * multiplier[mt[3].to_sym]) + dursec += (Float(mt[1]) * multiplier[mt[3].to_sym] * sign) durstr = durstr[mt[0].length..-1] end return dursec.seconds @@ -221,7 +236,7 @@ class ConfigLoader if erb yaml = ERB.new(yaml).result(binding) end - YAML.load(yaml, deserialize_symbols: false) + YAML.safe_load(yaml) else {} end