Encryption failure should generate a watchdog entry

Project: 
Ubercart
Category: 
feature request
Priority: 
normal
Status: 
active

When a call to uc_encryption_class::encrypt() produces an error, the error message is discarded, which makes debugging difficult. It would be helpful to write a watchdog log entry. The following patch to beta 5 provides this feature (note that encrypt() reports an error if it is called with an empty argument):

Index: payment/uc_credit/uc_credit.module
===================================================================
--- payment/uc_credit/uc_credit.module  (revision 32)
+++ payment/uc_credit/uc_credit.module  (revision 34)
@@ -162,13 +162,36 @@

         if ($key !== FALSE) {
           $crypt = new uc_encryption_class;
-          $cc_type = $crypt->encrypt($key, $cc_type, 64);
-          $cc_owner = $crypt->encrypt($key, $cc_owner);
-          $cc_number = $crypt->encrypt($key, $cc_number, 32);
-          $cc_exp_month = $crypt->encrypt($key, $cc_exp_month, 32);
-          $cc_exp_year = $crypt->encrypt($key, $cc_exp_year, 32);
-          $cc_cvv = $crypt->encrypt($key, $cc_cvv, 32);
-          $cc_bank = $crypt->encrypt($key, $cc_bank);
+         if (!empty($cc_type)) {
+            $cc_type = $crypt->encrypt($key, $cc_type, 64);
+          }
+         if (!empty($cc_owner)) {
+            $cc_owner = $crypt->encrypt($key, $cc_owner);
+          }
+         if (!empty($cc_number)) {
+            $cc_number = $crypt->encrypt($key, $cc_number, 32);
+          }
+         if (!empty($cc_exp_month)) {
+            $cc_exp_month = $crypt->encrypt($key, $cc_exp_month, 32);
+          }
+         if (!empty($cc_exp_year)) {
+            $cc_exp_year = $crypt->encrypt($key, $cc_exp_year, 32);
+          }
+         if (!empty($cc_cvv)) {
+            $cc_cvv = $crypt->encrypt($key, $cc_cvv, 32);
+          }
+         if (!empty($cc_bank)) {
+            $cc_bank = $crypt->encrypt($key, $cc_bank);
+          }
+         if (!empty($crypt->errors)) {
+            $message = '';
+           foreach($crypt->errors as $err_msg) {
+            $message .= ": $err_msg";
+            }
+            watchdog('uc_credit', "Credit card encryption failed$message",
+                     WATCHDOG_ERROR);
+            break;
+          }
         }

         db_query("UPDATE {uc_payment_credit} SET cc_type = '%s', cc_owner = '%s', "

Re: Encryption failure should generate a watchdog entry

Good idea. This isn't the only place that class is used, though, so I made a function in uc_store.module that can be reused. I added it into the appropriate places in the credit and recurring modules. The function is uc_store_encryption_errors($crypt, $module); where $crypt is the encryption object and $module is the module name.

EDIT: Didn't know that about the empty argument. I'll have it return an empty string instead of kicking out an error.