]> granicus.if.org Git - icinga2/commitdiff
Improve validation for times.{begin,end} in notification objects 7164/head
authorMichael Friedrich <michael.friedrich@icinga.com>
Tue, 30 Apr 2019 13:56:57 +0000 (15:56 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Tue, 7 May 2019 13:20:06 +0000 (15:20 +0200)
fixes #6939

lib/icinga/notification.cpp
lib/icinga/notification.hpp

index 998705efaeb1513eeda8d93b556dd3ad872e5985..5067116a9627722f5d9d0c9b5af60c182919c45e 100644 (file)
@@ -720,6 +720,35 @@ void Notification::ValidateTypes(const Lazy<Array::Ptr>& lvalue, const Validatio
                BOOST_THROW_EXCEPTION(ValidationError(this, { "types" }, "Type filter is invalid."));
 }
 
+void Notification::ValidateTimes(const Lazy<Dictionary::Ptr>& lvalue, const ValidationUtils& utils)
+{
+       ObjectImpl<Notification>::ValidateTimes(lvalue, utils);
+
+       Dictionary::Ptr times = lvalue();
+
+       if (!times)
+               return;
+
+       double begin;
+       double end;
+
+       try {
+               begin = Convert::ToDouble(times->Get("begin"));
+       } catch (const std::exception&) {
+               BOOST_THROW_EXCEPTION(ValidationError(this, { "times" }, "'begin' is invalid, must be duration or number." ));
+       }
+
+       try {
+               end = Convert::ToDouble(times->Get("end"));
+       } catch (const std::exception&) {
+               BOOST_THROW_EXCEPTION(ValidationError(this, { "times" }, "'end' is invalid, must be duration or number." ));
+       }
+
+       /* Also solve logical errors where begin > end. */
+       if (begin > 0 && end > 0 && begin > end)
+               BOOST_THROW_EXCEPTION(ValidationError(this, { "times" }, "'begin' must be smaller than 'end'."));
+}
+
 Endpoint::Ptr Notification::GetCommandEndpoint() const
 {
        return Endpoint::GetByName(GetCommandEndpointRaw());
index 5006e89d857c8fd45bad46ccf0297aa9f3bea586..c483eaf9178fa209d19b9bf50132a1381e36d150 100644 (file)
@@ -95,6 +95,7 @@ public:
 
        void ValidateStates(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils) override;
        void ValidateTypes(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils) override;
+       void ValidateTimes(const Lazy<Dictionary::Ptr>& lvalue, const ValidationUtils& utils) override;
 
        static void EvaluateApplyRules(const intrusive_ptr<Host>& host);
        static void EvaluateApplyRules(const intrusive_ptr<Service>& service);