GDAL
cpl_aws.h
1 /**********************************************************************
2  * $Id: cpl_aws.h 5318f6d39d2006a10cb6c1410334c56d76a74aa6 2018-06-20 16:38:42 +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 
40 #include "cpl_string.h"
41 
42 #include <curl/curl.h>
43 #include <map>
44 
45 CPLString CPLGetLowerCaseHexSHA256( const void *pabyData, size_t nBytes );
46 CPLString CPLGetLowerCaseHexSHA256( const CPLString& osStr );
47 
48 CPLString CPLGetAWS_SIGN4_Timestamp();
49 
50 CPLString CPLAWSURLEncode(const CPLString& osURL, bool bEncodeSlash = true);
51 
52 CPLString CPLAWSGetHeaderVal(const struct curl_slist* psExistingHeaders,
53  const char* pszKey);
54 
56 CPLGetAWS_SIGN4_Signature( const CPLString& osSecretAccessKey,
57  const CPLString& osAccessToken,
58  const CPLString& osRegion,
59  const CPLString& osRequestPayer,
60  const CPLString& osService,
61  const CPLString& osVerb,
62  const struct curl_slist* psExistingHeaders,
63  const CPLString& osHost,
64  const CPLString& osCanonicalURI,
65  const CPLString& osCanonicalQueryString,
66  const CPLString& osXAMZContentSHA256,
67  const CPLString& osTimestamp,
68  CPLString& osSignedHeaders );
69 
70 CPLString CPLGetAWS_SIGN4_Authorization(const CPLString& osSecretAccessKey,
71  const CPLString& osAccessKeyId,
72  const CPLString& osAccessToken,
73  const CPLString& osRegion,
74  const CPLString& osRequestPayer,
75  const CPLString& osService,
76  const CPLString& osVerb,
77  const struct curl_slist* psExistingHeaders,
78  const CPLString& osHost,
79  const CPLString& osCanonicalURI,
80  const CPLString& osCanonicalQueryString,
81  const CPLString& osXAMZContentSHA256,
82  const CPLString& osTimestamp);
83 
84 class IVSIS3LikeHandleHelper
85 {
86  CPL_DISALLOW_COPY_ASSIGN(IVSIS3LikeHandleHelper)
87 
88 protected:
89  std::map<CPLString, CPLString> m_oMapQueryParameters{};
90 
91  virtual void RebuildURL() = 0;
92  CPLString GetQueryString(bool bAddEmptyValueAfterEqual) const;
93 
94 public:
95  IVSIS3LikeHandleHelper() = default;
96  virtual ~IVSIS3LikeHandleHelper() = default;
97 
98  void ResetQueryParameters();
99  void AddQueryParameter(const CPLString& osKey, const CPLString& osValue);
100 
101  virtual struct curl_slist* GetCurlHeaders(const CPLString& osVerb,
102  const struct curl_slist* psExistingHeaders,
103  const void *pabyDataContent = nullptr,
104  size_t nBytesContent = 0) const = 0;
105 
106  virtual bool AllowAutomaticRedirection() { return true; }
107  virtual bool CanRestartOnError(const char*, const char* /* pszHeaders*/,
108  bool /*bSetError*/, bool* /*pbUpdateMap*/ = nullptr) { return false;}
109 
110  virtual const CPLString& GetURL() const = 0;
111 
112  static bool GetBucketAndObjectKey(const char* pszURI,
113  const char* pszFSPrefix,
114  bool bAllowNoObject,
115  CPLString &osBucketOut,
116  CPLString &osObjectKeyOut);
117 
118  static CPLString BuildCanonicalizedHeaders(
119  std::map<CPLString, CPLString>& oSortedMapHeaders,
120  const struct curl_slist* psExistingHeaders,
121  const char* pszHeaderPrefix);
122 
123  static CPLString GetRFC822DateTime();
124 };
125 
126 class VSIS3HandleHelper final: public IVSIS3LikeHandleHelper
127 {
128  CPL_DISALLOW_COPY_ASSIGN(VSIS3HandleHelper)
129 
130  CPLString m_osURL{};
131  CPLString m_osSecretAccessKey{};
132  CPLString m_osAccessKeyId{};
133  CPLString m_osSessionToken{};
134  CPLString m_osEndpoint{};
135  CPLString m_osRegion{};
136  CPLString m_osRequestPayer{};
137  CPLString m_osBucket{};
138  CPLString m_osObjectKey{};
139  bool m_bUseHTTPS = false;
140  bool m_bUseVirtualHosting = false;
141 
142  void RebuildURL() override;
143 
144  static bool GetConfigurationFromEC2(CPLString& osSecretAccessKey,
145  CPLString& osAccessKeyId,
146  CPLString& osSessionToken);
147 
148  static bool GetConfigurationFromAWSConfigFiles(
149  CPLString& osSecretAccessKey,
150  CPLString& osAccessKeyId,
151  CPLString& osSessionToken,
152  CPLString& osRegion,
153  CPLString& osCredentials);
154 
155  static bool GetConfiguration(CSLConstList papszOptions,
156  CPLString& osSecretAccessKey,
157  CPLString& osAccessKeyId,
158  CPLString& osSessionToken,
159  CPLString& osRegion);
160  protected:
161 
162  public:
163  VSIS3HandleHelper(const CPLString& osSecretAccessKey,
164  const CPLString& osAccessKeyId,
165  const CPLString& osSessionToken,
166  const CPLString& osEndpoint,
167  const CPLString& osRegion,
168  const CPLString& osRequestPayer,
169  const CPLString& osBucket,
170  const CPLString& osObjectKey,
171  bool bUseHTTPS, bool bUseVirtualHosting);
172  ~VSIS3HandleHelper();
173 
174  static VSIS3HandleHelper* BuildFromURI(const char* pszURI,
175  const char* pszFSPrefix,
176  bool bAllowNoObject,
177  CSLConstList papszOptions = nullptr);
178  static CPLString BuildURL(const CPLString& osEndpoint,
179  const CPLString& osBucket,
180  const CPLString& osObjectKey,
181  bool bUseHTTPS, bool bUseVirtualHosting);
182 
183  struct curl_slist* GetCurlHeaders(
184  const CPLString& osVerb,
185  const struct curl_slist* psExistingHeaders,
186  const void *pabyDataContent = nullptr,
187  size_t nBytesContent = 0) const override;
188 
189  bool AllowAutomaticRedirection() override { return false; }
190  bool CanRestartOnError(const char*, const char* pszHeaders,
191  bool bSetError,
192  bool* pbUpdateMap = nullptr) override;
193 
194  const CPLString& GetURL() const override { return m_osURL; }
195  const CPLString& GetBucket() const { return m_osBucket; }
196  const CPLString& GetObjectKey() const { return m_osObjectKey; }
197  const CPLString& GetEndpoint()const { return m_osEndpoint; }
198  const CPLString& GetRegion() const { return m_osRegion; }
199  const CPLString& GetRequestPayer() const { return m_osRequestPayer; }
200  bool GetVirtualHosting() const { return m_bUseVirtualHosting; }
201  void SetEndpoint(const CPLString &osStr);
202  void SetRegion(const CPLString &osStr);
203  void SetRequestPayer(const CPLString &osStr);
204  void SetVirtualHosting(bool b);
205 
206  CPLString GetSignedURL(CSLConstList papszOptions);
207 
208  static void CleanMutex();
209  static void ClearCache();
210 };
211 
212 class VSIS3UpdateParams
213 {
214  public:
215  CPLString m_osRegion{};
216  CPLString m_osEndpoint{};
217  CPLString m_osRequestPayer{};
218  bool m_bUseVirtualHosting = false;
219 
220  VSIS3UpdateParams() = default;
221 
222  explicit VSIS3UpdateParams(const VSIS3HandleHelper* poHelper) :
223  m_osRegion(poHelper->GetRegion()),
224  m_osEndpoint(poHelper->GetEndpoint()),
225  m_osRequestPayer(poHelper->GetRequestPayer()),
226  m_bUseVirtualHosting(poHelper->GetVirtualHosting()) {}
227 
228  void UpdateHandlerHelper(VSIS3HandleHelper* poHelper) {
229  poHelper->SetRegion(m_osRegion);
230  poHelper->SetEndpoint(m_osEndpoint);
231  poHelper->SetRequestPayer(m_osRequestPayer);
232  poHelper->SetVirtualHosting(m_bUseVirtualHosting);
233  }
234 };
235 
236 #endif /* HAVE_CURL */
237 
238 #endif /* #ifndef DOXYGEN_SKIP */
239 
240 #endif /* CPL_AWS_INCLUDED_H */
Convenient string class based on std::string.
Definition: cpl_string.h:329
Various convenience functions for working with strings and string lists.
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1186
#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:989

Generated for GDAL by doxygen 1.8.13.