[storage] Allow ObservationWriter to not encrypt

If a nullptr is passed instead of an EncryptedMessageMaker,
ObservationWriter will not encrypt observations before adding them to
the store.

Bug: 3842
Change-Id: I837297b73d8b4b45db7fe83e42ae4a4bc894ded5
diff --git a/src/logger/observation_writer.cc b/src/logger/observation_writer.cc
index cac0e7c..b41aeaf 100644
--- a/src/logger/observation_writer.cc
+++ b/src/logger/observation_writer.cc
@@ -8,22 +8,32 @@
 #include <utility>
 
 #include "src/logging.h"
+#include "src/observation_store/observation_store.h"
+#include "src/pb/observation2.pb.h"
 #include "src/tracing.h"
 
 namespace cobalt::logger {
 
 using ::cobalt::observation_store::ObservationStoreWriterInterface;
+using observation_store::ObservationStore;
 
 Status ObservationWriter::WriteObservation(const Observation2 &observation,
                                            std::unique_ptr<ObservationMetadata> metadata) const {
   TRACE_DURATION("cobalt_core", "ObservationWriter::WriteObservation");
-  auto encrypted_observation = std::make_unique<EncryptedMessage>();
-  if (!observation_encrypter_->Encrypt(observation, encrypted_observation.get())) {
-    LOG(ERROR) << "Encryption of an Observation failed.";
-    return kOther;
+  ObservationStore::StoreStatus store_status;
+  if (observation_encrypter_) {
+    auto encrypted_observation = std::make_unique<EncryptedMessage>();
+    if (!observation_encrypter_->Encrypt(observation, encrypted_observation.get())) {
+      LOG(ERROR) << "Encryption of an Observation failed.";
+      return kOther;
+    }
+    store_status =
+        observation_store_->StoreObservation(std::move(encrypted_observation), std::move(metadata));
+  } else {
+    // Make a copy of observation to add to the store.
+    store_status = observation_store_->StoreObservation(std::make_unique<Observation2>(observation),
+                                                        std::move(metadata));
   }
-  auto store_status =
-      observation_store_->StoreObservation(std::move(encrypted_observation), std::move(metadata));
   if (store_status != ObservationStoreWriterInterface::kOk) {
     LOG_FIRST_N(ERROR, 10) << "ObservationStore::StoreObservation() failed with status "
                            << store_status;
diff --git a/src/logger/observation_writer.h b/src/logger/observation_writer.h
index 4d952ca..eb3a621 100644
--- a/src/logger/observation_writer.h
+++ b/src/logger/observation_writer.h
@@ -25,20 +25,20 @@
  public:
   // Constructor:
   //
-  // |observation_store| A writer interface to the system's singleton instance
-  // of Observation Store. This must remain valid as long as the
+  // |observation_store| A writer interface to the system's singleton instance of Observation Store.
+  // This must remain valid as long as the ObservationWriter is being used.
+  //
+  // |update_recipient| The ObservationWriter uses this to notify the update recipient when an
+  // Observation has been added to the Observation Store. This must remain valid as long as the
   // ObservationWriter is being used.
   //
-  // |update_recipient| The ObservationWriter uses this to notify the update
-  // recipient when an Observation has been added to the Observation Store. This
-  // must remain valid as long as the ObservationWriter is being used.
-  //
-  // |observation_encrypter| This is used to encrypt Observations to the public
-  // key of Cobalt's Analyzer prior to writing them into the Observation Store.
-  // This must remain valid as long as the ObservationWriter is being used.
+  // |observation_encrypter| This is used to encrypt Observations to the public key of Cobalt's
+  // Analyzer prior to writing them into the Observation Store. If this value is equal to nullptr,
+  // then Observations will not be encrypted before being added to the Observation Store. Otherwise,
+  // this must remain valid as long as the ObservationWriter is being used.
   ObservationWriter(observation_store::ObservationStoreWriterInterface* observation_store,
                     observation_store::ObservationStoreUpdateRecipient* update_recipient,
-                    util::EncryptedMessageMaker* observation_encrypter)
+                    util::EncryptedMessageMaker* observation_encrypter = nullptr)
       : observation_store_(observation_store),
         update_recipient_(update_recipient),
         observation_encrypter_(observation_encrypter) {}