From d0b98288d596d2aeb2c63d388f97184b3aa8bdd7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ren=C3=A9=20Nussbaumer?= <rn@google.com>
Date: Tue, 7 Dec 2010 16:22:29 +0100
Subject: [PATCH] Check payload of OOB execution
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: RenΓ© Nussbaumer <rn@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/cmdlib.py | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index f5c3fcbcb..523043de2 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -3274,6 +3274,8 @@ class LUOutOfBand(NoHooksLU):
 
     result.Raise("An error occurred on execution of OOB helper")
 
+    self._CheckPayload(result)
+
     if self.op.command == constants.OOB_HEALTH:
       # For health we should log important events
       for item, status in result.payload:
@@ -3284,6 +3286,42 @@ class LUOutOfBand(NoHooksLU):
 
     return result.payload
 
+  def _CheckPayload(self, result):
+    """Checks if the payload is valid.
+
+    @param result: RPC result
+    @raises errors.OpExecError: If payload is not valid
+
+    """
+    errs = []
+    if self.op.command == constants.OOB_HEALTH:
+      if not isinstance(result.payload, list):
+        errs.append("command 'health' is expected to return a list but got %s" %
+                    type(result.payload))
+      for item, status in result.payload:
+        if status not in constants.OOB_STATUSES:
+          errs.append("health item '%s' has invalid status '%s'" %
+                      (item, status))
+
+    if self.op.command == constants.OOB_POWER_STATUS:
+      if not isinstance(result.payload, dict):
+        errs.append("power-status is expected to return a dict but got %s" %
+                    type(result.payload))
+
+    if self.op.command in [
+        constants.OOB_POWER_ON,
+        constants.OOB_POWER_OFF,
+        constants.OOB_POWER_CYCLE,
+        ]:
+      if result.payload is not None:
+        errs.append("%s is expected to not return payload but got '%s'" %
+                    (self.op.command, result.payload))
+
+    if errs:
+      raise errors.OpExecError("Check of out-of-band payload failed due to %s" %
+                               utils.CommaJoin(errs))
+
+
 
 class LUDiagnoseOS(NoHooksLU):
   """Logical unit for OS diagnose/query.
-- 
GitLab