Skip to content

Commit

Permalink
Detach 'destinationRestriction' in the MountParser constructor to a s…
Browse files Browse the repository at this point in the history
…eparate method
  • Loading branch information
Gwangmu Lee committed Jul 18, 2024
1 parent 803dc30 commit ca8b736
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/cli/CommandRun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class CommandRun : public Command {
void makeUserMountObjects() {
auto parser = libsarus::MountParser{conf->getRootfsDirectory(), conf->userIdentity};
if (conf->json.HasMember("userMounts")) {
parser = libsarus::MountParser{conf->getRootfsDirectory(), conf->userIdentity, conf->json["userMounts"]};
parser.setMountDestinationRestrictions(conf->json["userMounts"]);
}
for (const auto& mountString : conf->commandRun.userMounts) {
auto map = libsarus::string::parseMap(mountString);
Expand Down
16 changes: 7 additions & 9 deletions src/libsarus/MountParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@

namespace libsarus {

MountParser::MountParser(const boost::filesystem::path& rootfsDir, const libsarus::UserIdentity& userIdentity, const rapidjson::Value& destinationRestrictions)
MountParser::MountParser(const boost::filesystem::path& rootfsDir, const libsarus::UserIdentity& userIdentity)
: rootfsDir{rootfsDir}
, userIdentity{userIdentity}
{
validationSettings.destinationDisallowedWithPrefix = {};
validationSettings.destinationDisallowedExact = {};
}

void MountParser::setMountDestinationRestrictions(const rapidjson::Value& destinationRestrictions)
{
// Retrieve settings from Config struct
for (const auto& value : destinationRestrictions["notAllowedPrefixesOfPath"].GetArray()) {
Expand All @@ -34,14 +40,6 @@ MountParser::MountParser(const boost::filesystem::path& rootfsDir, const libsaru
}
}

MountParser::MountParser(const boost::filesystem::path& rootfsDir, const libsarus::UserIdentity& userIdentity)
: rootfsDir{rootfsDir}
, userIdentity{userIdentity}
{
validationSettings.destinationDisallowedWithPrefix = {};
validationSettings.destinationDisallowedExact = {};
}

/**
* Parses a custom mount request into a Mount object.
* The request comes in the form of a comma-separated list of key-value pairs.
Expand Down
2 changes: 1 addition & 1 deletion src/libsarus/MountParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ namespace libsarus {

class MountParser {
public:
MountParser(const boost::filesystem::path& rootfsDir, const libsarus::UserIdentity& userIdentity, const rapidjson::Value& userMounts);
MountParser(const boost::filesystem::path& rootfsDir, const libsarus::UserIdentity& userIdentity);
void setMountDestinationRestrictions(const rapidjson::Value& destinationRestrictions);
std::unique_ptr<libsarus::Mount> parseMountRequest(const std::unordered_map<std::string, std::string>& mountRequest);

private:
Expand Down
2 changes: 1 addition & 1 deletion src/libsarus/test/MountParserChecker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class MountParserChecker {
libsarus::MountParser parser = libsarus::MountParser{configRAII.config->getRootfsDirectory(), configRAII.config->userIdentity};

if (!isSiteMount && configRAII.config->json.HasMember("userMounts")) {
parser = libsarus::MountParser{configRAII.config->getRootfsDirectory(), configRAII.config->userIdentity, configRAII.config->json["userMounts"]};
parser.setMountDestinationRestrictions(configRAII.config->json["userMounts"]);
}

auto map = libsarus::string::parseMap(mountRequest);
Expand Down
22 changes: 0 additions & 22 deletions src/libsarus/test/test_MountParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,6 @@ TEST(MountParserTestGroup, site_flags_of_bind_mount) {
.parseAsSiteMount().expectFlags(MS_REC | MS_RDONLY | MS_PRIVATE);
}

TEST(MountParserTestGroup, constructors) {
auto configRAII = test_utility::config::makeConfig();
auto userIdentity = configRAII.config->userIdentity;
auto rootfsDir = boost::filesystem::path{ configRAII.config->json["OCIBundleDir"].GetString() }
/ configRAII.config->json["rootfsFolder"].GetString();

auto requestString = std::string("type=bind,src=/src,dst=/dest,readonly");
auto requestMap = libsarus::string::parseMap(requestString);

auto mp1 = libsarus::MountParser{configRAII.config->getRootfsDirectory(), configRAII.config->userIdentity};
if (configRAII.config->json.HasMember("userMounts")) {
mp1 = libsarus::MountParser{configRAII.config->getRootfsDirectory(), configRAII.config->userIdentity, configRAII.config->json["userMounts"]};
}
auto ctor1 = mp1.parseMountRequest(requestMap);
auto ctor2 = libsarus::MountParser{rootfsDir, userIdentity}
.parseMountRequest(requestMap);

CHECK(ctor1->getSource() == ctor2->getSource());
CHECK(ctor1->getDestination() == ctor2->getDestination());
CHECK(ctor1->getFlags() == ctor2->getFlags());
}

}}

SARUS_UNITTEST_MAIN_FUNCTION();

0 comments on commit ca8b736

Please sign in to comment.