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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
#pragma once
// DO_NOT_STYLE
#ifndef _TVM_AUTH_HIGH_LVL_WRAPPER_H_
#define _TVM_AUTH_HIGH_LVL_WRAPPER_H_
#include "high_lvl_client.h"
#include "tvmauth_wrapper.h"
#include <map>
namespace NTvmAuthWrapper {
/*!
* Uses local http-interface to get state: http://localhost/tvm/.
* This interface can be provided with tvmtool (local daemon) or Qloud/YP (local http api in container).
* See more: https://wiki.yandex-team.ru/passport/tvm2/tvm-daemon/.
*/
class TTvmToolClientSettings {
public:
/*!
* Create settings struct for tvmtool client
* Sets default values:
* - hostname: "localhost"
* - port detected with env["DEPLOY_TVM_TOOL_URL"] (provided with Yandex.Deploy),
* otherwise port == 1 (it is ok for Qloud)
* - authToken: env["TVMTOOL_LOCAL_AUTHTOKEN"] (provided with Yandex.Deploy),
* otherwise env["QLOUD_TVM_TOKEN"] (provided with Qloud)
*
* AuthToken is protection from SSRF.
*
* @param selfAias - alias for your TVM client, which you specified in tvmtool or YD interface
*/
TTvmToolClientSettings(const std::string& selfAlias)
: Ptr(nullptr, TA_TvmToolClientSettings_Delete)
{
TA_TTvmToolClientSettings* rawPtr;
ThrowIfFatal(TA_TvmToolClientSettings_Create(selfAlias.data(), selfAlias.size(), &rawPtr));
Ptr.reset(rawPtr);
}
/*!
* Look at comment for ctor
*/
void SetPort(uint16_t port) {
ThrowIfFatal(TA_TvmToolClientSettings_SetPort(Ptr.get(), port));
}
/*!
* Default value: hostname == "localhost"
*/
void SetHostname(const std::string& hostname) {
ThrowIfFatal(TA_TvmToolClientSettings_SetHostname(Ptr.get(), hostname.data(), hostname.size()));
}
/*!
* Look at comment for ctor
*/
void SetAuthtoken(const std::string& authtoken) {
ThrowIfFatal(TA_TvmToolClientSettings_SetAuthToken(Ptr.get(), authtoken.data(), authtoken.size()));
}
/*!
* Blackbox environmet is provided by tvmtool for client.
* You can override it for your purpose with limitations:
* (env from tvmtool) -> (override)
* - Prod/ProdYateam -> Prod/ProdYateam
* - Test/TestYateam -> Test/TestYateam
* - Stress -> Stress
*
* You can contact tvm-dev@yandex-team.ru if limitations are too strict
* @param[in] env
*/
void OverrideBlackboxEnv(TA_EBlackboxEnv env) {
ThrowIfFatal(TA_TvmToolClientSettings_OverrideBlackboxEnv(Ptr.get(), env));
}
private:
friend class TTvmClient;
std::unique_ptr<TA_TTvmToolClientSettings, decltype(&TA_TvmToolClientSettings_Delete)> Ptr;
};
/*!
* Uses general way to get state: https://tvm-api.yandex.net.
* It is not recomended for Qloud/YP.
*/
class TTvmApiClientSettings {
public:
/**
* Settings for TVM client
* At least one of them is required: EnableServiceTicketChecking(), EnableUserTicketChecking()
*/
TTvmApiClientSettings()
: Ptr(nullptr, TA_TvmApiClientSettings_Delete)
{
TA_TTvmApiClientSettings* rawPtr;
ThrowIfFatal(TA_TvmApiClientSettings_Create(&rawPtr));
Ptr.reset(rawPtr);
}
void SetSelfTvmId(TTvmId selfTvmId) {
if (selfTvmId == 0) {
throw std::runtime_error("selfTvmId cannot be 0");
}
ThrowIfFatal(TA_TvmApiClientSettings_SetSelfTvmId(Ptr.get(), selfTvmId));
}
/*!
* Prerequieres SetSelfTvmId()
*/
void EnableServiceTicketChecking() {
ThrowIfFatal(TA_TvmApiClientSettings_EnableServiceTicketChecking(Ptr.get()));
}
void EnableUserTicketChecking(TA_EBlackboxEnv env) {
ThrowIfFatal(TA_TvmApiClientSettings_EnableUserTicketChecking(Ptr.get(), env));
}
class TDst {
public:
TDst(TTvmId id)
: Id_(id)
{
if (id == 0) {
throw std::runtime_error("TvmId cannot be 0");
}
}
const TTvmId Id_;
};
using TAlias = std::string;
using TDstMap = std::map<TAlias, TDst>;
using TDstVector = std::vector<TDst>;
/**
* Alias is internal name of destination in your code. It allowes not to bring destination's
* tvm_id to each calling point. Useful for several environments: prod/test/etc.
* Overrides result of any other call of EnableServiceTicketsFetchOptions()
* @example:
* // init
* static const TString MY_BACKEND = "my backend";
* TDstMap map = {{MY_BACKEND, TDst(config.get("my_back_tvm_id"))}};
* ...
* // per request
* TString t = tvmClient.GetServiceTicket(MY_BACKEND);
*/
void EnableServiceTicketsFetchOptions(const std::string& selfSecret,
const TDstMap& dsts) {
std::string d;
for (const auto& p : dsts) {
d.append(p.first).push_back(':');
d.append(std::to_string(p.second.Id_)).push_back(';');
}
ThrowIfFatal(TA_TvmApiClientSettings_EnableServiceTicketsFetchOptionsWithAliases(
Ptr.get(),
selfSecret.data(),
selfSecret.size(),
d.data(),
d.size()));
}
void EnableServiceTicketsFetchOptions(const std::string& selfSecret,
const TDstVector& dsts) {
std::string d;
for (const TDst& dst : dsts) {
d.append(std::to_string(dst.Id_)).push_back(';');
}
ThrowIfFatal(TA_TvmApiClientSettings_EnableServiceTicketsFetchOptionsWithTvmIds(
Ptr.get(),
selfSecret.data(),
selfSecret.size(),
d.data(),
d.size()));
}
/*!
* Set path to directory for disk cache
* Requires read/write permissions. Checks permissions
* WARNING: The same directory can be used only:
* - for TVM clients with the same settings
* OR
* - for new client replacing previous - with another config.
* System user must be the same for processes with these clients inside.
* Implementation doesn't provide other scenarios.
* @param[in] dir
*/
void SetDiskCacheDir(const std::string& dir) {
ThrowIfFatal(TA_TvmApiClientSettings_SetDiskCacheDir(Ptr.get(), dir.data(), dir.size()));
}
private:
friend class TTvmClient;
std::unique_ptr<TA_TTvmApiClientSettings, decltype(&TA_TvmApiClientSettings_Delete)> Ptr;
};
struct TClientStatus {
TA_ETvmClientStatusCode Code = TA_TCSC_OK;
std::string LastError;
};
/**
* In 99% cases TvmClient shoud be created at service startup and live for the whole process lifetime.
* @brief Long lived thread-safe object for interacting with TVM.
*/
class TTvmClient {
public:
/*!
* Create client for tvmtool. Starts thread for updating of cache in background
* @param[in] settings
* @param[in] logger is usefull for monitoring and debuging
*/
TTvmClient(const TTvmToolClientSettings& settings, TA_TLoggerFunc logger)
: Ptr(nullptr, TA_TvmClient_Delete) {
TA_TTvmClient* rawPtr;
ThrowIfFatal(TA_TvmClient_CreateForTvmtool(settings.Ptr.get(), logger, &rawPtr));
Ptr.reset(rawPtr);
}
/*!
* Starts thread for updating of in-memory cache in background
* Reads cache from disk if specified
* @param[in] settings
* @param[in] logger is usefull for monitoring and debuging
*/
TTvmClient(const TTvmApiClientSettings& settings, TA_TLoggerFunc logger)
: Ptr(nullptr, TA_TvmClient_Delete) {
TA_TTvmClient* rawPtr;
ThrowIfFatal(TA_TvmClient_Create(settings.Ptr.get(), logger, &rawPtr));
Ptr.reset(rawPtr);
}
TTvmClient(TTvmClient&&) = default;
TTvmClient& operator=(TTvmClient&&) = default;
TClientStatus GetStatus() const {
TA_TTvmClientStatus* s = nullptr;
ThrowIfFatal(TA_TvmClient_GetStatus(Ptr.get(), &s));
std::unique_ptr<TA_TTvmClientStatus, decltype(&TA_TvmClient_DeleteStatus)> ptr(
s, TA_TvmClient_DeleteStatus);
TClientStatus res;
ThrowIfFatal(TA_TvmClient_Status_GetCode(ptr.get(), &res.Code));
const char* msg = nullptr;
size_t size = 0;
ThrowIfFatal(TA_TvmClient_Status_GetLastError(ptr.get(), &msg, &size));
res.LastError = std::string(msg, size);
return res;
}
/*!
* Chekcing must be enabled in TClientSettings
* Can throw exception if cache is out of date or wrong config
* @param[in] ticket
*/
TCheckedServiceTicket CheckServiceTicket(const std::string& ticket) const {
TA_TCheckedServiceTicket* ticketPtr = nullptr;
TA_EErrorCode resultCode = TA_TvmClient_CheckServiceTicket(Ptr.get(), ticket.data(), ticket.size(), &ticketPtr);
TCheckedServiceTicket t(ticketPtr, resultCode);
ThrowIfFatal(resultCode);
return t;
}
/*!
* Blackbox enviroment must be cofingured in TClientSettings
* Can throw exception if cache is out of date or wrong config
* @param[in] ticket
*/
TCheckedUserTicket CheckUserTicket(const std::string& ticket) const {
TA_TCheckedUserTicket* ticketPtr = nullptr;
TA_EErrorCode resultCode = TA_TvmClient_CheckUserTicket(Ptr.get(), ticket.data(), ticket.size(), &ticketPtr);
TCheckedUserTicket t(ticketPtr, resultCode);
ThrowIfFatal(resultCode);
return t;
}
/*!
* Blackbox enviroment must be cofingured in TClientSettings
* Can throw exception if cache is out of date or wrong config
* @param[in] ticket
* @param[in] env - allowes to overwrite env from settings
*/
TCheckedUserTicket CheckUserTicket(const std::string& ticket, TA_EBlackboxEnv env) const {
TA_TCheckedUserTicket* ticketPtr = nullptr;
TA_EErrorCode resultCode = TA_TvmClient_CheckUserTicketWithOverridedEnv(Ptr.get(), ticket.data(), ticket.size(), env, &ticketPtr);
TCheckedUserTicket t(ticketPtr, resultCode);
ThrowIfFatal(resultCode);
return t;
}
/*!
* Requires fetchinig options (from TClientSettings or Qloud/YP/tvmtool settings)
* Can throw exception if cache is invalid or wrong config
* @param[in] dst
*/
std::string GetServiceTicketFor(const TTvmApiClientSettings::TAlias& dst) {
char buffer[512];
size_t realSize = 0;
TA_EErrorCode code = TA_TvmClient_GetServiceTicketForAlias(Ptr.get(), dst.data(), dst.size(), sizeof(buffer), buffer, &realSize);
if (code == TA_EC_SMALL_BUFFER) {
std::string res(realSize, 0);
ThrowIfFatal(TA_TvmClient_GetServiceTicketForAlias(Ptr.get(), dst.data(), dst.size(), realSize, (char*)res.data(), &realSize));
return res;
}
ThrowIfFatal(code);
return std::string(buffer, realSize);
}
/*!
* Requires fetchinig options (from TClientSettings or Qloud/YP/tvmtool settings)
* Can throw exception if cache is invalid or wrong config
* @param[in] dst
*/
std::string GetServiceTicketFor(TTvmId dst) {
char buffer[512];
size_t realSize = 0;
TA_EErrorCode code = TA_TvmClient_GetServiceTicketForTvmId(Ptr.get(), dst, sizeof(buffer), buffer, &realSize);
if (code == TA_EC_SMALL_BUFFER) {
std::string res(realSize, 0);
ThrowIfFatal(TA_TvmClient_GetServiceTicketForTvmId(Ptr.get(), dst, realSize, (char*)res.data(), &realSize));
return res;
}
ThrowIfFatal(code);
return std::string(buffer, realSize);
}
private:
std::unique_ptr<TA_TTvmClient, decltype(&TA_TvmClient_Delete)> Ptr;
};
}
#endif
|