changeset 57:ba1be668e475

fix initialization of the aws static library
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 22 Jun 2021 16:55:24 +0200
parents b922ae86bbe1
children 37185ec1cf49
files Aws/AwsS3StoragePlugin.cpp Aws/AwsStaticConfiguration.cmake Aws/CMakeLists.txt Common/StoragePlugin.cpp
diffstat 4 files changed, 50 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/Aws/AwsS3StoragePlugin.cpp	Tue Jun 22 15:00:36 2021 +0200
+++ b/Aws/AwsS3StoragePlugin.cpp	Tue Jun 22 16:55:24 2021 +0200
@@ -26,6 +26,7 @@
 #include <aws/s3/model/DeleteObjectRequest.h>
 #include <aws/core/auth/AWSCredentialsProvider.h>
 #include <aws/core/utils/memory/stl/AWSStringStream.h>
+#include <aws/crt/Api.h>
 
 #include <boost/lexical_cast.hpp>
 #include <iostream>
@@ -45,6 +46,8 @@
 
   AwsS3StoragePlugin(const Aws::S3::S3Client& client, const std::string& bucketName, bool enableLegacyStorageStructure);
 
+  virtual ~AwsS3StoragePlugin();
+
   virtual IWriter* GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
   virtual IReader* GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
   virtual void DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
@@ -199,8 +202,26 @@
   return "AWS S3 Storage";
 }
 
+
+static std::unique_ptr<Aws::Crt::ApiHandle>  api_;
+static std::unique_ptr<Aws::SDKOptions>  sdkOptions_;
+
+
 IStoragePlugin* AwsS3StoragePluginFactory::CreateStoragePlugin(const OrthancPlugins::OrthancConfiguration& orthancConfig)
 {
+  if (sdkOptions_.get() != NULL)
+  {
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls, "Cannot initialize twice");
+  }
+
+  api_.reset(new Aws::Crt::ApiHandle);
+
+  sdkOptions_.reset(new Aws::SDKOptions);
+  sdkOptions_->cryptoOptions.initAndCleanupOpenSSL = false;  // Done by the Orthanc framework
+  sdkOptions_->httpOptions.initAndCleanupCurl = false;  // Done by the Orthanc framework
+  
+  Aws::InitAPI(*sdkOptions_);
+
   bool enableLegacyStorageStructure;
 
   if (!orthancConfig.IsSection(PLUGIN_SECTION))
@@ -234,10 +255,11 @@
     return nullptr;
   }
 
-  std::string endpoint = pluginSection.GetStringValue("Endpoint", "");
-  unsigned int connectTimeout = pluginSection.GetUnsignedIntegerValue("ConnectTimeout", 30);
-  unsigned int requestTimeout = pluginSection.GetUnsignedIntegerValue("RequestTimeout", 1200);
-  bool virtualAddressing = pluginSection.GetBooleanValue("VirtualAddressing", true);
+  const std::string endpoint = pluginSection.GetStringValue("Endpoint", "");
+  const unsigned int connectTimeout = pluginSection.GetUnsignedIntegerValue("ConnectTimeout", 30);
+  const unsigned int requestTimeout = pluginSection.GetUnsignedIntegerValue("RequestTimeout", 1200);
+  const bool virtualAddressing = pluginSection.GetBooleanValue("VirtualAddressing", true);
+  const std::string caFile = orthancConfig.GetStringValue("HttpsCACertificates", "");
   
   try
   {
@@ -245,6 +267,7 @@
     Aws::InitAPI(options);
 
     Aws::Client::ClientConfiguration configuration;
+
     configuration.region = region.c_str();
     configuration.scheme = Aws::Http::Scheme::HTTPS;
     configuration.connectTimeoutMs = connectTimeout * 1000;
@@ -256,6 +279,11 @@
       configuration.endpointOverride = endpoint.c_str();
     }
 
+    if (!caFile.empty())
+    {
+      configuration.caFile = caFile;
+    }
+    
     if (pluginSection.LookupStringValue(accessKey, "AccessKey") && pluginSection.LookupStringValue(secretKey, "SecretKey"))
     {
       OrthancPlugins::LogInfo("AWS S3 Storage: using credentials from the configuration file");
@@ -286,6 +314,15 @@
 
 }
 
+
+AwsS3StoragePlugin::~AwsS3StoragePlugin()
+{
+  assert(sdkOptions_.get() != NULL);
+  Aws::ShutdownAPI(*sdkOptions_);
+  api_.reset();
+}
+
+
 AwsS3StoragePlugin::AwsS3StoragePlugin(const Aws::S3::S3Client& client, const std::string& bucketName, bool enableLegacyStorageStructure)
   : BaseStoragePlugin(enableLegacyStorageStructure),
     client_(client),
--- a/Aws/AwsStaticConfiguration.cmake	Tue Jun 22 15:00:36 2021 +0200
+++ b/Aws/AwsStaticConfiguration.cmake	Tue Jun 22 16:55:24 2021 +0200
@@ -137,6 +137,7 @@
   ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/external/cjson/
   ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/external/tinyxml2/
   ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/http/
+  ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/http/curl/
   ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/http/standard/
   ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/internal/
   ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/monitoring/
@@ -145,6 +146,7 @@
   ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/utils/base64/
   ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/utils/crypto/
   ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/utils/crypto/factory/
+  ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/utils/crypto/openssl/
   ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/utils/event/
   ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/utils/json/
   ${AWS_SDK_CPP_SOURCES_DIR}/aws-cpp-sdk-core/source/utils/logging/
@@ -158,8 +160,10 @@
 
 
 add_definitions(
-  -DAWS_AFFINITY_METHOD=AWS_AFFINITY_METHOD_PTHREAD_ATTR
+  -DAWS_AFFINITY_METHOD=AWS_AFFINITY_METHOD_PTHREAD
   -DBYO_CRYPTO  # To have "aws_tls_server_ctx_new()" defined
+  -DENABLE_OPENSSL_ENCRYPTION=1
+  -DENABLE_CURL_CLIENT=1
   )
 
 list(APPEND AWS_SOURCES_SUBDIRS
--- a/Aws/CMakeLists.txt	Tue Jun 22 15:00:36 2021 +0200
+++ b/Aws/CMakeLists.txt	Tue Jun 22 16:55:24 2021 +0200
@@ -40,7 +40,7 @@
 set(ENABLE_MODULE_DICOM OFF)
 set(ENABLE_MODULE_IMAGES OFF)
 set(ENABLE_MODULE_JOBS OFF)
-# set(ENABLE_WEB_CLIENT ON)  # Access options related to curl
+set(ENABLE_WEB_CLIENT ON)  # Access options related to curl
 set(ORTHANC_FRAMEWORK_PLUGIN ON)
 
 include(${ORTHANC_FRAMEWORK_ROOT}/../Resources/CMake/OrthancFrameworkConfiguration.cmake)
--- a/Common/StoragePlugin.cpp	Tue Jun 22 15:00:36 2021 +0200
+++ b/Common/StoragePlugin.cpp	Tue Jun 22 16:55:24 2021 +0200
@@ -306,6 +306,7 @@
   {
     OrthancPlugins::SetGlobalContext(context);
 
+    Orthanc::InitializeFramework("", false);
     Orthanc::Logging::InitializePluginContext(context);
 
     OrthancPlugins::OrthancConfiguration orthancConfig;
@@ -402,6 +403,8 @@
   ORTHANC_PLUGINS_API void OrthancPluginFinalize()
   {
     OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + " plugin is finalizing");
+    plugin.reset();
+    Orthanc::FinalizeFramework();
   }