GDAL
cpl_aws.h
1 /**********************************************************************
2  * $Id: cpl_aws.h 35282e555185d7a64f56d1074ea12930d3f05dd7 2021-05-25 17:44:13 +0200 Even Rouault $
3  *
4  * Name: cpl_aws.h
5  * Project: CPL - Common Portability Library
6  * Purpose: Amazon Web Services routines
7  * Author: Even Rouault <even.rouault at spatialys.com>
8  *
9  **********************************************************************
10  * Copyright (c) 2015, Even Rouault <even.rouault at spatialys.com>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  ****************************************************************************/
30 
31 #ifndef CPL_AWS_INCLUDED_H
32 #define CPL_AWS_INCLUDED_H
33 
34 #ifndef DOXYGEN_SKIP
35 
36 #ifdef HAVE_CURL
37 
38 #include <cstddef>
39 #include <mutex>
40 
41 #include "cpl_string.h"
42 
43 #include <curl/curl.h>
44 #include <map>
45 
46 CPLString CPLGetLowerCaseHexSHA256( const void *pabyData, size_t nBytes );
47 CPLString CPLGetLowerCaseHexSHA256( const CPLString& osStr );
48 
49 CPLString CPLGetAWS_SIGN4_Timestamp();
50 
51 CPLString CPLAWSURLEncode(const CPLString& osURL, bool bEncodeSlash = true);
52 
53 CPLString CPLAWSGetHeaderVal(const struct curl_slist* psExistingHeaders,
54  const char* pszKey);
55 
57 CPLGetAWS_SIGN4_Signature( const CPLString& osSecretAccessKey,
58  const CPLString& osAccessToken,
59  const CPLString& osRegion,
60  const CPLString& osRequestPayer,
61  const CPLString& osService,
62  const CPLString& osVerb,
63  const struct curl_slist* psExistingHeaders,
64  const CPLString& osHost,
65  const CPLString& osCanonicalURI,
66  const CPLString& osCanonicalQueryString,
67  const CPLString& osXAMZContentSHA256,
68  const CPLString& osTimestamp,
69  CPLString& osSignedHeaders );
70 
71 CPLString CPLGetAWS_SIGN4_Authorization(const CPLString& osSecretAccessKey,
72  const CPLString& osAccessKeyId,
73  const CPLString& osAccessToken,
74  const CPLString& osRegion,
75  const CPLString& osRequestPayer,
76  const CPLString& osService,
77  const CPLString& osVerb,
78  const struct curl_slist* psExistingHeaders,
79  const CPLString& osHost,
80  const CPLString& osCanonicalURI,
81  const CPLString& osCanonicalQueryString,
82  const CPLString& osXAMZContentSHA256,
83  const CPLString& osTimestamp);
84 
85 class IVSIS3LikeHandleHelper
86 {
87  CPL_DISALLOW_COPY_ASSIGN(IVSIS3LikeHandleHelper)
88 
89 protected:
90  std::map<CPLString, CPLString> m_oMapQueryParameters{};
91 
92  virtual void RebuildURL() = 0;
93  CPLString GetQueryString(bool bAddEmptyValueAfterEqual) const;
94 
95 public:
96  IVSIS3LikeHandleHelper() = default;
97  virtual ~IVSIS3LikeHandleHelper() = default;
98 
99  void ResetQueryParameters();
100  void AddQueryParameter(const CPLString& osKey, const CPLString& osValue);
101 
102  virtual struct curl_slist* GetCurlHeaders(const CPLString& osVerb,
103  const struct curl_slist* psExistingHeaders,
104  const void *pabyDataContent = nullptr,
105  size_t nBytesContent = 0) const = 0;
106 
107  virtual bool AllowAutomaticRedirection() { return true; }
108  virtual bool CanRestartOnError(const char*, const char* /* pszHeaders*/,
109  bool /*bSetError*/, bool* /*pbUpdateMap*/ = nullptr) { return false;}
110 
111  virtual const CPLString& GetURL() const = 0;
112  CPLString GetURLNoKVP() const;
113 
114  virtual CPLString GetCopySourceHeader() const { return std::string(); }
115  virtual const char* GetMetadataDirectiveREPLACE() const { return ""; }
116 
117  static bool GetBucketAndObjectKey(const char* pszURI,
118  const char* pszFSPrefix,
119  bool bAllowNoObject,
120  CPLString &osBucketOut,
121  CPLString &osObjectKeyOut);
122 
123  static CPLString BuildCanonicalizedHeaders(
124  std::map<CPLString, CPLString>& oSortedMapHeaders,
125  const struct curl_slist* psExistingHeaders,
126  const char* pszHeaderPrefix);
127 
128  static CPLString GetRFC822DateTime();
129 };
130 
131 class VSIS3HandleHelper final: public IVSIS3LikeHandleHelper
132 {
133  CPL_DISALLOW_COPY_ASSIGN(VSIS3HandleHelper)
134 
135  CPLString m_osURL{};
136  mutable CPLString m_osSecretAccessKey{};
137  mutable CPLString m_osAccessKeyId{};
138  mutable CPLString m_osSessionToken{};
139  CPLString m_osEndpoint{};
140  CPLString m_osRegion{};
141  CPLString m_osRequestPayer{};
142  CPLString m_osBucket{};
143  CPLString m_osObjectKey{};
144  bool m_bUseHTTPS = false;
145  bool m_bUseVirtualHosting = false;
146  bool m_bFromEC2 = false;
147 
148  void RebuildURL() override;
149 
150  static bool GetConfigurationFromEC2(CPLString& osSecretAccessKey,
151  CPLString& osAccessKeyId,
152  CPLString& osSessionToken);
153 
154  static bool GetConfigurationFromAWSConfigFiles(
155  CPLString& osSecretAccessKey,
156  CPLString& osAccessKeyId,
157  CPLString& osSessionToken,
158  CPLString& osRegion,
159  CPLString& osCredentials);
160 
161  static bool GetConfiguration(CSLConstList papszOptions,
162  CPLString& osSecretAccessKey,
163  CPLString& osAccessKeyId,
164  CPLString& osSessionToken,
165  CPLString& osRegion,
166  bool& bFromEC2);
167  protected:
168 
169  public:
170  VSIS3HandleHelper(const CPLString& osSecretAccessKey,
171  const CPLString& osAccessKeyId,
172  const CPLString& osSessionToken,
173  const CPLString& osEndpoint,
174  const CPLString& osRegion,
175  const CPLString& osRequestPayer,
176  const CPLString& osBucket,
177  const CPLString& osObjectKey,
178  bool bUseHTTPS, bool bUseVirtualHosting, bool bFromEC2);
179  ~VSIS3HandleHelper();
180 
181  static VSIS3HandleHelper* BuildFromURI(const char* pszURI,
182  const char* pszFSPrefix,
183  bool bAllowNoObject,
184  CSLConstList papszOptions = nullptr);
185  static CPLString BuildURL(const CPLString& osEndpoint,
186  const CPLString& osBucket,
187  const CPLString& osObjectKey,
188  bool bUseHTTPS, bool bUseVirtualHosting);
189 
190  struct curl_slist* GetCurlHeaders(
191  const CPLString& osVerb,
192  const struct curl_slist* psExistingHeaders,
193  const void *pabyDataContent = nullptr,
194  size_t nBytesContent = 0) const override;
195 
196  bool AllowAutomaticRedirection() override { return false; }
197  bool CanRestartOnError(const char*, const char* pszHeaders,
198  bool bSetError,
199  bool* pbUpdateMap = nullptr) override;
200 
201  const CPLString& GetURL() const override { return m_osURL; }
202  const CPLString& GetBucket() const { return m_osBucket; }
203  const CPLString& GetObjectKey() const { return m_osObjectKey; }
204  const CPLString& GetEndpoint()const { return m_osEndpoint; }
205  const CPLString& GetRegion() const { return m_osRegion; }
206  const CPLString& GetRequestPayer() const { return m_osRequestPayer; }
207  bool GetVirtualHosting() const { return m_bUseVirtualHosting; }
208  void SetEndpoint(const CPLString &osStr);
209  void SetRegion(const CPLString &osStr);
210  void SetRequestPayer(const CPLString &osStr);
211  void SetVirtualHosting(bool b);
212 
213  CPLString GetCopySourceHeader() const override { return "x-amz-copy-source"; }
214  const char* GetMetadataDirectiveREPLACE() const override { return "x-amz-metadata-directive: REPLACE"; }
215 
216  CPLString GetSignedURL(CSLConstList papszOptions);
217 
218  static void CleanMutex();
219  static void ClearCache();
220 };
221 
222 class VSIS3UpdateParams
223 {
224  public:
225  CPLString m_osRegion{};
226  CPLString m_osEndpoint{};
227  CPLString m_osRequestPayer{};
228  bool m_bUseVirtualHosting = false;
229 
230  VSIS3UpdateParams() = default;
231 
232  explicit VSIS3UpdateParams(const VSIS3HandleHelper* poHelper) :
233  m_osRegion(poHelper->GetRegion()),
234  m_osEndpoint(poHelper->GetEndpoint()),
235  m_osRequestPayer(poHelper->GetRequestPayer()),
236  m_bUseVirtualHosting(poHelper->GetVirtualHosting()) {}
237 
238  void UpdateHandlerHelper(VSIS3HandleHelper* poHelper) {
239  poHelper->SetRegion(m_osRegion);
240  poHelper->SetEndpoint(m_osEndpoint);
241  poHelper->SetRequestPayer(m_osRequestPayer);
242  poHelper->SetVirtualHosting(m_bUseVirtualHosting);
243  }
244 
245  static std::mutex gsMutex;
246  static std::map< CPLString, VSIS3UpdateParams > goMapBucketsToS3Params;
247  static void UpdateMapFromHandle( IVSIS3LikeHandleHelper* poHandleHelper );
248  static void UpdateHandleFromMap( IVSIS3LikeHandleHelper* poHandleHelper );
249  static void ClearCache();
250 };
251 
252 #endif /* HAVE_CURL */
253 
254 #endif /* #ifndef DOXYGEN_SKIP */
255 
256 #endif /* CPL_AWS_INCLUDED_H */
Convenient string class based on std::string.
Definition: cpl_string.h:333
#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:955
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1169
Various convenience functions for working with strings and string lists.