14#ifndef CPL_AWS_INCLUDED_H
15#define CPL_AWS_INCLUDED_H
29std::string CPLGetLowerCaseHexSHA256(
const void *pabyData,
size_t nBytes);
30std::string CPLGetLowerCaseHexSHA256(
const std::string &osStr);
32std::string CPLGetAWS_SIGN4_Timestamp(
GIntBig timestamp);
34std::string CPLAWSURLEncode(
const std::string &osURL,
bool bEncodeSlash =
true);
36std::string CPLAWSGetHeaderVal(
const struct curl_slist *psExistingHeaders,
39class IVSIS3LikeHandleHelper
44 std::map<std::string, std::string> m_oMapQueryParameters{};
46 virtual void RebuildURL() = 0;
47 std::string GetQueryString(
bool bAddEmptyValueAfterEqual)
const;
50 IVSIS3LikeHandleHelper();
51 virtual ~IVSIS3LikeHandleHelper();
53 void ResetQueryParameters();
54 void AddQueryParameter(
const std::string &osKey,
55 const std::string &osValue);
57 virtual struct curl_slist *
58 GetCurlHeaders(
const std::string &osVerb,
struct curl_slist *psHeaders,
59 const void *pabyDataContent =
nullptr,
60 size_t nBytesContent = 0)
const = 0;
62 virtual bool AllowAutomaticRedirection()
67 virtual bool CanRestartOnError(
const char *,
const char * ,
73 virtual const std::string &GetURL()
const = 0;
74 std::string GetURLNoKVP()
const;
76 virtual std::string GetCopySourceHeader()
const
81 virtual const char *GetMetadataDirectiveREPLACE()
const
86 static bool GetBucketAndObjectKey(
const char *pszURI,
87 const char *pszFSPrefix,
89 std::string &osBucketOut,
90 std::string &osObjectKeyOut);
92 static std::string BuildCanonicalizedHeaders(
93 std::map<std::string, std::string> &oSortedMapHeaders,
94 const struct curl_slist *psExistingHeaders,
95 const char *pszHeaderPrefix);
97 static std::string GetRFC822DateTime();
100enum class AWSCredentialsSource
120class VSIS3HandleHelper final :
public IVSIS3LikeHandleHelper
124 std::string m_osURL{};
125 std::string m_osService{};
126 mutable std::string m_osSecretAccessKey{};
127 mutable std::string m_osAccessKeyId{};
128 mutable std::string m_osSessionToken{};
129 std::string m_osS3SessionToken{};
130 std::string m_osEndpoint{};
131 std::string m_osRegion{};
132 std::string m_osRequestPayer{};
133 std::string m_osBucket{};
134 std::string m_osObjectKey{};
135 bool m_bUseHTTPS =
false;
136 bool m_bUseVirtualHosting =
false;
137 bool m_bIsDirectoryBucket =
false;
138 AWSCredentialsSource m_eCredentialsSource = AWSCredentialsSource::REGULAR;
140 void RebuildURL()
override;
142 static bool GetOrRefreshTemporaryCredentialsForRole(
143 bool bForceRefresh, std::string &osSecretAccessKey,
144 std::string &osAccessKeyId, std::string &osSessionToken,
145 std::string &osRegion);
147 static bool GetOrRefreshTemporaryCredentialsForSSO(
148 bool bForceRefresh, std::string &osSecretAccessKey,
149 std::string &osAccessKeyId, std::string &osSessionToken,
150 std::string &osRegion);
152 static bool GetOrRefreshTemporaryCredentialsFromProcess(
153 bool bForceRefresh, std::string &osSecretAccessKey,
154 std::string &osAccessKeyId, std::string &osSessionToken);
156 static bool GetConfigurationFromAssumeRoleWithWebIdentity(
157 bool bForceRefresh,
const std::string &osPathForOption,
158 const std::string &osRoleArnIn,
159 const std::string &osWebIdentityTokenFileIn,
160 std::string &osSecretAccessKey, std::string &osAccessKeyId,
161 std::string &osSessionToken);
163 static bool GetConfigurationFromEC2(
bool bForceRefresh,
164 const std::string &osPathForOption,
165 std::string &osSecretAccessKey,
166 std::string &osAccessKeyId,
167 std::string &osSessionToken);
169 static bool GetConfigurationFromAWSConfigFiles(
170 const std::string &osPathForOption,
const char *pszProfile,
171 std::string &osSecretAccessKey, std::string &osAccessKeyId,
172 std::string &osSessionToken, std::string &osRegion,
173 std::string &osCredentials, std::string &osRoleArn,
174 std::string &osSourceProfile, std::string &osExternalId,
175 std::string &osMFASerial, std::string &osRoleSessionName,
176 std::string &osWebIdentityTokenFile, std::string &osSSOStartURL,
177 std::string &osSSOAccountID, std::string &osSSORoleName,
178 std::string &osSSOSession, std::string &osCredentialProcess);
180 static bool GetConfiguration(
const std::string &osPathForOption,
182 std::string &osSecretAccessKey,
183 std::string &osAccessKeyId,
184 std::string &osSessionToken,
185 std::string &osRegion,
186 AWSCredentialsSource &eCredentialsSource);
188 void RefreshCredentials(
const std::string &osPathForOption,
189 bool bForceRefresh)
const;
194 const std::string &osService,
const std::string &osSecretAccessKey,
195 const std::string &osAccessKeyId,
const std::string &osSessionToken,
196 const std::string &osS3SessionToken,
const std::string &osEndpoint,
197 const std::string &osRegion,
const std::string &osRequestPayer,
198 const std::string &osBucket,
const std::string &osObjectKey,
199 bool bUseHTTPS,
bool bUseVirtualHosting,
200 AWSCredentialsSource eCredentialsSource,
bool bIsDirectoryBucket);
201 ~VSIS3HandleHelper()
override;
203 static VSIS3HandleHelper *BuildFromURI(
const char *pszURI,
204 const char *pszFSPrefix,
207 static std::string BuildURL(
const std::string &osEndpoint,
208 const std::string &osBucket,
209 const std::string &osObjectKey,
bool bUseHTTPS,
210 bool bUseVirtualHosting);
212 struct curl_slist *GetCurlHeaders(
const std::string &osVerb,
213 struct curl_slist *psHeaders,
214 const void *pabyDataContent =
nullptr,
215 size_t nBytesContent = 0)
const override;
217 bool IsDirectoryBucket()
const
219 return m_bIsDirectoryBucket;
222 bool AllowAutomaticRedirection()
override
227 bool CanRestartOnError(
const char *,
const char *pszHeaders,
228 bool bSetError)
override;
230 const std::string &GetURL()
const override
235 const std::string &GetBucket()
const
240 const std::string &GetObjectKey()
const
242 return m_osObjectKey;
245 const std::string &GetEndpoint()
const
250 const std::string &GetRegion()
const
255 const std::string &GetRequestPayer()
const
257 return m_osRequestPayer;
260 bool GetVirtualHosting()
const
262 return m_bUseVirtualHosting;
265 void SetEndpoint(
const std::string &osStr);
266 void SetRegion(
const std::string &osStr);
267 void SetRequestPayer(
const std::string &osStr);
268 void SetVirtualHosting(
bool b);
270 std::string GetCopySourceHeader()
const override
272 return "x-amz-copy-source";
275 const char *GetMetadataDirectiveREPLACE()
const override
277 return "x-amz-metadata-directive: REPLACE";
282 static void CleanMutex();
283 static void ClearCache();
286class VSIS3UpdateParams
289 std::string m_osRegion{};
290 std::string m_osEndpoint{};
291 std::string m_osRequestPayer{};
292 bool m_bUseVirtualHosting =
false;
294 explicit VSIS3UpdateParams(
const VSIS3HandleHelper *poHelper)
295 : m_osRegion(poHelper->GetRegion()),
296 m_osEndpoint(poHelper->GetEndpoint()),
297 m_osRequestPayer(poHelper->GetRequestPayer()),
298 m_bUseVirtualHosting(poHelper->GetVirtualHosting())
302 void UpdateHandlerHelper(VSIS3HandleHelper *poHelper)
304 poHelper->SetRegion(m_osRegion);
305 poHelper->SetEndpoint(m_osEndpoint);
306 poHelper->SetRequestPayer(m_osRequestPayer);
307 poHelper->SetVirtualHosting(m_bUseVirtualHosting);
310 static std::mutex gsMutex;
311 static std::map<std::string, VSIS3UpdateParams> goMapBucketsToS3Params;
314 VSIS3UpdateParams() =
default;
316 static void UpdateMapFromHandle(VSIS3HandleHelper *poS3HandleHelper);
317 static void UpdateHandleFromMap(VSIS3HandleHelper *poS3HandleHelper);
318 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:936
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition cpl_port.h:1087
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition cpl_port.h:205
Various convenience functions for working with strings and string lists.