CernVM-FS  2.11.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swissknife_lease_curl.cc
Go to the documentation of this file.
1 
6 
7 #include "cvmfs_config.h"
8 
9 #include "crypto/hash.h"
10 #include "gateway_util.h"
11 #include "json_document.h"
12 #include "ssl.h"
13 #include "util/logging.h"
14 #include "util/pointer.h"
15 #include "util/string.h"
16 
17 namespace {
18 
19 CURL* PrepareCurl(const std::string& method) {
20  const char* user_agent_string = "cvmfs/" VERSION;
21 
22  CURL* h_curl = curl_easy_init();
23 
24  if (h_curl) {
25  curl_easy_setopt(h_curl, CURLOPT_NOPROGRESS, 1L);
26  curl_easy_setopt(h_curl, CURLOPT_USERAGENT, user_agent_string);
27  curl_easy_setopt(h_curl, CURLOPT_MAXREDIRS, 50L);
28  curl_easy_setopt(h_curl, CURLOPT_CUSTOMREQUEST, method.c_str());
29  }
30 
31  return h_curl;
32 }
33 
34 size_t RecvCB(void* buffer, size_t size, size_t nmemb, void* userp) {
35  CurlBuffer* my_buffer = static_cast<CurlBuffer*>(userp);
36 
37  if (size * nmemb < 1) {
38  return 0;
39  }
40 
41  my_buffer->data = static_cast<char*>(buffer);
42 
43  return my_buffer->data.size();
44 }
45 
46 } // namespace
47 
48 bool MakeAcquireRequest(const std::string& key_id, const std::string& secret,
49  const std::string& repo_path,
50  const std::string& repo_service_url,
51  CurlBuffer* buffer) {
52  CURLcode ret = static_cast<CURLcode>(0);
53 
54  CURL* h_curl = PrepareCurl("POST");
55  if (!h_curl) {
56  return false;
57  }
58 
59  const std::string payload = "{\"path\" : \"" + repo_path +
60  "\", \"api_version\" : \"" +
62 
64  shash::HmacString(secret, payload, &hmac);
65 
68  cs.ApplySslCertificatePath(h_curl);
69 
70  const std::string header_str = std::string("Authorization: ") + key_id + " " +
71  Base64(hmac.ToString(false));
72  struct curl_slist* auth_header = NULL;
73  auth_header = curl_slist_append(auth_header, header_str.c_str());
74  curl_easy_setopt(h_curl, CURLOPT_HTTPHEADER, auth_header);
75 
76  // Make request to acquire lease from repo services
77  curl_easy_setopt(h_curl, CURLOPT_URL, (repo_service_url + "/leases").c_str());
78  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDSIZE_LARGE,
79  static_cast<curl_off_t>(payload.length()));
80  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDS, payload.c_str());
81  curl_easy_setopt(h_curl, CURLOPT_WRITEFUNCTION, RecvCB);
82  curl_easy_setopt(h_curl, CURLOPT_WRITEDATA, buffer);
83 
84  ret = curl_easy_perform(h_curl);
85  if (ret) {
87  "Make lease acquire request failed: %d. Reply: %s", ret,
88  buffer->data.c_str());
89  }
90 
91  curl_easy_cleanup(h_curl);
92  h_curl = NULL;
93 
94  return !ret;
95 }
96 
97 bool MakeEndRequest(const std::string& method, const std::string& key_id,
98  const std::string& secret, const std::string& session_token,
99  const std::string& repo_service_url,
100  const std::string& request_payload, CurlBuffer* reply) {
101  CURLcode ret = static_cast<CURLcode>(0);
102 
103  CURL* h_curl = PrepareCurl(method);
104  if (!h_curl) {
105  return false;
106  }
107 
108  shash::Any hmac(shash::kSha1);
109  shash::HmacString(secret, session_token, &hmac);
110 
113  cs.ApplySslCertificatePath(h_curl);
114 
115  const std::string header_str = std::string("Authorization: ") + key_id + " " +
116  Base64(hmac.ToString(false));
117  struct curl_slist* auth_header = NULL;
118  auth_header = curl_slist_append(auth_header, header_str.c_str());
119  curl_easy_setopt(h_curl, CURLOPT_HTTPHEADER, auth_header);
120 
121  curl_easy_setopt(h_curl, CURLOPT_URL,
122  (repo_service_url + "/leases/" + session_token).c_str());
123  if (request_payload != "") {
124  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDSIZE_LARGE,
125  static_cast<curl_off_t>(request_payload.length()));
126  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDS, request_payload.c_str());
127  } else {
128  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDSIZE_LARGE,
129  static_cast<curl_off_t>(0));
130  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDS, NULL);
131  }
132  curl_easy_setopt(h_curl, CURLOPT_WRITEFUNCTION, RecvCB);
133  curl_easy_setopt(h_curl, CURLOPT_WRITEDATA, reply);
134 
135  ret = curl_easy_perform(h_curl);
136  if (ret) {
138  "Lease end request - curl_easy_perform failed: %d", ret);
139  }
140 
142  const JSON *reply_status =
143  JsonDocument::SearchInObject(reply_json->root(), "status", JSON_STRING);
144  const bool ok = (reply_status != NULL &&
145  std::string(reply_status->string_value) == "ok");
146  if (!ok) {
148  "Lease end request - error reply: %s",
149  reply->data.c_str());
150  }
151 
152  curl_easy_cleanup(h_curl);
153  h_curl = NULL;
154 
155  return ok && !ret;
156 }
#define LogCvmfs(source, mask,...)
Definition: logging.h:22
int APIVersion()
Definition: gateway_util.cc:26
bool MakeEndRequest(const std::string &method, const std::string &key_id, const std::string &secret, const std::string &session_token, const std::string &repo_service_url, const std::string &request_payload, CurlBuffer *reply)
static JSON * SearchInObject(const JSON *json_object, const std::string &name, const json_type type)
std::string ToString(const bool with_suffix=false) const
Definition: hash.h:249
static CURL * PrepareCurl(const std::string &method)
std::string data
static JsonDocument * Create(const std::string &text)
bool ApplySslCertificatePath(CURL *handle) const
Definition: ssl.cc:58
void UseSystemCertificatePath()
Definition: ssl.cc:68
size_t RecvCB(void *buffer, size_t size, size_t nmemb, void *userp)
string StringifyInt(const int64_t value)
Definition: string.cc:78
string Base64(const string &data)
Definition: string.cc:504
static void MakeAcquireRequest(const gateway::GatewayKey &key, const std::string &repo_path, const std::string &repo_service_url, int llvl, CurlBuffer *buffer)
void HmacString(const std::string &key, const std::string &content, Any *any_digest)
Definition: hash.h:527
struct json_value JSON
Definition: helper_allow.cc:11
static void size_t size
Definition: smalloc.h:47
const JSON * root() const
Definition: json_document.h:25