CernVM-FS  2.9.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 "gateway_util.h"
10 #include "hash.h"
11 #include "json_document.h"
12 #include "logging.h"
13 #include "util/pointer.h"
14 #include "util/string.h"
15 
16 namespace {
17 
18 CURL* PrepareCurl(const std::string& method) {
19  const char* user_agent_string = "cvmfs/" VERSION;
20 
21  CURL* h_curl = curl_easy_init();
22 
23  if (h_curl) {
24  curl_easy_setopt(h_curl, CURLOPT_NOPROGRESS, 1L);
25  curl_easy_setopt(h_curl, CURLOPT_USERAGENT, user_agent_string);
26  curl_easy_setopt(h_curl, CURLOPT_MAXREDIRS, 50L);
27  curl_easy_setopt(h_curl, CURLOPT_CUSTOMREQUEST, method.c_str());
28  }
29 
30  return h_curl;
31 }
32 
33 size_t RecvCB(void* buffer, size_t size, size_t nmemb, void* userp) {
34  CurlBuffer* my_buffer = static_cast<CurlBuffer*>(userp);
35 
36  if (size * nmemb < 1) {
37  return 0;
38  }
39 
40  my_buffer->data = static_cast<char*>(buffer);
41 
42  return my_buffer->data.size();
43 }
44 
45 } // namespace
46 
47 bool MakeAcquireRequest(const std::string& key_id, const std::string& secret,
48  const std::string& repo_path,
49  const std::string& repo_service_url,
50  CurlBuffer* buffer) {
51  CURLcode ret = static_cast<CURLcode>(0);
52 
53  CURL* h_curl = PrepareCurl("POST");
54  if (!h_curl) {
55  return false;
56  }
57 
58  const std::string payload = "{\"path\" : \"" + repo_path +
59  "\", \"api_version\" : \"" +
61 
63  shash::HmacString(secret, payload, &hmac);
64 
65  const std::string header_str = std::string("Authorization: ") + key_id + " " +
66  Base64(hmac.ToString(false));
67  struct curl_slist* auth_header = NULL;
68  auth_header = curl_slist_append(auth_header, header_str.c_str());
69  curl_easy_setopt(h_curl, CURLOPT_HTTPHEADER, auth_header);
70 
71  // Make request to acquire lease from repo services
72  curl_easy_setopt(h_curl, CURLOPT_URL, (repo_service_url + "/leases").c_str());
73  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDSIZE_LARGE,
74  static_cast<curl_off_t>(payload.length()));
75  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDS, payload.c_str());
76  curl_easy_setopt(h_curl, CURLOPT_WRITEFUNCTION, RecvCB);
77  curl_easy_setopt(h_curl, CURLOPT_WRITEDATA, buffer);
78 
79  ret = curl_easy_perform(h_curl);
80  if (ret) {
82  "Make lease acquire request failed: %d. Reply: %s", ret,
83  buffer->data.c_str());
84  }
85 
86  curl_easy_cleanup(h_curl);
87  h_curl = NULL;
88 
89  return !ret;
90 }
91 
92 bool MakeEndRequest(const std::string& method, const std::string& key_id,
93  const std::string& secret, const std::string& session_token,
94  const std::string& repo_service_url,
95  const std::string& request_payload, CurlBuffer* reply) {
96  CURLcode ret = static_cast<CURLcode>(0);
97 
98  CURL* h_curl = PrepareCurl(method);
99  if (!h_curl) {
100  return false;
101  }
102 
103  shash::Any hmac(shash::kSha1);
104  shash::HmacString(secret, session_token, &hmac);
105 
106  const std::string header_str = std::string("Authorization: ") + key_id + " " +
107  Base64(hmac.ToString(false));
108  struct curl_slist* auth_header = NULL;
109  auth_header = curl_slist_append(auth_header, header_str.c_str());
110  curl_easy_setopt(h_curl, CURLOPT_HTTPHEADER, auth_header);
111 
112  curl_easy_setopt(h_curl, CURLOPT_URL,
113  (repo_service_url + "/leases/" + session_token).c_str());
114  if (request_payload != "") {
115  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDSIZE_LARGE,
116  static_cast<curl_off_t>(request_payload.length()));
117  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDS, request_payload.c_str());
118  } else {
119  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDSIZE_LARGE,
120  static_cast<curl_off_t>(0));
121  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDS, NULL);
122  }
123  curl_easy_setopt(h_curl, CURLOPT_WRITEFUNCTION, RecvCB);
124  curl_easy_setopt(h_curl, CURLOPT_WRITEDATA, reply);
125 
126  ret = curl_easy_perform(h_curl);
127  if (ret) {
129  "Lease end request - curl_easy_perform failed: %d", ret);
130  }
131 
133  const JSON *reply_status =
134  JsonDocument::SearchInObject(reply_json->root(), "status", JSON_STRING);
135  const bool ok = (reply_status != NULL &&
136  std::string(reply_status->string_value) == "ok");
137  if (!ok) {
139  "Lease end request - error reply: %s",
140  reply->data.c_str());
141  }
142 
143  curl_easy_cleanup(h_curl);
144  h_curl = NULL;
145 
146  return ok && !ret;
147 }
#define LogCvmfs(source, mask,...)
Definition: logging.h:20
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:245
static CURL * PrepareCurl(const std::string &method)
std::string data
static JsonDocument * Create(const std::string &text)
size_t RecvCB(void *buffer, size_t size, size_t nmemb, void *userp)
string StringifyInt(const int64_t value)
Definition: string.cc:77
string Base64(const string &data)
Definition: string.cc:473
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:510
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