GDAL
cpl_aws.h
1 /**********************************************************************
2  * $Id: cpl_aws.h a0f6709dc75fe9cb5c842fd4395e6072db3d9620 2020-03-05 16:04:26 +0100 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 
113  virtual CPLString GetCopySourceHeader() const { return std::string(); }
114 
115  static bool GetBucketAndObjectKey(const char* pszURI,
116  const char* pszFSPrefix,
117  bool bAllowNoObject,
118  CPLString &osBucketOut,
119  CPLString &osObjectKeyOut);
120 
121  static CPLString BuildCanonicalizedHeaders(
122  std::map<CPLString, CPLString>& oSortedMapHeaders,
123  const struct curl_slist* psExistingHeaders,
124  const char* pszHeaderPrefix);
125 
126  static CPLString GetRFC822DateTime();
127 };
128 
129 class VSIS3HandleHelper final: public IVSIS3LikeHandleHelper
130 {
131  CPL_DISALLOW_COPY_ASSIGN(VSIS3HandleHelper)
132 
133  CPLString m_osURL{};
134  mutable CPLString m_osSecretAccessKey{};
135  mutable CPLString m_osAccessKeyId{};
136  mutable CPLString m_osSessionToken{};
137  CPLString m_osEndpoint{};
138  CPLString m_osRegion{};
139  CPLString m_osRequestPayer{};
140  CPLString m_osBucket{};
141  CPLString m_osObjectKey{};
142  bool m_bUseHTTPS = false;
143  bool m_bUseVirtualHosting = false;
144  bool m_bFromEC2 = false;
145 
146  void RebuildURL() override;
147 
148  static bool GetConfigurationFromEC2(CPLString& osSecretAccessKey,
149  CPLString& osAccessKeyId,
150  CPLString& osSessionToken);
151 
152  static bool GetConfigurationFromAWSConfigFiles(
153  CPLString& osSecretAccessKey,
154  CPLString& osAccessKeyId,
155  CPLString& osSessionToken,
156  CPLString& osRegion,
157  CPLString& osCredentials);
158 
159  static bool GetConfiguration(CSLConstList papszOptions,
160  CPLString& osSecretAccessKey,
161  CPLString& osAccessKeyId,
162  CPLString& osSessionToken,
163  CPLString& osRegion,
164  bool& bFromEC2);
165  protected:
166 
167  public:
168  VSIS3HandleHelper(const CPLString& osSecretAccessKey,
169  const CPLString& osAccessKeyId,
170  const CPLString& osSessionToken,
171  const CPLString& osEndpoint,
172  const CPLString& osRegion,
173  const CPLString& osRequestPayer,
174  const CPLString& osBucket,
175  const CPLString& osObjectKey,
176  bool bUseHTTPS, bool bUseVirtualHosting, bool bFromEC2);
177  ~VSIS3HandleHelper();
178 
179  static VSIS3HandleHelper* BuildFromURI(const char* pszURI,
180  const char* pszFSPrefix,
181  bool bAllowNoObject,
182  CSLConstList papszOptions = nullptr);
183  static CPLString BuildURL(const CPLString& osEndpoint,
184  const CPLString& osBucket,
185  const CPLString& osObjectKey,
186  bool bUseHTTPS, bool bUseVirtualHosting);
187 
188  struct curl_slist* GetCurlHeaders(
189  const CPLString& osVerb,
190  const struct curl_slist* psExistingHeaders,
191  const void *pabyDataContent = nullptr,
192  size_t nBytesContent = 0) const override;
193 
194  bool AllowAutomaticRedirection() override { return false; }
195  bool CanRestartOnError(const char*, const char* pszHeaders,
196  bool bSetError,
197  bool* pbUpdateMap = nullptr) override;
198 
199  const CPLString& GetURL() const override { return m_osURL; }
200  const CPLString& GetBucket() const { return m_osBucket; }
201  const CPLString& GetObjectKey() const { return m_osObjectKey; }
202  const CPLString& GetEndpoint()const { return m_osEndpoint; }
203  const CPLString& GetRegion() const { return m_osRegion; }
204  const CPLString& GetRequestPayer() const { return m_osRequestPayer; }
205  bool GetVirtualHosting() const { return m_bUseVirtualHosting; }
206  void SetEndpoint(const CPLString &osStr);
207  void SetRegion(const CPLString &osStr);
208  void SetRequestPayer(const CPLString &osStr);
209  void SetVirtualHosting(bool b);
210 
211  CPLString GetCopySourceHeader() const override { return "x-amz-copy-source"; }
212 
213  CPLString GetSignedURL(CSLConstList papszOptions);
214 
215  static void CleanMutex();
216  static void ClearCache();
217 };
218 
219 class VSIS3UpdateParams
220 {
221  public:
222  CPLString m_osRegion{};
223  CPLString m_osEndpoint{};
224  CPLString m_osRequestPayer{};
225  bool m_bUseVirtualHosting = false;
226 
227  VSIS3UpdateParams() = default;
228 
229  explicit VSIS3UpdateParams(const VSIS3HandleHelper* poHelper) :
230  m_osRegion(poHelper->GetRegion()),
231  m_osEndpoint(poHelper->GetEndpoint()),
232  m_osRequestPayer(poHelper->GetRequestPayer()),
233  m_bUseVirtualHosting(poHelper->GetVirtualHosting()) {}
234 
235  void UpdateHandlerHelper(VSIS3HandleHelper* poHelper) {
236  poHelper->SetRegion(m_osRegion);
237  poHelper->SetEndpoint(m_osEndpoint);
238  poHelper->SetRequestPayer(m_osRequestPayer);
239  poHelper->SetVirtualHosting(m_bUseVirtualHosting);
240  }
241 
242  static std::mutex gsMutex;
243  static std::map< CPLString, VSIS3UpdateParams > goMapBucketsToS3Params;
244  static void UpdateMapFromHandle( IVSIS3LikeHandleHelper* poHandleHelper );
245  static void UpdateHandleFromMap( IVSIS3LikeHandleHelper* poHandleHelper );
246  static void ClearCache();
247 };
248 
249 #endif /* HAVE_CURL */
250 
251 #endif /* #ifndef DOXYGEN_SKIP */
252 
253 #endif /* CPL_AWS_INCLUDED_H */
CPLString
Convenient string class based on std::string.
Definition: cpl_string.h:333
CSLConstList
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1212
cpl_string.h
CPL_DISALLOW_COPY_ASSIGN
#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:1003