| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) CERN 2015 |
| 4 |
* Author: Georgios Bitzes <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <iomanip> |
| 23 |
#include <utils/davix_azure_utils.hpp> |
| 24 |
#include "libs/datetime/datetime_utils.hpp" |
| 25 |
#include <davix_internal.hpp> |
| 26 |
#include <utils/davix_logger_internal.hpp> |
| 27 |
#include "libs/alibxx/crypto/hmacsha.hpp" |
| 28 |
#include "libs/alibxx/crypto/base64.hpp" |
| 29 |
|
| 30 |
#define AZURE_TIME_LEEWAY 300 |
| 31 |
|
| 32 |
namespace Davix { |
| 33 |
namespace Azure { |
| 34 |
|
| 35 |
std::string extract_azure_account(const Uri & url) { |
| 36 |
std::string host = url.getHost(); |
| 37 |
return host.substr(0, host.find(".")); |
| 38 |
} |
| 39 |
|
| 40 |
std::string extract_azure_container(const Uri & url) { |
| 41 |
std::string path = url.getPath(); |
| 42 |
std::string name = path.substr(1, path.find("/", 1)); |
| 43 |
if(name.compare(name.size()-1,1,"/") == 0) { |
| 44 |
name.erase(name.size()-1, name.size()); |
| 45 |
} |
| 46 |
return name; |
| 47 |
} |
| 48 |
|
| 49 |
std::string extract_azure_filename(const Uri & url) { |
| 50 |
std::string path = url.getPath(); |
| 51 |
std::size_t sep = path.find("/", 1); |
| 52 |
if(sep == std::string::npos) return ""; |
| 53 |
return path.substr(path.find("/", 1)+1, path.size()); |
| 54 |
} |
| 55 |
|
| 56 |
std::string hexEncode(std::string input, std::string separator="") { |
| 57 |
std::ostringstream ss; |
| 58 |
for(std::string::iterator it = input.begin(); it != input.end(); it++) { |
| 59 |
ss << std::setw(2) << std::setfill('0') << std::hex << (unsigned int) ( (unsigned char) *it) << separator; |
| 60 |
} |
| 61 |
return ss.str(); |
| 62 |
} |
| 63 |
|
| 64 |
Uri transformURI(const Uri & original_url, const RequestParams & params, const bool addDelimiter) { |
| 65 |
Uri newUri = original_url; |
| 66 |
newUri.setPath("/" + extract_azure_container(original_url) + "/"); |
| 67 |
newUri.addQueryParam("restype", "container"); |
| 68 |
newUri.addQueryParam("comp", "list"); |
| 69 |
|
| 70 |
std::string filename = extract_azure_filename(original_url); |
| 71 |
if(filename[filename.size()-1] != '/') { |
| 72 |
filename.append("/"); |
| 73 |
} |
| 74 |
|
| 75 |
// special case: listing top-dir |
| 76 |
if(filename == "/") |
| 77 |
filename = ""; |
| 78 |
|
| 79 |
newUri.addQueryParam("prefix", filename); |
| 80 |
newUri.addQueryParam("delimiter", "/"); |
| 81 |
|
| 82 |
return newUri; |
| 83 |
} |
| 84 |
|
| 85 |
// wrapper function, try to figure out what resourceType and permissions to use, |
| 86 |
// based on the URL and the method. |
| 87 |
// Try to always give as restrictive permissions as possible. |
| 88 |
Uri signURI(const AzureSecretKey key, const std::string method, const Uri & url, const time_t signDuration) { |
| 89 |
if(method == "DELETE") { |
| 90 |
return signURI(key, Azure::Resource::BLOB, Azure::Permission::DELETE, url, signDuration); |
| 91 |
} |
| 92 |
else if(method == "PUT") { |
| 93 |
return signURI(key, Azure::Resource::BLOB, Azure::Permission::WRITE, url, signDuration); |
| 94 |
} |
| 95 |
else if(method == "GET") { |
| 96 |
const std::string filename = extract_azure_filename(url); |
| 97 |
if(filename.size() == 0) { |
| 98 |
return signURI(key, Azure::Resource::CONTAINER, Azure::Permission::LIST, url, signDuration); |
| 99 |
} |
| 100 |
return signURI(key, Azure::Resource::BLOB, Azure::Permission::READ, url, signDuration); |
| 101 |
} |
| 102 |
else if(method == "HEAD") { |
| 103 |
return signURI(key, Azure::Resource::BLOB, Azure::Permission::READ, url, signDuration); |
| 104 |
} |
| 105 |
throw std::runtime_error("unsupported method given to azure"); |
| 106 |
} |
| 107 |
|
| 108 |
Uri signURI(const AzureSecretKey key, const Azure::Resource::Type resourceType, const Azure::Permission::Type permissions, const Uri & url, |
| 109 |
const time_t signDuration) { |
| 110 |
// reference: https://msdn.microsoft.com/en-us/library/azure/dn140255.aspx |
| 111 |
DAVIX_SLOG(DAVIX_LOG_VERBOSE, DAVIX_LOG_S3, "Signing Azure URL"); |
| 112 |
|
| 113 |
// build string to sign |
| 114 |
const std::string format("%Y-%m-%dT%H:%M:%SZ"); |
| 115 |
time_t present = time(NULL); |
| 116 |
|
| 117 |
std::string signedpermissions = permissions; |
| 118 |
if(permissions == Azure::Permission::WRITE) signedpermissions = "acw"; |
| 119 |
std::string signedstart = time_as_string(present-AZURE_TIME_LEEWAY, format); |
| 120 |
std::string signedexpiry = time_as_string(present+signDuration, format); |
| 121 |
|
| 122 |
std::string canonicalizedresource; |
| 123 |
if(resourceType == Resource::CONTAINER) { |
| 124 |
canonicalizedresource = "/blob/" + extract_azure_account(url) + "/" + extract_azure_container(url); |
| 125 |
} else if(resourceType == Resource::BLOB) { |
| 126 |
canonicalizedresource = "/blob/" + extract_azure_account(url) + url.getPath(); |
| 127 |
} |
| 128 |
|
| 129 |
std::string signedidentifier; |
| 130 |
std::string signedIP; |
| 131 |
std::string signedProtocol; |
| 132 |
std::string signedversion("2016-05-31"); |
| 133 |
std::string rscc; |
| 134 |
std::string rscd; |
| 135 |
std::string rsce; |
| 136 |
std::string rscl; |
| 137 |
std::string rsct; |
| 138 |
|
| 139 |
std::ostringstream stringToSign; |
| 140 |
stringToSign << signedpermissions << "\n" |
| 141 |
<< signedstart << "\n" |
| 142 |
<< signedexpiry << "\n" |
| 143 |
<< canonicalizedresource << "\n" |
| 144 |
<< signedidentifier << "\n" |
| 145 |
<< signedIP << "\n" |
| 146 |
<< signedProtocol << "\n" |
| 147 |
<< signedversion << "\n" |
| 148 |
<< rscc << "\n" |
| 149 |
<< rscd << "\n" |
| 150 |
<< rsce << "\n" |
| 151 |
<< rscl << "\n" |
| 152 |
<< rsct; |
| 153 |
|
| 154 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_S3, "string to sign: \n{}", stringToSign.str()); |
| 155 |
std::string decoded_key = Base64::base64_decode(key); |
| 156 |
std::string signature = hmac_sha256(decoded_key, stringToSign.str()); |
| 157 |
std::string base64sig = Base64::base64_encode((unsigned char*) signature.c_str(), signature.size()); |
| 158 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_S3, "signature: {}", base64sig); |
| 159 |
|
| 160 |
// add query parameters |
| 161 |
std::string signedresource; |
| 162 |
if(resourceType == Resource::CONTAINER) { |
| 163 |
signedresource = "c"; |
| 164 |
} else if(resourceType == Resource::BLOB) { |
| 165 |
signedresource = "b"; |
| 166 |
} |
| 167 |
|
| 168 |
Uri signedUrl(url); |
| 169 |
signedUrl.addQueryParam("sv", signedversion); |
| 170 |
signedUrl.addQueryParam("st", signedstart); |
| 171 |
signedUrl.addQueryParam("se", signedexpiry); |
| 172 |
signedUrl.addQueryParam("sr", signedresource); |
| 173 |
signedUrl.addQueryParam("sp", signedpermissions); |
| 174 |
signedUrl.addQueryParam("sig", base64sig); |
| 175 |
|
| 176 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_S3, "signed url: {}", signedUrl); |
| 177 |
return signedUrl; |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
} |