From 0487d63c3b1b2a109846576020f0d28bd28bf116 Mon Sep 17 00:00:00 2001 From: CalvinCheng-AWS <88687912+xlcheng1@users.noreply.github.com> Date: Thu, 14 Jul 2022 14:59:48 -0700 Subject: [PATCH] fixing runAsUser when building command (#292) * fixing runAsUser when building command * Added a unit test for runAsUser not provided --- source/jobs/JobEngine.cpp | 26 ++++++++++++++++++++------ test/jobs/TestJobEngine.cpp | 15 +++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/source/jobs/JobEngine.cpp b/source/jobs/JobEngine.cpp index 5c1a705df..b1c737f68 100644 --- a/source/jobs/JobEngine.cpp +++ b/source/jobs/JobEngine.cpp @@ -260,13 +260,27 @@ int JobEngine::exec_cmd(const string &operation, PlainJobDocument::JobAction act { argSize = action.input.args->size(); } - std::unique_ptr argv(new const char *[argSize + 3]); - argv[0] = operation.c_str(); - argv[1] = action.runAsUser->c_str(); - argv[argSize + 2] = nullptr; - for (size_t i = 0; i < argSize; i++) + std::unique_ptr argv; + if (action.runAsUser.has_value() && !action.runAsUser->empty()) { - argv[i + 2] = action.input.args->at(i).c_str(); + argv.reset(new const char *[argSize + 3]); + argv[0] = operation.c_str(); + argv[1] = action.runAsUser->c_str(); + argv[argSize + 2] = nullptr; + for (size_t i = 0; i < argSize; i++) + { + argv[i + 2] = action.input.args->at(i).c_str(); + } + } + else + { + argv.reset(new const char *[argSize + 2]); + argv[0] = operation.c_str(); + argv[argSize + 1] = nullptr; + for (size_t i = 0; i < argSize; i++) + { + argv[i + 1] = action.input.args->at(i).c_str(); + } } int execResult; diff --git a/test/jobs/TestJobEngine.cpp b/test/jobs/TestJobEngine.cpp index de19aaad3..a5ef378f8 100644 --- a/test/jobs/TestJobEngine.cpp +++ b/test/jobs/TestJobEngine.cpp @@ -102,6 +102,21 @@ TEST_F(TestJobEngine, ExecuteStepsHappy) ASSERT_STREQ(jobEngine.getStdOut().c_str(), std::string(testStdout + "\n").c_str()); } +TEST_F(TestJobEngine, ExecuteWithoutRunAsUser) +{ + vector steps; + vector args; + args.push_back("-c"); + args.push_back("echo " + testStdout); + steps.push_back(createJobAction("testAction", "runHandler", "sh", args, "/bin", false)); + PlainJobDocument jobDocument = createTestJobDocument(steps, true); + JobEngine jobEngine; + + int executionStatus = jobEngine.exec_steps(jobDocument, testHandlerDirectoryPath); + ASSERT_EQ(executionStatus, 0); + ASSERT_STREQ(jobEngine.getStdOut().c_str(), std::string(testStdout + "\n").c_str()); +} + TEST_F(TestJobEngine, ExecuteSucceedThenFail) { vector steps;