-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCacheFile.cpp
262 lines (218 loc) · 6.45 KB
/
CacheFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* File: CacheFile.cpp *
* Copyright (c) 2025 Snowflake Computing
*/
#include "CacheFile.hpp"
#if defined(__linux__) || defined(__APPLE__)
#include <sys/stat.h>
#endif
#include <fstream>
#include <string>
#include <sstream>
#include <picojson.h>
#include <boost/filesystem.hpp>
#include "snowflake/platform.h"
#include "../logger/SFLogger.hpp"
#include "../util/Sha256.hpp"
namespace {
using namespace Snowflake::Client;
const char* CREDENTIAL_FILE_NAME = "credential_cache_v1.json";
bool mkdirIfNotExists(const std::string& dir)
{
int result = sf_mkdir(dir.c_str());
if (result == 0)
{
CXX_LOG_DEBUG("Created %s directory.", dir.c_str());
return true;
}
if (errno == EEXIST)
{
CXX_LOG_TRACE("Directory %s already exists.", dir.c_str());
return true;
}
CXX_LOG_ERROR("Failed to create %s directory. Error: %d", dir.c_str(), errno);
return false;
}
boost::optional<std::string> getEnv(const std::string& envVar)
{
char *root = getenv(envVar.c_str());
if (root == nullptr)
{
return {};
}
return std::string(root);
}
void ensureObject(picojson::value &val)
{
if (!val.is<picojson::object>())
{
val = picojson::value(picojson::object());
}
}
picojson::object& getTokens(picojson::value& cache)
{
ensureObject(cache);
auto &obj = cache.get<picojson::object>();
auto pair = obj.emplace("tokens", picojson::value(picojson::object()));
auto& tokens = pair.first->second;
ensureObject(tokens);
return tokens.get<picojson::object>();
}
#if defined(__linux__) || defined(__APPLE__)
bool ensurePermissions(const std::string& path, mode_t mode)
{
if (chmod(path.c_str(), mode) == -1)
{
CXX_LOG_ERROR("Cannot ensure permissions. chmod(%s, %o) failed with errno=%d", path.c_str(), mode, errno);
return false;
}
return true;
}
#else
bool ensurePermissions(const std::string& path, unsigned mode)
{
CXX_LOG_ERROR("Cannot ensure permissions on current platform");
return false;
}
#endif
}
namespace Snowflake {
namespace Client {
boost::optional<std::string> getCacheDir(const std::string& envVar, const std::vector<std::string>& subPathSegments)
{
#ifdef __linux__
auto envVarValueOpt = getEnv(envVar);
if (!envVarValueOpt)
{
return {};
}
const std::string& envVarValue = envVarValueOpt.get();
struct stat s = {};
int err = stat(envVarValue.c_str(), &s);
if (err != 0)
{
CXX_LOG_INFO("Failed to stat %s=%s, errno=%d. Skipping it in cache file location lookup.", envVar.c_str(), envVarValue.c_str(), errno);
return {};
}
if (!S_ISDIR(s.st_mode))
{
CXX_LOG_INFO("%s=%s is not a directory. Skipping it in cache file location lookup.", envVar.c_str(), envVarValue.c_str());
return {};
}
auto cacheDir = envVarValue;
for (const auto& segment: subPathSegments)
{
cacheDir.append(PATH_SEP + segment);
if (!mkdirIfNotExists(cacheDir))
{
CXX_LOG_INFO("Could not create cache dir=%s. Skipping it in cache file location lookup.", cacheDir.c_str());
return {};
}
}
if (!subPathSegments.empty())
{
err = stat(cacheDir.c_str(), &s);
if (err != 0)
{
CXX_LOG_INFO("Failed to stat %s, errno=%d. Skipping it in cache file location lookup.", cacheDir.c_str(), errno);
return {};
}
}
if (s.st_uid != geteuid())
{
CXX_LOG_INFO("%s=%s is not owned by current user. Skipping it in cache file location lookup.", envVar.c_str(), envVarValue.c_str());
return {};
}
unsigned permissions = s.st_mode & 0777;
if (permissions != 0700)
{
CXX_LOG_INFO("Incorrect permissions=%o for cache dir %s. Changing permissions to 700.", permissions, cacheDir.c_str())
if (chmod(cacheDir.c_str(), 0700) != 0)
{
CXX_LOG_WARN("Failed to change permissions for a cache dir %s, errno=%d. Skipping it in cache file location lookup.", cacheDir.c_str(), errno);
return {};
}
}
return cacheDir;
#else
CXX_LOG_FATAL("Using NOOP implementation. This function is implemented only for linux.");
return {};
#endif
}
boost::optional<std::string> getCredentialFilePath()
{
std::vector<std::function<boost::optional<std::string>()>> lookupFunctions =
{
[]() { return getCacheDir("SF_TEMPORARY_CREDENTIAL_CACHE_DIR", {}); },
#ifdef __linux__
[](){ return getCacheDir("XDG_CACHE_HOME", {"snowflake"}); },
#endif
[](){ return getCacheDir("HOME", {".cache", "snowflake"}); },
};
for (const auto& lf: lookupFunctions) {
boost::optional<std::string> directory = lf();
if (directory)
{
auto path = directory.get() + PATH_SEP + CREDENTIAL_FILE_NAME;
CXX_LOG_TRACE("Successfully found credential file path=%s", path.c_str());
return path;
}
}
return {};
};
std::string readFile(const std::string &path, picojson::value &result) {
if (!boost::filesystem::exists(path))
{
result = picojson::value(picojson::object());
return {};
}
std::ifstream cacheFile(path);
if (!cacheFile.is_open())
{
return "Failed to open the file(path=" + path + ")";
}
std::string error = picojson::parse(result, cacheFile);
if (!error.empty())
{
return "Failed to parse the file: " + error;
}
return {};
}
std::string writeFile(const std::string &path, const picojson::value &result) {
std::ofstream cacheFile(path, std::ios_base::trunc);
if (!cacheFile.is_open())
{
return "Failed to open the file";
}
if (!ensurePermissions(path, 0600))
{
return "Cannot ensure correct permissions on a file";
}
cacheFile << result.serialize(true);
return {};
}
void cacheFileUpdate(picojson::value &cache, const std::string &key, const std::string &credential)
{
picojson::object& tokens = getTokens(cache);
tokens.emplace(key, credential);
}
void cacheFileRemove(picojson::value &cache, const std::string &key)
{
picojson::object& tokens = getTokens(cache);
tokens.erase(key);
}
boost::optional<std::string> cacheFileGet(picojson::value &cache, const std::string &key) {
picojson::object& tokens = getTokens(cache);
auto it = tokens.find(key);
if (it == tokens.end())
{
return {};
}
if (!it->second.is<std::string>())
{
return {};
}
return it->second.get<std::string>();
}
}
}