1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/**********************************************************************
* $Id$
*
* Name: cpl_aws.h
* Project: CPL - Common Portability Library
* Purpose: Amazon Web Services routines
* Author: Even Rouault <even.rouault at spatialys.com>
*
**********************************************************************
* Copyright (c) 2015, Even Rouault <even.rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#ifndef CPL_AWS_INCLUDED_H
#define CPL_AWS_INCLUDED_H
#ifndef DOXYGEN_SKIP
#ifdef HAVE_CURL
#include <cstddef>
#include <mutex>
#include "cpl_string.h"
#include <curl/curl.h>
#include <map>
CPLString CPLGetLowerCaseHexSHA256(const void *pabyData, size_t nBytes);
CPLString CPLGetLowerCaseHexSHA256(const CPLString &osStr);
CPLString CPLGetAWS_SIGN4_Timestamp(GIntBig timestamp);
CPLString CPLAWSURLEncode(const CPLString &osURL, bool bEncodeSlash = true);
CPLString CPLAWSGetHeaderVal(const struct curl_slist *psExistingHeaders,
const char *pszKey);
CPLString CPLGetAWS_SIGN4_Signature(
const CPLString &osSecretAccessKey, const CPLString &osAccessToken,
const CPLString &osRegion, const CPLString &osRequestPayer,
const CPLString &osService, const CPLString &osVerb,
const struct curl_slist *psExistingHeaders, const CPLString &osHost,
const CPLString &osCanonicalURI, const CPLString &osCanonicalQueryString,
const CPLString &osXAMZContentSHA256, bool bAddHeaderAMZContentSHA256,
const CPLString &osTimestamp, CPLString &osSignedHeaders);
CPLString CPLGetAWS_SIGN4_Authorization(
const CPLString &osSecretAccessKey, const CPLString &osAccessKeyId,
const CPLString &osAccessToken, const CPLString &osRegion,
const CPLString &osRequestPayer, const CPLString &osService,
const CPLString &osVerb, const struct curl_slist *psExistingHeaders,
const CPLString &osHost, const CPLString &osCanonicalURI,
const CPLString &osCanonicalQueryString,
const CPLString &osXAMZContentSHA256, bool bAddHeaderAMZContentSHA256,
const CPLString &osTimestamp);
class IVSIS3LikeHandleHelper
{
CPL_DISALLOW_COPY_ASSIGN(IVSIS3LikeHandleHelper)
protected:
std::map<CPLString, CPLString> m_oMapQueryParameters{};
virtual void RebuildURL() = 0;
CPLString GetQueryString(bool bAddEmptyValueAfterEqual) const;
public:
IVSIS3LikeHandleHelper() = default;
virtual ~IVSIS3LikeHandleHelper() = default;
void ResetQueryParameters();
void AddQueryParameter(const CPLString &osKey, const CPLString &osValue);
virtual struct curl_slist *
GetCurlHeaders(const CPLString &osVerb,
const struct curl_slist *psExistingHeaders,
const void *pabyDataContent = nullptr,
size_t nBytesContent = 0) const = 0;
virtual bool AllowAutomaticRedirection()
{
return true;
}
virtual bool CanRestartOnError(const char *, const char * /* pszHeaders*/,
bool /*bSetError*/,
bool * /*pbUpdateMap*/ = nullptr)
{
return false;
}
virtual const CPLString &GetURL() const = 0;
CPLString GetURLNoKVP() const;
virtual CPLString GetCopySourceHeader() const
{
return std::string();
}
virtual const char *GetMetadataDirectiveREPLACE() const
{
return "";
}
static bool GetBucketAndObjectKey(const char *pszURI,
const char *pszFSPrefix,
bool bAllowNoObject,
CPLString &osBucketOut,
CPLString &osObjectKeyOut);
static CPLString
BuildCanonicalizedHeaders(std::map<CPLString, CPLString> &oSortedMapHeaders,
const struct curl_slist *psExistingHeaders,
const char *pszHeaderPrefix);
static CPLString GetRFC822DateTime();
};
enum class AWSCredentialsSource
{
REGULAR, // credentials from env variables or ~/.aws/crediential
EC2, // credentials from EC2 private networking
WEB_IDENTITY, // credentials from Web Identity Token
// See
// https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html
ASSUMED_ROLE // credentials from an STS assumed role
// See
// https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-cli.html
// and
// https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_request.html
};
class VSIS3HandleHelper final : public IVSIS3LikeHandleHelper
{
CPL_DISALLOW_COPY_ASSIGN(VSIS3HandleHelper)
CPLString m_osURL{};
mutable CPLString m_osSecretAccessKey{};
mutable CPLString m_osAccessKeyId{};
mutable CPLString m_osSessionToken{};
CPLString m_osEndpoint{};
CPLString m_osRegion{};
CPLString m_osRequestPayer{};
CPLString m_osBucket{};
CPLString m_osObjectKey{};
bool m_bUseHTTPS = false;
bool m_bUseVirtualHosting = false;
AWSCredentialsSource m_eCredentialsSource = AWSCredentialsSource::REGULAR;
void RebuildURL() override;
static bool GetOrRefreshTemporaryCredentialsForRole(
bool bForceRefresh, CPLString &osSecretAccessKey,
CPLString &osAccessKeyId, CPLString &osSessionToken,
CPLString &osRegion);
static bool GetConfigurationFromAssumeRoleWithWebIdentity(
bool bForceRefresh, const std::string &osPathForOption,
const std::string &osRoleArnIn,
const std::string &osWebIdentityTokenFileIn,
CPLString &osSecretAccessKey, CPLString &osAccessKeyId,
CPLString &osSessionToken);
static bool GetConfigurationFromEC2(bool bForceRefresh,
const std::string &osPathForOption,
CPLString &osSecretAccessKey,
CPLString &osAccessKeyId,
CPLString &osSessionToken);
static bool GetConfigurationFromAWSConfigFiles(
const std::string &osPathForOption, const char *pszProfile,
CPLString &osSecretAccessKey, CPLString &osAccessKeyId,
CPLString &osSessionToken, CPLString &osRegion,
CPLString &osCredentials, CPLString &osRoleArn,
CPLString &osSourceProfile, CPLString &osExternalId,
CPLString &osMFASerial, CPLString &osRoleSessionName,
CPLString &osWebIdentityTokenFile);
static bool GetConfiguration(const std::string &osPathForOption,
CSLConstList papszOptions,
CPLString &osSecretAccessKey,
CPLString &osAccessKeyId,
CPLString &osSessionToken, CPLString &osRegion,
AWSCredentialsSource &eCredentialsSource);
void RefreshCredentials(const std::string &osPathForOption,
bool bForceRefresh) const;
protected:
public:
VSIS3HandleHelper(const CPLString &osSecretAccessKey,
const CPLString &osAccessKeyId,
const CPLString &osSessionToken,
const CPLString &osEndpoint, const CPLString &osRegion,
const CPLString &osRequestPayer,
const CPLString &osBucket, const CPLString &osObjectKey,
bool bUseHTTPS, bool bUseVirtualHosting,
AWSCredentialsSource eCredentialsSource);
~VSIS3HandleHelper();
static VSIS3HandleHelper *BuildFromURI(const char *pszURI,
const char *pszFSPrefix,
bool bAllowNoObject,
CSLConstList papszOptions = nullptr);
static CPLString BuildURL(const CPLString &osEndpoint,
const CPLString &osBucket,
const CPLString &osObjectKey, bool bUseHTTPS,
bool bUseVirtualHosting);
struct curl_slist *
GetCurlHeaders(const CPLString &osVerb,
const struct curl_slist *psExistingHeaders,
const void *pabyDataContent = nullptr,
size_t nBytesContent = 0) const override;
bool AllowAutomaticRedirection() override
{
return false;
}
bool CanRestartOnError(const char *, const char *pszHeaders, bool bSetError,
bool *pbUpdateMap = nullptr) override;
const CPLString &GetURL() const override
{
return m_osURL;
}
const CPLString &GetBucket() const
{
return m_osBucket;
}
const CPLString &GetObjectKey() const
{
return m_osObjectKey;
}
const CPLString &GetEndpoint() const
{
return m_osEndpoint;
}
const CPLString &GetRegion() const
{
return m_osRegion;
}
const CPLString &GetRequestPayer() const
{
return m_osRequestPayer;
}
bool GetVirtualHosting() const
{
return m_bUseVirtualHosting;
}
void SetEndpoint(const CPLString &osStr);
void SetRegion(const CPLString &osStr);
void SetRequestPayer(const CPLString &osStr);
void SetVirtualHosting(bool b);
CPLString GetCopySourceHeader() const override
{
return "x-amz-copy-source";
}
const char *GetMetadataDirectiveREPLACE() const override
{
return "x-amz-metadata-directive: REPLACE";
}
CPLString GetSignedURL(CSLConstList papszOptions);
static void CleanMutex();
static void ClearCache();
};
class VSIS3UpdateParams
{
public:
CPLString m_osRegion{};
CPLString m_osEndpoint{};
CPLString m_osRequestPayer{};
bool m_bUseVirtualHosting = false;
VSIS3UpdateParams() = default;
explicit VSIS3UpdateParams(const VSIS3HandleHelper *poHelper)
: m_osRegion(poHelper->GetRegion()),
m_osEndpoint(poHelper->GetEndpoint()),
m_osRequestPayer(poHelper->GetRequestPayer()),
m_bUseVirtualHosting(poHelper->GetVirtualHosting())
{
}
void UpdateHandlerHelper(VSIS3HandleHelper *poHelper)
{
poHelper->SetRegion(m_osRegion);
poHelper->SetEndpoint(m_osEndpoint);
poHelper->SetRequestPayer(m_osRequestPayer);
poHelper->SetVirtualHosting(m_bUseVirtualHosting);
}
static std::mutex gsMutex;
static std::map<CPLString, VSIS3UpdateParams> goMapBucketsToS3Params;
static void UpdateMapFromHandle(IVSIS3LikeHandleHelper *poHandleHelper);
static void UpdateHandleFromMap(IVSIS3LikeHandleHelper *poHandleHelper);
static void ClearCache();
};
#endif /* HAVE_CURL */
#endif /* #ifndef DOXYGEN_SKIP */
#endif /* CPL_AWS_INCLUDED_H */
|