From 8c2549f8a4746fb3a9e2c61e92822f8660f8a73c Mon Sep 17 00:00:00 2001
From: Nathaniel Kofalt <nathaniel@kofalt.com>
Date: Tue, 8 Dec 2015 11:27:38 -0600
Subject: [PATCH] Tolerate optional file keys

---
 api/rules.py | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/api/rules.py b/api/rules.py
index 35e26368..b7779b3a 100644
--- a/api/rules.py
+++ b/api/rules.py
@@ -51,6 +51,9 @@ HARDCODED_RULES = [
     }
 ]
 
+def _log_file_key_error(file_, container, error):
+    log.warning('file ' + file_['name'] + ' in container ' + str(container['_id']) + ' ' + error)
+
 def eval_match(match_type, match_param, file_, container):
     """
     Given a match entry, return if the match succeeded.
@@ -61,7 +64,11 @@ def eval_match(match_type, match_param, file_, container):
 
     # Match the file's type
     if match_type == 'file.type':
-        return file_['type'] == match_param
+        try:
+            return file_['type'] == match_param
+        except KeyError:
+            _log_file_key_error(file_, container, 'has no type key')
+            return False
 
     # Match a shell glob for the file name
     elif match_type == 'file.name':
@@ -69,7 +76,11 @@ def eval_match(match_type, match_param, file_, container):
 
     # Match any of the file's measurements
     elif match_type == 'file.measurements':
-        return match_param in file_[measurements]
+        try:
+            return match_param in file_[measurements]
+        except KeyError:
+            _log_file_key_error(file_, container, 'has no measurements key')
+            return False
 
     # Match the container's primary measurment
     elif match_type == 'container.measurement':
-- 
GitLab