OGR
cpl_aws.h
1 /**********************************************************************
2  * $Id: cpl_aws.h 6df253b7cb1bb04d8fa847106b60ff190f23235b 2019-06-07 20:50:53 +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  mutable CPLString m_osSecretAccessKey{};
132  mutable CPLString m_osAccessKeyId{};
133  mutable 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  bool m_bFromEC2 = false;
142 
143  void RebuildURL() override;
144 
145  static bool GetConfigurationFromEC2(CPLString& osSecretAccessKey,
146  CPLString& osAccessKeyId,
147  CPLString& osSessionToken);
148 
149  static bool GetConfigurationFromAWSConfigFiles(
150  CPLString& osSecretAccessKey,
151  CPLString& osAccessKeyId,
152  CPLString& osSessionToken,
153  CPLString& osRegion,
154  CPLString& osCredentials);
155 
156  static bool GetConfiguration(CSLConstList papszOptions,
157  CPLString& osSecretAccessKey,
158  CPLString& osAccessKeyId,
159  CPLString& osSessionToken,
160  CPLString& osRegion,
161  bool& bFromEC2);
162  protected:
163 
164  public:
165  VSIS3HandleHelper(const CPLString& osSecretAccessKey,
166  const CPLString& osAccessKeyId,
167  const CPLString& osSessionToken,
168  const CPLString& osEndpoint,
169  const CPLString& osRegion,
170  const CPLString& osRequestPayer,
171  const CPLString& osBucket,
172  const CPLString& osObjectKey,
173  bool bUseHTTPS, bool bUseVirtualHosting, bool bFromEC2);
174  ~VSIS3HandleHelper();
175 
176  static VSIS3HandleHelper* BuildFromURI(const char* pszURI,
177  const char* pszFSPrefix,
178  bool bAllowNoObject,
179  CSLConstList papszOptions = nullptr);
180  static CPLString BuildURL(const CPLString& osEndpoint,
181  const CPLString& osBucket,
182  const CPLString& osObjectKey,
183  bool bUseHTTPS, bool bUseVirtualHosting);
184 
185  struct curl_slist* GetCurlHeaders(
186  const CPLString& osVerb,
187  const struct curl_slist* psExistingHeaders,
188  const void *pabyDataContent = nullptr,
189  size_t nBytesContent = 0) const override;
190 
191  bool AllowAutomaticRedirection() override { return false; }
192  bool CanRestartOnError(const char*, const char* pszHeaders,
193  bool bSetError,
194  bool* pbUpdateMap = nullptr) override;
195 
196  const CPLString& GetURL() const override { return m_osURL; }
197  const CPLString& GetBucket() const { return m_osBucket; }
198  const CPLString& GetObjectKey() const { return m_osObjectKey; }
199  const CPLString& GetEndpoint()const { return m_osEndpoint; }
200  const CPLString& GetRegion() const { return m_osRegion; }
201  const CPLString& GetRequestPayer() const { return m_osRequestPayer; }
202  bool GetVirtualHosting() const { return m_bUseVirtualHosting; }
203  void SetEndpoint(const CPLString &osStr);
204  void SetRegion(const CPLString &osStr);
205  void SetRequestPayer(const CPLString &osStr);
206  void SetVirtualHosting(bool b);
207 
208  CPLString GetSignedURL(CSLConstList papszOptions);
209 
210  static void CleanMutex();
211  static void ClearCache();
212 };
213 
214 class VSIS3UpdateParams
215 {
216  public:
217  CPLString m_osRegion{};
218  CPLString m_osEndpoint{};
219  CPLString m_osRequestPayer{};
220  bool m_bUseVirtualHosting = false;
221 
222  VSIS3UpdateParams() = default;
223 
224  explicit VSIS3UpdateParams(const VSIS3HandleHelper* poHelper) :
225  m_osRegion(poHelper->GetRegion()),
226  m_osEndpoint(poHelper->GetEndpoint()),
227  m_osRequestPayer(poHelper->GetRequestPayer()),
228  m_bUseVirtualHosting(poHelper->GetVirtualHosting()) {}
229 
230  void UpdateHandlerHelper(VSIS3HandleHelper* poHelper) {
231  poHelper->SetRegion(m_osRegion);
232  poHelper->SetEndpoint(m_osEndpoint);
233  poHelper->SetRequestPayer(m_osRequestPayer);
234  poHelper->SetVirtualHosting(m_bUseVirtualHosting);
235  }
236 };
237 
238 #endif /* HAVE_CURL */
239 
240 #endif /* #ifndef DOXYGEN_SKIP */
241 
242 #endif /* CPL_AWS_INCLUDED_H */
CPLHTTPFetch
CPLHTTPResult * CPLHTTPFetch(const char *pszURL, CSLConstList papszOptions)
Fetch a document from an url and return in a string.
Definition: cpl_http.cpp:648
CPLReadLineL
const char * CPLReadLineL(VSILFILE *)
Definition: cpl_conv.cpp:630
CSLTokenizeString2
char ** CSLTokenizeString2(const char *pszString, const char *pszDelimiter, int nCSLTFlags)
Definition: cpl_string.cpp:836
GByte
unsigned char GByte
Definition: cpl_port.h:215
VSIFReadL
size_t VSIFReadL(void *, size_t, size_t, VSILFILE *) EXPERIMENTAL_CPL_WARN_UNUSED_RESULT
Read bytes from file.
Definition: cpl_vsil.cpp:1690
CPLPrintTime
int CPLPrintTime(char *, int, const char *, const struct tm *, const char *)
Definition: cpl_conv.cpp:1502
CPLHTTPDestroyResult
void CPLHTTPDestroyResult(CPLHTTPResult *psResult)
Clean the memory associated with the return value of CPLHTTPFetch()
Definition: cpl_http.cpp:2080
CPLStringList
String list class designed around our use of C "char**" string lists.
Definition: cpl_string.h:438
cpl_minixml.h
CPLPopErrorHandler
void CPLPopErrorHandler(void)
Definition: cpl_error.cpp:1242
CSLT_HONOURSTRINGS
#define CSLT_HONOURSTRINGS
Definition: cpl_string.h:83
CSLFetchNameValueDef
const char * CSLFetchNameValueDef(CSLConstList papszStrList, const char *pszName, const char *pszDefault)
Definition: cpl_string.cpp:1646
CSLSetNameValue
char ** CSLSetNameValue(char **papszStrList, const char *pszName, const char *pszValue)
Definition: cpl_string.cpp:1877
CPLString
Convenient string class based on std::string.
Definition: cpl_string.h:329
CPLParseXMLString
CPLXMLNode * CPLParseXMLString(const char *)
Parse an XML string into tree form.
Definition: cpl_minixml.cpp:614
CPLXMLNode
Definition: cpl_minixml.h:66
EQUAL
#define EQUAL(a, b)
Definition: cpl_port.h:569
VSIFCloseL
int VSIFCloseL(VSILFILE *) EXPERIMENTAL_CPL_WARN_UNUSED_RESULT
Close file.
Definition: cpl_vsil.cpp:1472
cpl_http.h
CPLPushErrorHandler
void CPLPushErrorHandler(CPLErrorHandler)
Definition: cpl_error.cpp:1185
CPLStringList::SetNameValue
CPLStringList & SetNameValue(const char *pszKey, const char *pszValue)
Definition: cplstringlist.cpp:402
CPLTestBool
bool CPLTestBool(const char *pszValue)
Definition: cpl_string.cpp:1526
CSLConstList
char ** CSLConstList
Definition: cpl_port.h:1194
CPLHTTPResult
Definition: cpl_http.h:66
CPLHTTPResult::pabyData
GByte * pabyData
Definition: cpl_http.h:82
CPLDestroyXMLNode
void CPLDestroyXMLNode(CPLXMLNode *)
Destroy a tree.
Definition: cpl_minixml.cpp:1405
CPLDebug
void CPLDebug(const char *, const char *,...)
Definition: cpl_error.cpp:602
STARTS_WITH
#define STARTS_WITH(a, b)
Definition: cpl_port.h:578
cpl_string.h
CPLSPrintf
const char * CPLSPrintf(const char *fmt,...)
Definition: cpl_string.cpp:977
CPLGetXMLValue
const char * CPLGetXMLValue(const CPLXMLNode *poRoot, const char *pszPath, const char *pszDefault)
Fetch element/attribute value.
Definition: cpl_minixml.cpp:1638
CPLMutexHolder
Definition: cpl_multiproc.h:139
CPLError
void CPLError(CPLErr eErrClass, CPLErrorNum err_no, const char *fmt,...)
Definition: cpl_error.cpp:308
CPLParseNameValue
const char * CPLParseNameValue(const char *pszNameValue, char **ppszKey)
Definition: cpl_string.cpp:1754
CPLString::Trim
CPLSTRING_METHOD_DLL CPLString & Trim()
Definition: cplstring.cpp:203
CPLHTTPResult::nStatus
int nStatus
Definition: cpl_http.h:68
CSLDestroy
void CSLDestroy(char **papszStrList)
Definition: cpl_string.cpp:200
GIntBig
long long GIntBig
Definition: cpl_port.h:248
EQUALN
#define EQUALN(a, b, n)
Definition: cpl_port.h:567
VSIFOpenL
VSILFILE * VSIFOpenL(const char *, const char *)
Open file.
Definition: cpl_vsil.cpp:997
CPLQuietErrorHandler
void CPLQuietErrorHandler(CPLErr, CPLErrorNum, const char *)
Definition: cpl_error.cpp:967
CPLFree
#define CPLFree
Definition: cpl_conv.h:81
CPLStringList::FetchNameValueDef
const char * FetchNameValueDef(const char *pszKey, const char *pszDefault) const
Definition: cplstringlist.cpp:725
CPL_DISALLOW_COPY_ASSIGN
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Definition: cpl_port.h:997
CPLGetConfigOption
const char * CPLGetConfigOption(const char *, const char *)
Definition: cpl_conv.cpp:1692
VSILFILE
FILE VSILFILE
Definition: cpl_vsi.h:156
STARTS_WITH_CI
#define STARTS_WITH_CI(a, b)
Definition: cpl_port.h:580
CPLE_AppDefined
#define CPLE_AppDefined
Definition: cpl_error.h:99

Generated for GDAL by doxygen 1.8.17.