From e71c81472711247b800d47d6f3f58cbb9d0f9b8c Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Mon, 6 Sep 2010 19:01:26 +0200
Subject: [PATCH] jqueue: Ensure only accepted priorities are allowed for
 submitting jobs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Quoting the design document: β€œSubmitted opcodes can have one of the priorities
listed below. Other priorities are reserved for internal use”. Submitting jobs
at priority -20 should not be allowed.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/constants.py | 6 ++++++
 lib/jqueue.py    | 8 ++++++++
 2 files changed, 14 insertions(+)

diff --git a/lib/constants.py b/lib/constants.py
index 51783416c..a0f1675ac 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -849,6 +849,12 @@ OP_PRIO_LOW = +10
 OP_PRIO_NORMAL = 0
 OP_PRIO_HIGH = -10
 
+OP_PRIO_SUBMIT_VALID = frozenset([
+  OP_PRIO_LOW,
+  OP_PRIO_NORMAL,
+  OP_PRIO_HIGH,
+  ])
+
 OP_PRIO_DEFAULT = OP_PRIO_NORMAL
 
 # Execution log types
diff --git a/lib/jqueue.py b/lib/jqueue.py
index fbcb19675..a6c0c7abe 100644
--- a/lib/jqueue.py
+++ b/lib/jqueue.py
@@ -1370,6 +1370,7 @@ class JobQueue(object):
     @return: the job object to be queued
     @raise errors.JobQueueDrainError: if the job queue is marked for draining
     @raise errors.JobQueueFull: if the job queue has too many jobs in it
+    @raise errors.GenericError: If an opcode is not valid
 
     """
     # Ok when sharing the big job queue lock, as the drain file is created when
@@ -1382,6 +1383,13 @@ class JobQueue(object):
 
     job = _QueuedJob(self, job_id, ops)
 
+    # Check priority
+    for idx, op in enumerate(job.ops):
+      if op.priority not in constants.OP_PRIO_SUBMIT_VALID:
+        allowed = utils.CommaJoin(constants.OP_PRIO_SUBMIT_VALID)
+        raise errors.GenericError("Opcode %s has invalid priority %s, allowed"
+                                  " are %s" % (idx, op.priority, allowed))
+
     # Write to disk
     self.UpdateJobUnlocked(job)
 
-- 
GitLab