| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/gateway_util.cc |
| Date: | 2025-11-02 02:35:35 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 15 | 35 | 42.9% |
| Branches: | 21 | 56 | 37.5% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "gateway_util.h" | ||
| 6 | |||
| 7 | #include <fcntl.h> | ||
| 8 | #include <unistd.h> | ||
| 9 | |||
| 10 | #include <algorithm> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "util/logging.h" | ||
| 14 | #include "util/posix.h" | ||
| 15 | #include "util/string.h" | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | |||
| 19 | 4833 | bool BothAreSpaces(const char &c1, const char &c2) { | |
| 20 |
4/4✓ Branch 0 taken 702 times.
✓ Branch 1 taken 4131 times.
✓ Branch 2 taken 270 times.
✓ Branch 3 taken 432 times.
|
4833 | return c1 == ' ' && (c1 == c2); |
| 21 | } | ||
| 22 | |||
| 23 | } // namespace | ||
| 24 | |||
| 25 | namespace gateway { | ||
| 26 | |||
| 27 | ✗ | int APIVersion() { return 2; } | |
| 28 | |||
| 29 | ✗ | GatewayKey ReadGatewayKey(const std::string &key_file_name) { | |
| 30 | ✗ | std::string id; | |
| 31 | ✗ | std::string secret; | |
| 32 | ✗ | const bool retval = ReadKeys(key_file_name, &id, &secret); | |
| 33 | ✗ | if (!retval) | |
| 34 | ✗ | return GatewayKey(); | |
| 35 | ✗ | return GatewayKey(id, secret); | |
| 36 | } | ||
| 37 | |||
| 38 | ✗ | bool ReadKeys(const std::string &key_file_name, std::string *key_id, | |
| 39 | std::string *secret) { | ||
| 40 | ✗ | if (!(key_id && secret)) { | |
| 41 | ✗ | return false; | |
| 42 | } | ||
| 43 | |||
| 44 | ✗ | const int key_file_fd = open(key_file_name.c_str(), O_RDONLY); | |
| 45 | ✗ | if (!key_file_fd) { | |
| 46 | ✗ | return false; | |
| 47 | } | ||
| 48 | |||
| 49 | ✗ | std::string body; | |
| 50 | ✗ | if (!SafeReadToString(key_file_fd, &body)) { | |
| 51 | ✗ | close(key_file_fd); | |
| 52 | ✗ | return false; | |
| 53 | } | ||
| 54 | |||
| 55 | ✗ | close(key_file_fd); | |
| 56 | |||
| 57 | ✗ | return ParseKey(body, key_id, secret); | |
| 58 | } | ||
| 59 | |||
| 60 | 216 | bool ParseKey(const std::string &body, std::string *key_id, | |
| 61 | std::string *secret) { | ||
| 62 |
1/2✓ Branch 3 taken 216 times.
✗ Branch 4 not taken.
|
216 | const std::string line = GetLineMem(body.data(), body.size()); |
| 63 |
4/8✓ Branch 2 taken 216 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 216 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 216 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 216 times.
✗ Branch 13 not taken.
|
432 | std::string l = Trim(ReplaceAll(line, "\t", " ")); |
| 64 |
2/4✓ Branch 5 taken 216 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 216 times.
✗ Branch 10 not taken.
|
216 | l.erase(std::unique(l.begin(), l.end(), BothAreSpaces), l.end()); |
| 65 |
1/2✓ Branch 1 taken 216 times.
✗ Branch 2 not taken.
|
216 | std::vector<std::string> tokens = SplitString(l, ' '); |
| 66 | |||
| 67 |
5/6✓ Branch 1 taken 216 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 189 times.
✓ Branch 6 taken 27 times.
✓ Branch 7 taken 189 times.
|
216 | if (tokens.size() < 2 || tokens.size() > 3) { |
| 68 | 27 | return false; | |
| 69 | } | ||
| 70 | |||
| 71 |
2/2✓ Branch 2 taken 135 times.
✓ Branch 3 taken 54 times.
|
189 | if (tokens[0] == "plain_text") { |
| 72 |
1/2✓ Branch 2 taken 135 times.
✗ Branch 3 not taken.
|
135 | *key_id = tokens[1]; |
| 73 |
1/2✓ Branch 2 taken 135 times.
✗ Branch 3 not taken.
|
135 | *secret = tokens[2]; |
| 74 | } else { | ||
| 75 | 54 | return false; | |
| 76 | } | ||
| 77 | |||
| 78 | 135 | return true; | |
| 79 | 216 | } | |
| 80 | |||
| 81 | } // namespace gateway | ||
| 82 |