| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/** |
| 2 |
|
|
* This file is part of the CernVM File System. |
| 3 |
|
|
*/ |
| 4 |
|
|
|
| 5 |
|
|
#include "swissknife_lease.h" |
| 6 |
|
|
|
| 7 |
|
|
#include <algorithm> |
| 8 |
|
|
#include <vector> |
| 9 |
|
|
|
| 10 |
|
|
#include "gateway_util.h" |
| 11 |
|
|
#include "swissknife_lease_curl.h" |
| 12 |
|
|
#include "swissknife_lease_json.h" |
| 13 |
|
|
#include "util/logging.h" |
| 14 |
|
|
#include "util/posix.h" |
| 15 |
|
|
#include "util/string.h" |
| 16 |
|
|
|
| 17 |
|
|
namespace { |
| 18 |
|
|
|
| 19 |
|
✗ |
bool CheckParams(const swissknife::CommandLease::Parameters &p) { |
| 20 |
|
✗ |
if (p.action != "acquire" && p.action != "drop") { |
| 21 |
|
✗ |
return false; |
| 22 |
|
|
} |
| 23 |
|
|
|
| 24 |
|
✗ |
return true; |
| 25 |
|
|
} |
| 26 |
|
|
|
| 27 |
|
|
} // namespace |
| 28 |
|
|
|
| 29 |
|
|
namespace swissknife { |
| 30 |
|
|
|
| 31 |
|
|
enum LeaseError { |
| 32 |
|
|
kLeaseSuccess, |
| 33 |
|
|
kLeaseBusy, |
| 34 |
|
|
kLeaseFailure, |
| 35 |
|
|
kLeaseParamError, |
| 36 |
|
|
kLeaseKeyParseError, |
| 37 |
|
|
kLeaseCurlInitError, |
| 38 |
|
|
kLeaseFileOpenError, |
| 39 |
|
|
kLeaseCurlReqError, |
| 40 |
|
|
}; |
| 41 |
|
|
|
| 42 |
|
✗ |
CommandLease::~CommandLease() { } |
| 43 |
|
|
|
| 44 |
|
✗ |
ParameterList CommandLease::GetParams() const { |
| 45 |
|
✗ |
ParameterList r; |
| 46 |
|
✗ |
r.push_back(Parameter::Mandatory('u', "repo service url")); |
| 47 |
|
✗ |
r.push_back(Parameter::Mandatory('a', "action (acquire or drop)")); |
| 48 |
|
✗ |
r.push_back(Parameter::Mandatory('k', "key file")); |
| 49 |
|
✗ |
r.push_back(Parameter::Mandatory('p', "lease path")); |
| 50 |
|
✗ |
return r; |
| 51 |
|
|
} |
| 52 |
|
|
|
| 53 |
|
✗ |
int CommandLease::Main(const ArgumentList &args) { |
| 54 |
|
✗ |
Parameters params; |
| 55 |
|
|
|
| 56 |
|
✗ |
params.repo_service_url = *(args.find('u')->second); |
| 57 |
|
✗ |
params.action = *(args.find('a')->second); |
| 58 |
|
✗ |
params.key_file = *(args.find('k')->second); |
| 59 |
|
|
|
| 60 |
|
✗ |
params.lease_path = *(args.find('p')->second); |
| 61 |
|
✗ |
std::vector<std::string> tokens = SplitString(params.lease_path, '/'); |
| 62 |
|
✗ |
const std::string lease_fqdn = tokens.front(); |
| 63 |
|
|
|
| 64 |
|
✗ |
if (!CheckParams(params)) { |
| 65 |
|
✗ |
return kLeaseParamError; |
| 66 |
|
|
} |
| 67 |
|
|
|
| 68 |
|
|
// Initialize curl |
| 69 |
|
✗ |
if (curl_global_init(CURL_GLOBAL_ALL)) { |
| 70 |
|
✗ |
return kLeaseCurlInitError; |
| 71 |
|
|
} |
| 72 |
|
|
|
| 73 |
|
✗ |
std::string key_id; |
| 74 |
|
✗ |
std::string secret; |
| 75 |
|
✗ |
if (!gateway::ReadKeys(params.key_file, &key_id, &secret)) { |
| 76 |
|
✗ |
LogCvmfs(kLogCvmfs, kLogStderr, "Error reading key file %s.", |
| 77 |
|
|
params.key_file.c_str()); |
| 78 |
|
✗ |
return kLeaseKeyParseError; |
| 79 |
|
|
} |
| 80 |
|
|
|
| 81 |
|
✗ |
LeaseError ret = kLeaseSuccess; |
| 82 |
|
✗ |
if (params.action == "acquire") { |
| 83 |
|
✗ |
CurlBuffer buffer; |
| 84 |
|
✗ |
if (MakeAcquireRequest(key_id, secret, params.lease_path, |
| 85 |
|
|
params.repo_service_url, &buffer)) { |
| 86 |
|
✗ |
std::string session_token; |
| 87 |
|
✗ |
const LeaseReply rep = ParseAcquireReply(buffer, &session_token); |
| 88 |
|
✗ |
switch (rep) { |
| 89 |
|
✗ |
case kLeaseReplySuccess: { |
| 90 |
|
✗ |
const std::string token_file_name = "/var/spool/cvmfs/" + lease_fqdn |
| 91 |
|
✗ |
+ "/session_token"; |
| 92 |
|
|
|
| 93 |
|
✗ |
if (!SafeWriteToFile(session_token, token_file_name, 0600)) { |
| 94 |
|
✗ |
LogCvmfs(kLogCvmfs, kLogStderr, "Error opening file: %s", |
| 95 |
|
|
std::strerror(errno)); |
| 96 |
|
✗ |
ret = kLeaseFileOpenError; |
| 97 |
|
|
} |
| 98 |
|
✗ |
} break; |
| 99 |
|
✗ |
case kLeaseReplyBusy: |
| 100 |
|
✗ |
return kLeaseBusy; |
| 101 |
|
|
break; |
| 102 |
|
✗ |
case kLeaseReplyFailure: |
| 103 |
|
|
default: |
| 104 |
|
✗ |
return kLeaseFailure; |
| 105 |
|
|
} |
| 106 |
|
✗ |
} else { |
| 107 |
|
✗ |
ret = kLeaseCurlReqError; |
| 108 |
|
|
} |
| 109 |
|
✗ |
} else if (params.action == "drop") { |
| 110 |
|
|
// Try to read session token from repository scratch directory |
| 111 |
|
✗ |
std::string session_token; |
| 112 |
|
✗ |
const std::string token_file_name = "/var/spool/cvmfs/" + lease_fqdn |
| 113 |
|
✗ |
+ "/session_token"; |
| 114 |
|
✗ |
FILE *token_file = std::fopen(token_file_name.c_str(), "r"); |
| 115 |
|
✗ |
if (token_file) { |
| 116 |
|
✗ |
GetLineFile(token_file, &session_token); |
| 117 |
|
|
LogCvmfs(kLogCvmfs, kLogDebug, "Read session token from file: %s", |
| 118 |
|
|
session_token.c_str()); |
| 119 |
|
|
|
| 120 |
|
✗ |
CurlBuffer buffer; |
| 121 |
|
✗ |
if (MakeEndRequest("DELETE", key_id, secret, session_token, |
| 122 |
|
|
params.repo_service_url, "", &buffer)) { |
| 123 |
|
✗ |
if (kLeaseReplySuccess == ParseDropReply(buffer)) { |
| 124 |
|
✗ |
std::fclose(token_file); |
| 125 |
|
✗ |
if (unlink(token_file_name.c_str())) { |
| 126 |
|
✗ |
LogCvmfs(kLogCvmfs, kLogStderr, |
| 127 |
|
|
"Warning - Could not delete session token file."); |
| 128 |
|
|
} |
| 129 |
|
✗ |
return kLeaseSuccess; |
| 130 |
|
|
} else { |
| 131 |
|
✗ |
LogCvmfs(kLogCvmfs, kLogStderr, "Could not drop active lease"); |
| 132 |
|
✗ |
ret = kLeaseFailure; |
| 133 |
|
|
} |
| 134 |
|
|
} else { |
| 135 |
|
✗ |
LogCvmfs(kLogCvmfs, kLogStderr, "Error making DELETE request"); |
| 136 |
|
✗ |
ret = kLeaseCurlReqError; |
| 137 |
|
|
} |
| 138 |
|
|
|
| 139 |
|
✗ |
std::fclose(token_file); |
| 140 |
|
✗ |
} else { |
| 141 |
|
✗ |
LogCvmfs(kLogCvmfs, kLogStderr, "Error reading session token from file"); |
| 142 |
|
✗ |
ret = kLeaseFileOpenError; |
| 143 |
|
|
} |
| 144 |
|
|
} |
| 145 |
|
|
|
| 146 |
|
✗ |
return ret; |
| 147 |
|
|
} |
| 148 |
|
|
|
| 149 |
|
|
} // namespace swissknife |
| 150 |
|
|
|