15#ifndef CPL_AWS_INCLUDED_H
16#define CPL_AWS_INCLUDED_H
30std::string CPLGetLowerCaseHexSHA256(
const void *pabyData,
size_t nBytes);
31std::string CPLGetLowerCaseHexSHA256(
const std::string &osStr);
33std::string CPLGetAWS_SIGN4_Timestamp(
GIntBig timestamp);
35std::string CPLAWSURLEncode(
const std::string &osURL,
bool bEncodeSlash =
true);
37std::string CPLAWSGetHeaderVal(
const struct curl_slist *psExistingHeaders,
40std::string CPLGetAWS_SIGN4_Signature(
41 const std::string &osSecretAccessKey,
const std::string &osAccessToken,
42 const std::string &osRegion,
const std::string &osRequestPayer,
43 const std::string &osService,
const std::string &osVerb,
44 const struct curl_slist *psExistingHeaders,
const std::string &osHost,
45 const std::string &osCanonicalURI,
46 const std::string &osCanonicalQueryString,
47 const std::string &osXAMZContentSHA256,
bool bAddHeaderAMZContentSHA256,
48 const std::string &osTimestamp, std::string &osSignedHeaders);
50std::string CPLGetAWS_SIGN4_Authorization(
51 const std::string &osSecretAccessKey,
const std::string &osAccessKeyId,
52 const std::string &osAccessToken,
const std::string &osRegion,
53 const std::string &osRequestPayer,
const std::string &osService,
54 const std::string &osVerb,
const struct curl_slist *psExistingHeaders,
55 const std::string &osHost,
const std::string &osCanonicalURI,
56 const std::string &osCanonicalQueryString,
57 const std::string &osXAMZContentSHA256,
bool bAddHeaderAMZContentSHA256,
58 const std::string &osTimestamp);
60class IVSIS3LikeHandleHelper
65 std::map<std::string, std::string> m_oMapQueryParameters{};
67 virtual void RebuildURL() = 0;
68 std::string GetQueryString(
bool bAddEmptyValueAfterEqual)
const;
71 IVSIS3LikeHandleHelper() =
default;
72 virtual ~IVSIS3LikeHandleHelper() =
default;
74 void ResetQueryParameters();
75 void AddQueryParameter(
const std::string &osKey,
76 const std::string &osValue);
78 virtual struct curl_slist *
79 GetCurlHeaders(
const std::string &osVerb,
80 const struct curl_slist *psExistingHeaders,
81 const void *pabyDataContent =
nullptr,
82 size_t nBytesContent = 0)
const = 0;
84 virtual bool AllowAutomaticRedirection()
89 virtual bool CanRestartOnError(
const char *,
const char * ,
95 virtual const std::string &GetURL()
const = 0;
96 std::string GetURLNoKVP()
const;
98 virtual std::string GetCopySourceHeader()
const
100 return std::string();
103 virtual const char *GetMetadataDirectiveREPLACE()
const
108 static bool GetBucketAndObjectKey(
const char *pszURI,
109 const char *pszFSPrefix,
111 std::string &osBucketOut,
112 std::string &osObjectKeyOut);
114 static std::string BuildCanonicalizedHeaders(
115 std::map<std::string, std::string> &oSortedMapHeaders,
116 const struct curl_slist *psExistingHeaders,
117 const char *pszHeaderPrefix);
119 static std::string GetRFC822DateTime();
122enum class AWSCredentialsSource
137class VSIS3HandleHelper final :
public IVSIS3LikeHandleHelper
141 std::string m_osURL{};
142 mutable std::string m_osSecretAccessKey{};
143 mutable std::string m_osAccessKeyId{};
144 mutable std::string m_osSessionToken{};
145 std::string m_osEndpoint{};
146 std::string m_osRegion{};
147 std::string m_osRequestPayer{};
148 std::string m_osBucket{};
149 std::string m_osObjectKey{};
150 bool m_bUseHTTPS =
false;
151 bool m_bUseVirtualHosting =
false;
152 AWSCredentialsSource m_eCredentialsSource = AWSCredentialsSource::REGULAR;
154 void RebuildURL()
override;
156 static bool GetOrRefreshTemporaryCredentialsForRole(
157 bool bForceRefresh, std::string &osSecretAccessKey,
158 std::string &osAccessKeyId, std::string &osSessionToken,
159 std::string &osRegion);
161 static bool GetOrRefreshTemporaryCredentialsForSSO(
162 bool bForceRefresh, std::string &osSecretAccessKey,
163 std::string &osAccessKeyId, std::string &osSessionToken,
164 std::string &osRegion);
166 static bool GetConfigurationFromAssumeRoleWithWebIdentity(
167 bool bForceRefresh,
const std::string &osPathForOption,
168 const std::string &osRoleArnIn,
169 const std::string &osWebIdentityTokenFileIn,
170 std::string &osSecretAccessKey, std::string &osAccessKeyId,
171 std::string &osSessionToken);
173 static bool GetConfigurationFromEC2(
bool bForceRefresh,
174 const std::string &osPathForOption,
175 std::string &osSecretAccessKey,
176 std::string &osAccessKeyId,
177 std::string &osSessionToken);
179 static bool GetConfigurationFromAWSConfigFiles(
180 const std::string &osPathForOption,
const char *pszProfile,
181 std::string &osSecretAccessKey, std::string &osAccessKeyId,
182 std::string &osSessionToken, std::string &osRegion,
183 std::string &osCredentials, std::string &osRoleArn,
184 std::string &osSourceProfile, std::string &osExternalId,
185 std::string &osMFASerial, std::string &osRoleSessionName,
186 std::string &osWebIdentityTokenFile, std::string &osSSOStartURL,
187 std::string &osSSOAccountID, std::string &osSSORoleName);
189 static bool GetConfiguration(
const std::string &osPathForOption,
191 std::string &osSecretAccessKey,
192 std::string &osAccessKeyId,
193 std::string &osSessionToken,
194 std::string &osRegion,
195 AWSCredentialsSource &eCredentialsSource);
197 void RefreshCredentials(
const std::string &osPathForOption,
198 bool bForceRefresh)
const;
203 const std::string &osSecretAccessKey,
const std::string &osAccessKeyId,
204 const std::string &osSessionToken,
const std::string &osEndpoint,
205 const std::string &osRegion,
const std::string &osRequestPayer,
206 const std::string &osBucket,
const std::string &osObjectKey,
207 bool bUseHTTPS,
bool bUseVirtualHosting,
208 AWSCredentialsSource eCredentialsSource);
209 ~VSIS3HandleHelper();
211 static VSIS3HandleHelper *BuildFromURI(
const char *pszURI,
212 const char *pszFSPrefix,
215 static std::string BuildURL(
const std::string &osEndpoint,
216 const std::string &osBucket,
217 const std::string &osObjectKey,
bool bUseHTTPS,
218 bool bUseVirtualHosting);
221 GetCurlHeaders(
const std::string &osVerb,
222 const struct curl_slist *psExistingHeaders,
223 const void *pabyDataContent =
nullptr,
224 size_t nBytesContent = 0)
const override;
226 bool AllowAutomaticRedirection()
override
231 bool CanRestartOnError(
const char *,
const char *pszHeaders,
232 bool bSetError)
override;
234 const std::string &GetURL()
const override
239 const std::string &GetBucket()
const
244 const std::string &GetObjectKey()
const
246 return m_osObjectKey;
249 const std::string &GetEndpoint()
const
254 const std::string &GetRegion()
const
259 const std::string &GetRequestPayer()
const
261 return m_osRequestPayer;
264 bool GetVirtualHosting()
const
266 return m_bUseVirtualHosting;
269 void SetEndpoint(
const std::string &osStr);
270 void SetRegion(
const std::string &osStr);
271 void SetRequestPayer(
const std::string &osStr);
272 void SetVirtualHosting(
bool b);
274 std::string GetCopySourceHeader()
const override
276 return "x-amz-copy-source";
279 const char *GetMetadataDirectiveREPLACE()
const override
281 return "x-amz-metadata-directive: REPLACE";
286 static void CleanMutex();
287 static void ClearCache();
290class VSIS3UpdateParams
293 std::string m_osRegion{};
294 std::string m_osEndpoint{};
295 std::string m_osRequestPayer{};
296 bool m_bUseVirtualHosting =
false;
298 explicit VSIS3UpdateParams(
const VSIS3HandleHelper *poHelper)
299 : m_osRegion(poHelper->GetRegion()),
300 m_osEndpoint(poHelper->GetEndpoint()),
301 m_osRequestPayer(poHelper->GetRequestPayer()),
302 m_bUseVirtualHosting(poHelper->GetVirtualHosting())
306 void UpdateHandlerHelper(VSIS3HandleHelper *poHelper)
308 poHelper->SetRegion(m_osRegion);
309 poHelper->SetEndpoint(m_osEndpoint);
310 poHelper->SetRequestPayer(m_osRequestPayer);
311 poHelper->SetVirtualHosting(m_bUseVirtualHosting);
314 static std::mutex gsMutex;
315 static std::map<std::string, VSIS3UpdateParams> goMapBucketsToS3Params;
318 VSIS3UpdateParams() =
default;
320 static void UpdateMapFromHandle(VSIS3HandleHelper *poS3HandleHelper);
321 static void UpdateHandleFromMap(VSIS3HandleHelper *poS3HandleHelper);
322 static void ClearCache();
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition cpl_port.h:1030
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition cpl_port.h:1179
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition cpl_port.h:199
Various convenience functions for working with strings and string lists.