changeset 95:33790e0000bb

Plugins are now disabled if their section is missing in the configuration file.
author Alain Mazy <am@osimis.io>
date Mon, 19 Dec 2022 09:58:23 +0100
parents 1bc055199cd2
children b0999e4e383a
files Common/StoragePlugin.cpp NEWS
diffstat 2 files changed, 105 insertions(+), 102 deletions(-) [+]
line wrap: on
line diff
--- a/Common/StoragePlugin.cpp	Mon Dec 19 09:52:58 2022 +0100
+++ b/Common/StoragePlugin.cpp	Mon Dec 19 09:58:23 2022 +0100
@@ -547,130 +547,132 @@
       const char* pluginSectionName = StoragePluginFactory::GetConfigurationSectionName();
       static const char* const ENCRYPTION_SECTION = "StorageEncryption";
 
-      if (orthancConfig.IsSection(pluginSectionName))
+      if (!orthancConfig.IsSection(pluginSectionName))
+      {
+        OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": no \"" + pluginSectionName +  "\" section found in configuration, plugin is disabled");
+        return 0;
+      }
+
+      OrthancPlugins::OrthancConfiguration pluginSection;
+      orthancConfig.GetSection(pluginSection, pluginSectionName);
+
+      bool migrationFromFileSystemEnabled = pluginSection.GetBooleanValue("MigrationFromFileSystemEnabled", false);
+      std::string hybridModeString = pluginSection.GetStringValue("HybridMode", "Disabled");
+
+      if (migrationFromFileSystemEnabled && hybridModeString == "Disabled")
       {
-        OrthancPlugins::OrthancConfiguration pluginSection;
-        orthancConfig.GetSection(pluginSection, pluginSectionName);
-
-        bool migrationFromFileSystemEnabled = pluginSection.GetBooleanValue("MigrationFromFileSystemEnabled", false);
-        std::string hybridModeString = pluginSection.GetStringValue("HybridMode", "Disabled");
+        hybridMode = HybridMode_WriteToObjectStorage;
+        OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": 'MigrationFromFileSystemEnabled' configuration is deprecated, use 'HybridMode': 'WriteToObjectStorage' instead");
+      }
+      else if (hybridModeString == "WriteToObjectStorage")
+      {
+        hybridMode = HybridMode_WriteToObjectStorage;
+        OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": WriteToObjectStorage HybridMode is enabled: writing to object-storage, try reading first from object-storage and, then, from file system");
+      }
+      else if (hybridModeString == "WriteToFileSystem")
+      {
+        hybridMode = HybridMode_WriteToFileSystem;
+        OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": WriteToFileSystem HybridMode is enabled: writing to file system, try reading first from file system and, then, from object-storage");
+      }
+      else
+      {
+        OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": HybridMode is disabled: writing to object-storage and reading only from object-storage");
+      }
 
-        if (migrationFromFileSystemEnabled && hybridModeString == "Disabled")
-        {
-          hybridMode = HybridMode_WriteToObjectStorage;
-          OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": 'MigrationFromFileSystemEnabled' configuration is deprecated, use 'HybridMode': 'WriteToObjectStorage' instead");
-        }
-        else if (hybridModeString == "WriteToObjectStorage")
-        {
-          hybridMode = HybridMode_WriteToObjectStorage;
-          OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": WriteToObjectStorage HybridMode is enabled: writing to object-storage, try reading first from object-storage and, then, from file system");
-        }
-        else if (hybridModeString == "WriteToFileSystem")
-        {
-          hybridMode = HybridMode_WriteToFileSystem;
-          OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": WriteToFileSystem HybridMode is enabled: writing to file system, try reading first from file system and, then, from object-storage");
-        }
-        else
-        {
-          OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": HybridMode is disabled: writing to object-storage and reading only from object-storage");
-        }
+      if (IsReadFromDisk())
+      {
+        fileSystemRootPath = orthancConfig.GetStringValue("StorageDirectory", "OrthancStorageNotDefined");
+        OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": HybridMode: reading from file system is enabled, source: " + fileSystemRootPath);
+      }
+
+      objectsRootPath = pluginSection.GetStringValue("RootPath", std::string());
+
+      if (objectsRootPath.size() >= 1 && objectsRootPath[0] == '/')
+      {
+        OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": The RootPath shall not start with a '/': " + objectsRootPath);
+        return -1;
+      }
 
-        if (IsReadFromDisk())
-        {
-          fileSystemRootPath = orthancConfig.GetStringValue("StorageDirectory", "OrthancStorageNotDefined");
-          OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": HybridMode: reading from file system is enabled, source: " + fileSystemRootPath);
-        }
+      std::string objecstStoragePluginName = StoragePluginFactory::GetStoragePluginName();
+      if (hybridMode == HybridMode_WriteToFileSystem)
+      {
+        objecstStoragePluginName += " (Secondary: object-storage)";
+      }
+      else if (hybridMode == HybridMode_WriteToObjectStorage)
+      {
+        objecstStoragePluginName += " (Primary: object-storage)";
+      }
 
-        objectsRootPath = pluginSection.GetStringValue("RootPath", std::string());
+      std::unique_ptr<IStorage> objectStoragePlugin(StoragePluginFactory::CreateStorage(objecstStoragePluginName, orthancConfig));
 
-        if (objectsRootPath.size() >= 1 && objectsRootPath[0] == '/')
-        {
-          OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": The RootPath shall not start with a '/': " + objectsRootPath);
-          return -1;
-        }
+      if (objectStoragePlugin.get() == nullptr)
+      {
+        return -1;
+      }
+
+      objectStoragePlugin->SetRootPath(objectsRootPath);
 
