CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gateway_util.cc
Go to the documentation of this file.
1 
5 #include "gateway_util.h"
6 
7 #include <fcntl.h>
8 
9 #include <algorithm>
10 #include <vector>
11 
12 #include "util/logging.h"
13 #include "util/posix.h"
14 #include "util/string.h"
15 
16 namespace {
17 
18 bool BothAreSpaces(const char& c1, const char& c2) {
19  return c1 == ' ' && (c1 == c2);
20 }
21 
22 }
23 
24 namespace gateway {
25 
26 int APIVersion() { return 2; }
27 
28 GatewayKey ReadGatewayKey(const std::string& key_file_name) {
29  std::string id;
30  std::string secret;
31  bool retval = ReadKeys(key_file_name, &id, &secret);
32  if (!retval)
33  return GatewayKey();
34  return GatewayKey(id, secret);
35 }
36 
37 bool ReadKeys(const std::string& key_file_name, std::string* key_id,
38  std::string* secret) {
39  if (!(key_id && secret)) {
40  return false;
41  }
42 
43  int key_file_fd = open(key_file_name.c_str(), O_RDONLY);
44  if (!key_file_fd) {
45  return false;
46  }
47 
48  std::string body;
49  if (!SafeReadToString(key_file_fd, &body)) {
50  close(key_file_fd);
51  return false;
52  }
53 
54  close(key_file_fd);
55 
56  return ParseKey(body, key_id, secret);
57 }
58 
59 bool ParseKey(const std::string& body, std::string* key_id,
60  std::string* secret) {
61  std::string line = GetLineMem(body.data(), body.size());
62  std::string l = Trim(ReplaceAll(line, "\t", " "));
63  l.erase(std::unique(l.begin(), l.end(), BothAreSpaces), l.end());
64  std::vector<std::string> tokens = SplitString(l, ' ');
65 
66  if (tokens.size() < 2 || tokens.size() > 3) {
67  return false;
68  }
69 
70  if (tokens[0] == "plain_text") {
71  *key_id = tokens[1];
72  *secret = tokens[2];
73  } else {
74  return false;
75  }
76 
77  return true;
78 }
79 
80 } // namespace gateway
int APIVersion()
Definition: gateway_util.cc:26
string GetLineMem(const char *text, const int text_size)
Definition: string.cc:380
bool BothAreSpaces(const char &c1, const char &c2)
Definition: gateway_util.cc:18
bool ParseKey(const std::string &body, std::string *key_id, std::string *secret)
Definition: gateway_util.cc:59
string Trim(const string &raw, bool trim_newline)
Definition: string.cc:428
string ReplaceAll(const string &haystack, const string &needle, const string &replace_by)
Definition: string.cc:484
vector< string > SplitString(const string &str, char delim)
Definition: string.cc:290
bool ReadKeys(const std::string &key_file_name, std::string *key_id, std::string *secret)
Definition: gateway_util.cc:37
bool SafeReadToString(int fd, std::string *final_result)
Definition: posix.cc:2068
GatewayKey ReadGatewayKey(const std::string &key_file_name)
Definition: gateway_util.cc:28