31 #ifndef CPL_AWS_INCLUDED_H
32 #define CPL_AWS_INCLUDED_H
43 #include <curl/curl.h>
46 CPLString CPLGetLowerCaseHexSHA256(
const void *pabyData,
size_t nBytes );
53 CPLString CPLAWSGetHeaderVal(
const struct curl_slist* psExistingHeaders,
57 CPLGetAWS_SIGN4_Signature(
const CPLString& osSecretAccessKey,
63 const struct curl_slist* psExistingHeaders,
68 bool bAddHeaderAMZContentSHA256,
79 const struct curl_slist* psExistingHeaders,
84 bool bAddHeaderAMZContentSHA256,
87 class IVSIS3LikeHandleHelper
92 std::map<CPLString, CPLString> m_oMapQueryParameters{};
94 virtual void RebuildURL() = 0;
95 CPLString GetQueryString(
bool bAddEmptyValueAfterEqual)
const;
98 IVSIS3LikeHandleHelper() =
default;
99 virtual ~IVSIS3LikeHandleHelper() =
default;
101 void ResetQueryParameters();
104 virtual struct curl_slist* GetCurlHeaders(
const CPLString& osVerb,
105 const struct curl_slist* psExistingHeaders,
106 const void *pabyDataContent =
nullptr,
107 size_t nBytesContent = 0)
const = 0;
109 virtual bool AllowAutomaticRedirection() {
return true; }
110 virtual bool CanRestartOnError(
const char*,
const char* ,
111 bool ,
bool* =
nullptr) {
return false;}
113 virtual const CPLString& GetURL()
const = 0;
116 virtual CPLString GetCopySourceHeader()
const {
return std::string(); }
117 virtual const char* GetMetadataDirectiveREPLACE()
const {
return ""; }
119 static bool GetBucketAndObjectKey(
const char* pszURI,
120 const char* pszFSPrefix,
125 static CPLString BuildCanonicalizedHeaders(
126 std::map<CPLString, CPLString>& oSortedMapHeaders,
127 const struct curl_slist* psExistingHeaders,
128 const char* pszHeaderPrefix);
133 enum class AWSCredentialsSource
144 class VSIS3HandleHelper final:
public IVSIS3LikeHandleHelper
157 bool m_bUseHTTPS =
false;
158 bool m_bUseVirtualHosting =
false;
159 AWSCredentialsSource m_eCredentialsSource = AWSCredentialsSource::REGULAR;
161 void RebuildURL()
override;
163 static bool GetOrRefreshTemporaryCredentialsForRole(
bool bForceRefresh,
169 static bool GetConfigurationFromAssumeRoleWithWebIdentity(
bool bForceRefresh,
170 const std::string& osPathForOption,
171 const std::string& osRoleArnIn,
172 const std::string& osWebIdentityTokenFileIn,
177 static bool GetConfigurationFromEC2(
bool bForceRefresh,
178 const std::string& osPathForOption,
183 static bool GetConfigurationFromAWSConfigFiles(
184 const std::string& osPathForOption,
185 const char* pszProfile,
198 static bool GetConfiguration(
const std::string& osPathForOption,
204 AWSCredentialsSource& eCredentialsSource);
206 void RefreshCredentials(
const std::string& osPathForOption,
207 bool bForceRefresh)
const;
212 VSIS3HandleHelper(
const CPLString& osSecretAccessKey,
220 bool bUseHTTPS,
bool bUseVirtualHosting,
221 AWSCredentialsSource eCredentialsSource);
222 ~VSIS3HandleHelper();
224 static VSIS3HandleHelper* BuildFromURI(
const char* pszURI,
225 const char* pszFSPrefix,
231 bool bUseHTTPS,
bool bUseVirtualHosting);
233 struct curl_slist* GetCurlHeaders(
235 const struct curl_slist* psExistingHeaders,
236 const void *pabyDataContent =
nullptr,
237 size_t nBytesContent = 0)
const override;
239 bool AllowAutomaticRedirection()
override {
return false; }
240 bool CanRestartOnError(
const char*,
const char* pszHeaders,
242 bool* pbUpdateMap =
nullptr)
override;
244 const CPLString& GetURL()
const override {
return m_osURL; }
245 const CPLString& GetBucket()
const {
return m_osBucket; }
246 const CPLString& GetObjectKey()
const {
return m_osObjectKey; }
247 const CPLString& GetEndpoint()
const {
return m_osEndpoint; }
248 const CPLString& GetRegion()
const {
return m_osRegion; }
249 const CPLString& GetRequestPayer()
const {
return m_osRequestPayer; }
250 bool GetVirtualHosting()
const {
return m_bUseVirtualHosting; }
251 void SetEndpoint(
const CPLString &osStr);
253 void SetRequestPayer(
const CPLString &osStr);
254 void SetVirtualHosting(
bool b);
256 CPLString GetCopySourceHeader()
const override {
return "x-amz-copy-source"; }
257 const char* GetMetadataDirectiveREPLACE()
const override {
return "x-amz-metadata-directive: REPLACE"; }
261 static void CleanMutex();
262 static void ClearCache();
265 class VSIS3UpdateParams
271 bool m_bUseVirtualHosting =
false;
273 VSIS3UpdateParams() =
default;
275 explicit VSIS3UpdateParams(
const VSIS3HandleHelper* poHelper) :
276 m_osRegion(poHelper->GetRegion()),
277 m_osEndpoint(poHelper->GetEndpoint()),
278 m_osRequestPayer(poHelper->GetRequestPayer()),
279 m_bUseVirtualHosting(poHelper->GetVirtualHosting()) {}
281 void UpdateHandlerHelper(VSIS3HandleHelper* poHelper) {
282 poHelper->SetRegion(m_osRegion);
283 poHelper->SetEndpoint(m_osEndpoint);
284 poHelper->SetRequestPayer(m_osRequestPayer);
285 poHelper->SetVirtualHosting(m_bUseVirtualHosting);
288 static std::mutex gsMutex;
289 static std::map< CPLString, VSIS3UpdateParams > goMapBucketsToS3Params;
290 static void UpdateMapFromHandle( IVSIS3LikeHandleHelper* poHandleHelper );
291 static void UpdateHandleFromMap( IVSIS3LikeHandleHelper* poHandleHelper );
292 static void ClearCache();