-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathDobbyBundleConfig.cpp
More file actions
422 lines (370 loc) · 12.4 KB
/
DobbyBundleConfig.cpp
File metadata and controls
422 lines (370 loc) · 12.4 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2016 Sky UK
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* File: DobbyBundleConfig.cpp
*
*/
#include "DobbyBundleConfig.h"
#include "IDobbyUtils.h"
#include <array>
#include <grp.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <fstream>
// -----------------------------------------------------------------------------
/**
* @brief Constructor that parses an OCI bundle's config file to be used by Dobby.
* Plugins under 'rdkPlugins' and 'legacyPlugins' are parsed if found (OCI bundle*).
*
* @param[in] utils The daemon utils object.
* @param[in] settings Dobby settings object.
* @param[in] id Container ID.
* @param[in] bundlePath Path to OCI bundle.
*/
DobbyBundleConfig::DobbyBundleConfig(const std::shared_ptr<IDobbyUtils>& utils,
const std::shared_ptr<const IDobbySettings>& settings,
const ContainerId& id,
const std::string& bundlePath)
: mUtilities(utils)
, mSettings(settings)
, mConf(nullptr)
, mUserId(-1)
, mGroupId(-1)
, mRestartOnCrash(false)
, mSystemDbus(IDobbyIPCUtils::BusType::NoneBus)
, mSessionDbus(IDobbyIPCUtils::BusType::NoneBus)
, mDebugDbus(IDobbyIPCUtils::BusType::NoneBus)
, mConsoleDisabled(true)
, mConsoleLimit(-1)
, mRootfsPath("rootfs")
{
if(!constructConfig(id, bundlePath))
{
AI_LOG_WARN("Failed to create dobby config, retrying with backup");
// We failed to create config, probably source file was corrupted
// try to recover original config and create it again from that
std::string backupConfig = bundlePath + "/config-dobby.json";
if( access( backupConfig.c_str(), F_OK ) == 0 )
{
// return config file to original state
std::ifstream src(backupConfig, std::ios::binary);
std::ofstream dst(bundlePath + "/config.json", std::ios::binary);
dst << src.rdbuf();
//close the file else on further operations on the file, the size of the file equals 32764 even if it is greater than 32764.
dst.close();
// we need to re-run post installation hook, so remove success flag
std::string postInstallPath = bundlePath + "/postinstallhooksuccess";
if (remove(postInstallPath.c_str()) != 0)
{
AI_LOG_ERROR("Failed to remove postinstallhooksuccess");
}
// Retry creation of config
constructConfig(id, bundlePath);
}
}
if(!mValid)
{
AI_LOG_ERROR("Failed to create dobby config");
}
}
DobbyBundleConfig::~DobbyBundleConfig()
{
}
// -----------------------------------------------------------------------------
/**
* @brief Creates config object
*
* This method parses OCI config and creates dobby config based on that.
* This was an old constructor for DobbyBundleConfig, but we need to be able
* to recover in case config gets damaged.
*
* @param[in] id Container ID.
* @param[in] bundlePath Path to the container's OCI bundle
*
*
* @return true if the config is valid, otherwise false.
*/
bool DobbyBundleConfig::constructConfig(const ContainerId& id, const std::string& bundlePath)
{
// because jsoncpp can throw exceptions if we fail to check the json types
// before performing conversions we wrap the whole parse operation in a
// try / catch
try
{
// go and parse the OCI config file for plugins to use
mValid = parseOCIConfig(bundlePath);
// de-serialise config.json
parser_error err = nullptr;
std::string configPath = bundlePath + "/config.json";
mConf = std::shared_ptr<rt_dobby_schema>(
rt_dobby_schema_parse_file(configPath.c_str(), nullptr, &err),
free_rt_dobby_schema);
if (mConf.get() == nullptr || err)
{
AI_LOG_ERROR_EXIT("Failed to parse bundle config, err '%s'", err);
if (err)
{
free(err);
err = nullptr;
}
mValid = false;
}
else
{
// convert OCI config to compliant using libocispec
mValid &= DobbyConfig::convertToCompliant(id, mConf, bundlePath);
}
}
catch (const Json::Exception& e)
{
AI_LOG_ERROR("exception thrown during config parsing - %s", e.what());
mValid = false;
}
return mValid;
}
bool DobbyBundleConfig::isValid() const
{
return mValid;
}
uid_t DobbyBundleConfig::userId() const
{
std::lock_guard<std::mutex> locker(mLock);
return mUserId;
}
gid_t DobbyBundleConfig::groupId() const
{
std::lock_guard<std::mutex> locker(mLock);
return mGroupId;
}
void DobbyBundleConfig::setUidGidMappings(uid_t userId, gid_t groupId)
{
std::shared_ptr<rt_dobby_schema> cfg = config();
if (cfg == nullptr)
{
AI_LOG_ERROR("Invalid bundle config.");
return;
}
rt_config_linux* linux = cfg->linux;
if (linux == nullptr)
{
linux = (rt_config_linux*)calloc(1, sizeof(rt_config_linux));
cfg->linux = linux;
}
if (linux == nullptr)
{
AI_LOG_ERROR("Unable to perform mappings");
return;
}
if ((linux->uid_mappings != nullptr) || (linux->gid_mappings != nullptr))
{
AI_LOG_ERROR("Unable to perform mappings by overriding");
return;
}
size_t len = 1;
linux->uid_mappings_len = len;
linux->gid_mappings_len = len;
linux->uid_mappings = (rt_defs_id_mapping**) calloc (len, sizeof (*linux->uid_mappings));
if (linux->uid_mappings == nullptr)
{
AI_LOG_ERROR("unable to perform user id mappings");
return;
}
linux->gid_mappings = (rt_defs_id_mapping**) calloc (len, sizeof (*linux->gid_mappings));
if (linux->gid_mappings == nullptr)
{
AI_LOG_ERROR("unable to perform group id mappings");
return;
}
linux->uid_mappings[0] = (rt_defs_id_mapping*) calloc (1, sizeof (rt_defs_id_mapping));
linux->gid_mappings[0] = (rt_defs_id_mapping*) calloc (1, sizeof (rt_defs_id_mapping));
rt_defs_id_mapping* uidMapping = linux->uid_mappings[0];
uidMapping->container_id = 0;
uidMapping->host_id = userId;
uidMapping->host_id_present = 1;
uidMapping->container_id_present = 1;
uidMapping->size = 1;
uidMapping->size_present = 1;
rt_defs_id_mapping* gidMapping = linux->gid_mappings[0];
gidMapping->container_id = 0;
gidMapping->host_id = groupId;
gidMapping->host_id_present = 1;
gidMapping->container_id_present = 1;
gidMapping->size = 10;
gidMapping->size_present = 1;
std::lock_guard<std::mutex> locker(mLock);
mUserId = userId;
mGroupId = groupId;
}
const std::string& DobbyBundleConfig::rootfsPath() const
{
return mRootfsPath;
}
bool DobbyBundleConfig::restartOnCrash() const
{
std::lock_guard<std::mutex> locker(mLock);
return mRestartOnCrash;
}
IDobbyIPCUtils::BusType DobbyBundleConfig::systemDbus() const
{
return mSystemDbus;
}
IDobbyIPCUtils::BusType DobbyBundleConfig::sessionDbus() const
{
return mSessionDbus;
}
IDobbyIPCUtils::BusType DobbyBundleConfig::debugDbus() const
{
return mDebugDbus;
}
bool DobbyBundleConfig::consoleDisabled() const
{
return mConsoleDisabled;
}
ssize_t DobbyBundleConfig::consoleLimit() const
{
return mConsoleLimit;
}
const std::string& DobbyBundleConfig::consolePath() const
{
return mConsolePath;
}
#if defined(LEGACY_COMPONENTS)
const std::map<std::string, Json::Value>& DobbyBundleConfig::legacyPlugins() const
{
return mLegacyPlugins;
}
#endif //defined(LEGACY_COMPONENTS)
const std::map<std::string, Json::Value>& DobbyBundleConfig::rdkPlugins() const
{
std::lock_guard<std::mutex> locker(mLock);
return mRdkPlugins;
}
std::shared_ptr<rt_dobby_schema> DobbyBundleConfig::config() const
{
return mValid ? std::shared_ptr<rt_dobby_schema>(mConf) : nullptr;
}
// -----------------------------------------------------------------------------
/**
* @brief Parses the bundle config's contents that are needed by plugins
*
* The function is atomic, therefore if it returns true you can
* guarantee it stuck and will be set for the lifetime of the function.
*
* @param[in] bundlePath path to the container's OCI bundle
*
* @return true if the path was set, otherwise false.
*/
bool DobbyBundleConfig::parseOCIConfig(const std::string& bundlePath)
{
AI_LOG_FN_ENTRY();
std::lock_guard<std::mutex> locker(mLock);
// Parse config.json to a Json::Value type
std::ifstream bundleConfigFs(bundlePath + "/config.json", std::ifstream::binary);
if (!bundleConfigFs)
{
AI_LOG_ERROR_EXIT("failed to open bundle config file at '%s'", bundlePath.c_str());
return false;
}
bundleConfigFs.seekg(0, std::ifstream::end);
uint32_t length = bundleConfigFs.tellg();
bundleConfigFs.seekg(0, std::ifstream::beg);
char* buffer = new char[length];
bundleConfigFs.read(buffer, length);
std::string jsonConfigString(buffer, length);
delete [] buffer;
std::istringstream sin(jsonConfigString);
sin >> mConfig;
// Populate the object with any needed values
mUserId = mConfig["process"]["user"]["uid"].asInt();
mGroupId = mConfig["process"]["user"]["gid"].asInt();
mRootfsPath = mConfig["root"]["path"].asString();
// Parse legacy plugins if present & not null
if (mConfig.isMember("legacyPlugins") && mConfig["legacyPlugins"].isObject())
{
#if defined(LEGACY_COMPONENTS)
if (!processLegacyPlugins(mConfig["legacyPlugins"]))
{
return false;
}
#else
AI_LOG_ERROR_EXIT("legacyPlugins is unsupported, build with "
"LEGACY_COMPONENTS=ON to use legacy plugins");
return false;
#endif //defined(LEGACY_COMPONENTS)
}
// Parse rdk plugins if present & not null
if (mConfig.isMember("rdkPlugins") && mConfig["rdkPlugins"].isObject())
{
Json::Value rdkPlugins = mConfig["rdkPlugins"];
if (!rdkPlugins.isObject())
{
AI_LOG_ERROR("invalid rdkPlugins field");
}
for (const auto &rdkPluginName : rdkPlugins.getMemberNames())
{
mRdkPlugins.emplace(rdkPluginName, rdkPlugins[rdkPluginName]);
}
}
AI_LOG_FN_EXIT();
return true;
}
#if defined(LEGACY_COMPONENTS)
// -----------------------------------------------------------------------------
/**
* @brief Processes the legacy plugins field.
*
* This parses the legacy Dobby plugins to mLegacyPlugins.
*
* @param[in] value The legacyPlugins field from extended bundle config.
*
* @return true if correctly processed the value, otherwise false.
*/
bool DobbyBundleConfig::processLegacyPlugins(const Json::Value& value)
{
if (!value.isObject())
{
AI_LOG_ERROR("invalid legacyPlugins field");
return false;
}
for (const std::string& id : value.getMemberNames())
{
const Json::Value& plugin = value.get(id, "");
if (!plugin.isObject())
{
AI_LOG_ERROR("invalid legacyPlugin entry %s", id.c_str());
return false;
}
// the name field must be a string
const Json::Value& name = id;
if (!name.isString())
{
AI_LOG_ERROR("invalid legacyPlugin.name entry %s", id.c_str());
return false;
}
// the data can be anything, we don't place any restrictions on it since
// it's just passed to the hook library for processing
Json::Value data = plugin["data"];
// add the hook to the list
mLegacyPlugins.emplace(name.asString(), std::move(data));
}
return true;
}
#endif //defined(LEGACY_COMPONENTS)