-        std::string objecstStoragePluginName = StoragePluginFactory::GetStoragePluginName();
+      std::unique_ptr<IStorage> fileSystemStoragePlugin;
+      if (IsHybridModeEnabled())
+      {
+        bool fsync = orthancConfig.GetBooleanValue("SyncStorageArea", true);
+
+        std::string filesystemStoragePluginName = StoragePluginFactory::GetStoragePluginName();
         if (hybridMode == HybridMode_WriteToFileSystem)
         {
-          objecstStoragePluginName += " (Secondary: object-storage)";
+          filesystemStoragePluginName += " (Primary: file-system)";
         }
         else if (hybridMode == HybridMode_WriteToObjectStorage)
         {
-          objecstStoragePluginName += " (Primary: object-storage)";
-        }
-
-        std::unique_ptr<IStorage> objectStoragePlugin(StoragePluginFactory::CreateStorage(objecstStoragePluginName, orthancConfig));
-
-        if (objectStoragePlugin.get() == nullptr)
-        {
-          return -1;
-        }
-
-        objectStoragePlugin->SetRootPath(objectsRootPath);
-
-        std::unique_ptr<IStorage> fileSystemStoragePlugin;
-        if (IsHybridModeEnabled())
-        {
-          bool fsync = orthancConfig.GetBooleanValue("SyncStorageArea", true);
-
-          std::string filesystemStoragePluginName = StoragePluginFactory::GetStoragePluginName();
-          if (hybridMode == HybridMode_WriteToFileSystem)
-          {
-            filesystemStoragePluginName += " (Primary: file-system)";
-          }
-          else if (hybridMode == HybridMode_WriteToObjectStorage)
-          {
-            filesystemStoragePluginName += " (Secondary: file-system)";
-          }
-
-          fileSystemStoragePlugin.reset(new FileSystemStoragePlugin(filesystemStoragePluginName, fileSystemRootPath, fsync));
+          filesystemStoragePluginName += " (Secondary: file-system)";
         }
 
-        if (hybridMode == HybridMode_Disabled || hybridMode == HybridMode_WriteToObjectStorage)
-        {
-          primaryStorage.reset(objectStoragePlugin.release());
-          
-          if (hybridMode == HybridMode_WriteToObjectStorage)
-          {
-            secondaryStorage.reset(fileSystemStoragePlugin.release());
-          }
-        }
-        else if (hybridMode == HybridMode_WriteToFileSystem)
-        {
-          primaryStorage.reset(fileSystemStoragePlugin.release());
-          secondaryStorage.reset(objectStoragePlugin.release());
-        }
-
-        if (pluginSection.IsSection(ENCRYPTION_SECTION))
-        {
-          OrthancPlugins::OrthancConfiguration cryptoSection;
-          pluginSection.GetSection(cryptoSection, ENCRYPTION_SECTION);
+        fileSystemStoragePlugin.reset(new FileSystemStoragePlugin(filesystemStoragePluginName, fileSystemRootPath, fsync));
+      }
 
-          crypto.reset(EncryptionConfigurator::CreateEncryptionHelpers(cryptoSection));
-          cryptoEnabled = crypto.get() != nullptr;
-        }
-
-        if (cryptoEnabled)
-        {
-          OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": client-side encryption is enabled");
-        }
-        else
+      if (hybridMode == HybridMode_Disabled || hybridMode == HybridMode_WriteToObjectStorage)
+      {
+        primaryStorage.reset(objectStoragePlugin.release());
+        
+        if (hybridMode == HybridMode_WriteToObjectStorage)
         {
-          OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": client-side encryption is disabled");
+          secondaryStorage.reset(fileSystemStoragePlugin.release());
         }
-
+      }
+      else if (hybridMode == HybridMode_WriteToFileSystem)
+      {
+        primaryStorage.reset(fileSystemStoragePlugin.release());
+        secondaryStorage.reset(objectStoragePlugin.release());
+      }
 
-        if (IsHybridModeEnabled())
-        {
-          OrthancPlugins::RegisterRestCallback<MoveStorage>("/move-storage", true);
-          OrthancPluginRegisterJobsUnserializer(context, JobUnserializer);
-        }
+      if (pluginSection.IsSection(ENCRYPTION_SECTION))
+      {
+        OrthancPlugins::OrthancConfiguration cryptoSection;
+        pluginSection.GetSection(cryptoSection, ENCRYPTION_SECTION);
 
+        crypto.reset(EncryptionConfigurator::CreateEncryptionHelpers(cryptoSection));
+        cryptoEnabled = crypto.get() != nullptr;
       }
 
       if (cryptoEnabled)
       {
-        // with encrypted file, we do not want to support ReadRange.  Therefore, we register the old interface
+        OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": client-side encryption is enabled");
+      }
+      else
+      {
+        OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": client-side encryption is disabled");
+      }
+
+
+      if (IsHybridModeEnabled())
+      {
+        OrthancPlugins::RegisterRestCallback<MoveStorage>("/move-storage", true);
+        OrthancPluginRegisterJobsUnserializer(context, JobUnserializer);
+      }
+
+      if (cryptoEnabled)
+      {
+        // with encrypted file, we can not support ReadRange.  Therefore, we register the old interface
         OrthancPluginRegisterStorageArea(context, StorageCreate, StorageReadWholeLegacy, StorageRemove);
       }
       else
--- a/NEWS	Mon Dec 19 09:52:58 2022 +0100
+++ b/NEWS	Mon Dec 19 09:58:23 2022 +0100
@@ -2,6 +2,7 @@
 ====================
 
 * Added a description for all plugins
+* Plugins are now disabled if their section is missing in the configuration file.
 
 
 2022-12-16 - v 2.1.1